Hideaki Tai / msgpack-embedded

Dependents:   hello_message_pack

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers vector_unsigned_char.hpp Source File

vector_unsigned_char.hpp

00001 //
00002 // MessagePack for C++ static resolution routine
00003 //
00004 // Copyright (C) 2014-2015 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_UNSIGNED_CHAR_HPP
00011 #define MSGPACK_TYPE_VECTOR_UNSIGNED_CHAR_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 template <typename Alloc>
00028 struct convert<std::vector<unsigned char, Alloc> > {
00029     msgpack::object const& operator()(msgpack::object const& o, std::vector<unsigned char, Alloc>& v) const {
00030         switch (o.type) {
00031         case msgpack::type::BIN:
00032             v.resize(o.via.bin.size);
00033             std::memcpy(&v.front(), o.via.bin.ptr, o.via.bin.size);
00034             break;
00035         case msgpack::type::STR:
00036             v.resize(o.via.str.size);
00037             std::memcpy(&v.front(), o.via.str.ptr, o.via.str.size);
00038             break;
00039         default:
00040             printf("throw msgpack::type_error()");
00041 //            throw msgpack::type_error();
00042             break;
00043         }
00044         return o;
00045     }
00046 };
00047 
00048 template <typename Alloc>
00049 struct pack<std::vector<unsigned char, Alloc> > {
00050     template <typename Stream>
00051     msgpack::packer<Stream> & operator()(msgpack::packer<Stream> & o, const std::vector<unsigned char, Alloc>& v) const {
00052         uint32_t size = checked_get_container_size(v.size());
00053         o.pack_bin(size);
00054         o.pack_bin_body(reinterpret_cast<char const*>(&v.front()), size);
00055 
00056         return o;
00057     }
00058 };
00059 
00060 template <typename Alloc>
00061 struct object<std::vector<unsigned char, Alloc> > {
00062     void operator()(msgpack::object& o, const std::vector<unsigned char, Alloc>& v) const {
00063         uint32_t size = checked_get_container_size(v.size());
00064         o.type = msgpack::type::BIN;
00065         o.via.bin.ptr = reinterpret_cast<char const*>(&v.front());
00066         o.via.bin.size = size;
00067     }
00068 };
00069 
00070 template <typename Alloc>
00071 struct object_with_zone<std::vector<unsigned char, Alloc> > {
00072     void operator()(msgpack::object::with_zone& o, const std::vector<unsigned char, Alloc>& v) const {
00073         uint32_t size = checked_get_container_size(v.size());
00074         o.type = msgpack::type::BIN;
00075         char* ptr = static_cast<char*>(o.zone.allocate_align(size));
00076         o.via.bin.ptr = ptr;
00077         o.via.bin.size = size;
00078         std::memcpy(ptr, &v.front(), size);
00079     }
00080 };
00081 
00082 } // namespace adaptor
00083 
00084 /// @cond
00085 } // MSGPACK_API_VERSION_NAMESPACE(v1)
00086 /// @endcond
00087 
00088 } // namespace msgpack
00089 
00090 #endif // MSGPACK_TYPE_VECTOR_UNSIGNED_CHAR_HPP