emitter.h 7.6 KB

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