Stroika Library 3.0d25
 
Loading...
Searching...
No Matches
Variant/Writer.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _Stroika_Foundation_DataExchange_Variant_Writer_h_
5#define _Stroika_Foundation_DataExchange_Variant_Writer_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <ostream>
10
13
14/**
15 * \file
16 *
17 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
18 *
19 * \em Design Note:
20 * One question was whether or not to natively include support for istream sources or not.
21 * Its easy todo if not supported, by just using BinaryInputStreamFromIStreamAdapter. However,
22 * I decided it would be best to directly support it so typical users (who may not want to
23 * lookup those mapper classes) will just get the right results automagically.
24 *
25 * Also note - since there are no virtual functions involved in the call, the linker/optimizer
26 * can eliminate the code if this feature isn't used.
27 *
28 * This comports with a similar choice made in the String and Container classes (direct builtin
29 * first-class support for native STL objects where appropriate).
30 */
31
32namespace Stroika::Foundation::DataExchange::Variant {
33
34 /**
35 * \brief abstract class specifying interface for writers VariantValue objects to serialized formats like JSON, CSV, XML, etc
36 *
37 * **A Writer is configured when it is constructed, and immutable after**: what the subclass's constructor was given (its
38 * Options, say) is how it writes, and every method is const. To write differently, construct another Writer - that is
39 * cheap. Copies share one representation, never modified, so a Writer is cheap to copy - to store, to pass by value, to
40 * hand out from a factory.
41 *
42 * \note \em Thread-Safety <a href="Thread-Safety.md#C++-Standard-Thread-Safety">C++-Standard-Thread-Safety</a>
43 */
44 class Writer {
45 protected:
46 class _IRep;
47
48 protected:
49 /**
50 * \pre rep != nullptr
51 */
52 explicit Writer (const shared_ptr<const _IRep>& rep);
53 Writer () = delete;
54
55 public:
56 /**
57 */
58 nonvirtual optional<filesystem::path> GetDefaultFileSuffix () const;
59
60 public:
61 /**
62 * Serialize (according to the subtype of Writer constructed) the argument VariantValue object to the
63 * argument output 'stream' accumulator, or in the /1 case, just return the result as a BLOB (same as WriteAsBLOB).
64 *
65 * @see WriteAsBLOB
66 * @see WriteAsString
67 *
68 * \note const methods because these don't change the state of the writer, but rather the underlying output stream (argument)
69 */
70 nonvirtual void Write (const VariantValue& v, const Streams::OutputStream::Ptr<byte>& out) const;
71 nonvirtual void Write (const VariantValue& v, const Streams::OutputStream::Ptr<Characters::Character>& out) const;
72 nonvirtual void Write (const VariantValue& v, ostream& out) const;
73 nonvirtual void Write (const VariantValue& v, wostream& out) const;
74
75 public:
76 /**
77 * Take the given variant value, and convert it to JSON, and return that JSON as a BLOB.
78 */
79 [[nodiscard]] nonvirtual Memory::BLOB WriteAsBLOB (const VariantValue& v) const;
80
81 public:
82 /**
83 * Take the given variant value, and Serialize (according to the subtype of Writer constructed),
84 * and return that JSON as a String.
85 */
86 [[nodiscard]] nonvirtual String WriteAsString (const VariantValue& v) const;
87
88 protected:
89 nonvirtual const _IRep& _ConstGetRep () const;
90
91 protected:
92 /**
93 * Helper for subclasses to take various kinds of output targets, and convert them to Streams::OutputStream<byte>::
94 * used by the IRep_.
95 */
98
99 protected:
100 /**
101 * Helper for subclasses to take various kinds of output targets, and convert them to Streams::OutputStream<byte>::
102 * used by the IRep_.
103 */
106
107 protected:
108 /**
109 * Helper for subclasses to take binary-ostream writer and return a BLOB result.
110 */
111 static Memory::BLOB _WriteAsBLOBHelper (const function<void (Streams::OutputStream::Ptr<byte>)>& f);
112
113 protected:
114 /**
115 * Helper for subclasses to take binary-ostream writer and return a String result.
116 */
118
119 private:
120 shared_ptr<const _IRep> fRep_;
121
122 public:
123 [[deprecated ("Since v3.0d1, use WriteAsBLOB or another Write overload)")]] Memory::BLOB Write (const VariantValue& v)
124 {
125 return WriteAsBLOB (v);
126 }
127 };
128
129 class Writer::_IRep {
130 public:
131 virtual ~_IRep () = default;
132 virtual optional<filesystem::path> GetDefaultFileSuffix () const = 0;
133 virtual void Write (const VariantValue& v, const Streams::OutputStream::Ptr<byte>& out) const = 0;
134 virtual void Write (const VariantValue& v, const Streams::OutputStream::Ptr<Characters::Character>& out) const = 0;
135 };
136
137}
138
139/*
140 ********************************************************************************
141 ***************************** Implementation Details ***************************
142 ********************************************************************************
143 */
144#include "Writer.inl"
145
146#endif /*_Stroika_Foundation_DataExchange_Variant_Writer_h_*/
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
abstract class specifying interface for writers VariantValue objects to serialized formats like JSON,...
nonvirtual String WriteAsString(const VariantValue &v) const
nonvirtual Memory::BLOB WriteAsBLOB(const VariantValue &v) const
static String _WriteAsStringHelper(const function< void(Streams::OutputStream::Ptr< Characters::Character >)> &f)
static Streams::OutputStream::Ptr< Characters::Character > _WrapTextOutput(const Streams::OutputStream::Ptr< Characters::Character > &out)
static Memory::BLOB _WriteAsBLOBHelper(const function< void(Streams::OutputStream::Ptr< byte >)> &f)
nonvirtual void Write(const VariantValue &v, const Streams::OutputStream::Ptr< byte > &out) const
static Streams::OutputStream::Ptr< byte > _WrapBinaryOutput(const Streams::OutputStream::Ptr< byte > &out)
Simple variant-value (case variant union) object, with (variant) basic types analogous to a value in ...
OutputStream<>::Ptr is Smart pointer to a stream-based sink of data.