Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ IF (MSGPACK_ENABLE_CXX)
include/msgpack/adaptor/boost/msgpack_variant_decl.hpp
include/msgpack/adaptor/boost/optional.hpp
include/msgpack/adaptor/boost/string_ref.hpp
include/msgpack/adaptor/carray.hpp
include/msgpack/adaptor/char_ptr.hpp
include/msgpack/adaptor/check_container_size.hpp
include/msgpack/adaptor/check_container_size_decl.hpp
Expand Down Expand Up @@ -255,6 +256,7 @@ IF (MSGPACK_ENABLE_CXX)
include/msgpack/v1/adaptor/boost/msgpack_variant_decl.hpp
include/msgpack/v1/adaptor/boost/optional.hpp
include/msgpack/v1/adaptor/boost/string_ref.hpp
include/msgpack/v1/adaptor/carray.hpp
include/msgpack/v1/adaptor/char_ptr.hpp
include/msgpack/v1/adaptor/check_container_size.hpp
include/msgpack/v1/adaptor/check_container_size_decl.hpp
Expand Down Expand Up @@ -331,7 +333,6 @@ IF (MSGPACK_ENABLE_CXX)
include/msgpack/v1/preprocessor.hpp
include/msgpack/v1/sbuffer.hpp
include/msgpack/v1/sbuffer_decl.hpp
include/msgpack/v1/type.hpp
include/msgpack/v1/unpack.hpp
include/msgpack/v1/unpack_decl.hpp
include/msgpack/v1/version.hpp
Expand Down
15 changes: 15 additions & 0 deletions include/msgpack/adaptor/carray.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// MessagePack for C++ static resolution routine
//
// Copyright (C) 2016 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_TYPE_CARRAY_HPP
#define MSGPACK_TYPE_CARRAY_HPP

#include "msgpack/v1/adaptor/carray.hpp"

