wstring.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // MessagePack for C++ static resolution routine
  3. //
  4. // Copyright (C) 2018 KONDO Takatoshi
  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_WSTRING_HPP
  11. #define MSGPACK_V1_TYPE_WSTRING_HPP
  12. #include "msgpack/versioning.hpp"
  13. #include "msgpack/adaptor/adaptor_base.hpp"
  14. #include "msgpack/object.hpp"
  15. #include "msgpack/adaptor/check_container_size.hpp"
  16. #include <vector>
  17. namespace msgpack {
  18. /// @cond
  19. MSGPACK_API_VERSION_NAMESPACE(v1) {
  20. /// @endcond
  21. namespace adaptor {
  22. #if !defined(MSGPACK_USE_CPP03)
  23. template <>
  24. struct as<std::wstring> {
  25. std::wstring operator()(const msgpack::object& o) const {
  26. if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
  27. std::wstring v;
  28. v.reserve(o.via.array.size);
  29. if (o.via.array.size > 0) {
  30. msgpack::object* p = o.via.array.ptr;
  31. msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
  32. do {
  33. v.push_back(p->as<wchar_t>());
  34. ++p;
  35. } while (p < pend);
  36. }
  37. return v;
  38. }
  39. };
  40. #endif // !defined(MSGPACK_USE_CPP03)
  41. template <>
  42. struct convert<std::wstring> {
  43. msgpack::object const& operator()(msgpack::object const& o, std::wstring& v) const {
  44. if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
  45. v.resize(o.via.array.size);
  46. if (o.via.array.size > 0) {
  47. msgpack::object* p = o.via.array.ptr;
  48. msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
  49. std::wstring::iterator it = v.begin();
  50. do {
  51. p->convert(*it);
  52. ++p;
  53. ++it;
  54. } while(p < pend);
  55. }
  56. return o;
  57. }
  58. };
  59. template <>
  60. struct pack<std::wstring> {
  61. template <typename Stream>
  62. msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::wstring& v) const {
  63. uint32_t size = checked_get_container_size(v.size());
  64. o.pack_array(size);
  65. for (std::wstring::const_iterator it(v.begin()), it_end(v.end());
  66. it != it_end; ++it) {
  67. o.pack(*it);
  68. }
  69. return o;
  70. }
  71. };
  72. template <>
  73. struct object_with_zone<std::wstring> {
  74. void operator()(msgpack::object::with_zone& o, const std::wstring& v) const {
  75. o.type = msgpack::type::ARRAY;
  76. if (v.empty()) {
  77. o.via.array.ptr = MSGPACK_NULLPTR;
  78. o.via.array.size = 0;
  79. }
  80. else {
  81. uint32_t size = checked_get_container_size(v.size());
  82. msgpack::object* p = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*size, MSGPACK_ZONE_ALIGNOF(msgpack::object)));
  83. msgpack::object* const pend = p + size;
  84. o.via.array.ptr = p;
  85. o.via.array.size = size;
  86. std::wstring::const_iterator it(v.begin());
  87. do {
  88. #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
  89. #pragma GCC diagnostic push
  90. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  91. #endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
  92. *p = msgpack::object(*it, o.zone);
  93. #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
  94. #pragma GCC diagnostic pop
  95. #endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
  96. ++p;
  97. ++it;
  98. } while(p < pend);
  99. }
  100. }
  101. };
  102. } // namespace adaptor
  103. /// @cond
  104. } // MSGPACK_API_VERSION_NAMESPACE(v1)
  105. /// @endcond
  106. } // namespace msgpack
  107. #endif // MSGPACK_V1_TYPE_WSTRING_HPP