create_object_visitor.hpp
Go to the documentation of this file.
//
// MessagePack for C++ deserializing routine
//
// Copyright (C) 2017 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_V2_CREATE_OBJECT_VISITOR_HPP
#define MSGPACK_V2_CREATE_OBJECT_VISITOR_HPP
#include "msgpack/unpack_decl.hpp"
#include "msgpack/unpack_exception.hpp"
#include "msgpack/v2/create_object_visitor_decl.hpp"
#include "msgpack/v2/null_visitor.hpp"
namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(v2) {
namespace detail {
class create_object_visitor : public msgpack::v2::null_visitor {
public:
create_object_visitor(unpack_reference_func f, void* user_data, unpack_limit const& limit)
:m_func(f), m_user_data(user_data), m_limit(limit) {
m_stack.reserve(MSGPACK_EMBED_STACK_SIZE);
m_stack.push_back(&m_obj);
}
#if !defined(MSGPACK_USE_CPP03)
create_object_visitor(create_object_visitor&& other)
:m_func(other.m_func),
m_user_data(other.m_user_data),
m_limit(std::move(other.m_limit)),
m_stack(std::move(other.m_stack)),
m_zone(other.m_zone),
m_referenced(other.m_referenced) {
other.m_zone = MSGPACK_NULLPTR;
m_stack[0] = &m_obj;
}
create_object_visitor& operator=(create_object_visitor&& other) {
this->~create_object_visitor();
new (this) create_object_visitor(std::move(other));
return *this;
}
#endif // !defined(MSGPACK_USE_CPP03)
void init() {
m_stack.resize(1);
m_obj = msgpack::object();
m_stack[0] = &m_obj;
}
msgpack::object const& data() const
{
return m_obj;
}
msgpack::zone const& zone() const { return *m_zone; }
msgpack::zone& zone() { return *m_zone; }
void set_zone(msgpack::zone& zone) { m_zone = &zone; }
bool referenced() const { return m_referenced; }
void set_referenced(bool referenced) { m_referenced = referenced; }
// visit functions
bool visit_nil() {
msgpack::object* obj = m_stack.back();
obj->type = msgpack::type::NIL;
return true;
}
bool visit_boolean(bool v) {
msgpack::object* obj = m_stack.back();
obj->type = msgpack::type::BOOLEAN;
obj->via.boolean = v;
return true;
}
bool visit_positive_integer(uint64_t v) {
msgpack::object* obj = m_stack.back();
obj->type = msgpack::type::POSITIVE_INTEGER;
obj->via.u64 = v;
return true;
}
bool visit_negative_integer(int64_t v) {
msgpack::object* obj = m_stack.back();
if(v >= 0) {
obj->type = msgpack::type::POSITIVE_INTEGER;
obj->via.u64 = v;
}
else {
obj->type = msgpack::type::NEGATIVE_INTEGER;
obj->via.i64 = v;
}
return true;
}
bool visit_float32(float v) {
msgpack::object* obj = m_stack.back();
obj->type = msgpack::type::FLOAT32;
obj->via.f64 = v;
return true;
}
bool visit_float64(double v) {
msgpack::object* obj = m_stack.back();
obj->type = msgpack::type::FLOAT64;
obj->via.f64 = v;
return true;
}
bool visit_str(const char* v, uint32_t size) {
if (size > m_limit.str()) throw msgpack::str_size_overflow("str size overflow");
msgpack::object* obj = m_stack.back();
obj->type = msgpack::type::STR;
if (m_func && m_func(obj->type, size, m_user_data)) {
obj->via.str.ptr = v;
set_referenced(true);
}
else {
char* tmp = static_cast<char*>(zone().allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
std::memcpy(tmp, v, size);
obj->via.str.ptr = tmp;
}
obj->via.str.size = size;
return true;
}
bool visit_bin(const char* v, uint32_t size) {
if (size > m_limit.bin()) throw msgpack::bin_size_overflow("bin size overflow");
msgpack::object* obj = m_stack.back();
obj->type = msgpack::type::BIN;
if (m_func && m_func(obj->type, size, m_user_data)) {
obj->via.bin.ptr = v;
set_referenced(true);
}
else {
char* tmp = static_cast<char*>(zone().allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
std::memcpy(tmp, v, size);
obj->via.bin.ptr = tmp;
}
obj->via.bin.size = size;
return true;
}
bool visit_ext(const char* v, uint32_t size) {
if (size > m_limit.ext()) throw msgpack::ext_size_overflow("ext size overflow");
msgpack::object* obj = m_stack.back();
obj->type = msgpack::type::EXT;
if (m_func && m_func(obj->type, size, m_user_data)) {
obj->via.ext.ptr = v;
set_referenced(true);
}
else {
char* tmp = static_cast<char*>(zone().allocate_align(size, MSGPACK_ZONE_ALIGNOF(char)));
std::memcpy(tmp, v, size);
obj->via.ext.ptr = tmp;
}
obj->via.ext.size = static_cast<uint32_t>(size - 1);
return true;
}
bool start_array(uint32_t num_elements) {
if (num_elements > m_limit.array()) throw msgpack::array_size_overflow("array size overflow");
if (m_stack.size() > m_limit.depth()) throw msgpack::depth_size_overflow("depth size overflow");
msgpack::object* obj = m_stack.back();
obj->type = msgpack::type::ARRAY;
obj->via.array.size = num_elements;
if (num_elements == 0) {
obj->via.array.ptr = MSGPACK_NULLPTR;
}
else {
size_t size = num_elements*sizeof(msgpack::object);
if (size / sizeof(msgpack::object) != num_elements) {
throw msgpack::array_size_overflow("array size overflow");
}
obj->via.array.ptr =
static_cast<msgpack::object*>(m_zone->allocate_align(size, MSGPACK_ZONE_ALIGNOF(msgpack::object)));
}
m_stack.push_back(obj->via.array.ptr);
return true;
}
bool start_array_item() {
return true;
}
bool end_array_item() {
++m_stack.back();
return true;
}
bool end_array() {
m_stack.pop_back();
return true;
}
bool start_map(uint32_t num_kv_pairs) {
if (num_kv_pairs > m_limit.map()) throw msgpack::map_size_overflow("map size overflow");
if (m_stack.size() > m_limit.depth()) throw msgpack::depth_size_overflow("depth size overflow");
msgpack::object* obj = m_stack.back();
obj->type = msgpack::type::MAP;
obj->via.map.size = num_kv_pairs;
if (num_kv_pairs == 0) {
obj->via.map.ptr = MSGPACK_NULLPTR;
}
else {
size_t size = num_kv_pairs*sizeof(msgpack::object_kv);
if (size / sizeof(msgpack::object_kv) != num_kv_pairs) {
throw msgpack::map_size_overflow("map size overflow");
}
obj->via.map.ptr =
static_cast<msgpack::object_kv*>(m_zone->allocate_align(size, MSGPACK_ZONE_ALIGNOF(msgpack::object_kv)));
}
m_stack.push_back(reinterpret_cast<msgpack::object*>(obj->via.map.ptr));
return true;
}
bool start_map_key() {
return true;
}
bool end_map_key() {
++m_stack.back();
return true;
}
bool start_map_value() {
return true;
}
bool end_map_value() {
++m_stack.back();
return true;
}
bool end_map() {
m_stack.pop_back();
return true;
}
void parse_error(size_t /*parsed_offset*/, size_t /*error_offset*/) {
throw msgpack::parse_error("parse error");
}
void insufficient_bytes(size_t /*parsed_offset*/, size_t /*error_offset*/) {
throw msgpack::insufficient_bytes("insufficient bytes");
}
private:
public:
unpack_reference_func m_func;
void* m_user_data;
unpack_limit m_limit;
msgpack::object m_obj;
std::vector<msgpack::object*> m_stack;
msgpack::zone* m_zone;
bool m_referenced;
};
} // detail
} // MSGPACK_API_VERSION_NAMESPACE(v2)
} // namespace msgpack
#endif // MSGPACK_V2_CREATE_OBJECT_VISITOR_HPP
msgpack::type::STR
Definition: object_fwd_decl.hpp:39
msgpack::detail::create_object_visitor::visit_ext
bool visit_ext(const char *v, uint32_t size)
Definition: create_object_visitor.hpp:140
msgpack::object_map::ptr
msgpack::object_kv * ptr
Definition: object_fwd.hpp:29
msgpack::unpack_limit
Definition: unpack_decl.hpp:87
msgpack::str_size_overflow
Definition: unpack_exception.hpp:79
msgpack::object_array::size
uint32_t size
Definition: object_fwd.hpp:23
msgpack::unpack_limit::bin
std::size_t bin() const
Definition: unpack_decl.hpp:105
msgpack::unpack_limit::ext
std::size_t ext() const
Definition: unpack_decl.hpp:106
msgpack::zone::allocate_align
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition: cpp03_zone.hpp:246
msgpack::detail::create_object_visitor::visit_bin
bool visit_bin(const char *v, uint32_t size)
Definition: create_object_visitor.hpp:124
msgpack::detail::create_object_visitor::zone
msgpack::zone & zone()
Definition: create_object_visitor.hpp:62
msgpack::detail::create_object_visitor::set_zone
void set_zone(msgpack::zone &zone)
Definition: create_object_visitor.hpp:63
msgpack::type::FLOAT32
Definition: object_fwd_decl.hpp:33
msgpack::type::BOOLEAN
Definition: object_fwd_decl.hpp:30
msgpack::object_bin::ptr
const char * ptr
Definition: object_fwd.hpp:39
v2
msgpack::unpack_limit::map
std::size_t map() const
Definition: unpack_decl.hpp:103
msgpack::detail::create_object_visitor::end_map
bool end_map()
Definition: create_object_visitor.hpp:221
create_object_visitor_decl.hpp
msgpack::object::via
union_type via
Definition: object_fwd.hpp:93
msgpack::detail::create_object_visitor::end_map_value
bool end_map_value()
Definition: create_object_visitor.hpp:217
msgpack::detail::create_object_visitor::start_array
bool start_array(uint32_t num_elements)
Definition: create_object_visitor.hpp:156
msgpack::detail::create_object_visitor::visit_boolean
bool visit_boolean(bool v)
Definition: create_object_visitor.hpp:72
msgpack::unpack_reference_func
bool(* unpack_reference_func)(msgpack::type::object_type type, std::size_t size, void *user_data)
The type of reference or copy judging function.
Definition: unpack_decl.hpp:74
null_visitor.hpp
msgpack::type::BIN
Definition: object_fwd_decl.hpp:40
msgpack::detail::create_object_visitor::start_map
bool start_map(uint32_t num_kv_pairs)
Definition: create_object_visitor.hpp:187
msgpack::depth_size_overflow
Definition: unpack_exception.hpp:106
msgpack::detail::create_object_visitor::init
void init()
Definition: create_object_visitor.hpp:52
msgpack::detail::create_object_visitor::m_limit
unpack_limit m_limit
Definition: create_object_visitor.hpp:235
msgpack::detail::create_object_visitor::visit_float64
bool visit_float64(double v)
Definition: create_object_visitor.hpp:102
msgpack::object_ext::ptr
const char * ptr
Definition: object_fwd.hpp:46
msgpack::object_array::ptr
msgpack::object * ptr
Definition: object_fwd.hpp:24
msgpack::detail::create_object_visitor::zone
msgpack::zone const & zone() const
Definition: create_object_visitor.hpp:61
msgpack::detail::create_object_visitor::operator=
create_object_visitor & operator=(create_object_visitor &&other)
Definition: create_object_visitor.hpp:45
msgpack::object_bin::size
uint32_t size
Definition: object_fwd.hpp:38
msgpack::detail::create_object_visitor::parse_error
void parse_error(size_t, size_t)
Definition: create_object_visitor.hpp:225
msgpack::detail::create_object_visitor::referenced
bool referenced() const
Definition: create_object_visitor.hpp:64
msgpack
Definition: adaptor_base.hpp:15
msgpack::parse_error
Definition: unpack_exception.hpp:34
msgpack::ext_size_overflow
Definition: unpack_exception.hpp:97
msgpack::object_str::ptr
const char * ptr
Definition: object_fwd.hpp:34
msgpack::zone
Definition: cpp03_zone.hpp:22
msgpack::detail::create_object_visitor::end_array
bool end_array()
Definition: create_object_visitor.hpp:183
msgpack::detail::create_object_visitor::create_object_visitor
create_object_visitor(create_object_visitor &&other)
Definition: create_object_visitor.hpp:35
msgpack::detail::create_object_visitor::visit_negative_integer
bool visit_negative_integer(int64_t v)
Definition: create_object_visitor.hpp:84
msgpack::object::union_type::boolean
bool boolean
Definition: object_fwd.hpp:77
msgpack::detail::create_object_visitor::m_referenced
bool m_referenced
Definition: create_object_visitor.hpp:239
msgpack::object_kv
Definition: object.hpp:30
msgpack::type::NIL
Definition: object_fwd_decl.hpp:29
msgpack::object::union_type::i64
int64_t i64
Definition: object_fwd.hpp:79
msgpack::type::NEGATIVE_INTEGER
Definition: object_fwd_decl.hpp:32
msgpack::detail::create_object_visitor::start_array_item
bool start_array_item()
Definition: create_object_visitor.hpp:176
msgpack::bin_size_overflow
Definition: unpack_exception.hpp:88
msgpack::object_ext::size
uint32_t size
Definition: object_fwd.hpp:45
msgpack::detail::create_object_visitor::m_zone
msgpack::zone * m_zone
Definition: create_object_visitor.hpp:238
msgpack::insufficient_bytes
Definition: unpack_exception.hpp:43
msgpack::array_size_overflow
Definition: unpack_exception.hpp:61
msgpack::type::size
std::size_t size(T const &t)
Definition: size_equal_only.hpp:24
msgpack::unpack_limit::str
std::size_t str() const
Definition: unpack_decl.hpp:104
msgpack::object::union_type::f64
double f64
Definition: object_fwd.hpp:84
msgpack::type::EXT
Definition: object_fwd_decl.hpp:43
msgpack::object::union_type::array
msgpack::object_array array
Definition: object_fwd.hpp:85
msgpack::detail::create_object_visitor::start_map_value
bool start_map_value()
Definition: create_object_visitor.hpp:214
unpack_decl.hpp
msgpack::object::union_type::map
msgpack::object_map map
Definition: object_fwd.hpp:86
msgpack::object::union_type::str
msgpack::object_str str
Definition: object_fwd.hpp:87
msgpack::unpack_limit::array
std::size_t array() const
Definition: unpack_decl.hpp:102
msgpack::object
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:75
msgpack::map_size_overflow
Definition: unpack_exception.hpp:70
msgpack::detail::create_object_visitor::create_object_visitor
create_object_visitor(unpack_reference_func f, void *user_data, unpack_limit const &limit)
Definition: create_object_visitor.hpp:28
msgpack::detail::create_object_visitor::start_map_key
bool start_map_key()
Definition: create_object_visitor.hpp:207
msgpack::detail::create_object_visitor::visit_positive_integer
bool visit_positive_integer(uint64_t v)
Definition: create_object_visitor.hpp:78
msgpack::object::type
msgpack::type::object_type type
Definition: object_fwd.hpp:92
msgpack::detail::create_object_visitor::visit_str
bool visit_str(const char *v, uint32_t size)
Definition: create_object_visitor.hpp:108
MSGPACK_API_VERSION_NAMESPACE
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:58
msgpack::type::ARRAY
Definition: object_fwd_decl.hpp:41
msgpack::detail::create_object_visitor::m_obj
msgpack::object m_obj
Definition: create_object_visitor.hpp:236
msgpack::object_map::size
uint32_t size
Definition: object_fwd.hpp:28
msgpack::type::MAP
Definition: object_fwd_decl.hpp:42
msgpack::type::FLOAT64
Definition: object_fwd_decl.hpp:34
msgpack::detail::create_object_visitor::m_stack
std::vector< msgpack::object * > m_stack
Definition: create_object_visitor.hpp:237
unpack_exception.hpp
msgpack::object_str::size
uint32_t size
Definition: object_fwd.hpp:33
MSGPACK_ZONE_ALIGNOF
#define MSGPACK_ZONE_ALIGNOF(type)
Definition: cpp03_zone_decl.hpp:30
msgpack::detail::create_object_visitor::m_func
unpack_reference_func m_func
Definition: create_object_visitor.hpp:233
msgpack::detail::create_object_visitor::end_array_item
bool end_array_item()
Definition: create_object_visitor.hpp:179
msgpack::detail::create_object_visitor::end_map_key
bool end_map_key()
Definition: create_object_visitor.hpp:210
MSGPACK_NULLPTR
#define MSGPACK_NULLPTR
Definition: cpp_config_decl.hpp:35
msgpack::move
T & move(T &t)
msgpack::detail::create_object_visitor::visit_float32
bool visit_float32(float v)
Definition: create_object_visitor.hpp:96
msgpack::object::union_type::ext
msgpack::object_ext ext
Definition: object_fwd.hpp:89
msgpack::detail::create_object_visitor::data
msgpack::object const & data() const
Definition: create_object_visitor.hpp:57
msgpack::detail::create_object_visitor::m_user_data
void * m_user_data
Definition: create_object_visitor.hpp:234
msgpack::detail::create_object_visitor
Definition: create_object_visitor.hpp:26
msgpack::unpack_limit::depth
std::size_t depth() const
Definition: unpack_decl.hpp:107
msgpack::detail::create_object_visitor::visit_nil
bool visit_nil()
Definition: create_object_visitor.hpp:67
msgpack::type::POSITIVE_INTEGER
Definition: object_fwd_decl.hpp:31
msgpack::detail::create_object_visitor::set_referenced
void set_referenced(bool referenced)
Definition: create_object_visitor.hpp:65
msgpack::object::union_type::u64
uint64_t u64
Definition: object_fwd.hpp:78
msgpack::object::union_type::bin
msgpack::object_bin bin
Definition: object_fwd.hpp:88
msgpack::detail::create_object_visitor::insufficient_bytes
void insufficient_bytes(size_t, size_t)
Definition: create_object_visitor.hpp:228