Qpid Proton C++ API  0.18.1
scalar_base.hpp
Go to the documentation of this file.
1 #ifndef PROTON_SCALAR_BASE_HPP
2 #define PROTON_SCALAR_BASE_HPP
3 
4 /*
5  *
6  * Licensed to the Apache Software Foundation (ASF) under one
7  * or more contributor license agreements. See the NOTICE file
8  * distributed with this work for additional information
9  * regarding copyright ownership. The ASF licenses this file
10  * to you under the Apache License, Version 2.0 (the
11  * "License"); you may not use this file except in compliance
12  * with the License. You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing,
17  * software distributed under the License is distributed on an
18  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19  * KIND, either express or implied. See the License for the
20  * specific language governing permissions and limitations
21  * under the License.
22  *
23  */
24 
25 #include "./binary.hpp"
26 #include "./decimal.hpp"
27 #include "./error.hpp"
28 #include "./internal/comparable.hpp"
29 #include "./internal/export.hpp"
30 #include "./internal/type_traits.hpp"
31 #include "./symbol.hpp"
32 #include "./timestamp.hpp"
33 #include "./type_id.hpp"
34 #include "./types_fwd.hpp"
35 #include "./uuid.hpp"
36 
37 #include <proton/type_compat.h>
38 
39 #include <iosfwd>
40 #include <string>
41 #include <typeinfo>
42 
45 
46 namespace proton {
47 
48 class scalar_base;
49 
50 namespace codec {
51 class decoder;
52 class encoder;
53 }
54 
55 namespace internal {
56 template<class T> T get(const scalar_base& s);
57 }
58 
60 class scalar_base : private internal::comparable<scalar_base> {
61  public:
63  PN_CPP_EXTERN type_id type() const;
64 
66  PN_CPP_EXTERN bool empty() const;
67 
69  friend PN_CPP_EXTERN bool operator<(const scalar_base& x, const scalar_base& y);
71  friend PN_CPP_EXTERN bool operator==(const scalar_base& x, const scalar_base& y);
73  friend PN_CPP_EXTERN std::ostream& operator<<(std::ostream& o, const scalar_base& x);
74 
75  protected:
76  PN_CPP_EXTERN scalar_base(const pn_atom_t& a);
77  PN_CPP_EXTERN scalar_base();
78  PN_CPP_EXTERN scalar_base(const scalar_base&);
79  PN_CPP_EXTERN scalar_base& operator=(const scalar_base&);
80 
81  PN_CPP_EXTERN void put_(bool);
82  PN_CPP_EXTERN void put_(uint8_t);
83  PN_CPP_EXTERN void put_(int8_t);
84  PN_CPP_EXTERN void put_(uint16_t);
85  PN_CPP_EXTERN void put_(int16_t);
86  PN_CPP_EXTERN void put_(uint32_t);
87  PN_CPP_EXTERN void put_(int32_t);
88  PN_CPP_EXTERN void put_(uint64_t);
89  PN_CPP_EXTERN void put_(int64_t);
90  PN_CPP_EXTERN void put_(wchar_t);
91  PN_CPP_EXTERN void put_(float);
92  PN_CPP_EXTERN void put_(double);
93  PN_CPP_EXTERN void put_(timestamp);
94  PN_CPP_EXTERN void put_(const decimal32&);
95  PN_CPP_EXTERN void put_(const decimal64&);
96  PN_CPP_EXTERN void put_(const decimal128&);
97  PN_CPP_EXTERN void put_(const uuid&);
98  PN_CPP_EXTERN void put_(const std::string&);
99  PN_CPP_EXTERN void put_(const symbol&);
100  PN_CPP_EXTERN void put_(const binary&);
101  PN_CPP_EXTERN void put_(const char* s);
102  PN_CPP_EXTERN void put_(const null&);
103 
104  template<class T> void put(const T& x) { putter<T>::put(*this, x); }
105 
106  private:
107  PN_CPP_EXTERN void get_(bool&) const;
108  PN_CPP_EXTERN void get_(uint8_t&) const;
109  PN_CPP_EXTERN void get_(int8_t&) const;
110  PN_CPP_EXTERN void get_(uint16_t&) const;
111  PN_CPP_EXTERN void get_(int16_t&) const;
112  PN_CPP_EXTERN void get_(uint32_t&) const;
113  PN_CPP_EXTERN void get_(int32_t&) const;
114  PN_CPP_EXTERN void get_(uint64_t&) const;
115  PN_CPP_EXTERN void get_(int64_t&) const;
116  PN_CPP_EXTERN void get_(wchar_t&) const;
117  PN_CPP_EXTERN void get_(float&) const;
118  PN_CPP_EXTERN void get_(double&) const;
119  PN_CPP_EXTERN void get_(timestamp&) const;
120  PN_CPP_EXTERN void get_(decimal32&) const;
121  PN_CPP_EXTERN void get_(decimal64&) const;
122  PN_CPP_EXTERN void get_(decimal128&) const;
123  PN_CPP_EXTERN void get_(uuid&) const;
124  PN_CPP_EXTERN void get_(std::string&) const;
125  PN_CPP_EXTERN void get_(symbol&) const;
126  PN_CPP_EXTERN void get_(binary&) const;
127  PN_CPP_EXTERN void get_(null&) const;
128 
129  // use template structs, functions cannot be partially specialized.
130  template <class T, class Enable=void> struct putter {
131  static void put(scalar_base& s, const T& x) { s.put_(x); }
132  };
133  template <class T>
134  struct putter<T, typename internal::enable_if<internal::is_unknown_integer<T>::value>::type> {
135  static void put(scalar_base& s, const T& x) {
136  s.put_(static_cast<typename internal::known_integer<T>::type>(x));
137  }
138  };
139  template <class T, class Enable=void>
140  struct getter {
141  static T get(const scalar_base& s) { T x; s.get_(x); return x; }
142  };
143  template <class T>
144  struct getter<T, typename internal::enable_if<internal::is_unknown_integer<T>::value>::type> {
145  static T get(const scalar_base& s) {
146  typename internal::known_integer<T>::type x; s.get_(x); return x;
147  }
148  };
149 
150  void ok(pn_type_t) const;
151  void set(const pn_atom_t&);
152  void set(const binary& x, pn_type_t t);
153 
154  pn_atom_t atom_;
155  binary bytes_; // Hold binary data.
156 
158  friend class message;
159  friend class codec::encoder;
160  friend class codec::decoder;
161  template<class T> friend T internal::get(const scalar_base& s);
163 };
164 
165 namespace internal {
166 
167 template<class T> T get(const scalar_base& s) {
168  return scalar_base::getter<T>::get(s);
169 }
170 
171 template <class R, class F> R visit(const scalar_base& s, F f) {
172  switch(s.type()) {
173  case BOOLEAN: return f(internal::get<bool>(s));
174  case UBYTE: return f(internal::get<uint8_t>(s));
175  case BYTE: return f(internal::get<int8_t>(s));
176  case USHORT: return f(internal::get<uint16_t>(s));
177  case SHORT: return f(internal::get<int16_t>(s));
178  case UINT: return f(internal::get<uint32_t>(s));
179  case INT: return f(internal::get<int32_t>(s));
180  case CHAR: return f(internal::get<wchar_t>(s));
181  case ULONG: return f(internal::get<uint64_t>(s));
182  case LONG: return f(internal::get<int64_t>(s));
183  case TIMESTAMP: return f(internal::get<timestamp>(s));
184  case FLOAT: return f(internal::get<float>(s));
185  case DOUBLE: return f(internal::get<double>(s));
186  case DECIMAL32: return f(internal::get<decimal32>(s));
187  case DECIMAL64: return f(internal::get<decimal64>(s));
188  case DECIMAL128: return f(internal::get<decimal128>(s));
189  case UUID: return f(internal::get<uuid>(s));
190  case BINARY: return f(internal::get<binary>(s));
191  case STRING: return f(internal::get<std::string>(s));
192  case SYMBOL: return f(internal::get<symbol>(s));
193  default: throw conversion_error("invalid scalar type "+type_name(s.type()));
194  }
195 }
196 
197 PN_CPP_EXTERN conversion_error make_coercion_error(const char* cpp_type, type_id amqp_type);
198 
199 template<class T> struct coerce_op {
200  template <class U>
201  typename enable_if<is_convertible<U, T>::value, T>::type operator()(const U& x) {
202  return static_cast<T>(x);
203  }
204  template <class U>
205  typename enable_if<!is_convertible<U, T>::value, T>::type operator()(const U&) {
206  throw make_coercion_error(typeid(T).name(), type_id_of<U>::value);
207  }
208 };
209 
210 template <class T> T coerce(const scalar_base& s) { return visit<T>(s, coerce_op<T>()); }
211 } // namespace internal
212 
214 PN_CPP_EXTERN std::string to_string(const scalar_base& x);
215 
216 } // proton
217 
218 #endif // PROTON_SCALAR_BASE_HPP
16-byte UUID.
Definition: type_id.hpp:55
An AMQP message.
Definition: message.hpp:50
Unsettled API - A stream-like encoder from C++ values to AMQP bytes.
Definition: encoder.hpp:50
encoder & operator<<(encoder &e, const std::deque< T, A > &x)
std::deque<T> for most T is encoded as an amqp::ARRAY (same type elements)
Definition: deque.hpp:37
Variable-length utf8-encoded string.
Definition: type_id.hpp:57
A 16-byte universally unique identifier.
Definition: uuid.hpp:37
std::string type_name(type_id)
Get the name of the AMQP type.
An error converting between AMQP and C++ data.
Definition: error.hpp:54
A 64-bit decimal floating-point value.
Definition: decimal.hpp:49
AMQP decimal types.
Variable-length encoded string.
Definition: type_id.hpp:58
The base class for scalar types.
Definition: scalar_base.hpp:60
Signed 64-bit milliseconds since the epoch.
Definition: type_id.hpp:49
Variable-length sequence of bytes.
Definition: type_id.hpp:56
64-bit binary floating point.
Definition: type_id.hpp:51
symbol get< symbol >(const annotation_key &x)
Get the symbol value or throw conversion_error.
Definition: annotation_key.hpp:77
A string that represents the AMQP symbol type.
Definition: symbol.hpp:35
Type IDs for AMQP data types.
32-bit decimal floating point.
Definition: type_id.hpp:52
Arbitrary binary data.
Definition: binary.hpp:40
A 128-bit decimal floating-point value.
Definition: decimal.hpp:52
Signed 8-bit integer.
Definition: type_id.hpp:41
Unsigned 8-bit integer.
Definition: type_id.hpp:40
Signed 64-bit integer.
Definition: type_id.hpp:48
std::string to_string(const scalar_base &x)
Return a readable string representation of x for display purposes.
Boolean true or false.
Definition: type_id.hpp:39
type_id
An identifier for AMQP types.
Definition: type_id.hpp:37
A string that represents the AMQP symbol type.
A 32-bit decimal floating-point value.
Definition: decimal.hpp:46
32-bit unicode character.
Definition: type_id.hpp:46
Forward declarations for Proton types used to represent AMQP types.
type_id type() const
AMQP type of data stored in the scalar.
T coerce(const annotation_key &x)
Get the binary value or throw conversion_error.
Definition: annotation_key.hpp:83
A 64-bit timestamp in milliseconds since the Unix epoch.
Definition: timestamp.hpp:35
Unsigned 32-bit integer.
Definition: type_id.hpp:44
uuid get< uuid >(const message_id &x)
Get the uuid value or throw conversion_error.
Definition: message_id.hpp:83
Unsigned 64-bit integer.
Definition: type_id.hpp:47
The base Proton error.
Signed 16-bit integer.
Definition: type_id.hpp:43
Signed 32-bit integer.
Definition: type_id.hpp:45
Unsigned 16-bit integer.
Definition: type_id.hpp:42
Arbitrary binary data.
A 16-byte universally unique identifier.
64-bit decimal floating point.
Definition: type_id.hpp:53
128-bit decimal floating point.
Definition: type_id.hpp:54
The main Proton namespace.
Definition: annotation_key.hpp:33
A 64-bit timestamp in milliseconds since the Unix epoch.
binary get< binary >(const message_id &x)
Get the binary value or throw conversion_error.
Definition: message_id.hpp:85
Unsettled API - A stream-like decoder from AMQP bytes to C++ values.
Definition: decoder.hpp:56
uint64_t get< uint64_t >(const annotation_key &x)
Get the uint64_t value or throw conversion_error.
Definition: annotation_key.hpp:72
32-bit binary floating point.
Definition: type_id.hpp:50