Coverage Report

Created: 2025-12-05 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/yaml-cpp/include/yaml-cpp/emitter.h
Line
Count
Source
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 <cmath>
11
#include <cstddef>
12
#include <cstring>
13
#include <limits>
14
#include <memory>
15
#include <sstream>
16
#include <string>
17
#include <type_traits>
18
19
#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L)
20
#include <string_view>
21
#endif
22
23
#include "yaml-cpp/binary.h"
24
#include "yaml-cpp/dll.h"
25
#include "yaml-cpp/emitterdef.h"
26
#include "yaml-cpp/emittermanip.h"
27
#include "yaml-cpp/null.h"
28
#include "yaml-cpp/ostream_wrapper.h"
29
#include "yaml-cpp/fptostring.h"
30
31
namespace YAML {
32
class Binary;
33
struct _Null;
34
}  // namespace YAML
35
36
namespace YAML {
37
class EmitterState;
38
39
class YAML_CPP_API Emitter {
40
 public:
41
  Emitter();
42
  explicit Emitter(std::ostream& stream);
43
  Emitter(const Emitter&) = delete;
44
  Emitter& operator=(const Emitter&) = delete;
45
  ~Emitter();
46
47
  // output
48
  const char* c_str() const;
49
  std::size_t size() const;
50
51
  // state checking
52
  bool good() const;
53
  const std::string GetLastError() const;
54
55
  // global setters
56
  bool SetOutputCharset(EMITTER_MANIP value);
57
  bool SetStringFormat(EMITTER_MANIP value);
58
  bool SetBoolFormat(EMITTER_MANIP value);
59
  bool SetNullFormat(EMITTER_MANIP value);
60
  bool SetIntBase(EMITTER_MANIP value);
61
  bool SetSeqFormat(EMITTER_MANIP value);
62
  bool SetMapFormat(EMITTER_MANIP value);
63
  bool SetIndent(std::size_t n);
64
  bool SetPreCommentIndent(std::size_t n);
65
  bool SetPostCommentIndent(std::size_t n);
66
  bool SetFloatPrecision(std::size_t n);
67
  bool SetDoublePrecision(std::size_t n);
68
  void RestoreGlobalModifiedSettings();
69
70
  // local setters
71
  Emitter& SetLocalValue(EMITTER_MANIP value);
72
  Emitter& SetLocalIndent(const _Indent& indent);
73
  Emitter& SetLocalPrecision(const _Precision& precision);
74
75
  // overloads of write
76
  Emitter& Write(const char* str, std::size_t size);
77
  Emitter& Write(const std::string& str);
78
  Emitter& Write(bool b);
79
  Emitter& Write(char ch);
80
  Emitter& Write(const _Alias& alias);
81
  Emitter& Write(const _Anchor& anchor);
82
  Emitter& Write(const _Tag& tag);
83
  Emitter& Write(const _Comment& comment);
84
  Emitter& Write(const _Null& n);
85
  Emitter& Write(const Binary& binary);
86
87
  template <typename T>
88
  Emitter& WriteIntegralType(T value);
89
90
  template <typename T>
91
  Emitter& WriteStreamable(T value);
92
93
 private:
94
  template <typename T>
95
  void SetStreamablePrecision(std::stringstream&) {}
96
  std::size_t GetFloatPrecision() const;
97
  std::size_t GetDoublePrecision() const;
98
99
  void PrepareIntegralStream(std::stringstream& stream) const;
100
  void StartedScalar();
101
102
 private:
103
  void EmitBeginDoc();
104
  void EmitEndDoc();
105
  void EmitBeginSeq();
106
  void EmitEndSeq();
107
  void EmitBeginMap();
108
  void EmitEndMap();
109
  void EmitNewline();
110
  void EmitKindTag();
111
  void EmitTag(bool verbatim, const _Tag& tag);
112
113
  void PrepareNode(EmitterNodeType::value child);
114
  void PrepareTopNode(EmitterNodeType::value child);
115
  void FlowSeqPrepareNode(EmitterNodeType::value child);
116
  void BlockSeqPrepareNode(EmitterNodeType::value child);
117
118
  void FlowMapPrepareNode(EmitterNodeType::value child);
119
120
  void FlowMapPrepareLongKey(EmitterNodeType::value child);
121
  void FlowMapPrepareLongKeyValue(EmitterNodeType::value child);
122
  void FlowMapPrepareSimpleKey(EmitterNodeType::value child);
123
  void FlowMapPrepareSimpleKeyValue(EmitterNodeType::value child);
124
125
  void BlockMapPrepareNode(EmitterNodeType::value child);
126
127
  void BlockMapPrepareLongKey(EmitterNodeType::value child);
128
  void BlockMapPrepareLongKeyValue(EmitterNodeType::value child);
129
  void BlockMapPrepareSimpleKey(EmitterNodeType::value child);
130
  void BlockMapPrepareSimpleKeyValue(EmitterNodeType::value child);
131
132
  void SpaceOrIndentTo(bool requireSpace, std::size_t indent);
133
134
  const char* ComputeFullBoolName(bool b) const;
135
  const char* ComputeNullName() const;
136
  bool CanEmitNewline() const;
137
138
 private:
139
  std::unique_ptr<EmitterState> m_pState;
140
  ostream_wrapper m_stream;
141
};
142
143
template <typename T>
144
0
inline Emitter& Emitter::WriteIntegralType(T value) {
145
0
  if (!good())
146
0
    return *this;
147
0
148
0
  PrepareNode(EmitterNodeType::Scalar);
149
0
150
0
  std::stringstream stream;
151
0
  stream.imbue(std::locale::classic());
152
0
  PrepareIntegralStream(stream);
153
0
  stream << value;
154
0
  m_stream << stream.str();
155
0
156
0
  StartedScalar();
157
0
158
0
  return *this;
159
0
}
Unexecuted instantiation: YAML::Emitter& YAML::Emitter::WriteIntegralType<int>(int)
Unexecuted instantiation: YAML::Emitter& YAML::Emitter::WriteIntegralType<unsigned int>(unsigned int)
Unexecuted instantiation: YAML::Emitter& YAML::Emitter::WriteIntegralType<short>(short)
Unexecuted instantiation: YAML::Emitter& YAML::Emitter::WriteIntegralType<unsigned short>(unsigned short)
Unexecuted instantiation: YAML::Emitter& YAML::Emitter::WriteIntegralType<long>(long)
Unexecuted instantiation: YAML::Emitter& YAML::Emitter::WriteIntegralType<unsigned long>(unsigned long)
Unexecuted instantiation: YAML::Emitter& YAML::Emitter::WriteIntegralType<long long>(long long)
Unexecuted instantiation: YAML::Emitter& YAML::Emitter::WriteIntegralType<unsigned long long>(unsigned long long)
160
161
template <typename T>
162
0
inline Emitter& Emitter::WriteStreamable(T value) {
163
0
  if (!good())
164
0
    return *this;
165
0
166
0
  PrepareNode(EmitterNodeType::Scalar);
167
0
168
0
  std::stringstream stream;
169
0
  stream.imbue(std::locale::classic());
170
0
  SetStreamablePrecision<T>(stream);
171
0
172
0
  bool special = false;
173
0
  if (std::is_floating_point<T>::value) {
174
0
    if ((std::numeric_limits<T>::has_quiet_NaN ||
175
0
         std::numeric_limits<T>::has_signaling_NaN) &&
176
0
        std::isnan(value)) {
177
0
      special = true;
178
0
      stream << ".nan";
179
0
    } else if (std::numeric_limits<T>::has_infinity && std::isinf(value)) {
180
0
      special = true;
181
0
      if (std::signbit(value)) {
182
0
        stream << "-.inf";
183
0
      } else {
184
0
        stream << ".inf";
185
0
      }
186
0
    }
187
0
  }
188
0
189
0
  if (!special) {
190
0
    stream << FpToString(value, stream.precision());
191
0
  }
192
0
  m_stream << stream.str();
193
0
194
0
  StartedScalar();
195
0
196
0
  return *this;
197
0
}
Unexecuted instantiation: YAML::Emitter& YAML::Emitter::WriteStreamable<float>(float)
Unexecuted instantiation: YAML::Emitter& YAML::Emitter::WriteStreamable<double>(double)
198
199
template <>
200
0
inline void Emitter::SetStreamablePrecision<float>(std::stringstream& stream) {
201
0
  stream.precision(static_cast<std::streamsize>(GetFloatPrecision()));
202
0
}
203
204
template <>
205
0
inline void Emitter::SetStreamablePrecision<double>(std::stringstream& stream) {
206
0
  stream.precision(static_cast<std::streamsize>(GetDoublePrecision()));
207
0
}
208
209
// overloads of insertion
210
#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L)
211
0
inline Emitter& operator<<(Emitter& emitter, const std::string_view& v) {
212
0
  return emitter.Write(v.data(), v.size());
213
0
}
214
#endif
215
0
inline Emitter& operator<<(Emitter& emitter, const std::string& v) {
216
0
  return emitter.Write(v.data(), v.size());
217
0
}
218
0
inline Emitter& operator<<(Emitter& emitter, bool v) {
219
0
  return emitter.Write(v);
220
0
}
221
0
inline Emitter& operator<<(Emitter& emitter, char v) {
222
0
  return emitter.Write(v);
223
0
}
224
0
inline Emitter& operator<<(Emitter& emitter, unsigned char v) {
225
0
  return emitter.Write(static_cast<char>(v));
226
0
}
227
0
inline Emitter& operator<<(Emitter& emitter, const _Alias& v) {
228
0
  return emitter.Write(v);
229
0
}
230
0
inline Emitter& operator<<(Emitter& emitter, const _Anchor& v) {
231
0
  return emitter.Write(v);
232
0
}
233
0
inline Emitter& operator<<(Emitter& emitter, const _Tag& v) {
234
0
  return emitter.Write(v);
235
0
}
236
0
inline Emitter& operator<<(Emitter& emitter, const _Comment& v) {
237
0
  return emitter.Write(v);
238
0
}
239
0
inline Emitter& operator<<(Emitter& emitter, const _Null& v) {
240
0
  return emitter.Write(v);
241
0
}
242
0
inline Emitter& operator<<(Emitter& emitter, const Binary& b) {
243
0
  return emitter.Write(b);
244
0
}
245
246
0
inline Emitter& operator<<(Emitter& emitter, const char* v) {
247
0
  return emitter.Write(v, std::strlen(v));
248
0
}
249
250
0
inline Emitter& operator<<(Emitter& emitter, int v) {
251
0
  return emitter.WriteIntegralType(v);
252
0
}
253
0
inline Emitter& operator<<(Emitter& emitter, unsigned int v) {
254
0
  return emitter.WriteIntegralType(v);
255
0
}
256
0
inline Emitter& operator<<(Emitter& emitter, short v) {
257
0
  return emitter.WriteIntegralType(v);
258
0
}
259
0
inline Emitter& operator<<(Emitter& emitter, unsigned short v) {
260
0
  return emitter.WriteIntegralType(v);
261
0
}
262
0
inline Emitter& operator<<(Emitter& emitter, long v) {
263
0
  return emitter.WriteIntegralType(v);
264
0
}
265
0
inline Emitter& operator<<(Emitter& emitter, unsigned long v) {
266
0
  return emitter.WriteIntegralType(v);
267
0
}
268
0
inline Emitter& operator<<(Emitter& emitter, long long v) {
269
0
  return emitter.WriteIntegralType(v);
270
0
}
271
0
inline Emitter& operator<<(Emitter& emitter, unsigned long long v) {
272
0
  return emitter.WriteIntegralType(v);
273
0
}
274
275
0
inline Emitter& operator<<(Emitter& emitter, float v) {
276
0
  return emitter.WriteStreamable(v);
277
0
}
278
0
inline Emitter& operator<<(Emitter& emitter, double v) {
279
0
  return emitter.WriteStreamable(v);
280
0
}
281
282
0
inline Emitter& operator<<(Emitter& emitter, EMITTER_MANIP value) {
283
0
  return emitter.SetLocalValue(value);
284
0
}
285
286
0
inline Emitter& operator<<(Emitter& emitter, _Indent indent) {
287
0
  return emitter.SetLocalIndent(indent);
288
0
}
289
290
0
inline Emitter& operator<<(Emitter& emitter, _Precision precision) {
291
0
  return emitter.SetLocalPrecision(precision);
292
0
}
293
}  // namespace YAML
294
295
#endif  // EMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66