emitter.h 7.0 KB

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