pair.hpp 2.5 KB

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