Debian Codegraph @

cpp/src/proton_bits.hpp

raw
1 #ifndef PROTON_BITS_HPP
2 #define PROTON_BITS_HPP
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21 #include <proton/link.h>
22 #include <proton/session.h>
23
24 #include <string>
25 #include <iosfwd>
26
27 #include "contexts.hpp"
28
29 /**@file
30 *
31 * Assorted internal proton utilities.
32 */
33
34 struct pn_error_t;
35
36 struct pn_data_t;
37 struct pn_transport_t;
38 struct pn_sasl_t;
39 struct pn_ssl_t;
40 struct pn_connection_t;
41 struct pn_session_t;
42 struct pn_link_t;
43 struct pn_delivery_t;
44 struct pn_condition_t;
45 struct pn_acceptor_t;
46 struct pn_terminus_t;
47 struct pn_reactor_t;
48 struct pn_record_t;
49
50 namespace proton {
51
52 namespace internal { class data; }
53 class transport;
54 class sasl;
55 class ssl;
56 class connection;
57 class session;
58 class link;
59 class sender;
60 class receiver;
61 class transfer;
62 class tracker;
63 class delivery;
64 class error_condition;
65 class acceptor;
66 class terminus;
67 class source;
68 class target;
69 class reactor;
70 class messaging_handler;
71
72 std::string error_str(long code);
73
74 /** Print the error string from pn_error_t, or from code if pn_error_t has no error. */
75 std::string error_str(pn_error_t*, long code=0);
76
77 /** Make a void* inspectable via operator <<. */
78 struct inspectable { void* value; inspectable(void* o) : value(o) {} };
79
80 /** Stream a proton object via pn_inspect. */
81 std::ostream& operator<<(std::ostream& o, const inspectable& object);
82
83 void set_error_condition(const error_condition&, pn_condition_t*);
84
85 /// Convert a const char* to std::string, convert NULL to the empty string.
86 inline std::string str(const char* s) { return s ? s : std::string(); }
87
88 namespace internal {
89
90 // These traits relate the wrapped and wrapper classes for the templated factories below
91 template <class T> struct wrapped {};
92 template <> struct wrapped<internal::data> { typedef pn_data_t type; };
93 template <> struct wrapped<transport> { typedef pn_transport_t type; };
94 template <> struct wrapped<connection> { typedef pn_connection_t type; };
95 template <> struct wrapped<session> { typedef pn_session_t type; };
96 template <> struct wrapped<link> { typedef pn_link_t type; };
97 template <> struct wrapped<sender> { typedef pn_link_t type; };
98 template <> struct wrapped<receiver> { typedef pn_link_t type; };
99 template <> struct wrapped<transfer> { typedef pn_delivery_t type; };
100 template <> struct wrapped<tracker> { typedef pn_delivery_t type; };
101 template <> struct wrapped<delivery> { typedef pn_delivery_t type; };
102 template <> struct wrapped<error_condition> { typedef pn_condition_t type; };
103 template <> struct wrapped<terminus> { typedef pn_terminus_t type; };
104 template <> struct wrapped<source> { typedef pn_terminus_t type; };
105 template <> struct wrapped<target> { typedef pn_terminus_t type; };
106
107 template <class T> struct wrapper {};
108 template <> struct wrapper<pn_data_t> { typedef internal::data type; };
109 template <> struct wrapper<pn_transport_t> { typedef transport type; };
110 template <> struct wrapper<pn_connection_t> { typedef connection type; };
111 template <> struct wrapper<pn_session_t> { typedef session type; };
112 template <> struct wrapper<pn_link_t> { typedef link type; };
113 template <> struct wrapper<pn_delivery_t> { typedef transfer type; };
114 template <> struct wrapper<pn_condition_t> { typedef error_condition type; };
115 template <> struct wrapper<pn_terminus_t> { typedef terminus type; };
116
117 // Factory for wrapper types
118 template <class T>
119 class factory {
120 public:
121 static T wrap(typename wrapped<T>::type* t) { return t; }
122 static typename wrapped<T>::type* unwrap(const T& t) { return t.pn_object(); }
123 };
124
125 template <class T> struct context {};
126 template <> struct context<link> {typedef link_context type; };
127 template <> struct context<receiver> {typedef link_context type; };
128 template <> struct context<sender> {typedef link_context type; };
129 template <> struct context<session> {typedef session_context type; };
130 template <> struct context<connection> {typedef connection_context type; };
131
132 template <class T>
133 inline void set_messaging_handler(T t, messaging_handler* mh) { context<T>::type::get(factory<T>::unwrap(t)).handler = mh; }
134
135 template <class T>
136 inline messaging_handler* get_messaging_handler(T* t) { return context<typename internal::wrapper<T>::type>::type::get(t).handler; }
137
138 class returned_factory {
139 public:
140 template <class T> static returned<T> make(typename internal::wrapped<T>::type* pn) {
141 return returned<T>(pn);
142 }
143 };
144
145 } // namespace internal
146
147 template <class T>
148 typename internal::wrapper<T>::type make_wrapper(T* t) { return internal::factory<typename internal::wrapper<T>::type>::wrap(t); }
149
150 template <class U>
151 U make_wrapper(typename internal::wrapped<U>::type* t) { return internal::factory<U>::wrap(t); }
152
153 template <class T>
154 typename internal::wrapped<T>::type* unwrap(const T& t) { return internal::factory<T>::unwrap(t); }
155
156 template <class T> returned<T> make_returned(typename internal::wrapped<T>::type* pn) {
157 return internal::returned_factory::make<T>(pn);
158 }
159
160 // Get an AMQP "multiple" field from a value. A "multiple" field can be encoded as a single
161 // value or as an array. This function always extracts it as a sequence, a sequence of one
162 // if it is encoded as a single value.
163 //
164 // T should be a valid sequence type for proton::get() with a T::value_type typedef.
165 //
166 template<class T>
167 void get_multiple(const value& v, T& x) {
168 if (v.empty()) {
169 x.clear();
170 } else if (v.type() == ARRAY) {
171 proton::get(v,x);
172 } else {
173 x.resize(1);
174 proton::get(v,x[0]);
175 }
176 }
177
178 // Same as previous but returns the value.
179 template<class T>
180 T get_multiple(const value& v) {
181 T x;
182 get_multiple(v, x);
183 return x;
184 }
185
186 } // namespace proton
187
188 #endif // PROTON_BITS_HPP