Qpid Proton C++ API  0.32.0
value.hpp
Go to the documentation of this file.
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 
37 
38 namespace proton {
39 
40 namespace internal {
41 
42 // Separate value data from implicit conversion constructors to avoid template recursion.
43 class value_base {
44  protected:
45  internal::data& data();
46  internal::data data_;
47 
48  friend class codec::encoder;
49  friend class codec::decoder;
50 };
51 
52 } // internal
53 
57 class value : public internal::value_base, private internal::comparable<value> {
58  private:
59  // Enabler for encodable types excluding proton::value.
60  template<class T, class U=void> struct assignable :
61  public internal::enable_if<codec::is_encodable<T>::value, U> {};
62  template<class U> struct assignable<value, U> {};
63 
64  public:
66  PN_CPP_EXTERN value();
67 
70  PN_CPP_EXTERN value(const value&);
71  PN_CPP_EXTERN value& operator=(const value&);
72 #if PN_CPP_HAS_RVALUE_REFERENCES
73  PN_CPP_EXTERN value(value&&);
74  PN_CPP_EXTERN value& operator=(value&&);
75 #endif
76 
79  template <class T> value(const T& x, typename assignable<T>::type* = 0) { *this = x; }
80 
82  template <class T> typename assignable<T, value&>::type operator=(const T& x) {
83  codec::encoder e(*this);
84  e << x;
85  return *this;
86  }
87 
89  PN_CPP_EXTERN type_id type() const;
90 
92  PN_CPP_EXTERN bool empty() const;
93 
95  PN_CPP_EXTERN void clear();
96 
98  template<class T> PN_CPP_DEPRECATED("Use 'proton::get'") void get(T &t) const;
99  template<class T> PN_CPP_DEPRECATED("Use 'proton::get'") T get() const;
101 
103  friend PN_CPP_EXTERN void swap(value&, value&);
104 
107  friend PN_CPP_EXTERN bool operator==(const value& x, const value& y);
108  friend PN_CPP_EXTERN bool operator<(const value& x, const value& y);
110 
115  friend PN_CPP_EXTERN std::ostream& operator<<(std::ostream&, const value&);
116 
119  value(pn_data_t* d); // Refer to existing pn_data_t
120  void reset(pn_data_t* d = 0); // Refer to a new pn_data_t
122 };
123 
126 template<class T> T get(const value& v) { T x; get(v, x); return x; }
127 
133 template<class T> void get(const value& v, T& x) { codec::decoder d(v, true); d >> x; }
134 
136 template<class T, class U> inline void get(const U& u, T& x) { const value v(u); get(v, x); }
137 
140 template<class T> T coerce(const value& v) { T x; coerce(v, x); return x; }
141 
147 template<class T> void coerce(const value& v, T& x) {
148  codec::decoder d(v, false);
149  scalar s;
150  if (type_id_is_scalar(v.type())) {
151  d >> s;
152  x = internal::coerce<T>(s);
153  } else {
154  d >> x;
155  }
156 }
157 
159 template<> inline void get<null>(const value& v, null&) {
161 }
162 #if PN_CPP_HAS_NULLPTR
163 template<> inline void get<decltype(nullptr)>(const value& v, decltype(nullptr)&) {
165  assert_type_equal(NULL_TYPE, v.type());
166 }
167 #endif
168 
170 PN_CPP_EXTERN std::string to_string(const value& x);
171 
173 template<class T> void value::get(T &x) const { x = proton::get<T>(*this); }
174 template<class T> T value::get() const { return proton::get<T>(*this); }
176 
177 } // proton
178 
179 #endif // PROTON_VALUE_HPP
proton::scalar
A holder for an instance of any scalar AMQP type.
Definition: scalar.hpp:37
proton::value::type
type_id type() const
Get the type ID for the current value.
proton::value::value
value()
Create a null value.
proton::get< null >
void get< null >(const value &v, null &)
Special case for null, just checks that value contains NULL.
Definition: value.hpp:159
scalar.hpp
A holder for an instance of any scalar AMQP type.
proton::value::empty
bool empty() const
True if the value is null.
proton::value
A holder for any AMQP value, simple or complex.
Definition: value.hpp:57
proton::value::clear
void clear()
Reset the value to null/empty.
proton::NULL_TYPE
@ NULL_TYPE
The null type, contains no data.
Definition: type_id.hpp:38
proton::to_string
std::string to_string(const message &)
Human readable string representation.
proton::value::get
T get(const value &v)
Get a contained value of type T.
Definition: value.hpp:126
proton::type_id
type_id
An identifier for AMQP types.
Definition: type_id.hpp:37
proton::codec::decoder
Unsettled API - A stream-like decoder from AMQP bytes to C++ values.
Definition: decoder.hpp:56
decoder.hpp
Unsettled API - A stream-like decoder from AMQP bytes to C++ values.
proton::value::coerce
T coerce(const value &v)
Coerce the contained value to type T.
Definition: value.hpp:140
proton::get< decltype(nullptr)>
void get< decltype(nullptr)>(const value &v, decltype(nullptr)&)
Special case for null, just checks that value contains NULL.
Definition: value.hpp:164
proton::value::operator=
assignable< T, value & >::type operator=(const T &x)
Assign from any allowed type T.
Definition: value.hpp:82
proton::value::value
value(const T &x, typename assignable< T >::type *=0)
Copy from any allowed type T.
Definition: value.hpp:79
proton::codec::encoder
Unsettled API - A stream-like encoder from C++ values to AMQP bytes.
Definition: encoder.hpp:50
proton
The main Proton namespace.
Definition: annotation_key.hpp:33
encoder.hpp
Unsettled API - A stream-like encoder from C++ values to AMQP bytes.
proton::value::swap
friend void swap(value &, value &)
swap values
proton::assert_type_equal
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.
types_fwd.hpp
Forward declarations for Proton types used to represent AMQP types.