Plasma Engine  2.0
Loading...
Searching...
No Matches
OpenDdlWriter.h
1#pragma once
2
3#include <Foundation/Basics.h>
4#include <Foundation/IO/OpenDdlParser.h>
5
6// TODO
7// Write primitives in HEX (esp. float)
8
12class PL_FOUNDATION_DLL plOpenDdlWriter
13{
14public:
15 enum class TypeStringMode
16 {
17 Compliant,
18 ShortenedUnsignedInt,
19 Shortest
21 };
22
24 {
25 Readable,
26 Exact,
27 };
28
31
32 virtual ~plOpenDdlWriter() = default;
33
35 void SetOutputStream(plStreamWriter* pOutput) { m_pOutput = pOutput; } // [tested]
36
38 void SetCompactMode(bool bCompact) { m_bCompactMode = bCompact; } // [tested]
39
41 void SetPrimitiveTypeStringMode(TypeStringMode mode) { m_TypeStringMode = mode; }
42
44 void SetFloatPrecisionMode(FloatPrecisionMode mode) { m_FloatPrecisionMode = mode; }
45
47 FloatPrecisionMode GetFloatPrecisionMode() const { return m_FloatPrecisionMode; }
48
51 void SetIndentation(plInt8 iIndentation) { m_iIndentation = iIndentation; }
52
54 void BeginObject(plStringView sType, plStringView sName = {}, bool bGlobalName = false, bool bSingleLine = false); // [tested]
55
57 void EndObject(); // [tested]
58
60 void BeginPrimitiveList(plOpenDdlPrimitiveType type, plStringView sName = {}, bool bGlobalName = false); // [tested]
61
63 void EndPrimitiveList(); // [tested]
64
66 void WriteBool(const bool* pValues, plUInt32 uiCount = 1); // [tested]
67
69 void WriteInt8(const plInt8* pValues, plUInt32 uiCount = 1); // [tested]
70
72 void WriteInt16(const plInt16* pValues, plUInt32 uiCount = 1); // [tested]
73
75 void WriteInt32(const plInt32* pValues, plUInt32 uiCount = 1); // [tested]
76
78 void WriteInt64(const plInt64* pValues, plUInt32 uiCount = 1); // [tested]
79
81 void WriteUInt8(const plUInt8* pValues, plUInt32 uiCount = 1); // [tested]
82
84 void WriteUInt16(const plUInt16* pValues, plUInt32 uiCount = 1); // [tested]
85
87 void WriteUInt32(const plUInt32* pValues, plUInt32 uiCount = 1); // [tested]
88
90 void WriteUInt64(const plUInt64* pValues, plUInt32 uiCount = 1); // [tested]
91
93 void WriteFloat(const float* pValues, plUInt32 uiCount = 1); // [tested]
94
96 void WriteDouble(const double* pValues, plUInt32 uiCount = 1); // [tested]
97
99 void WriteString(const plStringView& sString); // [tested]
100
102 void WriteBinaryAsString(const void* pData, plUInt32 uiBytes);
103
104
105protected:
106 enum State
107 {
108 Invalid = -5,
109 Empty = -4,
110 ObjectSingleLine = -3,
111 ObjectMultiLine = -2,
112 ObjectStart = -1,
113 PrimitivesBool = 0, // same values as in plOpenDdlPrimitiveType to enable casting
114 PrimitivesInt8,
115 PrimitivesInt16,
116 PrimitivesInt32,
117 PrimitivesInt64,
118 PrimitivesUInt8,
119 PrimitivesUInt16,
120 PrimitivesUInt32,
121 PrimitivesUInt64,
122 PrimitivesFloat,
123 PrimitivesDouble,
124 PrimitivesString,
125 };
126
127 struct DdlState
128 {
129 State m_State = State::Empty;
130 bool m_bPrimitivesWritten = false;
131 };
132
133 PL_ALWAYS_INLINE void OutputString(plStringView s) { m_pOutput->WriteBytes(s.GetStartPointer(), s.GetElementCount()).IgnoreResult(); }
134 PL_ALWAYS_INLINE void OutputString(plStringView s, plUInt32 uiElementCount) { m_pOutput->WriteBytes(s.GetStartPointer(), uiElementCount).IgnoreResult(); }
135 void OutputEscapedString(const plStringView& string);
136 void OutputIndentation();
137 void OutputPrimitiveTypeNameCompliant(plOpenDdlPrimitiveType type);
138 void OutputPrimitiveTypeNameShort(plOpenDdlPrimitiveType type);
139 void OutputPrimitiveTypeNameShortest(plOpenDdlPrimitiveType type);
140 void WritePrimitiveType(plOpenDdlWriter::State exp);
141 void OutputObjectName(plStringView sName, bool bGlobalName);
142 void WriteBinaryAsHex(const void* pData, plUInt32 uiBytes);
143 void OutputObjectBeginning();
144
145 plInt32 m_iIndentation = 0;
146 bool m_bCompactMode = false;
147 TypeStringMode m_TypeStringMode = TypeStringMode::ShortenedUnsignedInt;
148 FloatPrecisionMode m_FloatPrecisionMode = FloatPrecisionMode::Exact;
149 plStreamWriter* m_pOutput = nullptr;
150 plStringBuilder m_sTemp;
151
152 plHybridArray<DdlState, 16> m_StateStack;
153};
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
The base class for OpenDDL writers.
Definition OpenDdlWriter.h:13
void SetPrimitiveTypeStringMode(TypeStringMode mode)
Configures how verbose the type strings are going to be written.
Definition OpenDdlWriter.h:41
void SetFloatPrecisionMode(FloatPrecisionMode mode)
Configures how float values are output.
Definition OpenDdlWriter.h:44
FloatPrecisionMode GetFloatPrecisionMode() const
Returns how float values are output.
Definition OpenDdlWriter.h:47
TypeStringMode
Definition OpenDdlWriter.h:16
void SetCompactMode(bool bCompact)
Configures how much whitespace is output.
Definition OpenDdlWriter.h:38
void SetIndentation(plInt8 iIndentation)
Allows to set the indentation. Negative values are possible. This makes it possible to set the indent...
Definition OpenDdlWriter.h:51
void SetOutputStream(plStreamWriter *pOutput)
All output is written to this binary stream.
Definition OpenDdlWriter.h:35
FloatPrecisionMode
Definition OpenDdlWriter.h:24
Interface for binary out (write) streams.
Definition Stream.h:107
plStringBuilder is a class that is meant for creating and modifying strings.
Definition StringBuilder.h:35
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
plUInt32 GetElementCount() const
Returns the number of bytes from the start position up to its end.
Definition StringView.h:93
const char * GetStartPointer() const
Returns the start of the view range.
Definition StringView.h:102
Definition OpenDdlWriter.h:128