#endif // MSGPACK_TYPE_CARRAY_HPP
1 change: 1 addition & 0 deletions include/msgpack/type.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "cpp_config.hpp"
#include "adaptor/array_ref.hpp"
#include "adaptor/bool.hpp"
#include "adaptor/carray.hpp"
#include "adaptor/char_ptr.hpp"
#include "adaptor/deque.hpp"
#include "adaptor/ext.hpp"
Expand Down
42 changes: 38 additions & 4 deletions include/msgpack/v1/adaptor/adaptor_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,61 @@ struct object_with_zone {

template <typename T>
inline
msgpack::object const& operator>> (msgpack::object const& o, T& v) {
typename msgpack::enable_if<
!is_array<T>::value,
msgpack::object const&
>::type
operator>> (msgpack::object const& o, T& v) {
return msgpack::adaptor::convert<T>()(o, v);
}
template <typename T, std::size_t N>
inline
msgpack::object const& operator>> (msgpack::object const& o, T(&v)[N]) {
return msgpack::adaptor::convert<T[N]>()(o, v);
}

template <typename Stream, typename T>
inline
msgpack::packer<Stream>& operator<< (msgpack::packer<Stream>& o, T const& v) {
typename msgpack::enable_if<
!is_array<T>::value,
msgpack::packer<Stream>&
>::type
operator<< (msgpack::packer<Stream>& o, T const& v) {
return msgpack::adaptor::pack<T>()(o, v);
}
template <typename Stream, typename T, std::size_t N>
inline
msgpack::packer<Stream>& operator<< (msgpack::packer<Stream>& o, const T(&v)[N]) {
return msgpack::adaptor::pack<T[N]>()(o, v);
}

template <typename T>
inline
void operator<< (msgpack::object& o, T const& v) {
typename msgpack::enable_if<
!is_array<T>::value
>::type
operator<< (msgpack::object& o, T const& v) {
msgpack::adaptor::object<T>()(o, v);
}
template <typename T, std::size_t N>
inline
void operator<< (msgpack::v1::object& o, const T(&v)[N]) {
msgpack::v1::adaptor::object<T[N]>()(o, v);
}

template <typename T>
inline
void operator<< (msgpack::object::with_zone& o, T const& v) {
typename msgpack::enable_if<
!is_array<T>::value
>::type
operator<< (msgpack::object::with_zone& o, T const& v) {
msgpack::adaptor::object_with_zone<T>()(o, v);
}
template <typename T, std::size_t N>
inline
void operator<< (msgpack::object::with_zone& o, const T(&v)[N]) {
msgpack::adaptor::object_with_zone<T[N]>()(o, v);
}

/// @cond
} // MSGPACK_API_VERSION_NAMESPACE(v1)
Expand Down
31 changes: 27 additions & 4 deletions include/msgpack/v1/adaptor/adaptor_base_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "msgpack/versioning.hpp"
#include "msgpack/object_fwd.hpp"
#include "msgpack/pack.hpp"

namespace msgpack {

Expand Down Expand Up @@ -43,16 +44,38 @@ struct object_with_zone;
// operators

template <typename T>
msgpack::object const& operator>> (msgpack::object const& o, T& v);
typename msgpack::enable_if<
!is_array<T>::value,
msgpack::object const&
>::type
operator>> (msgpack::object const& o, T& v);
template <typename T, std::size_t N>
msgpack::object const& operator>> (msgpack::object const& o, T(&v)[N]);

template <typename Stream, typename T>
msgpack::packer<Stream>& operator<< (msgpack::packer<Stream>& o, T const& v);
typename msgpack::enable_if<
!is_array<T>::value,
msgpack::packer<Stream>&
>::type
operator<< (msgpack::packer<Stream>& o, T const& v);
template <typename Stream, typename T, std::size_t N>
msgpack::packer<Stream>& operator<< (msgpack::packer<Stream>& o, const T(&v)[N]);

template <typename T>
void operator<< (msgpack::object& o, T const& v);
typename msgpack::enable_if<
!is_array<T>::value
>::type
operator<< (msgpack::object& o, T const& v);
template <typename T, std::size_t N>
void operator<< (msgpack::object& o, const T(&v)[N]);

template <typename T>
void operator<< (msgpack::object::with_zone& o, T const& v);
typename msgpack::enable_if<
!is_array<T>::value
>::type
operator<< (msgpack::object::with_zone& o, T const& v);
template <typename T, std::size_t N>
void operator<< (msgpack::object::with_zone& o, const T(&v)[N]);

/// @cond
} // MSGPACK_API_VERSION_NAMESPACE(v1)
Expand Down
142 changes: 137 additions & 5 deletions include/msgpack/v1/adaptor/array_ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "msgpack/v1/adaptor/array_ref.hpp"
#include "msgpack/adaptor/check_container_size.hpp"
#include "msgpack/cpp_config.hpp"
#include <cstring>
#include <string>

Expand All @@ -30,6 +31,10 @@ struct array_ref {

T* data;

std::size_t size() const {
return data->size();
}

template <typename U>
bool operator==(array_ref<U> const& t) const {
return *data == *t.data;
Expand Down Expand Up @@ -60,16 +65,88 @@ struct array_ref {
}
};

template <typename T, std::size_t N>
struct array_ref<T[N]> {
array_ref() : data(nullptr) {}
array_ref(T(&t)[N]) : data(t) {}

T* data;

std::size_t size() const {
return N;
}

template <typename U>
bool operator==(array_ref<U> const& t) const {
if (N != t.size()) return false;
T const* pself = data;
U const* pother = t.data;
for (; pself != &data[N]; ++pself, ++pother) {
if (*pself != *pother) return false;
}
return true;
}
template <typename U>
bool operator!=(array_ref<U> const& t) const {
return !(*this == t);
}
template <typename U>
bool operator< (array_ref<U> const& t) const
{
T const* pself = data;
U const* pother = t.data;
for (; pself != &data[N] && pother != t.data[t.size()]; ++pself, ++pother) {
if (*pself < *pother) return true;
}
if (N < t.size()) return true;
return false;
}
template <typename U>
bool operator> (array_ref<U> const& t) const
{
return t.data < data;
}
template <typename U>
bool operator<= (array_ref<U> const& t) const
{
return !(t.data < data);
}
template <typename U>
bool operator>= (array_ref<U> const& t) const
{
return !(data < t.data);
}
};

template <typename T>
inline array_ref<T const> make_array_ref(T const& t) {
inline
typename msgpack::enable_if<
!msgpack::is_array<T const>::value,
array_ref<T const>
>::type
make_array_ref(const T& t) {
return array_ref<T const>(t);
}

template <typename T>
inline array_ref<T> make_array_ref(T& t) {
inline
typename msgpack::enable_if<
!msgpack::is_array<T>::value,
array_ref<T>
>::type
make_array_ref(T& t) {
return array_ref<T>(t);
}

template <typename T, std::size_t N>
inline array_ref<const T[N]> make_array_ref(const T(&t)[N]) {
return array_ref<const T[N]>(t);
}

template <typename T, std::size_t N>
inline array_ref<T[N]> make_array_ref(T(&t)[N]) {
return array_ref<T[N]>(t);
}

} // namespace type

Expand All @@ -80,7 +157,7 @@ struct convert<msgpack::type::array_ref<T> > {
msgpack::object const& operator()(msgpack::object const& o, msgpack::type::array_ref<T>& v) const {
if (!v.data) { throw msgpack::type_error(); }
if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
if (v.data->size() < o.via.bin.size) { throw msgpack::type_error(); }
if (v.size() < o.via.bin.size) { throw msgpack::type_error(); }
if (o.via.array.size > 0) {
msgpack::object* p = o.via.array.ptr;
msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
Expand All @@ -95,6 +172,26 @@ struct convert<msgpack::type::array_ref<T> > {
}
};

template <typename T, std::size_t N>
struct convert<msgpack::type::array_ref<T[N]> > {
msgpack::object const& operator()(msgpack::object const& o, msgpack::type::array_ref<T[N]>& v) const {
if (!v.data) { throw msgpack::type_error(); }
if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
if (v.size() < o.via.bin.size) { throw msgpack::type_error(); }
if (o.via.array.size > 0) {
msgpack::object* p = o.via.array.ptr;
msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
T* it = v.data;
do {
p->convert(*it);
++p;
++it;
} while(p < pend);
}
return o;
}
};

template <typename T>
struct convert<msgpack::type::array_ref<std::vector<T> > > {
msgpack::object const& operator()(msgpack::object const& o, msgpack::type::array_ref<std::vector<T> >& v) const {
Expand All @@ -120,7 +217,7 @@ struct pack<msgpack::type::array_ref<T> > {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const msgpack::type::array_ref<T>& v) const {
if (!v.data) { throw msgpack::type_error(); }
uint32_t size = checked_get_container_size(v.data->size());
uint32_t size = checked_get_container_size(v.size());
o.pack_array(size);
for (typename T::const_iterator it(v.data->begin()), it_end(v.data->end());
it != it_end; ++it) {
Expand All @@ -130,6 +227,21 @@ struct pack<msgpack::type::array_ref<T> > {
}
};

template <typename T, std::size_t N>
struct pack<msgpack::type::array_ref<T[N]> > {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const msgpack::type::array_ref<T[N]>& v) const {
if (!v.data) { throw msgpack::type_error(); }
uint32_t size = checked_get_container_size(v.size());
o.pack_array(size);
for (T const* it = v.data;
it != &v.data[v.size()]; ++it) {
o.pack(*it);
}
return o;
}
};

template <typename T>
struct object_with_zone<msgpack::type::array_ref<T> > {
void operator()(msgpack::object::with_zone& o, const msgpack::type::array_ref<T>& v) const {
Expand All @@ -140,7 +252,7 @@ struct object_with_zone<msgpack::type::array_ref<T> > {
o.via.array.size = 0;
}
else {
uint32_t size = checked_get_container_size(v.data->size());
uint32_t size = checked_get_container_size(v.size());
msgpack::object* p = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*size));
msgpack::object* const pend = p + size;
o.via.array.ptr = p;
Expand All @@ -162,6 +274,26 @@ struct object_with_zone<msgpack::type::array_ref<T> > {
}
};

template <typename T, std::size_t N>
struct object_with_zone<msgpack::type::array_ref<T[N]> > {
void operator()(msgpack::object::with_zone& o, const msgpack::type::array_ref<T[N]>& v) const {
if (!v.data) { throw msgpack::type_error(); }
o.type = msgpack::type::ARRAY;
uint32_t size = checked_get_container_size(v.size());
msgpack::object* p = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*size));
msgpack::object* const pend = p + size;
o.via.array.ptr = p;
o.via.array.size = size;
T const* it = v.data;
do {
*p = msgpack::object(*it, o.zone);
++p;
++it;
} while(p < pend);
}
};


} // namespace adaptor

/// @cond
Expand Down
Loading