emitterstate.h 4.91 KB
Newer Older
1
2
3
#ifndef EMITTERSTATE_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define EMITTERSTATE_H_62B23520_7C8E_11DE_8A39_0800200C9A66

4
#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
5
6
7
#pragma once
#endif

8

9
#include "ptr_stack.h"
10
#include "setting.h"
11
#include "yaml-cpp/emittermanip.h"
12
13
14
15
#include <cassert>
#include <vector>
#include <stack>
#include <memory>
Jesse Beder's avatar
Jesse Beder committed
16
#include <stdexcept>
17
18
19

namespace YAML
{
20
21
22
23
	struct FmtScope { enum value { Local, Global }; };
	struct GroupType { enum value { None, Seq, Map }; };
	struct FlowType { enum value { None, Flow, Block }; };

24
25
26
27
28
29
30
31
32
	class EmitterState
	{
	public:
		EmitterState();
		~EmitterState();
		
		// basic state checking
		bool good() const { return m_isGood; }
		const std::string GetLastError() const { return m_lastError; }
Jesse Beder's avatar
Jesse Beder committed
33
		void SetError(const std::string& error) { throw std::runtime_error(error); m_isGood = false; m_lastError = error; }
34
		
Jesse Beder's avatar
Jesse Beder committed
35
36
		// node handling
        void BeginScalar();
37
38
		void BeginGroup(GroupType::value type);
		void EndGroup(GroupType::value type);
39
		
Jesse Beder's avatar
Jesse Beder committed
40
41
42
43
44
		GroupType::value CurGroupType() const;
        FlowType::value CurGroupFlowType() const;
		int CurIndent() const { return m_curIndent; }
        bool HasAnchor() const { return m_hasAnchor; }
        bool HasTag() const { return m_hasTag; }
45
46
47
48

		void ClearModifiedSettings();

		// formatters
49
50
		void SetLocalValue(EMITTER_MANIP value);

51
		bool SetOutputCharset(EMITTER_MANIP value, FmtScope::value scope);
52
53
		EMITTER_MANIP GetOutputCharset() const { return m_charset.get(); }

54
		bool SetStringFormat(EMITTER_MANIP value, FmtScope::value scope);
55
56
		EMITTER_MANIP GetStringFormat() const { return m_strFmt.get(); }
		
57
		bool SetBoolFormat(EMITTER_MANIP value, FmtScope::value scope);
58
59
		EMITTER_MANIP GetBoolFormat() const { return m_boolFmt.get(); }

60
		bool SetBoolLengthFormat(EMITTER_MANIP value, FmtScope::value scope);
61
62
		EMITTER_MANIP GetBoolLengthFormat() const { return m_boolLengthFmt.get(); }

63
		bool SetBoolCaseFormat(EMITTER_MANIP value, FmtScope::value scope);
64
65
		EMITTER_MANIP GetBoolCaseFormat() const { return m_boolCaseFmt.get(); }

66
		bool SetIntFormat(EMITTER_MANIP value, FmtScope::value scope);
67
68
		EMITTER_MANIP GetIntFormat() const { return m_intFmt.get(); }

69
		bool SetIndent(unsigned value, FmtScope::value scope);
70
71
		int GetIndent() const { return m_indent.get(); }
		
72
		bool SetPreCommentIndent(unsigned value, FmtScope::value scope);
73
		int GetPreCommentIndent() const { return m_preCommentIndent.get(); }
74
		bool SetPostCommentIndent(unsigned value, FmtScope::value scope);
75
76
		int GetPostCommentIndent() const { return m_postCommentIndent.get(); }
		
77
78
		bool SetFlowType(GroupType::value groupType, EMITTER_MANIP value, FmtScope::value scope);
		EMITTER_MANIP GetFlowType(GroupType::value groupType) const;
79
		
80
		bool SetMapKeyFormat(EMITTER_MANIP value, FmtScope::value scope);
81
		EMITTER_MANIP GetMapKeyFormat() const { return m_mapKeyFmt.get(); }
82
        
83
        bool SetFloatPrecision(int value, FmtScope::value scope);
84
        unsigned GetFloatPrecision() const { return m_floatPrecision.get(); }
85
        bool SetDoublePrecision(int value, FmtScope::value scope);
86
        unsigned GetDoublePrecision() const { return m_doublePrecision.get(); }
87
88
89
		
	private:
		template <typename T>
90
		void _Set(Setting<T>& fmt, T value, FmtScope::value scope);
Jesse Beder's avatar
Jesse Beder committed
91
92
        
        void BeginNode();
93
94
95
96
97
98
99
		
	private:
		// basic state ok?
		bool m_isGood;
		std::string m_lastError;
		
		// other state
100
101
102
103
104
105
106
107
108
109
110
		Setting<EMITTER_MANIP> m_charset;
		Setting<EMITTER_MANIP> m_strFmt;
		Setting<EMITTER_MANIP> m_boolFmt;
		Setting<EMITTER_MANIP> m_boolLengthFmt;
		Setting<EMITTER_MANIP> m_boolCaseFmt;
		Setting<EMITTER_MANIP> m_intFmt;
		Setting<unsigned> m_indent;
		Setting<unsigned> m_preCommentIndent, m_postCommentIndent;
		Setting<EMITTER_MANIP> m_seqFmt;
		Setting<EMITTER_MANIP> m_mapFmt;
		Setting<EMITTER_MANIP> m_mapKeyFmt;
111
112
        Setting<int> m_floatPrecision;
        Setting<int> m_doublePrecision;
113
114
115
116
117
		
		SettingChanges m_modifiedSettings;
		SettingChanges m_globalModifiedSettings;
		
		struct Group {
Jesse Beder's avatar
Jesse Beder committed
118
			explicit Group(GroupType::value type_): type(type_), indent(0), childCount(0) {}
119
			
120
            GroupType::value type;
121
122
			EMITTER_MANIP flow;
			int indent;
Jesse Beder's avatar
Jesse Beder committed
123
            std::size_t childCount;
124
125
126
			
			SettingChanges modifiedSettings;
		};
127
128

		ptr_stack<Group> m_groups;
129
		unsigned m_curIndent;
Jesse Beder's avatar
Jesse Beder committed
130
131
        bool m_hasAnchor;
        bool m_hasTag;
132
133
134
	};

	template <typename T>
135
	void EmitterState::_Set(Setting<T>& fmt, T value, FmtScope::value scope) {
136
		switch(scope) {
137
			case FmtScope::Local:
138
139
				m_modifiedSettings.push(fmt.set(value));
				break;
140
			case FmtScope::Global:
141
142
143
144
145
146
147
148
149
				fmt.set(value);
				m_globalModifiedSettings.push(fmt.set(value));  // this pushes an identity set, so when we restore,
				                                                // it restores to the value here, and not the previous one
				break;
			default:
				assert(false);
		}
	}
}
150
151

#endif // EMITTERSTATE_H_62B23520_7C8E_11DE_8A39_0800200C9A66