Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/catch2/internal/catch_jsonwriter.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 
0002 //              Copyright Catch2 Authors
0003 // Distributed under the Boost Software License, Version 1.0.
0004 //   (See accompanying file LICENSE.txt or copy at
0005 //        https://www.boost.org/LICENSE_1_0.txt)
0006 
0007 // SPDX-License-Identifier: BSL-1.0
0008 #ifndef CATCH_JSONWRITER_HPP_INCLUDED
0009 #define CATCH_JSONWRITER_HPP_INCLUDED
0010 
0011 #include <catch2/internal/catch_reusable_string_stream.hpp>
0012 #include <catch2/internal/catch_stringref.hpp>
0013 
0014 #include <cstdint>
0015 #include <sstream>
0016 
0017 namespace Catch {
0018     class JsonObjectWriter;
0019     class JsonArrayWriter;
0020 
0021     struct JsonUtils {
0022         static void indent( std::ostream& os, std::uint64_t level );
0023         static void appendCommaNewline( std::ostream& os,
0024                                         bool& should_comma,
0025                                         std::uint64_t level );
0026     };
0027 
0028     class JsonValueWriter {
0029     public:
0030         JsonValueWriter( std::ostream& os );
0031         JsonValueWriter( std::ostream& os, std::uint64_t indent_level );
0032 
0033         JsonObjectWriter writeObject() &&;
0034         JsonArrayWriter writeArray() &&;
0035 
0036         template <typename T>
0037         void write( T const& value ) && {
0038             writeImpl( value, !std::is_arithmetic<T>::value );
0039         }
0040         void write( StringRef value ) &&;
0041         void write( bool value ) &&;
0042 
0043     private:
0044         void writeImpl( StringRef value, bool quote );
0045 
0046         // Without this SFINAE, this overload is a better match
0047         // for `std::string`, `char const*`, `char const[N]` args.
0048         // While it would still work, it would cause code bloat
0049         // and multiple iteration over the strings
0050         template <typename T,
0051                   typename = typename std::enable_if_t<
0052                       !std::is_convertible<T, StringRef>::value>>
0053         void writeImpl( T const& value, bool quote_value ) {
0054             m_sstream << value;
0055             writeImpl( m_sstream.str(), quote_value );
0056         }
0057 
0058         std::ostream& m_os;
0059         std::stringstream m_sstream;
0060         std::uint64_t m_indent_level;
0061     };
0062 
0063     class JsonObjectWriter {
0064     public:
0065         JsonObjectWriter( std::ostream& os );
0066         JsonObjectWriter( std::ostream& os, std::uint64_t indent_level );
0067 
0068         JsonObjectWriter( JsonObjectWriter&& source ) noexcept;
0069         JsonObjectWriter& operator=( JsonObjectWriter&& source ) = delete;
0070 
0071         ~JsonObjectWriter();
0072 
0073         JsonValueWriter write( StringRef key );
0074 
0075     private:
0076         std::ostream& m_os;
0077         std::uint64_t m_indent_level;
0078         bool m_should_comma = false;
0079         bool m_active = true;
0080     };
0081 
0082     class JsonArrayWriter {
0083     public:
0084         JsonArrayWriter( std::ostream& os );
0085         JsonArrayWriter( std::ostream& os, std::uint64_t indent_level );
0086 
0087         JsonArrayWriter( JsonArrayWriter&& source ) noexcept;
0088         JsonArrayWriter& operator=( JsonArrayWriter&& source ) = delete;
0089 
0090         ~JsonArrayWriter();
0091 
0092         JsonObjectWriter writeObject();
0093         JsonArrayWriter writeArray();
0094 
0095         template <typename T>
0096         JsonArrayWriter& write( T const& value ) {
0097             return writeImpl( value );
0098         }
0099 
0100         JsonArrayWriter& write( bool value );
0101 
0102     private:
0103         template <typename T>
0104         JsonArrayWriter& writeImpl( T const& value ) {
0105             JsonUtils::appendCommaNewline(
0106                 m_os, m_should_comma, m_indent_level + 1 );
0107             JsonValueWriter{ m_os }.write( value );
0108 
0109             return *this;
0110         }
0111 
0112         std::ostream& m_os;
0113         std::uint64_t m_indent_level;
0114         bool m_should_comma = false;
0115         bool m_active = true;
0116     };
0117 
0118 } // namespace Catch
0119 
0120 #endif // CATCH_JSONWRITER_HPP_INCLUDED