Hideaki Tai / msgpack-embedded

Dependents:   hello_message_pack

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers vector.hpp Source File

vector.hpp

00001 //
00002 // MessagePack for C++ static resolution routine
00003 //
00004 // Copyright (C) 2008-2015 FURUHASHI Sadayuki and KONDO Takatoshi
00005 //
00006 //    Distributed under the Boost Software License, Version 1.0.
00007 //    (See accompanying file LICENSE_1_0.txt or copy at
00008 //    http://www.boost.org/LICENSE_1_0.txt)
00009 //
00010 #ifndef MSGPACK_TYPE_VECTOR_HPP
00011 #define MSGPACK_TYPE_VECTOR_HPP
00012 
00013 #include "msgpack/versioning.hpp"
00014 #include "msgpack/adaptor/adaptor_base.hpp"
00015 #include "msgpack/adaptor/check_container_size.hpp"
00016 
00017 #include <vector>
00018 
00019 namespace msgpack {
00020 
00021 /// @cond
00022 MSGPACK_API_VERSION_NAMESPACE(v1) {
00023 /// @endcond
00024 
00025 namespace adaptor {
00026 
00027 #if !defined(MSGPACK_USE_CPP03)
00028 
00029 template <typename T, typename Alloc>
00030 struct as<std::vector<T, Alloc>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
00031     std::vector<T, Alloc> operator()(const msgpack::object& o) const {
00032         if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
00033         std::vector<T, Alloc> v;
00034         v.reserve(o.via.array.size);
00035         if (o.via.array.size > 0) {
00036             msgpack::object* p = o.via.array.ptr;
00037             msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
00038             do {
00039                 v.push_back(p->as<T>());
00040                 ++p;
00041             } while (p < pend);
00042         }
00043         return v;
00044     }
00045 };
00046 
00047 #endif // !defined(MSGPACK_USE_CPP03)
00048 
00049 template <typename T, typename Alloc>
00050 struct convert<std::vector<T, Alloc> > {
00051     msgpack::object const& operator()(msgpack::object const& o, std::vector<T, Alloc>& v) const {
00052         if (o.type != msgpack::type::ARRAY) { 
00053 //            throw msgpack::type_error(); 
00054         }
00055         v.resize(o.via.array.size);
00056         if (o.via.array.size > 0) {
00057             msgpack::object* p = o.via.array.ptr;
00058             msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
00059             typename std::vector<T, Alloc>::iterator it = v.begin();
00060             do {
00061                 p->convert(*it);
00062                 ++p;
00063                 ++it;
00064             } while(p < pend);
00065         }
00066         return o;
00067     }
00068 };
00069 
00070 template <typename T, typename Alloc>
00071 struct pack<std::vector<T, Alloc> > {
00072     template <typename Stream>
00073     msgpack::packer<Stream> & operator()(msgpack::packer<Stream> & o, const std::vector<T, Alloc>& v) const {
00074         uint32_t size = checked_get_container_size(v.size());
00075         o.pack_array(size);
00076         for (typename std::vector<T, Alloc>::const_iterator it(v.begin()), it_end(v.end());
00077             it != it_end; ++it) {
00078             o.pack(*it);
00079         }
00080         return o;
00081     }
00082 };
00083 
00084 template <typename T, typename Alloc>
00085 struct object_with_zone<std::vector<T, Alloc> > {
00086     void operator()(msgpack::object::with_zone& o, const std::vector<T, Alloc>& v) const {
00087         o.type = msgpack::type::ARRAY;
00088         if (v.empty()) {
00089             o.via.array.ptr = nullptr;
00090             o.via.array.size = 0;
00091         }
00092         else {
00093             uint32_t size = checked_get_container_size(v.size());
00094             msgpack::object* p = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*size));
00095             msgpack::object* const pend = p + size;
00096             o.via.array.ptr = p;
00097             o.via.array.size = size;
00098             typename std::vector<T, Alloc>::const_iterator it(v.begin());
00099             do {
00100 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
00101 #pragma GCC diagnostic push
00102 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
00103 #endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
00104                 *p = msgpack::object(*it, o.zone);
00105 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
00106 #pragma GCC diagnostic pop
00107 #endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__)
00108                 ++p;
00109                 ++it;
00110             } while(p < pend);
00111         }
00112     }
00113 };
00114 
00115 } // namespace adaptor
00116 
00117 /// @cond
00118 } // MSGPACK_API_VERSION_NAMESPACE(v1)
00119 /// @endcond
00120 
00121 } // namespace msgpack
00122 
00123 #endif // MSGPACK_TYPE_VECTOR_HPP