Loading...
Searching...
No Matches
decoder.hpp
Go to the documentation of this file.
#ifndef PROTON_CODEC_DECODER_HPP
#define PROTON_CODEC_DECODER_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 "../internal/data.hpp"
#include "../internal/type_traits.hpp"
#include "../types_fwd.hpp"
#include "./common.hpp"
#include <proton/type_compat.h>
#include <utility>
namespace proton {
class annotation_key;
class message_id;
class scalar;
class value;
namespace internal {
class value_base;
}
namespace codec {
class decoder : public internal::data {
public:
explicit decoder(const data& d, bool exact=false) : data(d), exact_(exact) {}
PN_CPP_EXTERN explicit decoder(const internal::value_base&, bool exact=false);
PN_CPP_EXTERN void decode(const char* buffer, size_t size);
PN_CPP_EXTERN void decode(const std::string&);
PN_CPP_EXTERN bool more();
PN_CPP_EXTERN type_id next_type();
PN_CPP_EXTERN decoder& operator>>(bool&);
PN_CPP_EXTERN decoder& operator>>(uint8_t&);
PN_CPP_EXTERN decoder& operator>>(int8_t&);
PN_CPP_EXTERN decoder& operator>>(uint16_t&);
PN_CPP_EXTERN decoder& operator>>(int16_t&);
PN_CPP_EXTERN decoder& operator>>(uint32_t&);
PN_CPP_EXTERN decoder& operator>>(int32_t&);
PN_CPP_EXTERN decoder& operator>>(wchar_t&);
PN_CPP_EXTERN decoder& operator>>(uint64_t&);
PN_CPP_EXTERN decoder& operator>>(int64_t&);
PN_CPP_EXTERN decoder& operator>>(timestamp&);
PN_CPP_EXTERN decoder& operator>>(float&);
PN_CPP_EXTERN decoder& operator>>(double&);
PN_CPP_EXTERN decoder& operator>>(decimal32&);
PN_CPP_EXTERN decoder& operator>>(decimal64&);
PN_CPP_EXTERN decoder& operator>>(decimal128&);
PN_CPP_EXTERN decoder& operator>>(uuid&);
PN_CPP_EXTERN decoder& operator>>(std::string&);
PN_CPP_EXTERN decoder& operator>>(symbol&);
PN_CPP_EXTERN decoder& operator>>(binary&);
PN_CPP_EXTERN decoder& operator>>(message_id&);
PN_CPP_EXTERN decoder& operator>>(annotation_key&);
PN_CPP_EXTERN decoder& operator>>(scalar&);
PN_CPP_EXTERN decoder& operator>>(internal::value_base&);
PN_CPP_EXTERN decoder& operator>>(null&);
PN_CPP_EXTERN decoder& operator>>(decltype(nullptr)&);
PN_CPP_EXTERN decoder& operator>>(start&);
PN_CPP_EXTERN decoder& operator>>(const finish&);
template <class T> struct sequence_ref { T& ref; sequence_ref(T& r) : ref(r) {} };
template <class T> struct associative_ref { T& ref; associative_ref(T& r) : ref(r) {} };
template <class T> struct pair_sequence_ref { T& ref; pair_sequence_ref(T& r) : ref(r) {} };
template <class T> static sequence_ref<T> sequence(T& x) { return sequence_ref<T>(x); }
template <class T> static associative_ref<T> associative(T& x) { return associative_ref<T>(x); }
template <class T> static pair_sequence_ref<T> pair_sequence(T& x) { return pair_sequence_ref<T>(x); }
template <class T> decoder& operator>>(sequence_ref<T> r) {
start s;
*this >> s;
if (s.is_described) next();
r.ref.resize(s.size);
for (typename T::iterator i = r.ref.begin(); i != r.ref.end(); ++i)
*this >> *i;
return *this;
}
template <class T> decoder& operator>>(associative_ref<T> r) {
using namespace internal;
start s;
*this >> s;
assert_type_equal(MAP, s.type);
r.ref.clear();
for (size_t i = 0; i < s.size/2; ++i) {
typename std::remove_const<typename T::key_type>::type k;
typename std::remove_const<typename T::mapped_type>::type v;
*this >> k >> v;
r.ref[k] = v;
}
return *this;
}
template <class T> decoder& operator>>(pair_sequence_ref<T> r) {
using namespace internal;
start s;
*this >> s;
assert_type_equal(MAP, s.type);
r.ref.clear();
for (size_t i = 0; i < s.size/2; ++i) {
typedef typename T::value_type value_type;
typename std::remove_const<typename value_type::first_type>::type k;
typename std::remove_const<typename value_type::second_type>::type v;
*this >> k >> v;
r.ref.push_back(value_type(k, v));
}
return *this;
}
private:
type_id pre_get();
template <class T, class U> decoder& extract(T& x, U (*get)(pn_data_t*));
bool exact_;
friend class message;
};
template<class T> T get(decoder& d) {
assert_type_equal(internal::type_id_of<T>::value, d.next_type());
T x;
d >> x;
return x;
}
template <class T> typename std::enable_if<internal::is_unknown_integer<T>::value, decoder&>::type
operator>>(decoder& d, T& i) {
using namespace internal;
typename integer_type<sizeof(T), std::is_signed<T>::value>::type v;
d >> v; // Extract as a known integer type
i = v; // C++ conversion to the target type.
return d;
}
} // codec
} // proton
#endif // PROTON_CODEC_DECODER_HPP
proton::annotation_key
A key for use with AMQP annotation maps.
Definition annotation_key.hpp:38
proton::binary
Arbitrary binary data.
Definition binary.hpp:40
proton::codec::decoder
Unsettled API - A stream-like decoder from AMQP bytes to C++ values.
Definition decoder.hpp:56
proton::codec::decoder::operator>>
decoder & operator>>(sequence_ref< T > r)
Extract any AMQP sequence (ARRAY, LIST or MAP) to a C++ sequence container of T if the elements types...
Definition decoder.hpp:142
proton::codec::decoder::decode
void decode(const char *buffer, size_t size)
Decode AMQP data from a buffer and add it to the end of the decoders stream.
proton::codec::decoder::operator>>
decoder & operator>>(const finish &)
Finish decoding a container type, and move on to the next value in the stream.
proton::codec::decoder::operator>>
decoder & operator>>(pair_sequence_ref< T > r)
Extract an AMQP MAP to a C++ push_back sequence of pairs preserving encoded order.
Definition decoder.hpp:170
proton::codec::decoder::operator>>
decoder & operator>>(start &)
Start decoding a container type, such as an ARRAY, LIST or MAP.
proton::codec::decoder::decode
void decode(const std::string &)
Decode AMQP data from a std::string and add it to the end of the decoders stream.
proton::codec::decoder::more
bool more()
Return true if there are more value to extract at the current level.
proton::codec::decoder::operator>>
decoder & operator>>(associative_ref< T > r)
Extract an AMQP MAP to a C++ associative container.
Definition decoder.hpp:153
proton::codec::decoder::decoder
decoder(const data &d, bool exact=false)
Wrap a Proton C data object.
Definition decoder.hpp:61
proton::codec::decoder::decoder
decoder(const internal::value_base &, bool exact=false)
Attach decoder to a proton::value.
proton::codec::decoder::next_type
type_id next_type()
Get the type of the next value that will be read by operator>>.
proton::decimal128
A 128-bit decimal floating-point value.
Definition decimal.hpp:52
proton::decimal32
A 32-bit decimal floating-point value.
Definition decimal.hpp:46
proton::decimal64
A 64-bit decimal floating-point value.
Definition decimal.hpp:49
proton::map
A collection of key-value pairs.
Definition map.hpp:69
proton::map::size
size_t size() const
Get the number of map entries.
proton::map::clear
void clear()
Remove all map entries.
proton::message_id
An AMQP message ID.
Definition message_id.hpp:47
proton::message
An AMQP message.
Definition message.hpp:48
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::symbol
A string that represents the AMQP symbol type.
Definition symbol.hpp:35
proton::timestamp
A 64-bit timestamp in milliseconds since the Unix epoch.
Definition timestamp.hpp:35
proton::uuid
A 16-byte universally unique identifier.
Definition uuid.hpp:37
common.hpp
Unsettled API - Shared codec functions.
proton::codec::operator>>
std::enable_if< internal::is_unknown_integer< T >::value, decoder & >::type operator>>(decoder &d, T &i)
operator>> for integer types that are not covered by the standard overrides.
Definition decoder.hpp:207
proton::codec::finish
Unsettled API - Finish inserting or extracting a complex type.
Definition common.hpp:57
proton::codec::start
Unsettled API - Start encoding a complex type.
Definition common.hpp:34
proton
The main Proton namespace.
Definition annotation_key.hpp:33
proton::get
T get(const scalar &s)
Get a contained value of type T.
Definition scalar.hpp:60
proton::type_id
type_id
An identifier for AMQP types.
Definition type_id.hpp:37
proton::MAP
@ MAP
A sequence of key-value pairs.
Definition type_id.hpp:62
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.