pair.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // MessagePack for C++ static resolution routine
  3. //
  4. // Copyright (C) 2008-2009 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_PAIR_HPP
  11. #define MSGPACK_V1_TYPE_PAIR_HPP
  12. #include "msgpack/versioning.hpp"
  13. #include "msgpack/adaptor/adaptor_base.hpp"
  14. #include "msgpack/object.hpp"
  15. #include "msgpack/meta.hpp"
  16. #include <utility>
  17. namespace msgpack {
  18. /// @cond
  19. MSGPACK_API_VERSION_NAMESPACE(v1) {
  20. /// @endcond
  21. namespace adaptor {
  22. #if !defined(MSGPACK_USE_CPP03)
  23. template <typename T1, typename T2>
  24. struct as<std::pair<T1, T2>,
  25. typename std::enable_if<msgpack::any_of<msgpack::has_as, T1, T2>::value>::type> {
  26. std::pair<T1, T2> operator()(msgpack::object const& o) const {
  27. if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
  28. if (o.via.array.size != 2) { throw msgpack::type_error(); }
  29. return std::make_pair(o.via.array.ptr[0].as<T1>(), o.via.array.ptr[1].as<T2>());
  30. }
  31. };
  32. #endif // !defined(MSGPACK_USE_CPP03)
  33. template <typename T1, typename T2>
  34. struct convert<std::pair<T1, T2> > {
  35. msgpack::object const& operator()(msgpack::object const& o, std::pair<T1, T2>& v) const {
  36. if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
  37. if(o.via.array.size != 2) { throw msgpack::type_error(); }
  38. o.via.array.ptr[0].convert(v.first);
  39. o.via.array.ptr[1].convert(v.second);
  40. return o;
  41. }
  42. };
  43. template <typename T1, typename T2>
  44. struct pack<std::pair<T1, T2> > {
  45. template <typename Stream>
  46. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::pair<T1, T2>& v) const {
  47. o.pack_array(2);
  48. o.pack(v.first);
  49. o.pack(v.second);
  50. return o;
  51. }
  52. };
  53. template <typename T1, typename T2>
  54. struct object_with_zone<std::pair<T1, T2> > {
  55. void operator()(msgpack::object::with_zone& o, const std::pair<T1, T2>& v) const {
  56. o.type = msgpack::type::ARRAY;
  57. msgpack::object* p = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*2, MSGPACK_ZONE_ALIGNOF(msgpack::object)));
  58. o.via.array.ptr = p;
  59. o.via.array.size = 2;
  60. p[0] = msgpack::object(v.first, o.zone);
  61. p[1] = msgpack::object(v.second, o.zone);
  62. }
  63. };
  64. } // namespace adaptor
  65. /// @cond
  66. } // MSGPACK_API_VERSION_NAMESPACE(v1)
  67. /// @endcond
  68. } // namespace msgpack
  69. #endif // MSGPACK_V1_TYPE_PAIR_HPP