MessagePack for C++
vector_bool.hpp
Go to the documentation of this file.
1 //
2 // MessagePack for C++ static resolution routine
3 //
4 // Copyright (C) 2015 KONDO Takatoshi
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //
10 #ifndef MSGPACK_TYPE_VECTOR_BOOL_HPP
11 #define MSGPACK_TYPE_VECTOR_BOOL_HPP
12 
13 #include "msgpack/versioning.hpp"
14 #include "msgpack/object_fwd.hpp"
15 #include <vector>
16 
17 namespace msgpack {
18 
22 
23 namespace adaptor {
24 
25 template <typename Alloc>
26 struct convert<std::vector<bool, Alloc> > {
27  msgpack::object const& operator()(msgpack::object const& o, std::vector<bool, Alloc>& v) const {
28  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
29  if (o.via.array.size > 0) {
30  v.resize(o.via.array.size);
32  for (typename std::vector<bool, Alloc>::iterator it = v.begin(), end = v.end();
33  it != end;
34  ++it) {
35  *it = p->as<bool>();
36  ++p;
37  }
38  }
39  return o;
40  }
41 };
42 
43 template <typename Alloc>
44 struct pack<std::vector<bool, Alloc> > {
45  template <typename Stream>
46  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::vector<bool, Alloc>& v) const {
47  uint32_t size = checked_get_container_size(v.size());
48  o.pack_array(size);
49  for(typename std::vector<bool, Alloc>::const_iterator it(v.begin()), it_end(v.end());
50  it != it_end; ++it) {
51  o.pack(static_cast<bool>(*it));
52  }
53  return o;
54  }
55 };
56 
57 template <typename Alloc>
58 struct object_with_zone<std::vector<bool, Alloc> > {
59  void operator()(msgpack::object::with_zone& o, const std::vector<bool, Alloc>& v) const {
61  if(v.empty()) {
62  o.via.array.ptr = nullptr;
63  o.via.array.size = 0;
64  } else {
65  uint32_t size = checked_get_container_size(v.size());
66  msgpack::object* p = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*size));
67  msgpack::object* const pend = p + size;
68  o.via.array.ptr = p;
69  o.via.array.size = size;
70  typename std::vector<bool, Alloc>::const_iterator it(v.begin());
71  do {
72  *p = msgpack::object(static_cast<bool>(*it), o.zone);
73  ++p;
74  ++it;
75  } while(p < pend);
76  }
77  }
78 };
79 
80 } // namespace adaptor
81 
83 } // MSGPACK_API_VERSION_NAMESPACE(v1)
85 
86 } // namespace msgpack
87 
88 #endif // MSGPACK_TYPE_VECTOR_BOOL_HPP
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:58
uint32_t size
Definition: object_fwd.hpp:50
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition: cpp03_zone.hpp:248
uint32_t checked_get_container_size(T size)
Definition: check_container_size.hpp:55
union_type via
Definition: object_fwd.hpp:123
std::enable_if< msgpack::has_as< T >::value, T >::type as() const
Get value as T.
Definition: object.hpp:558
msgpack::zone & zone
Definition: object_fwd.hpp:262
msgpack::object * ptr
Definition: object_fwd.hpp:51
packer< Stream > & pack_array(uint32_t n)
Packing array header and size.
Definition: pack.hpp:1163
Definition: adaptor_base.hpp:15
Definition: object_fwd.hpp:260
packer< Stream > & pack(const T &v)
Packing function template.
Definition: adaptor_base.hpp:45
Definition: object_fwd.hpp:253
Definition: adaptor_base.hpp:34
void operator()(msgpack::object::with_zone &o, const std::vector< bool, Alloc > &v) const
Definition: vector_bool.hpp:59
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::vector< bool, Alloc > &v) const
Definition: vector_bool.hpp:46
msgpack::object_array array
Definition: object_fwd.hpp:115
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:106
msgpack::type::object_type type
Definition: object_fwd.hpp:122
Definition: object_fwd.hpp:39
The class template that supports continuous packing.
Definition: adaptor_base.hpp:22
object_kv * end(object_map &map)
Definition: iterator.hpp:25
msgpack::object const & operator()(msgpack::object const &o, std::vector< bool, Alloc > &v) const
Definition: vector_bool.hpp:27
Definition: adaptor_base.hpp:29