Hideaki Tai / msgpack-embedded

Dependents:   hello_message_pack

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers deque.hpp Source File

deque.hpp

00001 //
00002 // MessagePack for C++ static resolution routine
00003 //
00004 // Copyright (C) 2008-2015 FURUHASHI Sadayuki
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_DEQUE_HPP
00011 #define MSGPACK_TYPE_DEQUE_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 <deque>
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::deque<T, Alloc>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
00031     std::deque<T, Alloc> operator()(const msgpack::object& o) const {
00032         if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
00033         std::deque<T, Alloc> v;
00034         if (o.via.array.size > 0) {
00035             msgpack::object* p = o.via.array.ptr;
00036             msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
00037             do {
00038                 v.push_back(p->as<T>());
00039                 ++p;
00040             } while (p < pend);
00041         }
00042         return v;
00043     }
00044 };
00045 
00046 #endif // !defined(MSGPACK_USE_CPP03)
00047 
00048 template <typename T, typename Alloc>
00049 struct convert<std::deque<T, Alloc> > {
00050     msgpack::object const& operator()(msgpack::object const& o, std::deque<T, Alloc>& v) const {
00051         if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
00052         v.resize(o.via.array.size);
00053         msgpack::object* p = o.via.array.ptr;
00054         msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
00055         typename std::deque<T, Alloc>::iterator it = v.begin();
00056         for(; p < pend; ++p, ++it) {
00057             p->convert(*it);
00058         }
00059         return o;
00060     }
00061 };
00062 
00063 template <typename T, typename Alloc>
00064 struct pack<std::deque<T, Alloc> > {
00065     template <typename Stream>
00066     msgpack::packer<Stream> & operator()(msgpack::packer<Stream> & o, const std::deque<T, Alloc>& v) const {
00067         uint32_t size = checked_get_container_size(v.size());
00068         o.pack_array(size);
00069         for(typename std::deque<T, Alloc>::const_iterator it(v.begin()), it_end(v.end());
00070             it != it_end; ++it) {
00071             o.pack(*it);
00072         }
00073         return o;
00074     }
00075 };
00076 
00077 template <typename T, typename Alloc>
00078 struct object_with_zone<std::deque<T, Alloc> > {
00079     void operator()(msgpack::object::with_zone& o, const std::deque<T, Alloc>& v) const {
00080         o.type = msgpack::type::ARRAY;
00081         if(v.empty()) {
00082             o.via.array.ptr = nullptr;
00083             o.via.array.size = 0;
00084         } else {
00085             uint32_t size = checked_get_container_size(v.size());
00086             msgpack::object* p = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*size));
00087             msgpack::object* const pend = p + size;
00088             o.via.array.ptr = p;
00089             o.via.array.size = size;
00090             typename std::deque<T, Alloc>::const_iterator it(v.begin());
00091             do {
00092                 *p = msgpack::object(*it, o.zone);
00093                 ++p;
00094                 ++it;
00095             } while(p < pend);
00096         }
00097     }
00098 };
00099 
00100 } // namespace adaptor
00101 
00102 /// @cond
00103 }  // MSGPACK_API_VERSION_NAMESPACE(v1)
00104 /// @endcond
00105 
00106 }  // namespace msgpack
00107 
00108 #endif /* msgpack/type/deque.hpp */