Qpid Proton C++  0.17.0
value.hpp
1 #ifndef PROTON_VALUE_HPP
2 #define PROTON_VALUE_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 "./codec/encoder.hpp"
26 #include "./codec/decoder.hpp"
27 #include "./internal/type_traits.hpp"
28 #include "./scalar.hpp"
29 #include "./types_fwd.hpp"
30 
31 #include <proton/type_compat.h>
32 
33 #include <iosfwd>
34 
35 namespace proton {
36 
37 namespace internal {
38 
39 // Separate value data from implicit conversion constructors to avoid template recursion.
40 class value_base {
41  protected:
42  internal::data& data();
43  internal::data data_;
44 
45  friend class value_ref;
46  friend class codec::encoder;
47  friend class codec::decoder;
48 };
49 
50 } // internal
51 
55 class value : public internal::value_base, private internal::comparable<value> {
56  private:
57  // Enabler for encodable types excluding proton::value.
58  template<class T, class U=void> struct assignable :
59  public internal::enable_if<codec::is_encodable<T>::value, U> {};
60  template<class U> struct assignable<value, U> {};
61 
62  public:
64  PN_CPP_EXTERN value();
65 
68  PN_CPP_EXTERN value(const value&);
69  PN_CPP_EXTERN value& operator=(const value&);
70 #if PN_CPP_HAS_RVALUE_REFERENCES
71  PN_CPP_EXTERN value(value&&);
72  PN_CPP_EXTERN value& operator=(value&&);
73 #endif
74 
77  template <class T> value(const T& x, typename assignable<T>::type* = 0) { *this = x; }
78 
80  template <class T> typename assignable<T, value&>::type operator=(const T& x) {
81  codec::encoder e(*this);
82  e << x;
83  return *this;
84  }
85 
87  PN_CPP_EXTERN type_id type() const;
88 
90  PN_CPP_EXTERN bool empty() const;
91 
92 
94  PN_CPP_EXTERN void clear();
95 
97  template<class T> void get(T &t) const;
98  template<class T> T get() const;
99  PN_CPP_EXTERN int64_t as_int() const;
100  PN_CPP_EXTERN uint64_t as_uint() const;
101  PN_CPP_EXTERN double as_double() const;
102  PN_CPP_EXTERN std::string as_string() const;
104 
106  friend PN_CPP_EXTERN void swap(value&, value&);
107 
110  friend PN_CPP_EXTERN bool operator==(const value& x, const value& y);
111  friend PN_CPP_EXTERN bool operator<(const value& x, const value& y);
113 
118  friend PN_CPP_EXTERN std::ostream& operator<<(std::ostream&, const value&);
119 };
120 
121 namespace internal {
122 
123 // value_ref is a `pn_data_t* p` that can be returned as a value& and used to modify
124 // the underlying value in-place.
125 //
126 // Classes with a value_ref member can return it as a value& in accessor functions.
127 // It can also be used to copy a pn_data_t* p to a proton::value via: value(value_ref(p));
128 // None of the constructors make copies, they just refer to the same value.
129 //
130 class value_ref : public value {
131  public:
132  value_ref(pn_data_t* = 0);
133  value_ref(const internal::data&);
134  value_ref(const value_base&);
135 
136  // Use refer() not operator= to avoid confusion with value op=
137  void refer(pn_data_t*);
138  void refer(const internal::data&);
139  void refer(const value_base&);
140 
141  // Reset to refer to nothing, release existing references. Equivalent to refer(0).
142  void reset();
143 
144  // Assignments to value_ref means assigning to the value.
145  template <class T> value_ref& operator=(const T& x) {
146  static_cast<value&>(*this) = x;
147  return *this;
148  }
149 };
150 
151 }
152 
153 
156 template<class T> T get(const value& v) { T x; get(v, x); return x; }
157 
163 template<class T> void get(const value& v, T& x) { codec::decoder d(v, true); d >> x; }
164 
166 template<class T, class U> inline void get(const U& u, T& x) { const value v(u); get(v, x); }
167 
170 template<class T> T coerce(const value& v) { T x; coerce(v, x); return x; }
171 
177 template<class T> void coerce(const value& v, T& x) {
178  codec::decoder d(v, false);
179  if (type_id_is_scalar(v.type())) {
180  scalar s;
181  d >> s;
182  x = internal::coerce<T>(s);
183  } else {
184  d >> x;
185  }
186 }
187 
189 template<> inline void get<null>(const value& v, null&) {
190  assert_type_equal(NULL_TYPE, v.type());
191 }
192 
194 PN_CPP_EXTERN std::string to_string(const value& x);
195 
197 template<class T> void value::get(T &x) const { x = proton::get<T>(*this); }
198 template<class T> T value::get() const { return proton::get<T>(*this); }
199 inline int64_t value::as_int() const { return proton::coerce<int64_t>(*this); }
200 inline uint64_t value::as_uint() const { return proton::coerce<uint64_t>(*this); }
201 inline double value::as_double() const { return proton::coerce<double>(*this); }
202 inline std::string value::as_string() const { return proton::coerce<std::string>(*this); }
204 
205 } // proton
206 
207 #endif // PROTON_VALUE_HPP
A holder for an instance of any scalar AMQP type.
Definition: scalar.hpp:32
std::ostream & operator<<(std::ostream &, const binary &)
Print a binary value.
T get(const value &v)
Get a contained value of type T.
Definition: value.hpp:156
The null type, contains no data.
Definition: type_id.hpp:39
void get< null >(const value &v, null &)
Special case for get<null>(), just checks that value contains NULL.
Definition: value.hpp:189
Experimental - Stream-like encoder from C++ values to AMQP bytes.
Definition: encoder.hpp:47
assignable< T, value & >::type operator=(const T &x)
Assign from any allowed type T.
Definition: value.hpp:80
value(const T &x, typename assignable< T >::type *=0)
Construct from any allowed type T.
Definition: value.hpp:77
void coerce(const value &v, T &x)
Like coerce(const value&) but assigns the value to a reference instead of returning it...
Definition: value.hpp:177
std::string to_string(const 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
T coerce(const value &v)
Coerce the contained value to type T.
Definition: value.hpp:170
Forward declarations for all the C++ types used by Proton to represent AMQP types.
A holder for any AMQP value, simple or complex.
Definition: value.hpp:55
void assert_type_equal(type_id want, type_id got)
Throw a conversion_error if want != got with a message including the names of the types...
The main Proton namespace.
Definition: annotation_key.hpp:30
type_id type() const
Get the type ID for the current value.
Experimental - Stream-like decoder from AMQP bytes to C++ values.
Definition: decoder.hpp:53