MessagePack for C++
array_ref.hpp
Go to the documentation of this file.
1 //
2 // MessagePack for C++ static resolution routine
3 //
4 // Copyright (C) 2008-2016 FURUHASHI Sadayuki
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_V1_TYPE_ARRAY_REF_HPP
11 #define MSGPACK_V1_TYPE_ARRAY_REF_HPP
12 
15 #include <cstring>
16 #include <string>
17 
18 namespace msgpack {
19 
23 
24 namespace type {
25 
26 template <typename T>
27 struct array_ref {
29  array_ref(T& t) : data(&t) {}
30 
31  T* data;
32 
33  template <typename U>
34  bool operator==(array_ref<U> const& t) const {
35  return *data == *t.data;
36  }
37  template <typename U>
38  bool operator!=(array_ref<U> const& t) const {
39  return !(*data == *t.data);
40  }
41  template <typename U>
42  bool operator< (array_ref<U> const& t) const
43  {
44  return *data < *t.data;
45  }
46  template <typename U>
47  bool operator> (array_ref<U> const& t) const
48  {
49  return *t.data < *data;
50  }
51  template <typename U>
52  bool operator<= (array_ref<U> const& t) const
53  {
54  return !(*t.data < *data);
55  }
56  template <typename U>
57  bool operator>= (array_ref<U> const& t) const
58  {
59  return !(*data < *t.data);
60  }
61 };
62 
63 template <typename T>
64 inline array_ref<T const> make_array_ref(T const& t) {
65  return array_ref<T const>(t);
66 }
67 
68 template <typename T>
70  return array_ref<T>(t);
71 }
72 
73 
74 } // namespace type
75 
76 namespace adaptor {
77 
78 template <typename T>
81  if (!v.data) { throw msgpack::type_error(); }
82  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
83  if (v.data->size() < o.via.bin.size) { throw msgpack::type_error(); }
84  if (o.via.array.size > 0) {
86  msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
87  typename T::iterator it = v.data->begin();
88  do {
89  p->convert(*it);
90  ++p;
91  ++it;
92  } while(p < pend);
93  }
94  return o;
95  }
96 };
97 
98 template <typename T>
99 struct convert<msgpack::type::array_ref<std::vector<T> > > {
100  msgpack::object const& operator()(msgpack::object const& o, msgpack::type::array_ref<std::vector<T> >& v) const {
101  if (!v.data) { throw msgpack::type_error(); }
102  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
103  v.data->resize(o.via.bin.size);
104  if (o.via.array.size > 0) {
105  msgpack::object* p = o.via.array.ptr;
106  msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
107  typename std::vector<T>::iterator it = v.data->begin();
108  do {
109  p->convert(*it);
110  ++p;
111  ++it;
112  } while(p < pend);
113  }
114  return o;
115  }
116 };
117 
118 template <typename T>
120  template <typename Stream>
122  if (!v.data) { throw msgpack::type_error(); }
123  uint32_t size = checked_get_container_size(v.data->size());
124  o.pack_array(size);
125  for (typename T::const_iterator it(v.data->begin()), it_end(v.data->end());
126  it != it_end; ++it) {
127  o.pack(*it);
128  }
129  return o;
130  }
131 };
132 
133 template <typename T>
136  if (!v.data) { throw msgpack::type_error(); }
138  if (v.data->empty()) {
139  o.via.array.ptr = nullptr;
140  o.via.array.size = 0;
141  }
142  else {
143  uint32_t size = checked_get_container_size(v.data->size());
144  msgpack::object* p = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*size));
145  msgpack::object* const pend = p + size;
146  o.via.array.ptr = p;
147  o.via.array.size = size;
148  typename T::const_iterator it(v.data->begin());
149  do {
150 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
151 #pragma GCC diagnostic push
152 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
153 #endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
154  *p = msgpack::object(*it, o.zone);
155 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
156 #pragma GCC diagnostic pop
157 #endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
158  ++p;
159  ++it;
160  } while(p < pend);
161  }
162  }
163 };
164 
165 } // namespace adaptor
166 
168 } // MSGPACK_API_VERSION_NAMESPACE(v1)
170 
171 } // namespace msgpack
172 
173 #endif // MSGPACK_V1_TYPE_ARRAY_REF_HPP
uint32_t size
Definition: object_fwd.hpp:23
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition: cpp03_zone.hpp:236
msgpack::object const & operator()(msgpack::object const &o, msgpack::type::array_ref< T > &v) const
Definition: array_ref.hpp:80
uint32_t checked_get_container_size(T size)
Definition: check_container_size.hpp:55
bool operator>=(array_ref< U > const &t) const
Definition: array_ref.hpp:57
union_type via
Definition: object_fwd.hpp:92
T * data
Definition: array_ref.hpp:31
msgpack::zone & zone
Definition: object.hpp:36
msgpack::object * ptr
Definition: object_fwd.hpp:24
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const msgpack::type::array_ref< T > &v) const
Definition: array_ref.hpp:121
packer< Stream > & pack_array(uint32_t n)
Packing array header and size.
Definition: pack.hpp:1160
uint32_t size
Definition: object_fwd.hpp:38
Definition: adaptor_base.hpp:15
Definition: object.hpp:34
packer< Stream > & pack(const T &v)
Packing function template.
Definition: array_ref.hpp:27
Definition: adaptor_base.hpp:43
Definition: object_fwd.hpp:222
array_ref()
Definition: array_ref.hpp:28
bool operator==(array_ref< U > const &t) const
Definition: array_ref.hpp:34
Definition: adaptor_base.hpp:32
array_ref< T const > make_array_ref(T const &t)
Definition: array_ref.hpp:64
#define nullptr
Definition: cpp_config_decl.hpp:30
msgpack::object_array array
Definition: object_fwd.hpp:84
T & convert(T &v) const
Convert the object.
Definition: object.hpp:529
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:75
msgpack::object const & operator()(msgpack::object const &o, msgpack::type::array_ref< std::vector< T > > &v) const
Definition: array_ref.hpp:100
msgpack::type::object_type type
Definition: object_fwd.hpp:91
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:58
Definition: object_fwd_decl.hpp:39
bool operator>(array_ref< U > const &t) const
Definition: array_ref.hpp:47
bool operator!=(array_ref< U > const &t) const
Definition: array_ref.hpp:38
The class template that supports continuous packing.
Definition: adaptor_base_decl.hpp:23
void operator()(msgpack::object::with_zone &o, const msgpack::type::array_ref< T > &v) const
Definition: array_ref.hpp:135
array_ref(T &t)
Definition: array_ref.hpp:29
Definition: adaptor_base.hpp:27
msgpack::object_bin bin
Definition: object_fwd.hpp:87