Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Writer.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. 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 class Writer {
38 protected:
39 class _IRep;
40
41 protected:
42 /**
43 * \pre rep != nullptr
44 */
45 explicit Writer (const shared_ptr<_IRep>& rep);
46 Writer () = delete;
47
48 public:
49 /**
50 */
51 nonvirtual optional<filesystem::path> GetDefaultFileSuffix () const;
52
53 public:
54 /**
55 * Serialize (according to the subtype of Writer constructed) the argument VariantValue object to the
56 * argument output 'stream' accumulator, or in the /1 case, just return the result as a BLOB (same as WriteAsBLOB).
57 *
58 * @see WriteAsBLOB
59 * @see WriteAsString
60 *
61 * \note const methods because these don't change the state of the writer, but rather the underlying output stream (argument)
62 */
63 nonvirtual void Write (const VariantValue& v, const Streams::OutputStream::Ptr<byte>& out) const;
64 nonvirtual void Write (const VariantValue& v, const Streams::OutputStream::Ptr<Characters::Character>& out) const;
65 nonvirtual void Write (const VariantValue& v, ostream& out) const;
66 nonvirtual void Write (const VariantValue& v, wostream& out) const;
67
68 public:
69 /**
70 * Take the given variant value, and convert it to JSON, and return that JSON as a BLOB.
71 */
72 [[nodiscard]] nonvirtual Memory::BLOB WriteAsBLOB (const VariantValue& v) const;
73
74 public:
75 /**
76 * Take the given variant value, and Serialize (according to the subtype of Writer constructed),
77 * and return that JSON as a String.
78 */
79 [[nodiscard]] nonvirtual String WriteAsString (const VariantValue& v) const;
80
81 protected:
82 nonvirtual _IRep& _GetRep ();
83 nonvirtual const _IRep& _GetRep () const;
84
85 protected:
86 using _SharedPtrIRep = shared_ptr<_IRep>;
87
88 protected:
89 /**
90 * Helper for subclasses to take various kinds of output targets, and convert them to Streams::OutputStream<byte>::
91 * used by the IRep_.
92 */
95
96 protected:
97 /**
98 * Helper for subclasses to take various kinds of output targets, and convert them to Streams::OutputStream<byte>::
99 * used by the IRep_.
100 */
103
104 protected:
105 /**
106 * Helper for subclasses to take binary-ostream writer and return a BLOB result.
107 */
108 static Memory::BLOB _WriteAsBLOBHelper (const function<void (Streams::OutputStream::Ptr<byte>)>& f);
109
110 protected:
111 /**
112 * Helper for subclasses to take binary-ostream writer and return a String result.
113 */
115
116 private:
117 struct _Rep_Cloner {
118 inline _SharedPtrIRep operator() (const _IRep& t) const;
119 };
121
122 private:
123 SharedRepByValuePtr_ fRep_;
124
125 public:
126 [[deprecated ("Since v3.0d1, use WriteAsBLOB or another Write overload)")]] Memory::BLOB Write (const VariantValue& v)
127 {
128 return WriteAsBLOB (v);
129 }
130 };
131
132 class Writer::_IRep {
133 public:
134 virtual ~_IRep () = default;
135 virtual _SharedPtrIRep Clone () const = 0;
136 virtual optional<filesystem::path> GetDefaultFileSuffix () const = 0;
137 virtual void Write (const VariantValue& v, const Streams::OutputStream::Ptr<byte>& out) const = 0;
138 virtual void Write (const VariantValue& v, const Streams::OutputStream::Ptr<Characters::Character>& out) const = 0;
139 };
140
141}
142
143/*
144 ********************************************************************************
145 ***************************** Implementation Details ***************************
146 ********************************************************************************
147 */
148#include "Writer.inl"
149
150#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,...
Definition Writer.h:37
nonvirtual String WriteAsString(const VariantValue &v) const
Definition Writer.cpp:53
nonvirtual Memory::BLOB WriteAsBLOB(const VariantValue &v) const
Definition Writer.cpp:48
static String _WriteAsStringHelper(const function< void(Streams::OutputStream::Ptr< Characters::Character >)> &f)
Definition Writer.cpp:31
static Memory::BLOB _WriteAsBLOBHelper(const function< void(Streams::OutputStream::Ptr< byte >)> &f)
Definition Writer.cpp:24
nonvirtual void Write(const VariantValue &v, const Streams::OutputStream::Ptr< byte > &out) const
Definition Writer.inl:30
static Streams::OutputStream::Ptr< Characters::Character > _WrapTextOutput(const Streams::OutputStream::Ptr< Characters::Character > &out)
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 ...
SharedByValue is a utility class to implement Copy-On-Write (aka COW) - sort of halfway between uniqu...
OutputStream<>::Ptr is Smart pointer to a stream-based sink of data.