Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:02:20

0001 //     __ _____ _____ _____
0002 //  __|  |   __|     |   | |  JSON for Modern C++
0003 // |  |  |__   |  |  | | | |  version 3.11.2
0004 // |_____|_____|_____|_|___|  https://github.com/nlohmann/json
0005 //
0006 // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
0007 // SPDX-License-Identifier: MIT
0008 
0009 #pragma once
0010 
0011 #include <algorithm> // copy
0012 #include <cstddef> // size_t
0013 #include <iterator> // back_inserter
0014 #include <memory> // shared_ptr, make_shared
0015 #include <string> // basic_string
0016 #include <vector> // vector
0017 
0018 #ifndef JSON_NO_IO
0019     #include <ios>      // streamsize
0020     #include <ostream>  // basic_ostream
0021 #endif  // JSON_NO_IO
0022 
0023 #include <nlohmann/detail/macro_scope.hpp>
0024 
0025 NLOHMANN_JSON_NAMESPACE_BEGIN
0026 namespace detail
0027 {
0028 
0029 /// abstract output adapter interface
0030 template<typename CharType> struct output_adapter_protocol
0031 {
0032     virtual void write_character(CharType c) = 0;
0033     virtual void write_characters(const CharType* s, std::size_t length) = 0;
0034     virtual ~output_adapter_protocol() = default;
0035 
0036     output_adapter_protocol() = default;
0037     output_adapter_protocol(const output_adapter_protocol&) = default;
0038     output_adapter_protocol(output_adapter_protocol&&) noexcept = default;
0039     output_adapter_protocol& operator=(const output_adapter_protocol&) = default;
0040     output_adapter_protocol& operator=(output_adapter_protocol&&) noexcept = default;
0041 };
0042 
0043 /// a type to simplify interfaces
0044 template<typename CharType>
0045 using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
0046 
0047 /// output adapter for byte vectors
0048 template<typename CharType, typename AllocatorType = std::allocator<CharType>>
0049 class output_vector_adapter : public output_adapter_protocol<CharType>
0050 {
0051   public:
0052     explicit output_vector_adapter(std::vector<CharType, AllocatorType>& vec) noexcept
0053         : v(vec)
0054     {}
0055 
0056     void write_character(CharType c) override
0057     {
0058         v.push_back(c);
0059     }
0060 
0061     JSON_HEDLEY_NON_NULL(2)
0062     void write_characters(const CharType* s, std::size_t length) override
0063     {
0064         v.insert(v.end(), s, s + length);
0065     }
0066 
0067   private:
0068     std::vector<CharType, AllocatorType>& v;
0069 };
0070 
0071 #ifndef JSON_NO_IO
0072 /// output adapter for output streams
0073 template<typename CharType>
0074 class output_stream_adapter : public output_adapter_protocol<CharType>
0075 {
0076   public:
0077     explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
0078         : stream(s)
0079     {}
0080 
0081     void write_character(CharType c) override
0082     {
0083         stream.put(c);
0084     }
0085 
0086     JSON_HEDLEY_NON_NULL(2)
0087     void write_characters(const CharType* s, std::size_t length) override
0088     {
0089         stream.write(s, static_cast<std::streamsize>(length));
0090     }
0091 
0092   private:
0093     std::basic_ostream<CharType>& stream;
0094 };
0095 #endif  // JSON_NO_IO
0096 
0097 /// output adapter for basic_string
0098 template<typename CharType, typename StringType = std::basic_string<CharType>>
0099 class output_string_adapter : public output_adapter_protocol<CharType>
0100 {
0101   public:
0102     explicit output_string_adapter(StringType& s) noexcept
0103         : str(s)
0104     {}
0105 
0106     void write_character(CharType c) override
0107     {
0108         str.push_back(c);
0109     }
0110 
0111     JSON_HEDLEY_NON_NULL(2)
0112     void write_characters(const CharType* s, std::size_t length) override
0113     {
0114         str.append(s, length);
0115     }
0116 
0117   private:
0118     StringType& str;
0119 };
0120 
0121 template<typename CharType, typename StringType = std::basic_string<CharType>>
0122 class output_adapter
0123 {
0124   public:
0125     template<typename AllocatorType = std::allocator<CharType>>
0126     output_adapter(std::vector<CharType, AllocatorType>& vec)
0127         : oa(std::make_shared<output_vector_adapter<CharType, AllocatorType>>(vec)) {}
0128 
0129 #ifndef JSON_NO_IO
0130     output_adapter(std::basic_ostream<CharType>& s)
0131         : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
0132 #endif  // JSON_NO_IO
0133 
0134     output_adapter(StringType& s)
0135         : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
0136 
0137     operator output_adapter_t<CharType>()
0138     {
0139         return oa;
0140     }
0141 
0142   private:
0143     output_adapter_t<CharType> oa = nullptr;
0144 };
0145 
0146 }  // namespace detail
0147 NLOHMANN_JSON_NAMESPACE_END