output_adapters.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #pragma once
  2. #include <algorithm> // copy
  3. #include <cstddef> // size_t
  4. #include <ios> // streamsize
  5. #include <iterator> // back_inserter
  6. #include <memory> // shared_ptr, make_shared
  7. #include <ostream> // basic_ostream
  8. #include <string> // basic_string
  9. #include <vector> // vector
  10. #include <nlohmann/detail/macro_scope.hpp>
  11. namespace nlohmann
  12. {
  13. namespace detail
  14. {
  15. /// abstract output adapter interface
  16. template<typename CharType> struct output_adapter_protocol
  17. {
  18. virtual void write_character(CharType c) = 0;
  19. virtual void write_characters(const CharType* s, std::size_t length) = 0;
  20. virtual ~output_adapter_protocol() = default;
  21. };
  22. /// a type to simplify interfaces
  23. template<typename CharType>
  24. using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
  25. /// output adapter for byte vectors
  26. template<typename CharType>
  27. class output_vector_adapter : public output_adapter_protocol<CharType>
  28. {
  29. public:
  30. explicit output_vector_adapter(std::vector<CharType>& vec) noexcept
  31. : v(vec)
  32. {}
  33. void write_character(CharType c) override
  34. {
  35. v.push_back(c);
  36. }
  37. JSON_HEDLEY_NON_NULL(2)
  38. void write_characters(const CharType* s, std::size_t length) override
  39. {
  40. std::copy(s, s + length, std::back_inserter(v));
  41. }
  42. private:
  43. std::vector<CharType>& v;
  44. };
  45. /// output adapter for output streams
  46. template<typename CharType>
  47. class output_stream_adapter : public output_adapter_protocol<CharType>
  48. {
  49. public:
  50. explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
  51. : stream(s)
  52. {}
  53. void write_character(CharType c) override
  54. {
  55. stream.put(c);
  56. }
  57. JSON_HEDLEY_NON_NULL(2)
  58. void write_characters(const CharType* s, std::size_t length) override
  59. {
  60. stream.write(s, static_cast<std::streamsize>(length));
  61. }
  62. private:
  63. std::basic_ostream<CharType>& stream;
  64. };
  65. /// output adapter for basic_string
  66. template<typename CharType, typename StringType = std::basic_string<CharType>>
  67. class output_string_adapter : public output_adapter_protocol<CharType>
  68. {
  69. public:
  70. explicit output_string_adapter(StringType& s) noexcept
  71. : str(s)
  72. {}
  73. void write_character(CharType c) override
  74. {
  75. str.push_back(c);
  76. }
  77. JSON_HEDLEY_NON_NULL(2)
  78. void write_characters(const CharType* s, std::size_t length) override
  79. {
  80. str.append(s, length);
  81. }
  82. private:
  83. StringType& str;
  84. };
  85. template<typename CharType, typename StringType = std::basic_string<CharType>>
  86. class output_adapter
  87. {
  88. public:
  89. output_adapter(std::vector<CharType>& vec)
  90. : oa(std::make_shared<output_vector_adapter<CharType>>(vec)) {}
  91. output_adapter(std::basic_ostream<CharType>& s)
  92. : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
  93. output_adapter(StringType& s)
  94. : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
  95. operator output_adapter_t<CharType>()
  96. {
  97. return oa;
  98. }
  99. private:
  100. output_adapter_t<CharType> oa = nullptr;
  101. };
  102. } // namespace detail
  103. } // namespace nlohmann