Qpid Proton C++  0.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
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 <iosfwd>
32 
33 namespace proton {
34 
35 class message;
36 
37 namespace internal {
38 
39 class value_base;
40 PN_CPP_EXTERN std::ostream& operator<<(std::ostream& o, const value_base& x);
41 
44 class value_base {
45  public:
47  PN_CPP_EXTERN type_id type() const;
48 
50  PN_CPP_EXTERN bool empty() const;
51 
52  protected:
53  internal::data& data() const;
54  mutable class internal::data data_;
55 
57  friend class proton::message;
58  friend class codec::encoder;
59  friend class codec::decoder;
60  friend PN_CPP_EXTERN std::ostream& operator<<(std::ostream&, const value_base&);
62 };
63 
64 } // internal
65 
69 class value : public internal::value_base, private internal::comparable<value> {
70  private:
71  // Enabler for encodable types excluding proton::value.
72  template<class T, class U=void> struct assignable :
73  public internal::enable_if<codec::is_encodable<T>::value, U> {};
74  template<class U> struct assignable<value, U> {};
75 
76  public:
78  PN_CPP_EXTERN value();
79 
82  PN_CPP_EXTERN value(const value&);
83  PN_CPP_EXTERN value& operator=(const value&);
84 #if PN_CPP_HAS_RVALUE_REFERENCES
85  PN_CPP_EXTERN value(value&&);
86  PN_CPP_EXTERN value& operator=(value&&);
87 #endif
88 
91  template <class T> value(const T& x, typename assignable<T>::type* = 0) { *this = x; }
92 
94  template <class T> typename assignable<T, value&>::type operator=(const T& x) {
95  codec::encoder e(*this);
96  e << x;
97  return *this;
98  }
99 
101  PN_CPP_EXTERN void clear();
102 
104  template<class T> void get(T &t) const;
105  template<class T> T get() const;
106  PN_CPP_EXTERN int64_t as_int() const;
107  PN_CPP_EXTERN uint64_t as_uint() const;
108  PN_CPP_EXTERN double as_double() const;
109  PN_CPP_EXTERN std::string as_string() const;
111 
113  friend PN_CPP_EXTERN void swap(value&, value&);
114 
117  friend PN_CPP_EXTERN bool operator==(const value& x, const value& y);
118  friend PN_CPP_EXTERN bool operator<(const value& x, const value& y);
120 
122  PN_CPP_EXTERN explicit value(const internal::data&);
124 };
125 
128 template<class T> T get(const value& v) { T x; get(v, x); return x; }
129 
135 template<class T> void get(const value& v, T& x) { codec::decoder d(v, true); d >> x; }
136 
139 template<class T> T coerce(const value& v) { T x; coerce(v, x); return x; }
140 
146 template<class T> void coerce(const value& v, T& x) {
147  codec::decoder d(v, false);
148  if (type_id_is_scalar(v.type())) {
149  scalar s;
150  d >> s;
151  x = internal::coerce<T>(s);
152  } else {
153  d >> x;
154  }
155 }
156 
158 template<> inline void get<null>(const value& v, null&) { assert_type_equal(NULL_TYPE, v.type()); }
159 
161 template<class T> void value::get(T &x) const { x = proton::get<T>(*this); }
162 template<class T> T value::get() const { return proton::get<T>(*this); }
163 inline int64_t value::as_int() const { return proton::coerce<int64_t>(*this); }
164 inline uint64_t value::as_uint() const { return proton::coerce<uint64_t>(*this); }
165 inline double value::as_double() const { return proton::coerce<double>(*this); }
166 inline std::string value::as_string() const { return proton::coerce<std::string>(*this); }
168 
169 } // proton
170 
171 #endif // PROTON_VALUE_HPP
A holder for an instance of any scalar AMQP type.
Definition: scalar.hpp:35
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:128
An AMQP message.
Definition: message.hpp:51
The null type, contains no data.
Definition: type_id.hpp:39
void get< null >(const value &v, null &)
Special case for get&lt;null&gt;(), just checks that value contains NULL.
Definition: value.hpp:158
Experimental - Stream-like encoder from AMQP bytes to C++ values.
Definition: encoder.hpp:45
value()
Create a null value.
assignable< T, value & >::type operator=(const T &x)
Assign from any allowed type T.
Definition: value.hpp:94
friend void swap(value &, value &)
swap values
value(const T &x, typename assignable< T >::type *=0)
Construct from any allowed type T.
Definition: value.hpp:91
void coerce(const value &v, T &x)
Like coerce(const value&amp;) but assigns the value to a reference instead of returning it...
Definition: value.hpp:146
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:139
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:69
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...
void clear()
Reset the value to null.
Experimental - Stream-like decoder from AMQP bytes to C++ values.
Definition: decoder.hpp:51