00001 #pragma once
00002
00003 #ifndef EMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
00004 #define EMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
00005
00006
00007 #include "emittermanip.h"
00008 #include "ostream.h"
00009 #include "null.h"
00010 #include <memory>
00011 #include <string>
00012 #include <sstream>
00013
00014 namespace YAML
00015 {
00016 class EmitterState;
00017
00018 class Emitter
00019 {
00020 public:
00021 Emitter();
00022 ~Emitter();
00023
00024
00025 const char *c_str() const;
00026 unsigned size() const;
00027
00028
00029 bool good() const;
00030 const std::string GetLastError() const;
00031
00032
00033 bool SetOutputCharset(EMITTER_MANIP value);
00034 bool SetStringFormat(EMITTER_MANIP value);
00035 bool SetBoolFormat(EMITTER_MANIP value);
00036 bool SetIntBase(EMITTER_MANIP value);
00037 bool SetSeqFormat(EMITTER_MANIP value);
00038 bool SetMapFormat(EMITTER_MANIP value);
00039 bool SetIndent(unsigned n);
00040 bool SetPreCommentIndent(unsigned n);
00041 bool SetPostCommentIndent(unsigned n);
00042
00043
00044 Emitter& SetLocalValue(EMITTER_MANIP value);
00045 Emitter& SetLocalIndent(const _Indent& indent);
00046
00047
00048 Emitter& Write(const std::string& str);
00049 Emitter& Write(bool b);
00050 Emitter& Write(const _Alias& alias);
00051 Emitter& Write(const _Anchor& anchor);
00052 Emitter& Write(const _Tag& tag);
00053 Emitter& Write(const _Comment& comment);
00054 Emitter& Write(const _Null& null);
00055
00056 template <typename T>
00057 Emitter& WriteIntegralType(T value);
00058
00059 template <typename T>
00060 Emitter& WriteStreamable(T value);
00061
00062 private:
00063 void PreWriteIntegralType(std::stringstream& str);
00064 void PostWriteIntegralType(const std::stringstream& str);
00065
00066 private:
00067 enum ATOMIC_TYPE { AT_SCALAR, AT_SEQ, AT_BLOCK_SEQ, AT_FLOW_SEQ, AT_MAP, AT_BLOCK_MAP, AT_FLOW_MAP };
00068
00069 void PreAtomicWrite();
00070 bool GotoNextPreAtomicState();
00071 void PostAtomicWrite();
00072 void EmitSeparationIfNecessary();
00073
00074 void EmitBeginSeq();
00075 void EmitEndSeq();
00076 void EmitBeginMap();
00077 void EmitEndMap();
00078 void EmitKey();
00079 void EmitValue();
00080
00081 private:
00082 ostream m_stream;
00083 std::auto_ptr <EmitterState> m_pState;
00084 };
00085
00086 template <typename T>
00087 inline Emitter& Emitter::WriteIntegralType(T value)
00088 {
00089 if(!good())
00090 return *this;
00091
00092 std::stringstream str;
00093 PreWriteIntegralType(str);
00094 str << value;
00095 PostWriteIntegralType(str);
00096 return *this;
00097 }
00098
00099 template <typename T>
00100 inline Emitter& Emitter::WriteStreamable(T value)
00101 {
00102 if(!good())
00103 return *this;
00104
00105 PreAtomicWrite();
00106 EmitSeparationIfNecessary();
00107
00108 std::stringstream str;
00109 str << value;
00110 m_stream << str.str();
00111
00112 PostAtomicWrite();
00113 return *this;
00114 }
00115
00116
00117 inline Emitter& operator << (Emitter& emitter, const std::string& v) { return emitter.Write(v); }
00118 inline Emitter& operator << (Emitter& emitter, bool v) { return emitter.Write(v); }
00119 inline Emitter& operator << (Emitter& emitter, const _Alias& v) { return emitter.Write(v); }
00120 inline Emitter& operator << (Emitter& emitter, const _Anchor& v) { return emitter.Write(v); }
00121 inline Emitter& operator << (Emitter& emitter, const _Tag& v) { return emitter.Write(v); }
00122 inline Emitter& operator << (Emitter& emitter, const _Comment& v) { return emitter.Write(v); }
00123 inline Emitter& operator << (Emitter& emitter, const _Null& v) { return emitter.Write(v); }
00124
00125 inline Emitter& operator << (Emitter& emitter, const char *v) { return emitter.Write(std::string(v)); }
00126
00127 inline Emitter& operator << (Emitter& emitter, int v) { return emitter.WriteIntegralType(v); }
00128 inline Emitter& operator << (Emitter& emitter, unsigned int v) { return emitter.WriteIntegralType(v); }
00129 inline Emitter& operator << (Emitter& emitter, short v) { return emitter.WriteIntegralType(v); }
00130 inline Emitter& operator << (Emitter& emitter, unsigned short v) { return emitter.WriteIntegralType(v); }
00131 inline Emitter& operator << (Emitter& emitter, long v) { return emitter.WriteIntegralType(v); }
00132 inline Emitter& operator << (Emitter& emitter, unsigned long v) { return emitter.WriteIntegralType(v); }
00133
00134 inline Emitter& operator << (Emitter& emitter, float v) { return emitter.WriteStreamable(v); }
00135 inline Emitter& operator << (Emitter& emitter, double v) { return emitter.WriteStreamable(v); }
00136
00137 inline Emitter& operator << (Emitter& emitter, EMITTER_MANIP value) {
00138 return emitter.SetLocalValue(value);
00139 }
00140
00141 inline Emitter& operator << (Emitter& emitter, _Indent indent) {
00142 return emitter.SetLocalIndent(indent);
00143 }
00144 }
00145
00146 #endif // EMITTER_H_62B23520_7C8E_11DE_8A39_0800200C9A66