object.hpp
Go to the documentation of this file.
//
// MessagePack for C++ static resolution routine
//
// Copyright (C) 2008-2014 FURUHASHI Sadayuki and KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef MSGPACK_V1_OBJECT_HPP
#define MSGPACK_V1_OBJECT_HPP
#include "msgpack/object_decl.hpp"
#include "msgpack/adaptor/check_container_size.hpp"
#include <cstring>
#include <stdexcept>
#include <typeinfo>
#include <limits>
#include <ostream>
#include <typeinfo>
#include <iomanip>
namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(v1) {
struct object_kv {
msgpack::object key;
msgpack::object val;
};
struct object::with_zone : msgpack::object {
with_zone(msgpack::zone& z) : zone(z) { }
msgpack::zone& zone;
private:
with_zone();
};
class object_handle {
public:
object_handle() {}
object_handle(
msgpack::object const& obj,
#if defined(MSGPACK_USE_CPP03)
msgpack::unique_ptr<msgpack::zone> z
#else // defined(MSGPACK_USE_CPP03)
msgpack::unique_ptr<msgpack::zone>&& z
#endif // defined(MSGPACK_USE_CPP03)
) :
m_obj(obj), m_zone(msgpack::move(z)) { }
void set(msgpack::object const& obj)
{ m_obj = obj; }
const msgpack::object& get() const
{ return m_obj; }
const msgpack::object& operator*() const
{ return get(); }
const msgpack::object* operator->() const
{ return &get(); }
msgpack::unique_ptr<msgpack::zone>& zone()
{ return m_zone; }
const msgpack::unique_ptr<msgpack::zone>& zone() const
{ return m_zone; }
#if defined(MSGPACK_USE_CPP03)
struct object_handle_ref {
object_handle_ref(object_handle* oh):m_oh(oh) {}
object_handle* m_oh;
};
object_handle(object_handle& other):
m_obj(other.m_obj),
m_zone(msgpack::move(other.m_zone)) {
}
object_handle(object_handle_ref ref):
m_obj(ref.m_oh->m_obj),
m_zone(msgpack::move(ref.m_oh->m_zone)) {
}
object_handle& operator=(object_handle& other) {
m_obj = other.m_obj;
m_zone = msgpack::move(other.m_zone);
return *this;
}
object_handle& operator=(object_handle_ref ref) {
m_obj = ref.m_oh->m_obj;
m_zone = msgpack::move(ref.m_oh->m_zone);
return *this;
}
operator object_handle_ref() {
return object_handle_ref(this);
}
#endif // defined(MSGPACK_USE_CPP03)
private:
msgpack::object m_obj;
msgpack::unique_ptr<msgpack::zone> m_zone;
};
namespace detail {
template <std::size_t N>
inline std::size_t add_ext_type_size(std::size_t size) {
return size + 1;
}
template <>
inline std::size_t add_ext_type_size<4>(std::size_t size) {
return size == 0xffffffff ? size : size + 1;
}
} // namespace detail
class object_parser {
private:
enum next_ret {
cont,
finish,
abort
};
struct elem {
elem(msgpack::object const* p, std::size_t r)
: rest(r), is_map(false), is_key(false) {
as.obj_ptr = p;
}
elem(msgpack::object_kv const* p, std::size_t r)
: rest(r), is_map(true), is_key(true) {
as.kv_ptr = p;
}
msgpack::object const& get() const {
if (is_map) {
if (is_key) {
return as.kv_ptr->key;
}
else {
return as.kv_ptr->val;
}
}
else {
return *as.obj_ptr;
}
}
template <typename Visitor>
next_ret next(Visitor& v) {
if (rest == 0) {
if (is_map) {
if (!v.end_map()) return abort;
}
else {
if (!v.end_array()) return abort;
}
return finish;
}
else {
if (is_map) {
if (is_key) {
if (!v.end_map_key()) return abort;
if (!v.start_map_value()) return abort;
is_key = false;
}
else {
if (!v.end_map_value()) return abort;
--rest;
if (rest == 0) {
if (!v.end_map()) return abort;
return finish;
}
if (!v.start_map_key()) return abort;
++as.kv_ptr;
is_key = true;
}
}
else {
if (!v.end_array_item()) return abort;
--rest;
if (rest == 0) {
if (!v.end_array()) return abort;
return finish;
}
if (!v.start_array_item()) return abort;
++as.obj_ptr;
}
return cont;
}
}
union {
msgpack::object const* obj_ptr;
msgpack::object_kv const* kv_ptr;
} as;
std::size_t rest;
bool is_map;
bool is_key;
};
public:
explicit object_parser(msgpack::object const& obj):m_current(&obj) {}
template <typename Visitor>
void parse(Visitor& v) {
while (true) {
bool start_collection = false;
switch(m_current->type) {
case msgpack::type::NIL:
if (!v.visit_nil()) return;
break;
case msgpack::type::BOOLEAN:
if (!v.visit_boolean(m_current->via.boolean)) return;
break;
case msgpack::type::POSITIVE_INTEGER:
if (!v.visit_positive_integer(m_current->via.u64)) return;
break;
case msgpack::type::NEGATIVE_INTEGER:
if (!v.visit_negative_integer(m_current->via.i64)) return;
break;
case msgpack::type::FLOAT32:
if (!v.visit_float32(static_cast<float>(m_current->via.f64))) return;
break;
case msgpack::type::FLOAT64:
if (!v.visit_float64(m_current->via.f64)) return;
break;
case msgpack::type::STR:
if (!v.visit_str(m_current->via.str.ptr, m_current->via.str.size)) return;
break;
case msgpack::type::BIN:
if (!v.visit_bin(m_current->via.bin.ptr, m_current->via.bin.size)) return;
break;
case msgpack::type::EXT:
msgpack::detail::check_container_size<sizeof(std::size_t)>(m_current->via.ext.size);
if (!v.visit_ext(m_current->via.ext.ptr, m_current->via.ext.size + 1)) return;
break;
case msgpack::type::ARRAY:
if (!v.start_array(m_current->via.array.size)) return;
m_ctx.push_back(elem(m_current->via.array.ptr, m_current->via.array.size));
start_collection = m_current->via.array.size != 0;
if (start_collection) {
if (!v.start_array_item()) return;
}
break;
case msgpack::type::MAP:
if (!v.start_map(m_current->via.map.size)) return;
m_ctx.push_back(elem(m_current->via.map.ptr, m_current->via.map.size));
start_collection = m_current->via.map.size != 0;
if (start_collection) {
if (!v.start_map_key()) return;
}
break;
default:
throw msgpack::type_error();
break;
}
if (m_ctx.empty()) return;
if (!start_collection) {
while (true) {
next_ret r = m_ctx.back().next(v);
if (r == finish) {
m_ctx.pop_back();
if (m_ctx.empty()) return;
}
else if (r == cont) {
break;
}
else {
// abort
return;
}
}
}
m_current = &m_ctx.back().get();
}
}
private:
msgpack::object const* m_current;
std::vector<elem> m_ctx;
};
template <typename Stream>
struct object_pack_visitor {
explicit object_pack_visitor(msgpack::packer<Stream>& pk)
:m_packer(pk) {}
bool visit_nil() {
m_packer.pack_nil();
return true;
}
bool visit_boolean(bool v) {
if (v) m_packer.pack_true();
else m_packer.pack_false();
return true;
}
bool visit_positive_integer(uint64_t v) {
m_packer.pack_uint64(v);
return true;
}
bool visit_negative_integer(int64_t v) {
m_packer.pack_int64(v);
return true;
}
bool visit_float32(float v) {
m_packer.pack_float(v);
return true;
}
bool visit_float64(double v) {
m_packer.pack_double(v);
return true;
}
bool visit_str(const char* v, uint32_t size) {
m_packer.pack_str(size);
m_packer.pack_str_body(v, size);
return true;
}
bool visit_bin(const char* v, uint32_t size) {
m_packer.pack_bin(size);
m_packer.pack_bin_body(v, size);
return true;
}
bool visit_ext(const char* v, uint32_t size) {
m_packer.pack_ext(size, *v);
m_packer.pack_ext_body(v, size);
return true;
}
bool start_array(uint32_t num_elements) {
m_packer.pack_array(num_elements);
return true;
}
bool start_array_item() {
return true;
}
bool end_array_item() {
return true;
}
bool end_array() {
return true;
}
bool start_map(uint32_t num_kv_pairs) {
m_packer.pack_array(num_kv_pairs);
return true;
}
bool start_map_key() {
return true;
}
bool end_map_key() {
return true;
}
bool start_map_value() {
return true;
}
bool end_map_value() {
return true;
}
bool end_map() {
return true;
}
private:
msgpack::packer<Stream>& m_packer;
};
struct object_stringize_visitor {
explicit object_stringize_visitor(std::ostream& os)
:m_os(os) {}
bool visit_nil() {
m_os << "null";
return true;
}
bool visit_boolean(bool v) {
if (v) m_os << "true";
else m_os << "false";
return true;
}
bool visit_positive_integer(uint64_t v) {
m_os << v;
return true;
}
bool visit_negative_integer(int64_t v) {
m_os << v;
return true;
}
bool visit_float32(float v) {
m_os << v;
return true;
}
bool visit_float64(double v) {
m_os << v;
return true;
}
bool visit_str(const char* v, uint32_t size) {
m_os << '"';
for (uint32_t i = 0; i < size; ++i) {
char c = v[i];
switch (c) {
case '\\':
m_os << "\\\\";
break;
case '"':
m_os << "\\\"";
break;
case '/':
m_os << "\\/";
break;
case '\b':
m_os << "\\b";
break;
case '\f':
m_os << "\\f";
break;
case '\n':
m_os << "\\n";
break;
case '\r':
m_os << "\\r";
break;
case '\t':
m_os << "\\t";
break;
default: {
unsigned int code = static_cast<unsigned int>(c);
if (code < 0x20 || code == 0x7f) {
std::ios::fmtflags flags(m_os.flags());
m_os << "\\u" << std::hex << std::setw(4) << std::setfill('0') << (code & 0xff);
m_os.flags(flags);
}
else {
m_os << c;
}
} break;
}
}
m_os << '"';
return true;
}
bool visit_bin(const char* v, uint32_t size) {
(m_os << '"').write(v, size) << '"';
return true;
}
bool visit_ext(const char* /*v*/, uint32_t /*size*/) {
m_os << "EXT";
return true;
}
bool start_array(uint32_t num_elements) {
m_current_size.push_back(num_elements);
m_os << "[";
return true;
}
bool start_array_item() {
return true;
}
bool end_array_item() {
--m_current_size.back();
if (m_current_size.back() != 0) {
m_os << ",";
}
return true;
}
bool end_array() {
m_current_size.pop_back();
m_os << "]";
return true;
}
bool start_map(uint32_t num_kv_pairs) {
m_current_size.push_back(num_kv_pairs);
m_os << "{";
return true;
}
bool start_map_key() {
return true;
}
bool end_map_key() {
m_os << ":";
return true;
}
bool start_map_value() {
return true;
}
bool end_map_value() {
--m_current_size.back();
if (m_current_size.back() != 0) {
m_os << ",";
}
return true;
}
bool end_map() {
m_current_size.pop_back();
m_os << "}";
return true;
}
private:
std::ostream& m_os;
std::vector<uint32_t> m_current_size;
};
struct aligned_zone_size_visitor {
explicit aligned_zone_size_visitor(std::size_t s)
:m_size(s) {}
bool visit_nil() {
return true;
}
bool visit_boolean(bool) {
return true;
}
bool visit_positive_integer(uint64_t) {
return true;
}
bool visit_negative_integer(int64_t) {
return true;
}
bool visit_float32(float) {
return true;
}
bool visit_float64(double) {
return true;
}
bool visit_str(const char*, uint32_t size) {
m_size += msgpack::aligned_size(size, MSGPACK_ZONE_ALIGNOF(char));
return true;
}
bool visit_bin(const char*, uint32_t size) {
m_size += msgpack::aligned_size(size, MSGPACK_ZONE_ALIGNOF(char));
return true;
}
bool visit_ext(const char*, uint32_t size) {
m_size += msgpack::aligned_size(size, MSGPACK_ZONE_ALIGNOF(char));
return true;
}
bool start_array(uint32_t num_elements) {
m_size += msgpack::aligned_size(
sizeof(msgpack::object) * num_elements,
MSGPACK_ZONE_ALIGNOF(msgpack::object));
return true;
}
bool start_array_item() {
return true;
}
bool end_array_item() {
return true;
}
bool end_array() {
return true;
}
bool start_map(uint32_t num_kv_pairs) {
m_size += msgpack::aligned_size(
sizeof(msgpack::object_kv) * num_kv_pairs,
MSGPACK_ZONE_ALIGNOF(msgpack::object_kv));
return true;
}
bool start_map_key() {
return true;
}
bool end_map_key() {
return true;
}
bool start_map_value() {
return true;
}
bool end_map_value() {
return true;
}
bool end_map() {
return true;
}
private:
std::size_t m_size;
};
inline std::size_t aligned_zone_size(msgpack::object const& obj) {
std::size_t s = 0;
aligned_zone_size_visitor vis(s);
msgpack::object_parser(obj).parse(vis);
return s;
}
inline object_handle clone(msgpack::object const& obj) {
std::size_t size = msgpack::aligned_zone_size(obj);
msgpack::unique_ptr<msgpack::zone> z(size == 0 ? MSGPACK_NULLPTR : new msgpack::zone(size));
msgpack::object newobj = z.get() ? msgpack::object(obj, *z) : obj;
return object_handle(newobj, msgpack::move(z));
}
template <typename T>
inline object::implicit_type::operator T() { return obj.as<T>(); }
namespace detail {
template <typename Stream, typename T>
struct packer_serializer {
static msgpack::packer<Stream>& pack(msgpack::packer<Stream>& o, const T& v) {
v.msgpack_pack(o);
return o;
}
};
} // namespace detail
// Adaptor functors' member functions definitions.
template <typename T, typename Enabler>
inline
msgpack::object const&
adaptor::convert<T, Enabler>::operator()(msgpack::object const& o, T& v) const {
v.msgpack_unpack(o.convert());
return o;
}
template <typename T, typename Enabler>
template <typename Stream>
inline
msgpack::packer<Stream>&
adaptor::pack<T, Enabler>::operator()(msgpack::packer<Stream>& o, T const& v) const {
return msgpack::detail::packer_serializer<Stream, T>::pack(o, v);
}
template <typename T, typename Enabler>
inline
void
adaptor::object_with_zone<T, Enabler>::operator()(msgpack::object::with_zone& o, T const& v) const {
v.msgpack_object(static_cast<msgpack::object*>(&o), o.zone);
}
// Adaptor functor specialization to object
namespace adaptor {
template <>
struct convert<msgpack::object> {
msgpack::object const& operator()(msgpack::object const& o, msgpack::object& v) const {
v = o;
return o;
}
};
template <>
struct pack<msgpack::object> {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, msgpack::object const& v) const {
object_pack_visitor<Stream> vis(o);
msgpack::object_parser(v).parse(vis);
return o;
}
};
template <>
struct object_with_zone<msgpack::object> {
void operator()(msgpack::object::with_zone& o, msgpack::object const& v) const {
object_with_zone_visitor vis(o);
msgpack::object_parser(v).parse(vis);
}
private:
struct object_with_zone_visitor {
explicit object_with_zone_visitor(msgpack::object::with_zone& owz)
:m_zone(owz.zone), m_ptr(&owz) {
m_objs.push_back(&owz);
}
bool visit_nil() {
m_ptr->type = msgpack::type::NIL;
return true;
}
bool visit_boolean(bool v) {
m_ptr->type = msgpack::type::BOOLEAN;
m_ptr->via.boolean = v;
return true;
}
bool visit_positive_integer(uint64_t v) {
m_ptr->type = msgpack::type::POSITIVE_INTEGER;
m_ptr->via.u64 = v;
return true;
}
bool visit_negative_integer(int64_t v) {
m_ptr->type = msgpack::type::NEGATIVE_INTEGER;
m_ptr->via.i64 = v;
return true;
}
bool visit_float32(float v) {
m_ptr->type = msgpack::type::FLOAT32;
m_ptr->via.f64 = v;
return true;
}
bool visit_float64(double v) {
m_ptr->type = msgpack::type::FLOAT64;
m_ptr->via.f64 = v;
return true;
}
bool visit_str(const char* v, uint32_t size) {
m_ptr->type = msgpack::type::STR;
m_ptr->via.str.size = size;
char* ptr = static_cast<char*>(m_zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
m_ptr->via.str.ptr = ptr;
std::memcpy(ptr, v, size);
return true;
}
bool visit_bin(const char* v, uint32_t size) {
m_ptr->type = msgpack::type::BIN;
m_ptr->via.bin.size = size;
char* ptr = static_cast<char*>(m_zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
m_ptr->via.bin.ptr = ptr;
std::memcpy(ptr, v, size);
return true;
}
bool visit_ext(const char* v, uint32_t size) {
m_ptr->type = msgpack::type::EXT;
// v contains type but length(size) doesn't count the type byte.
// See https://github.com/msgpack/msgpack/blob/master/spec.md#ext-format-family
m_ptr->via.ext.size = size - 1;
char* ptr = static_cast<char*>(m_zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
m_ptr->via.ext.ptr = ptr;
std::memcpy(ptr, v, size);
return true;
}
bool start_array(uint32_t num_elements) {
m_ptr->type = msgpack::type::ARRAY;
m_ptr->via.array.ptr = static_cast<msgpack::object*>(
m_zone.allocate_align(
sizeof(msgpack::object) * num_elements, MSGPACK_ZONE_ALIGNOF(msgpack::object)));
m_ptr->via.array.size = num_elements;
m_objs.push_back(elem(m_ptr->via.array.ptr));
return true;
}
bool start_array_item() {
m_ptr = m_objs.back().get_item();
return true;
}
bool end_array_item() {
++m_objs.back().as.obj;
return true;
}
bool end_array() {
m_objs.pop_back();
return true;
}
bool start_map(uint32_t num_kv_pairs) {
m_ptr->type = msgpack::type::MAP;
m_ptr->via.map.ptr = (msgpack::object_kv*)m_zone.allocate_align(
sizeof(msgpack::object_kv) * num_kv_pairs, MSGPACK_ZONE_ALIGNOF(msgpack::object_kv));
m_ptr->via.map.size = num_kv_pairs;
m_objs.push_back(elem(m_ptr->via.map.ptr));
return true;
}
bool start_map_key() {
m_ptr = m_objs.back().get_key();
return true;
}
bool end_map_key() {
return true;
}
bool start_map_value() {
m_ptr = m_objs.back().get_val();
return true;
}
bool end_map_value() {
++m_objs.back().as.kv;
return true;
}
bool end_map() {
m_objs.pop_back();
return true;
}
private:
struct elem {
elem(msgpack::object* obj)
:is_obj(true) {
as.obj = obj;
}
elem(msgpack::object_kv* kv)
:is_obj(false) {
as.kv = kv;
}
msgpack::object* get_item() {
return as.obj;
}
msgpack::object* get_key() {
return &as.kv->key;
}
msgpack::object* get_val() {
return &as.kv->val;
}
union {
msgpack::object* obj;
msgpack::object_kv* kv;
} as;
bool is_obj;
};
std::vector<elem> m_objs;
msgpack::zone& m_zone;
msgpack::object* m_ptr;
};
};
// Adaptor functor specialization to object::with_zone
template <>
struct object_with_zone<msgpack::object::with_zone> {
void operator()(
msgpack::object::with_zone& o,
msgpack::object::with_zone const& v) const {
o << static_cast<msgpack::object const&>(v);
}
};
} // namespace adaptor
// obsolete
template <typename Type>
class define : public Type {
public:
typedef Type msgpack_type;
typedef define<Type> define_type;
define() {}
define(const msgpack_type& v) : msgpack_type(v) {}
template <typename Packer>
void msgpack_pack(Packer& o) const
{
msgpack::operator<<(o, static_cast<const msgpack_type&>(*this));
}
void msgpack_unpack(object const& o)
{
msgpack::operator>>(o, static_cast<msgpack_type&>(*this));
}
};
// deconvert operator
template <typename Stream>
template <typename T>
inline msgpack::packer<Stream>& packer<Stream>::pack(const T& v)
{
msgpack::operator<<(*this, v);
return *this;
}
struct object_equal_visitor {
object_equal_visitor(msgpack::object const& obj, bool& result)
:m_ptr(&obj), m_result(result) {}
bool visit_nil() {
if (m_ptr->type != msgpack::type::NIL) {
m_result = false;
return false;
}
return true;
}
bool visit_boolean(bool v) {
if (m_ptr->type != msgpack::type::BOOLEAN || m_ptr->via.boolean != v) {
m_result = false;
return false;
}
return true;
}
bool visit_positive_integer(uint64_t v) {
if (m_ptr->type != msgpack::type::POSITIVE_INTEGER || m_ptr->via.u64 != v) {
m_result = false;
return false;
}
return true;
}
bool visit_negative_integer(int64_t v) {
if (m_ptr->type != msgpack::type::NEGATIVE_INTEGER || m_ptr->via.i64 != v) {
m_result = false;
return false;
}
return true;
}
bool visit_float32(float v) {
if (m_ptr->type != msgpack::type::FLOAT32 || m_ptr->via.f64 != v) {
m_result = false;
return false;
}
return true;
}
bool visit_float64(double v) {
if (m_ptr->type != msgpack::type::FLOAT64 || m_ptr->via.f64 != v) {
m_result = false;
return false;
}
return true;
}
bool visit_str(const char* v, uint32_t size) {
if (m_ptr->type != msgpack::type::STR ||
m_ptr->via.str.size != size ||
std::memcmp(m_ptr->via.str.ptr, v, size) != 0) {
m_result = false;
return false;
}
return true;
}
bool visit_bin(const char* v, uint32_t size) {
if (m_ptr->type != msgpack::type::BIN ||
m_ptr->via.bin.size != size ||
std::memcmp(m_ptr->via.bin.ptr, v, size) != 0) {
m_result = false;
return false;
}
return true;
}
bool visit_ext(const char* v, uint32_t size) {
if (m_ptr->type != msgpack::type::EXT ||
m_ptr->via.ext.size != size ||
std::memcmp(m_ptr->via.ext.ptr, v, size) != 0) {
m_result = false;
return false;
}
return true;
}
bool start_array(uint32_t num_elements) {
if (m_ptr->type != msgpack::type::ARRAY ||
m_ptr->via.array.size != num_elements) {
m_result = false;
return false;
}
m_objs.push_back(elem(m_ptr->via.array.ptr));
return true;
}
bool start_array_item() {
m_ptr = m_objs.back().get_item();
return true;
}
bool end_array_item() {
++m_objs.back().as.obj;
return true;
}
bool end_array() {
m_objs.pop_back();
return true;
}
bool start_map(uint32_t num_kv_pairs) {
if (m_ptr->type != msgpack::type::MAP ||
m_ptr->via.array.size != num_kv_pairs) {
m_result = false;
return false;
}
m_objs.push_back(elem(m_ptr->via.map.ptr));
return true;
}
bool start_map_key() {
m_ptr = m_objs.back().get_key();
return true;
}
bool end_map_key() {
return true;
}
bool start_map_value() {
m_ptr = m_objs.back().get_val();
return true;
}
bool end_map_value() {
++m_objs.back().as.kv;
return true;
}
bool end_map() {
m_objs.pop_back();
return true;
}
private:
struct elem {
elem(msgpack::object const* obj)
:is_obj(true) {
as.obj = obj;
}
elem(msgpack::object_kv const* kv)
:is_obj(false) {
as.kv = kv;
}
msgpack::object const* get_item() {
return as.obj;
}
msgpack::object const* get_key() {
return &as.kv->key;
}
msgpack::object const* get_val() {
return &as.kv->val;
}
union {
msgpack::object const* obj;
msgpack::object_kv const* kv;
} as;
bool is_obj;
};
std::vector<elem> m_objs;
msgpack::object const* m_ptr;
bool& m_result;
};
inline bool operator==(const msgpack::object& x, const msgpack::object& y)
{
if(x.type != y.type) { return false; }
bool b = true;
object_equal_visitor vis(y, b);
msgpack::object_parser(x).parse(vis);
return b;
}
template <typename T>
inline bool operator==(const msgpack::object& x, const T& y)
try {
return x == msgpack::object(y);
} catch (msgpack::type_error&) {
return false;
}
inline bool operator!=(const msgpack::object& x, const msgpack::object& y)
{ return !(x == y); }
template <typename T>
inline bool operator==(const T& y, const msgpack::object& x)
{ return x == y; }
template <typename T>
inline bool operator!=(const msgpack::object& x, const T& y)
{ return !(x == y); }
template <typename T>
inline bool operator!=(const T& y, const msgpack::object& x)
{ return x != y; }
inline object::implicit_type object::convert() const
{
return object::implicit_type(*this);
}
template <typename T>
inline
typename msgpack::enable_if<
!msgpack::is_array<T>::value && !msgpack::is_pointer<T>::value,
T&
>::type
object::convert(T& v) const
{
msgpack::operator>>(*this, v);
return v;
}
template <typename T, std::size_t N>
inline T(&object::convert(T(&v)[N]) const)[N]
{
msgpack::operator>>(*this, v);
return v;
}
#if !defined(MSGPACK_DISABLE_LEGACY_CONVERT)
template <typename T>
inline
typename msgpack::enable_if<
msgpack::is_pointer<T>::value,
T
>::type
object::convert(T v) const
{
convert(*v);
return v;
}
#endif // !defined(MSGPACK_DISABLE_LEGACY_CONVERT)
template <typename T>
inline bool object::convert_if_not_nil(T& v) const
{
if (is_nil()) {
return false;
}
convert(v);
return true;
}
#if defined(MSGPACK_USE_CPP03)
template <typename T>
inline T object::as() const
{
T v;
convert(v);
return v;
}
#else // defined(MSGPACK_USE_CPP03)
template <typename T>
inline typename std::enable_if<msgpack::has_as<T>::value, T>::type object::as() const {
return msgpack::adaptor::as<T>()(*this);
}
template <typename T>
inline typename std::enable_if<!msgpack::has_as<T>::value, T>::type object::as() const {
T v;
convert(v);
return v;
}
#endif // defined(MSGPACK_USE_CPP03)
inline object::object()
{
type = msgpack::type::NIL;
}
template <typename T>
inline object::object(const T& v)
{
*this << v;
}
template <typename T>
inline object& object::operator=(const T& v)
{
*this = object(v);
return *this;
}
template <typename T>
inline object::object(const T& v, msgpack::zone& z)
{
with_zone oz(z);
msgpack::operator<<(oz, v);
type = oz.type;
via = oz.via;
}
template <typename T>
inline object::object(const T& v, msgpack::zone* z)
{
with_zone oz(*z);
msgpack::operator<<(oz, v);
type = oz.type;
via = oz.via;
}
inline object::object(const msgpack_object& o)
{
// FIXME beter way?
std::memcpy(this, &o, sizeof(o));
}
inline void operator<< (msgpack::object& o, const msgpack_object& v)
{
// FIXME beter way?
std::memcpy(static_cast<void*>(&o), &v, sizeof(v));
}
inline object::operator msgpack_object() const
{
// FIXME beter way?
msgpack_object obj;
std::memcpy(&obj, this, sizeof(obj));
return obj;
}
// obsolete
template <typename T>
inline void convert(T& v, msgpack::object const& o)
{
o.convert(v);
}
// obsolete
template <typename Stream, typename T>
inline void pack(msgpack::packer<Stream>& o, const T& v)
{
o.pack(v);
}
// obsolete
template <typename Stream, typename T>
inline void pack_copy(msgpack::packer<Stream>& o, T v)
{
pack(o, v);
}
template <typename Stream>
inline msgpack::packer<Stream>& operator<< (msgpack::packer<Stream>& o, const msgpack::object& v)
{
object_pack_visitor<Stream> vis(o);
msgpack::object_parser(v).parse(vis);
return o;
}
template <typename Stream>
inline msgpack::packer<Stream>& operator<< (msgpack::packer<Stream>& o, const msgpack::object::with_zone& v)
{
return o << static_cast<msgpack::object>(v);
}
inline std::ostream& operator<< (std::ostream& s, const msgpack::object& v)
{
object_stringize_visitor vis(s);
msgpack::object_parser(v).parse(vis);
return s;
}
} // MSGPACK_API_VERSION_NAMESPACE(v1)
} // namespace msgpack
#endif // MSGPACK_V1_OBJECT_HPP
msgpack::define::define
define()
Definition: object.hpp:851
msgpack::object_pack_visitor::end_array_item
bool end_array_item()
Definition: object.hpp:366
msgpack::aligned_zone_size_visitor::end_map
bool end_map()
Definition: object.hpp:595
msgpack::is_array
Definition: cpp_config_decl.hpp:71
msgpack::object_parser::object_parser
object_parser(msgpack::object const &obj)
Definition: object.hpp:236
msgpack::aligned_zone_size_visitor::end_map_value
bool end_map_value()
Definition: object.hpp:592
msgpack::type::STR
Definition: object_fwd_decl.hpp:39
msgpack::object_stringize_visitor::end_map
bool end_map()
Definition: object.hpp:519
msgpack::adaptor::object_with_zone::operator()
void operator()(msgpack::object::with_zone &o, T const &v) const
Definition: object.hpp:657
msgpack::object_pack_visitor::start_array
bool start_array(uint32_t num_elements)
Definition: object.hpp:359
msgpack::object_stringize_visitor::object_stringize_visitor
object_stringize_visitor(std::ostream &os)
Definition: object.hpp:397
msgpack::object_array::size
uint32_t size
Definition: object_fwd.hpp:23
msgpack::object_handle::object_handle
object_handle()
Constructor that creates nil object and null zone.
Definition: object.hpp:47
msgpack::aligned_zone_size_visitor::start_array
bool start_array(uint32_t num_elements)
Definition: object.hpp:562
msgpack::operator==
bool operator==(const msgpack::object &x, const msgpack::object &y)
Definition: object.hpp:1027
msgpack::object_stringize_visitor::visit_float32
bool visit_float32(float v)
Definition: object.hpp:416
msgpack::define::define
define(const msgpack_type &v)
Definition: object.hpp:852
msgpack::define::msgpack_pack
void msgpack_pack(Packer &o) const
Definition: object.hpp:855
msgpack::object_equal_visitor::end_map_key
bool end_map_key()
Definition: object.hpp:982
msgpack::aligned_zone_size_visitor::start_map
bool start_map(uint32_t num_kv_pairs)
Definition: object.hpp:577
msgpack::aligned_zone_size_visitor::aligned_zone_size_visitor
aligned_zone_size_visitor(std::size_t s)
Definition: object.hpp:530
msgpack::aligned_zone_size_visitor::end_array
bool end_array()
Definition: object.hpp:574
msgpack::type::FLOAT32
Definition: object_fwd_decl.hpp:33
msgpack::type::BOOLEAN
Definition: object_fwd_decl.hpp:30
msgpack::object_equal_visitor::start_array_item
bool start_array_item()
Definition: object.hpp:957
msgpack::object_equal_visitor::start_array
bool start_array(uint32_t num_elements)
Definition: object.hpp:948
msgpack::define::msgpack_type
Type msgpack_type
Definition: object.hpp:849
msgpack::object_equal_visitor::end_array
bool end_array()
Definition: object.hpp:965
msgpack::object_equal_visitor::visit_nil
bool visit_nil()
Definition: object.hpp:879
v2::object::with_zone::with_zone
with_zone(msgpack::zone &z)
Definition: object.hpp:36
msgpack::object_equal_visitor::start_map_value
bool start_map_value()
Definition: object.hpp:985
msgpack::adaptor::as
Definition: object_fwd_decl.hpp:62
msgpack::object::via
union_type via
Definition: object_fwd.hpp:93
msgpack::object_stringize_visitor::visit_negative_integer
bool visit_negative_integer(int64_t v)
Definition: object.hpp:412
msgpack::object_pack_visitor::visit_ext
bool visit_ext(const char *v, uint32_t size)
Definition: object.hpp:354
msgpack::object::as
std::enable_if< msgpack::has_as< T >::value, T >::type as() const
Get value as T.
Definition: object.hpp:1121
msgpack::aligned_zone_size_visitor::visit_ext
bool visit_ext(const char *, uint32_t size)
Definition: object.hpp:558
msgpack::object::object
object()
Default constructor. The object is set to nil.
Definition: object.hpp:1134
msgpack::type::BIN
Definition: object_fwd_decl.hpp:40
msgpack::aligned_zone_size_visitor::end_array_item
bool end_array_item()
Definition: object.hpp:571
object_decl.hpp
msgpack::aligned_zone_size_visitor::visit_boolean
bool visit_boolean(bool)
Definition: object.hpp:535
msgpack::define
Definition: object.hpp:847
msgpack::object_pack_visitor::visit_boolean
bool visit_boolean(bool v)
Definition: object.hpp:323
msgpack::object_equal_visitor::visit_positive_integer
bool visit_positive_integer(uint64_t v)
Definition: object.hpp:893
msgpack::object_kv::val
msgpack::object val
Definition: object.hpp:32
msgpack
Definition: adaptor_base.hpp:15
msgpack::aligned_size
std::size_t aligned_size(std::size_t size, std::size_t align)
Definition: cpp03_zone.hpp:344
msgpack::adaptor::object_with_zone< msgpack::object::with_zone >::operator()
void operator()(msgpack::object::with_zone &o, msgpack::object::with_zone const &v) const
Definition: object.hpp:834
msgpack::object_pack_visitor::visit_nil
bool visit_nil()
Definition: object.hpp:319
msgpack::object::implicit_type
Definition: object_fwd.hpp:238
msgpack::object_pack_visitor::end_array
bool end_array()
Definition: object.hpp:369
msgpack::object_handle::object_handle
object_handle(msgpack::object const &obj, msgpack::unique_ptr< msgpack::zone > &&z)
Constructor that creates an object_handle holding object obj and zone z.
Definition: object.hpp:54
msgpack::zone
Definition: cpp03_zone.hpp:22
msgpack::adaptor::convert< msgpack::object >::operator()
msgpack::object const & operator()(msgpack::object const &o, msgpack::object &v) const
Definition: object.hpp:666
msgpack::object_stringize_visitor::end_map_key
bool end_map_key()
Definition: object.hpp:505
msgpack::object_pack_visitor::end_map_key
bool end_map_key()
Definition: object.hpp:379
msgpack::convert
void convert(T &v, msgpack::object const &o)
Definition: object.hpp:1194
v2::object::with_zone
Definition: object.hpp:35
msgpack::aligned_zone_size_visitor::start_array_item
bool start_array_item()
Definition: object.hpp:568
msgpack::object_pack_visitor
Definition: object.hpp:316
msgpack::packer::pack
packer< Stream > & pack(const T &v)
Packing function template.
msgpack::object_kv
Definition: object.hpp:30
msgpack::unique_ptr< msgpack::zone >
msgpack::aligned_zone_size_visitor::end_map_key
bool end_map_key()
Definition: object.hpp:586
msgpack::type::NIL
Definition: object_fwd_decl.hpp:29
msgpack::object_equal_visitor::start_map
bool start_map(uint32_t num_kv_pairs)
Definition: object.hpp:969
msgpack::object_stringize_visitor
Definition: object.hpp:396
msgpack::object_stringize_visitor::end_array
bool end_array()
Definition: object.hpp:492
msgpack::adaptor::pack< msgpack::object >::operator()
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, msgpack::object const &v) const
Definition: object.hpp:675
msgpack::object_stringize_visitor::visit_ext
bool visit_ext(const char *, uint32_t)
Definition: object.hpp:473
msgpack::object_pack_visitor::start_map_key
bool start_map_key()
Definition: object.hpp:376
msgpack::type::NEGATIVE_INTEGER
Definition: object_fwd_decl.hpp:32
msgpack::object_equal_visitor::visit_boolean
bool visit_boolean(bool v)
Definition: object.hpp:886
MSGPACK_USE_CPP03
#define MSGPACK_USE_CPP03
Definition: cpp_config_decl.hpp:21
msgpack::object::convert
implicit_type convert() const
Definition: object.hpp:1060
msgpack::object_equal_visitor
Definition: object.hpp:876
msgpack::object_equal_visitor::end_array_item
bool end_array_item()
Definition: object.hpp:961
msgpack::object_equal_visitor::end_map
bool end_map()
Definition: object.hpp:993
msgpack::adaptor::object_with_zone
Definition: adaptor_base.hpp:43
msgpack::object_pack_visitor::visit_positive_integer
bool visit_positive_integer(uint64_t v)
Definition: object.hpp:328
msgpack::object_stringize_visitor::visit_float64
bool visit_float64(double v)
Definition: object.hpp:420
msgpack::object_equal_visitor::visit_float64
bool visit_float64(double v)
Definition: object.hpp:914
msgpack::is_pointer
Definition: cpp_config_decl.hpp:81
msgpack::object_equal_visitor::start_map_key
bool start_map_key()
Definition: object.hpp:978
msgpack::aligned_zone_size_visitor::visit_nil
bool visit_nil()
Definition: object.hpp:532
msgpack::pack_copy
void pack_copy(msgpack::packer< Stream > &o, T v)
Definition: object.hpp:1208
msgpack::aligned_zone_size_visitor
Definition: object.hpp:529
msgpack::aligned_zone_size_visitor::visit_negative_integer
bool visit_negative_integer(int64_t)
Definition: object.hpp:541
msgpack::object_stringize_visitor::end_array_item
bool end_array_item()
Definition: object.hpp:485
msgpack::object_pack_visitor::visit_negative_integer
bool visit_negative_integer(int64_t v)
Definition: object.hpp:332
msgpack::object_equal_visitor::visit_bin
bool visit_bin(const char *v, uint32_t size)
Definition: object.hpp:930
msgpack::type_error
Definition: object_fwd.hpp:236
msgpack::aligned_zone_size_visitor::visit_bin
bool visit_bin(const char *, uint32_t size)
Definition: object.hpp:554
msgpack::object_stringize_visitor::end_map_value
bool end_map_value()
Definition: object.hpp:512
msgpack::object_stringize_visitor::visit_positive_integer
bool visit_positive_integer(uint64_t v)
Definition: object.hpp:408
msgpack::define::define_type
define< Type > define_type
Definition: object.hpp:850
msgpack::adaptor::pack
Definition: adaptor_base.hpp:32
msgpack::type::size
std::size_t size(T const &t)
Definition: size_equal_only.hpp:24
msgpack::object::operator=
object & operator=(const T &v)
Definition: object.hpp:1146
msgpack::object::convert_if_not_nil
bool convert_if_not_nil(T &v) const
Convert the object if not nil.
Definition: object.hpp:1099
msgpack::detail::add_ext_type_size< 4 >
std::size_t add_ext_type_size< 4 >(std::size_t size)
Definition: object.hpp:146
msgpack::operator<<
msgpack::enable_if< !is_array< T >::value, msgpack::packer< Stream > & >::type operator<<(msgpack::packer< Stream > &o, T const &v)
Definition: adaptor_base.hpp:72
msgpack::object_pack_visitor::start_map
bool start_map(uint32_t num_kv_pairs)
Definition: object.hpp:372
msgpack::type::EXT
Definition: object_fwd_decl.hpp:43
msgpack::object_parser
Definition: object.hpp:151
msgpack::object::union_type::array
msgpack::object_array array
Definition: object_fwd.hpp:85
msgpack::object_stringize_visitor::start_array
bool start_array(uint32_t num_elements)
Definition: object.hpp:477
msgpack::object_parser::parse
void parse(Visitor &v)
Definition: object.hpp:238
msgpack::pack
void pack(msgpack::packer< Stream > &o, const T &v)
Definition: object.hpp:1201
msgpack::enable_if
Definition: cpp_config_decl.hpp:56
msgpack::object_stringize_visitor::visit_bin
bool visit_bin(const char *v, uint32_t size)
Definition: object.hpp:469
msgpack::object_stringize_visitor::visit_str
bool visit_str(const char *v, uint32_t size)
Definition: object.hpp:424
msgpack::adaptor::object_with_zone< msgpack::object >::operator()
void operator()(msgpack::object::with_zone &o, msgpack::object const &v) const
Definition: object.hpp:684
msgpack::object
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:75
msgpack::object_stringize_visitor::visit_nil
bool visit_nil()
Definition: object.hpp:399
msgpack::object_pack_visitor::visit_str
bool visit_str(const char *v, uint32_t size)
Definition: object.hpp:344
msgpack::aligned_zone_size_visitor::visit_positive_integer
bool visit_positive_integer(uint64_t)
Definition: object.hpp:538
msgpack::detail::packer_serializer
Definition: object.hpp:629
msgpack::object::type
msgpack::type::object_type type
Definition: object_fwd.hpp:92
msgpack::aligned_zone_size_visitor::visit_str
bool visit_str(const char *, uint32_t size)
Definition: object.hpp:550
msgpack::object_pack_visitor::end_map_value
bool end_map_value()
Definition: object.hpp:385
MSGPACK_API_VERSION_NAMESPACE
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:58
msgpack::type::ARRAY
Definition: object_fwd_decl.hpp:41
msgpack::object_equal_visitor::end_map_value
bool end_map_value()
Definition: object.hpp:989
msgpack::object_stringize_visitor::start_array_item
bool start_array_item()
Definition: object.hpp:482
msgpack::object_stringize_visitor::start_map
bool start_map(uint32_t num_kv_pairs)
Definition: object.hpp:497
msgpack::object_equal_visitor::object_equal_visitor
object_equal_visitor(msgpack::object const &obj, bool &result)
Definition: object.hpp:877
msgpack::object_equal_visitor::visit_ext
bool visit_ext(const char *v, uint32_t size)
Definition: object.hpp:939
msgpack::object_handle::operator*
const msgpack::object & operator*() const
Definition: object.hpp:77
msgpack::object_kv::key
msgpack::object key
Definition: object.hpp:31
msgpack::object_handle::zone
const msgpack::unique_ptr< msgpack::zone > & zone() const
Get unique_ptr const reference of zone.
Definition: object.hpp:97
msgpack::type::MAP
Definition: object_fwd_decl.hpp:42
msgpack::aligned_zone_size_visitor::start_map_key
bool start_map_key()
Definition: object.hpp:583
msgpack::object_handle::zone
msgpack::unique_ptr< msgpack::zone > & zone()
Get unique_ptr reference of zone.
Definition: object.hpp:90
msgpack::type::FLOAT64
Definition: object_fwd_decl.hpp:34
msgpack::object_stringize_visitor::start_map_key
bool start_map_key()
Definition: object.hpp:502
msgpack::object_pack_visitor::visit_bin
bool visit_bin(const char *v, uint32_t size)
Definition: object.hpp:349
MSGPACK_ZONE_ALIGNOF
#define MSGPACK_ZONE_ALIGNOF(type)
Definition: cpp03_zone_decl.hpp:30
check_container_size.hpp
msgpack::object::convert
msgpack::enable_if< !msgpack::is_array< T >::value &&!msgpack::is_pointer< T >::value, T &>::type convert(T &v) const
Convert the object.
Definition: object.hpp:1071
msgpack::object_equal_visitor::visit_str
bool visit_str(const char *v, uint32_t size)
Definition: object.hpp:921
msgpack::detail::add_ext_type_size
std::size_t add_ext_type_size(std::size_t size)
Definition: object.hpp:141
msgpack::adaptor::object
Definition: adaptor_base.hpp:38
msgpack::object_equal_visitor::visit_float32
bool visit_float32(float v)
Definition: object.hpp:907
msgpack::packer
The class template that supports continuous packing.
Definition: adaptor_base_decl.hpp:24
msgpack::object_pack_visitor::visit_float32
bool visit_float32(float v)
Definition: object.hpp:336
msgpack::object_handle::operator->
const msgpack::object * operator->() const
Definition: object.hpp:83
MSGPACK_NULLPTR
#define MSGPACK_NULLPTR
Definition: cpp_config_decl.hpp:35
msgpack::move
T & move(T &t)
msgpack::clone
object_handle clone(msgpack::object const &obj)
clone object
Definition: object.hpp:617
msgpack::object_pack_visitor::end_map
bool end_map()
Definition: object.hpp:388
v2::object::with_zone::zone
msgpack::zone & zone
Definition: object.hpp:37
msgpack::object_equal_visitor::visit_negative_integer
bool visit_negative_integer(int64_t v)
Definition: object.hpp:900
msgpack::define::msgpack_unpack
void msgpack_unpack(object const &o)
Definition: object.hpp:860
msgpack::object_pack_visitor::start_map_value
bool start_map_value()
Definition: object.hpp:382
msgpack::object_stringize_visitor::visit_boolean
bool visit_boolean(bool v)
Definition: object.hpp:403
msgpack::aligned_zone_size_visitor::visit_float32
bool visit_float32(float)
Definition: object.hpp:544
msgpack::adaptor::convert
Definition: adaptor_base.hpp:27
msgpack::detail::packer_serializer::pack
static msgpack::packer< Stream > & pack(msgpack::packer< Stream > &o, const T &v)
Definition: object.hpp:630
msgpack::object_handle
The class holds object and zone.
Definition: object.hpp:44
msgpack::object_pack_visitor::start_array_item
bool start_array_item()
Definition: object.hpp:363
msgpack::aligned_zone_size
std::size_t aligned_zone_size(msgpack::object const &obj)
Definition: object.hpp:602
msgpack::operator!=
bool operator!=(const msgpack::object &x, const msgpack::object &y)
Definition: object.hpp:1044
msgpack::adaptor::convert::operator()
msgpack::object const & operator()(msgpack::object const &o, T &v) const
Definition: object.hpp:641
msgpack::object_stringize_visitor::start_map_value
bool start_map_value()
Definition: object.hpp:509
msgpack::adaptor::pack::operator()
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, T const &v) const
Definition: object.hpp:650
msgpack::type::POSITIVE_INTEGER
Definition: object_fwd_decl.hpp:31
msgpack::operator>>
msgpack::enable_if< !is_array< T >::value, msgpack::object const & >::type operator>>(msgpack::object const &o, T &v)
Definition: adaptor_base.hpp:57
msgpack::object_pack_visitor::object_pack_visitor
object_pack_visitor(msgpack::packer< Stream > &pk)
Definition: object.hpp:317
msgpack::object_pack_visitor::visit_float64
bool visit_float64(double v)
Definition: object.hpp:340
msgpack::aligned_zone_size_visitor::start_map_value
bool start_map_value()
Definition: object.hpp:589
msgpack::aligned_zone_size_visitor::visit_float64
bool visit_float64(double)
Definition: object.hpp:547