BRE12
emitter.h
1 #ifndef EMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
2 #define EMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
3 
4 #if defined(_MSC_VER) || \
5  (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
6  (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
7 #pragma once
8 #endif
9 
10 #include <cstddef>
11 #include <memory>
12 #include <sstream>
13 #include <string>
14 
15 #include "yaml-cpp/binary.h"
16 #include "yaml-cpp/dll.h"
17 #include "yaml-cpp/emitterdef.h"
18 #include "yaml-cpp/emittermanip.h"
19 #include "yaml-cpp/noncopyable.h"
20 #include "yaml-cpp/null.h"
21 #include "yaml-cpp/ostream_wrapper.h"
22 
23 namespace YAML {
24 class Binary;
25 struct _Null;
26 } // namespace YAML
27 
28 namespace YAML {
29 class EmitterState;
30 
31 class YAML_CPP_API Emitter : private noncopyable {
32  public:
33  Emitter();
34  explicit Emitter(std::ostream& stream);
35  ~Emitter();
36 
37  // output
38  const char* c_str() const;
39  std::size_t size() const;
40 
41  // state checking
42  bool good() const;
43  const std::string GetLastError() const;
44 
45  // global setters
46  bool SetOutputCharset(EMITTER_MANIP value);
47  bool SetStringFormat(EMITTER_MANIP value);
48  bool SetBoolFormat(EMITTER_MANIP value);
49  bool SetIntBase(EMITTER_MANIP value);
50  bool SetSeqFormat(EMITTER_MANIP value);
51  bool SetMapFormat(EMITTER_MANIP value);
52  bool SetIndent(std::size_t n);
53  bool SetPreCommentIndent(std::size_t n);
54  bool SetPostCommentIndent(std::size_t n);
55  bool SetFloatPrecision(std::size_t n);
56  bool SetDoublePrecision(std::size_t n);
57 
58  // local setters
59  Emitter& SetLocalValue(EMITTER_MANIP value);
60  Emitter& SetLocalIndent(const _Indent& indent);
61  Emitter& SetLocalPrecision(const _Precision& precision);
62 
63  // overloads of write
64  Emitter& Write(const std::string& str);
65  Emitter& Write(bool b);
66  Emitter& Write(char ch);
67  Emitter& Write(const _Alias& alias);
68  Emitter& Write(const _Anchor& anchor);
69  Emitter& Write(const _Tag& tag);
70  Emitter& Write(const _Comment& comment);
71  Emitter& Write(const _Null& n);
72  Emitter& Write(const Binary& binary);
73 
74  template <typename T>
75  Emitter& WriteIntegralType(T value);
76 
77  template <typename T>
78  Emitter& WriteStreamable(T value);
79 
80  private:
81  template <typename T>
82  void SetStreamablePrecision(std::stringstream&) {}
83  std::size_t GetFloatPrecision() const;
84  std::size_t GetDoublePrecision() const;
85 
86  void PrepareIntegralStream(std::stringstream& stream) const;
87  void StartedScalar();
88 
89  private:
90  void EmitBeginDoc();
91  void EmitEndDoc();
92  void EmitBeginSeq();
93  void EmitEndSeq();
94  void EmitBeginMap();
95  void EmitEndMap();
96  void EmitNewline();
97  void EmitKindTag();
98  void EmitTag(bool verbatim, const _Tag& tag);
99 
100  void PrepareNode(EmitterNodeType::value child);
101  void PrepareTopNode(EmitterNodeType::value child);
102  void FlowSeqPrepareNode(EmitterNodeType::value child);
103  void BlockSeqPrepareNode(EmitterNodeType::value child);
104 
105  void FlowMapPrepareNode(EmitterNodeType::value child);
106 
107  void FlowMapPrepareLongKey(EmitterNodeType::value child);
108  void FlowMapPrepareLongKeyValue(EmitterNodeType::value child);
109  void FlowMapPrepareSimpleKey(EmitterNodeType::value child);
110  void FlowMapPrepareSimpleKeyValue(EmitterNodeType::value child);
111 
112  void BlockMapPrepareNode(EmitterNodeType::value child);
113 
114  void BlockMapPrepareLongKey(EmitterNodeType::value child);
115  void BlockMapPrepareLongKeyValue(EmitterNodeType::value child);
116  void BlockMapPrepareSimpleKey(EmitterNodeType::value child);
117  void BlockMapPrepareSimpleKeyValue(EmitterNodeType::value child);
118 
119  void SpaceOrIndentTo(bool requireSpace, std::size_t indent);
120 
121  const char* ComputeFullBoolName(bool b) const;
122  bool CanEmitNewline() const;
123 
124  private:
125  std::unique_ptr<EmitterState> m_pState;
126  ostream_wrapper m_stream;
127 };
128 
129 template <typename T>
130 inline Emitter& Emitter::WriteIntegralType(T value) {
131  if (!good())
132  return *this;
133 
134  PrepareNode(EmitterNodeType::Scalar);
135 
136  std::stringstream stream;
137  PrepareIntegralStream(stream);
138  stream << value;
139  m_stream << stream.str();
140 
141  StartedScalar();
142 
143  return *this;
144 }
145 
146 template <typename T>
147 inline Emitter& Emitter::WriteStreamable(T value) {
148  if (!good())
149  return *this;
150 
151  PrepareNode(EmitterNodeType::Scalar);
152 
153  std::stringstream stream;
154  SetStreamablePrecision<T>(stream);
155  stream << value;
156  m_stream << stream.str();
157 
158  StartedScalar();
159 
160  return *this;
161 }
162 
163 template <>
164 inline void Emitter::SetStreamablePrecision<float>(std::stringstream& stream) {
165  stream.precision(static_cast<std::streamsize>(GetFloatPrecision()));
166 }
167 
168 template <>
169 inline void Emitter::SetStreamablePrecision<double>(std::stringstream& stream) {
170  stream.precision(static_cast<std::streamsize>(GetDoublePrecision()));
171 }
172 
173 // overloads of insertion
174 inline Emitter& operator<<(Emitter& emitter, const std::string& v) {
175  return emitter.Write(v);
176 }
177 inline Emitter& operator<<(Emitter& emitter, bool v) {
178  return emitter.Write(v);
179 }
180 inline Emitter& operator<<(Emitter& emitter, char v) {
181  return emitter.Write(v);
182 }
183 inline Emitter& operator<<(Emitter& emitter, unsigned char v) {
184  return emitter.Write(static_cast<char>(v));
185 }
186 inline Emitter& operator<<(Emitter& emitter, const _Alias& v) {
187  return emitter.Write(v);
188 }
189 inline Emitter& operator<<(Emitter& emitter, const _Anchor& v) {
190  return emitter.Write(v);
191 }
192 inline Emitter& operator<<(Emitter& emitter, const _Tag& v) {
193  return emitter.Write(v);
194 }
195 inline Emitter& operator<<(Emitter& emitter, const _Comment& v) {
196  return emitter.Write(v);
197 }
198 inline Emitter& operator<<(Emitter& emitter, const _Null& v) {
199  return emitter.Write(v);
200 }
201 inline Emitter& operator<<(Emitter& emitter, const Binary& b) {
202  return emitter.Write(b);
203 }
204 
205 inline Emitter& operator<<(Emitter& emitter, const char* v) {
206  return emitter.Write(std::string(v));
207 }
208 
209 inline Emitter& operator<<(Emitter& emitter, int v) {
210  return emitter.WriteIntegralType(v);
211 }
212 inline Emitter& operator<<(Emitter& emitter, unsigned int v) {
213  return emitter.WriteIntegralType(v);
214 }
215 inline Emitter& operator<<(Emitter& emitter, short v) {
216  return emitter.WriteIntegralType(v);
217 }
218 inline Emitter& operator<<(Emitter& emitter, unsigned short v) {
219  return emitter.WriteIntegralType(v);
220 }
221 inline Emitter& operator<<(Emitter& emitter, long v) {
222  return emitter.WriteIntegralType(v);
223 }
224 inline Emitter& operator<<(Emitter& emitter, unsigned long v) {
225  return emitter.WriteIntegralType(v);
226 }
227 inline Emitter& operator<<(Emitter& emitter, long long v) {
228  return emitter.WriteIntegralType(v);
229 }
230 inline Emitter& operator<<(Emitter& emitter, unsigned long long v) {
231  return emitter.WriteIntegralType(v);
232 }
233 
234 inline Emitter& operator<<(Emitter& emitter, float v) {
235  return emitter.WriteStreamable(v);
236 }
237 inline Emitter& operator<<(Emitter& emitter, double v) {
238  return emitter.WriteStreamable(v);
239 }
240 
241 inline Emitter& operator<<(Emitter& emitter, EMITTER_MANIP value) {
242  return emitter.SetLocalValue(value);
243 }
244 
245 inline Emitter& operator<<(Emitter& emitter, _Indent indent) {
246  return emitter.SetLocalIndent(indent);
247 }
248 
249 inline Emitter& operator<<(Emitter& emitter, _Precision precision) {
250  return emitter.SetLocalPrecision(precision);
251 }
252 }
253 
254 #endif // EMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
Definition: emittermanip.h:122
Definition: binary.h:20
Definition: emitter.h:31
Definition: emittermanip.h:115
Definition: emittermanip.h:86
Definition: emittermanip.h:72
Definition: null.h:16
Definition: emittermanip.h:65
Definition: noncopyable.h:14
Definition: DrawableObjectLoader.h:10
Definition: emittermanip.h:79
Definition: ostream_wrapper.h:16