Loading...
Searching...
No Matches
value.hpp
Go to the documentation of this file.
#ifndef PROTON_VALUE_HPP
#define PROTON_VALUE_HPP
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
#include "./codec/encoder.hpp"
#include "./codec/decoder.hpp"
#include "./internal/type_traits.hpp"
#include "./scalar.hpp"
#include "./types_fwd.hpp"
#include <proton/type_compat.h>
#include <iosfwd>
namespace proton {
namespace internal {
// Separate value data from implicit conversion constructors to avoid template recursion.
class value_base {
protected:
internal::data& data();
internal::data data_;
friend class codec::encoder;
friend class codec::decoder;
};
} // internal
class value : public internal::value_base, private internal::comparable<value> {
private:
// Enabler for encodable types excluding proton::value.
template<class T, class U=void> struct assignable :
public std::enable_if<codec::is_encodable<T>::value, U> {};
template<class U> struct assignable<value, U> {};
public:
PN_CPP_EXTERN value();
PN_CPP_EXTERN value(const value&);
PN_CPP_EXTERN value& operator=(const value&);
PN_CPP_EXTERN value(value&&);
PN_CPP_EXTERN value& operator=(value&&);
template <class T> value(const T& x, typename assignable<T>::type* = 0) { *this = x; }
template <class T> typename assignable<T, value&>::type operator=(const T& x) {
codec::encoder e(*this);
e << x;
return *this;
}
PN_CPP_EXTERN type_id type() const;
PN_CPP_EXTERN bool empty() const;
PN_CPP_EXTERN void clear();
template<class T> PN_CPP_DEPRECATED("Use 'proton::get'") void get(T &t) const;
template<class T> PN_CPP_DEPRECATED("Use 'proton::get'") T get() const;
friend PN_CPP_EXTERN void swap(value&, value&);
friend PN_CPP_EXTERN bool operator==(const value& x, const value& y);
friend PN_CPP_EXTERN bool operator<(const value& x, const value& y);
friend PN_CPP_EXTERN std::ostream& operator<<(std::ostream&, const value&);
value(pn_data_t* d); // Refer to existing pn_data_t
void reset(pn_data_t* d = 0); // Refer to a new pn_data_t
};
template<class T> T get(const value& v) { T x; get(v, x); return x; }
template<class T> void get(const value& v, T& x) { codec::decoder d(v, true); d >> x; }
template<class T, class U> inline void get(const U& u, T& x) { const value v(u); get(v, x); }
template<class T> T coerce(const value& v) { T x; coerce(v, x); return x; }
template<class T> void coerce(const value& v, T& x) {
codec::decoder d(v, false);
scalar s;
if (type_id_is_scalar(v.type())) {
d >> s;
x = internal::coerce<T>(s);
} else {
d >> x;
}
}
template<> inline void get<null>(const value& v, null&) {
assert_type_equal(NULL_TYPE, v.type());
}
template<> inline void get<decltype(nullptr)>(const value& v, decltype(nullptr)&) {
assert_type_equal(NULL_TYPE, v.type());
}
PN_CPP_EXTERN std::string to_string(const value& x);
template<class T> void value::get(T &x) const { x = proton::get<T>(*this); }
template<class T> T value::get() const { return proton::get<T>(*this); }
} // proton
#endif // PROTON_VALUE_HPP
proton::codec::decoder
Unsettled API - A stream-like decoder from AMQP bytes to C++ values.
Definition decoder.hpp:56
proton::codec::encoder
Unsettled API - A stream-like encoder from C++ values to AMQP bytes.
Definition encoder.hpp:50
proton::map
A collection of key-value pairs.
Definition map.hpp:69
proton::null
The type of the AMQP null value.
Definition null.hpp:38
proton::scalar
A holder for an instance of any scalar AMQP type.
Definition scalar.hpp:37
proton::value
A holder for any AMQP value, simple or complex.
Definition value.hpp:57
proton::value::get
T get(const value &v)
Get a contained value of type T.
Definition value.hpp:124
proton::value::get
void get(const value &v, T &x)
Like get(const value&) but extracts the value to a reference x instead of returning it.
Definition value.hpp:131
proton::value::coerce
T coerce(const value &v)
Coerce the contained value to type T.
Definition value.hpp:138
proton::value::coerce
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:145
proton::value::empty
bool empty() const
True if the value is null.
proton::value::operator=
assignable< T, value & >::type operator=(const T &x)
Assign from any allowed type T.
Definition value.hpp:80
proton::value::type
type_id type() const
Get the type ID for the current value.
proton::value::clear
void clear()
Reset the value to null/empty.
proton::value::value
value(const T &x, typename assignable< T >::type *=0)
Copy from any allowed type T.
Definition value.hpp:77
proton::value::value
value()
Create a null value.
proton::value::swap
friend void swap(value &, value &)
swap values
decoder.hpp
Unsettled API - A stream-like decoder from AMQP bytes to C++ values.
encoder.hpp
Unsettled API - A stream-like encoder from C++ values to AMQP bytes.
proton
The main Proton namespace.
Definition annotation_key.hpp:33
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:162
proton::to_string
std::string to_string(const message &)
Human readable string representation.
proton::get< null >
void get< null >(const value &v, null &)
Special case for null, just checks that value contains NULL.
Definition value.hpp:157
proton::type_id
type_id
An identifier for AMQP types.
Definition type_id.hpp:37
proton::NULL_TYPE
@ NULL_TYPE
The null type, contains no data.
Definition type_id.hpp:38
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.
scalar.hpp
A holder for an instance of any scalar AMQP type.
types_fwd.hpp
Forward declarations for Proton types used to represent AMQP types.