Qpid Proton C++  0.14.0
scalar_base.hpp
1 #ifndef PROTON_INTERNAL_SCALAR_BASE_HPP
2 #define PROTON_INTERNAL_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 "./export.hpp"
29 #include "./comparable.hpp"
30 #include "./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 <iosfwd>
38 #include <string>
39 #include <typeinfo>
40 
41 namespace proton {
42 class message;
43 
44 namespace codec {
45 class decoder;
46 class encoder;
47 }
48 
49 namespace internal {
50 
51 class scalar_base;
52 template<class T> T get(const scalar_base& s);
53 
55 class scalar_base : private comparable<scalar_base> {
56  public:
58  PN_CPP_EXTERN type_id type() const;
59 
60  // XXX I don't think many folks ever used this stuff. Let's
61  // remove it. - Yes, try to remove them.
64  template <class T> void get(T& x) const { get_(x); }
65  template <class T> T get() const { T x; get_(x); return x; }
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> struct putter<T, typename enable_if<is_unknown_integer<T>::value>::type> {
134  static void put(scalar_base& s, const T& x) { s.put_(static_cast<typename known_integer<T>::type>(x)); }
135  };
136  template <class T, class Enable=void> struct getter {
137  static T get(const scalar_base& s) { T x; s.get_(x); return x; }
138  };
139  template <class T> struct getter<T, typename enable_if<is_unknown_integer<T>::value>::type> {
140  static T get(const scalar_base& s) { typename known_integer<T>::type x; s.get_(x); return x; }
141  };
142 
143  void ok(pn_type_t) const;
144  void set(const pn_atom_t&);
145  void set(const binary& x, pn_type_t t);
146 
147  pn_atom_t atom_;
148  binary bytes_; // Hold binary data.
149 
151  friend class proton::message;
152  friend class codec::encoder;
153  friend class codec::decoder;
154  template<class T> friend T get(const scalar_base& s) { return scalar_base::getter<T>::get(s); }
156 };
157 
158 template <class R, class F> R visit(const scalar_base& s, F f) {
159  switch(s.type()) {
160  case BOOLEAN: return f(s.get<bool>());
161  case UBYTE: return f(s.get<uint8_t>());
162  case BYTE: return f(s.get<int8_t>());
163  case USHORT: return f(s.get<uint16_t>());
164  case SHORT: return f(s.get<int16_t>());
165  case UINT: return f(s.get<uint32_t>());
166  case INT: return f(s.get<int32_t>());
167  case CHAR: return f(s.get<wchar_t>());
168  case ULONG: return f(s.get<uint64_t>());
169  case LONG: return f(s.get<int64_t>());
170  case TIMESTAMP: return f(s.get<timestamp>());
171  case FLOAT: return f(s.get<float>());
172  case DOUBLE: return f(s.get<double>());
173  case DECIMAL32: return f(s.get<decimal32>());
174  case DECIMAL64: return f(s.get<decimal64>());
175  case DECIMAL128: return f(s.get<decimal128>());
176  case UUID: return f(s.get<uuid>());
177  case BINARY: return f(s.get<binary>());
178  case STRING: return f(s.get<std::string>());
179  case SYMBOL: return f(s.get<symbol>());
180  default: throw conversion_error("invalid scalar type "+type_name(s.type()));
181  }
182 }
183 
184 PN_CPP_EXTERN conversion_error make_coercion_error(const char* cpp_type, type_id amqp_type);
185 
186 template<class T> struct coerce_op {
187  template <class U>
188  typename enable_if<is_convertible<U, T>::value, T>::type operator()(const U& x) {
189  return static_cast<T>(x);
190  }
191  template <class U>
192  typename enable_if<!is_convertible<U, T>::value, T>::type operator()(const U&) {
193  throw make_coercion_error(typeid(T).name(), type_id_of<U>::value);
194  }
195 };
196 
197 template <class T> T coerce(const scalar_base& s) { return visit<T>(s, coerce_op<T>()); }
198 
199 } // internal
200 
202 PN_CPP_EXTERN std::string to_string(const internal::scalar_base& x);
203 
204 } // proton
205 
206 #endif // PROTON_INTERNAL_SCALAR_BASE_HPP
16-byte UUID.
Definition: type_id.hpp:56
An AMQP message.
Definition: message.hpp:51
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:34
Variable-length utf8-encoded string.
Definition: type_id.hpp:58
std::string type_name(type_id)
Get the name of the AMQP type.
Variable-length encoded string.
Definition: type_id.hpp:59
Signed 64-bit milliseconds since the epoch.
Definition: type_id.hpp:50
Variable-length sequence of bytes.
Definition: type_id.hpp:57
64-bit binary floating point.
Definition: type_id.hpp:52
32-bit decimal floating point.
Definition: type_id.hpp:53
Signed 8-bit integer.
Definition: type_id.hpp:42
Unsigned 8-bit integer.
Definition: type_id.hpp:41
Signed 64-bit integer.
Definition: type_id.hpp:49
Boolean true or false.
Definition: type_id.hpp:40
std::string to_string(const internal::scalar_base &x)
Return a readable string representation of x for display purposes.
type_id
An identifier for AMQP types.
Definition: type_id.hpp:38
32-bit unicode character.
Definition: type_id.hpp:47
Unsigned 32-bit integer.
Definition: type_id.hpp:45
Unsigned 64-bit integer.
Definition: type_id.hpp:48
Signed 16-bit integer.
Definition: type_id.hpp:44
Signed 32-bit integer.
Definition: type_id.hpp:46
Unsigned 16-bit integer.
Definition: type_id.hpp:43
64-bit decimal floating point.
Definition: type_id.hpp:54
128-bit decimal floating point.
Definition: type_id.hpp:55
Type traits for mapping between AMQP and C++ types.
Definition: annotation_key.hpp:28
32-bit binary floating point.
Definition: type_id.hpp:51