Qpid Proton C++  0.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
types.hpp
Go to the documentation of this file.
1 #ifndef TYPES_H
2 #define TYPES_H
3 
4 /*
5  * Licensed to the Apache Software Foundation (ASF) under one
6  * or more contributor license agreements. See the NOTICE file
7  * distributed with this work for additional information
8  * regarding copyright ownership. The ASF licenses this file
9  * to you under the Apache License, Version 2.0 (the
10  * "License"); you may not use this file except in compliance
11  * with the License. You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing,
16  * software distributed under the License is distributed on an
17  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18  * KIND, either express or implied. See the License for the
19  * specific language governing permissions and limitations
20  * under the License.
21  */
22 
26 
27 #include "proton/comparable.hpp"
28 #include "proton/export.hpp"
29 #include "proton/error.hpp"
30 
31 #include <proton/codec.h> // XXX everywhere else folks are using "codec.h"
32 #include <proton/type_compat.h>
33 #include <algorithm>
34 #include <bitset>
35 #include <string>
36 #include <memory.h>
37 
38 namespace proton {
39 
41 enum type_id {
42  NULL_TYPE = PN_NULL,
43  BOOLEAN = PN_BOOL,
44  UBYTE = PN_UBYTE,
45  BYTE = PN_BYTE,
46  USHORT = PN_USHORT,
47  SHORT = PN_SHORT,
48  UINT = PN_UINT,
49  INT = PN_INT,
50  CHAR = PN_CHAR,
51  ULONG = PN_ULONG,
52  LONG = PN_LONG,
53  TIMESTAMP = PN_TIMESTAMP,
54  FLOAT = PN_FLOAT,
55  DOUBLE = PN_DOUBLE,
56  DECIMAL32 = PN_DECIMAL32,
57  DECIMAL64 = PN_DECIMAL64,
58  DECIMAL128 = PN_DECIMAL128,
59  UUID = PN_UUID,
60  BINARY = PN_BINARY,
61  STRING = PN_STRING,
62  SYMBOL = PN_SYMBOL,
63  DESCRIBED = PN_DESCRIBED,
64  ARRAY = PN_ARRAY,
65  LIST = PN_LIST,
66  MAP = PN_MAP
67 };
68 
70 PN_CPP_EXTERN std::string type_name(type_id);
71 
73 PN_CPP_EXTERN std::ostream& operator<<(std::ostream&, type_id);
74 
76 
80 struct type_error : public decode_error {
81  PN_CPP_EXTERN explicit type_error(type_id want, type_id got, const std::string& =std::string());
82  type_id want;
83  type_id got;
84 };
85 
86 PN_CPP_EXTERN pn_bytes_t pn_bytes(const std::string&);
87 PN_CPP_EXTERN std::string str(const pn_bytes_t& b);
88 
90 struct amqp_null {};
92 typedef bool amqp_boolean;
94 typedef ::uint8_t amqp_ubyte;
96 typedef ::int8_t amqp_byte;
98 typedef ::uint16_t amqp_ushort;
100 typedef ::int16_t amqp_short;
102 typedef ::uint32_t amqp_uint;
104 typedef ::int32_t amqp_int;
106 typedef wchar_t amqp_char;
108 typedef ::uint64_t amqp_ulong;
110 typedef ::int64_t amqp_long;
112 typedef float amqp_float;
114 typedef double amqp_double;
115 
117 struct amqp_string : public std::string {
118  explicit amqp_string(const std::string& s=std::string()) : std::string(s) {}
119  explicit amqp_string(const char* s) : std::string(s) {}
120  explicit amqp_string(const pn_bytes_t& b) : std::string(b.start, b.size) {}
121 };
122 
124 struct amqp_symbol : public std::string {
125  explicit amqp_symbol(const std::string& s=std::string()) : std::string(s) {}
126  explicit amqp_symbol(const char* s) : std::string(s) {}
127  explicit amqp_symbol(const pn_bytes_t& b) : std::string(b.start, b.size) {}
128 };
129 
131 struct amqp_binary : public std::string {
132  explicit amqp_binary(const std::string& s=std::string()) : std::string(s) {}
133  explicit amqp_binary(const char* s) : std::string(s) {}
134  explicit amqp_binary(const pn_bytes_t& b) : std::string(b.start, b.size) {}
135 };
136 
139 template <class P> struct opaque : public comparable<opaque<P> > {
140  P value;
141  opaque(const P& p=P()) : value(p) {}
142  operator P() const { return value; }
143 
144  static size_t size() { return sizeof(P); }
145  char* begin() { return reinterpret_cast<char*>(&value); }
146  char* end() { return reinterpret_cast<char*>(&value)+size(); }
147  const char* begin() const { return reinterpret_cast<const char*>(&value); }
148  const char* end() const { return reinterpret_cast<const char*>(&value)+size(); }
149  char& operator[](size_t i) { return *(begin()+i); }
150  const char& operator[](size_t i) const { return *(begin()+i); }
151 };
152 
153 template <class T> bool operator==(const opaque<T>& x, const opaque<T>& y) { return std::equal(x.begin(), x.end(), y.begin()); }
154 template <class T> bool operator<(const opaque<T>& x, const opaque<T>& y) { return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
155 
157 typedef opaque<pn_uuid_t> amqp_uuid;
158 PN_CPP_EXTERN std::ostream& operator<<(std::ostream&, const amqp_uuid&);
159 
161 typedef opaque<pn_decimal32_t> amqp_decimal32;
162 PN_CPP_EXTERN std::ostream& operator<<(std::ostream&, const amqp_decimal32&);
163 
165 typedef opaque<pn_decimal64_t> amqp_decimal64;
166 PN_CPP_EXTERN std::ostream& operator<<(std::ostream&, const amqp_decimal64&);
167 
169 typedef opaque<pn_decimal128_t> amqp_decimal128;
170 PN_CPP_EXTERN std::ostream& operator<<(std::ostream&, const amqp_decimal128&);
171 
173 struct amqp_timestamp : public comparable<amqp_timestamp> {
174  pn_timestamp_t milliseconds;
175  amqp_timestamp(::int64_t ms=0) : milliseconds(ms) {}
176  operator pn_timestamp_t() const { return milliseconds; }
177 };
178 inline bool operator==(amqp_timestamp x, amqp_timestamp y) { return x.milliseconds == y.milliseconds; }
179 inline bool operator<(amqp_timestamp x, amqp_timestamp y) { return x.milliseconds < y.milliseconds; }
180 PN_CPP_EXTERN std::ostream& operator<<(std::ostream&, const amqp_timestamp&);
181 
183 
184 // TODO aconway 2015-06-16: described types.
185 
193 
195 PN_CPP_EXTERN bool type_id_is_scalar(type_id);
197 PN_CPP_EXTERN bool type_id_is_signed_int(type_id);
199 PN_CPP_EXTERN bool type_id_is_unsigned_int(type_id);
201 PN_CPP_EXTERN bool type_id_is_integral(type_id);
203 PN_CPP_EXTERN bool type_id_is_floating_point(type_id);
205 PN_CPP_EXTERN bool type_id_is_signed(type_id);
207 PN_CPP_EXTERN bool type_id_is_decimal(type_id);
209 PN_CPP_EXTERN bool type_id_is_string_like(type_id);
211 PN_CPP_EXTERN bool type_id_is_container(type_id);
212 
214 
216 PN_CPP_EXTERN std::ostream& operator<<(std::ostream&, type_id);
217 
220 
225 struct start {
226  PN_CPP_EXTERN start(type_id type=NULL_TYPE, type_id element=NULL_TYPE, bool described=false, size_t size=0);
227  type_id type;
228  type_id element;
229  bool is_described;
230  size_t size;
231 
233  PN_CPP_EXTERN static start array(type_id element, bool described=false);
234 
236  PN_CPP_EXTERN static start list();
237 
239  PN_CPP_EXTERN static start map();
240 
242  PN_CPP_EXTERN static start described();
243 };
244 
246 struct finish {};
247 
249 
250 }
251 
252 #endif // TYPES_H