Loading...
Searching...
No Matches
encoder.hpp
Go to the documentation of this file.
#ifndef PROTON_CODEC_ENCODER_HPP
#define PROTON_CODEC_ENCODER_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>
namespace proton {
class scalar_base;
namespace internal{
class value_base;
}
namespace codec {
class encoder : public internal::data {
public:
explicit encoder(const data& d) : data(d) {}
PN_CPP_EXTERN explicit encoder(internal::value_base& v);
PN_CPP_EXTERN bool encode(char* buffer, size_t& size);
PN_CPP_EXTERN void encode(std::string&);
PN_CPP_EXTERN std::string encode();
PN_CPP_EXTERN encoder& operator<<(bool);
PN_CPP_EXTERN encoder& operator<<(uint8_t);
PN_CPP_EXTERN encoder& operator<<(int8_t);
PN_CPP_EXTERN encoder& operator<<(uint16_t);
PN_CPP_EXTERN encoder& operator<<(int16_t);
PN_CPP_EXTERN encoder& operator<<(uint32_t);
PN_CPP_EXTERN encoder& operator<<(int32_t);
PN_CPP_EXTERN encoder& operator<<(wchar_t);
PN_CPP_EXTERN encoder& operator<<(uint64_t);
PN_CPP_EXTERN encoder& operator<<(int64_t);
PN_CPP_EXTERN encoder& operator<<(timestamp);
PN_CPP_EXTERN encoder& operator<<(float);
PN_CPP_EXTERN encoder& operator<<(double);
PN_CPP_EXTERN encoder& operator<<(decimal32);
PN_CPP_EXTERN encoder& operator<<(decimal64);
PN_CPP_EXTERN encoder& operator<<(decimal128);
PN_CPP_EXTERN encoder& operator<<(const uuid&);
PN_CPP_EXTERN encoder& operator<<(const std::string&);
PN_CPP_EXTERN encoder& operator<<(const symbol&);
PN_CPP_EXTERN encoder& operator<<(const binary&);
PN_CPP_EXTERN encoder& operator<<(const scalar_base&);
PN_CPP_EXTERN encoder& operator<<(const null&);
PN_CPP_EXTERN encoder& operator<<(decltype(nullptr));
PN_CPP_EXTERN encoder& operator<<(const internal::value_base&);
PN_CPP_EXTERN encoder& operator<<(const start&);
PN_CPP_EXTERN encoder& operator<<(const finish&);
// Undefined template to prevent pointers being implicitly converted to bool.
template <class T> void* operator<<(const T*);
template <class T> struct list_cref { T& ref; list_cref(T& r) : ref(r) {} };
template <class T> struct map_cref { T& ref; map_cref(T& r) : ref(r) {} };
template <class T> struct array_cref {
start array_start;
T& ref;
array_cref(T& r, type_id el, bool described) : array_start(ARRAY, el, described), ref(r) {}
};
template <class T> static list_cref<T> list(T& x) { return list_cref<T>(x); }
template <class T> static map_cref<T> map(T& x) { return map_cref<T>(x); }
template <class T> static array_cref<T> array(T& x, type_id element, bool described=false) {
return array_cref<T>(x, element, described);
}
template <class T> encoder& operator<<(const map_cref<T>& x) {
internal::state_guard sg(*this);
*this << start::map();
for (typename T::const_iterator i = x.ref.begin(); i != x.ref.end(); ++i)
*this << i->first << i->second;
*this << finish();
return *this;
}
template <class T> encoder& operator<<(const list_cref<T>& x) {
internal::state_guard sg(*this);
*this << start::list();
for (typename T::const_iterator i = x.ref.begin(); i != x.ref.end(); ++i)
*this << *i;
*this << finish();
return *this;
}
template <class T> encoder& operator<<(const array_cref<T>& x) {
internal::state_guard sg(*this);
*this << x.array_start;
for (typename T::const_iterator i = x.ref.begin(); i != x.ref.end(); ++i)
*this << *i;
*this << finish();
return *this;
}
private:
template<class T, class U> encoder& insert(const T& x, int (*put)(pn_data_t*, U));
void check(long result);
};
inline encoder& operator<<(encoder& e, const char* s) { return e << std::string(s); }
template <class T> typename std::enable_if<internal::is_unknown_integer<T>::value, encoder&>::type
operator<<(encoder& e, T i) {
using namespace internal;
return e << static_cast<typename integer_type<sizeof(T), std::is_signed<T>::value>::type>(i);
}
namespace is_encodable_impl { // Protect the world from fallback operator<<
using namespace internal;
sfinae::no operator<<(encoder const&, const sfinae::any_t &); // Fallback
template<typename T> struct is_encodable : public sfinae {
static yes test(encoder&);
static no test(...); // Failed test, no match.
static encoder* e;
static const T* t;
static bool const value = sizeof(test(*e << *t)) == sizeof(yes);
};
// Avoid recursion
template <> struct is_encodable<value> : public std::true_type {};
} // is_encodable_impl
using is_encodable_impl::is_encodable;
// Metafunction to test if a class looks like an encodable map from K to T.
template <class M, class K, class T, class Enable = void>
struct is_encodable_map : public std::false_type {};
template <class M, class K, class T> struct is_encodable_map<
M, K, T, typename std::enable_if<
std::is_same<K, typename M::key_type>::value &&
std::is_same<T, typename M::mapped_type>::value &&
is_encodable<M>::value
>::type
> : public std::true_type {};
} // codec
} // proton
#endif
proton::binary
Arbitrary binary data.
Definition binary.hpp:40
proton::codec::encoder
Unsettled API - A stream-like encoder from C++ values to AMQP bytes.
Definition encoder.hpp:50
proton::codec::encoder::encode
bool encode(char *buffer, size_t &size)
Encode the current values into buffer and update size to reflect the number of bytes encoded.
proton::codec::encoder::operator<<
encoder & operator<<(const internal::value_base &)
Insert a proton::value.
proton::codec::encoder::encoder
encoder(const data &d)
Wrap Proton-C data object.
Definition encoder.hpp:53
proton::codec::encoder::operator<<
encoder & operator<<(const start &)
Start a complex type.
proton::codec::encoder::encoder
encoder(internal::value_base &v)
Encoder into v. Clears any current value in v.
proton::codec::encoder::encode
void encode(std::string &)
Encode the current values into a std::string and resize the string if necessary.
proton::codec::encoder::encode
std::string encode()
Encode the current values into a std::string.
proton::codec::encoder::operator<<
encoder & operator<<(const finish &)
Finish a complex type.
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::null
The type of the AMQP null value.
Definition null.hpp:38
proton::scalar_base
The base class for scalar types.
Definition scalar_base.hpp:60
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
proton::value
A holder for any AMQP value, simple or complex.
Definition value.hpp:57
common.hpp
Unsettled API - Shared codec functions.
proton::codec
Unsettled API - AMQP data encoding and decoding.
Definition common.hpp:31
proton::codec::operator<<
encoder & operator<<(encoder &e, const std::deque< T, A > &x)
std::deque<T> for most T is encoded as an amqp::ARRAY (same type elements)
Definition deque.hpp:37
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::type_id
type_id
An identifier for AMQP types.
Definition type_id.hpp:37
proton::ARRAY
@ ARRAY
A sequence of values of the same type.
Definition type_id.hpp:60
proton::operator<<
std::ostream & operator<<(std::ostream &, const binary &)
Print a binary value.
types_fwd.hpp
Forward declarations for Proton types used to represent AMQP types.