emitterstate.h 4.6 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
16
17
18
#include <cassert>
#include <vector>
#include <stack>
#include <memory>

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

23
24
25
26
27
28
29
30
31
32
33
34
	class EmitterState
	{
	public:
		EmitterState();
		~EmitterState();
		
		// basic state checking
		bool good() const { return m_isGood; }
		const std::string GetLastError() const { return m_lastError; }
		void SetError(const std::string& error) { m_isGood = false; m_lastError = error; }
		
		// group handling
35
36
		void BeginGroup(GroupType::value type);
		void EndGroup(GroupType::value type);
37
		
38
39
		GroupType::value GetCurGroupType() const;
        FlowType::value GetCurGroupFlowType() const;
40
41
42
43
44
		int GetCurIndent() const { return m_curIndent; }

		void ClearModifiedSettings();

		// formatters
45
46
		void SetLocalValue(EMITTER_MANIP value);

47
		bool SetOutputCharset(EMITTER_MANIP value, FmtScope::value scope);
48
49
		EMITTER_MANIP GetOutputCharset() const { return m_charset.get(); }

50
		bool SetStringFormat(EMITTER_MANIP value, FmtScope::value scope);
51
52
		EMITTER_MANIP GetStringFormat() const { return m_strFmt.get(); }
		
53
		bool SetBoolFormat(EMITTER_MANIP value, FmtScope::value scope);
54
55
		EMITTER_MANIP GetBoolFormat() const { return m_boolFmt.get(); }

56
		bool SetBoolLengthFormat(EMITTER_MANIP value, FmtScope::value scope);
57
58
		EMITTER_MANIP GetBoolLengthFormat() const { return m_boolLengthFmt.get(); }

59
		bool SetBoolCaseFormat(EMITTER_MANIP value, FmtScope::value scope);
60
61
		EMITTER_MANIP GetBoolCaseFormat() const { return m_boolCaseFmt.get(); }

62
		bool SetIntFormat(EMITTER_MANIP value, FmtScope::value scope);
63
64
		EMITTER_MANIP GetIntFormat() const { return m_intFmt.get(); }

65
		bool SetIndent(unsigned value, FmtScope::value scope);
66
67
		int GetIndent() const { return m_indent.get(); }
		
68
		bool SetPreCommentIndent(unsigned value, FmtScope::value scope);
69
		int GetPreCommentIndent() const { return m_preCommentIndent.get(); }
70
		bool SetPostCommentIndent(unsigned value, FmtScope::value scope);
71
72
		int GetPostCommentIndent() const { return m_postCommentIndent.get(); }
		
73
74
		bool SetFlowType(GroupType::value groupType, EMITTER_MANIP value, FmtScope::value scope);
		EMITTER_MANIP GetFlowType(GroupType::value groupType) const;
75
		
76
		bool SetMapKeyFormat(EMITTER_MANIP value, FmtScope::value scope);
77
		EMITTER_MANIP GetMapKeyFormat() const { return m_mapKeyFmt.get(); }
78
        
79
        bool SetFloatPrecision(int value, FmtScope::value scope);
80
        unsigned GetFloatPrecision() const { return m_floatPrecision.get(); }
81
        bool SetDoublePrecision(int value, FmtScope::value scope);
82
        unsigned GetDoublePrecision() const { return m_doublePrecision.get(); }
83
84
85
		
	private:
		template <typename T>
86
		void _Set(Setting<T>& fmt, T value, FmtScope::value scope);
87
88
89
90
91
92
93
		
	private:
		// basic state ok?
		bool m_isGood;
		std::string m_lastError;
		
		// other state
94
95
96
97
98
99
100
101
102
103
104
		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;
105
106
        Setting<int> m_floatPrecision;
        Setting<int> m_doublePrecision;
107
108
109
110
111
		
		SettingChanges m_modifiedSettings;
		SettingChanges m_globalModifiedSettings;
		
		struct Group {
112
			Group(GroupType::value type_): type(type_), indent(0) {}
113
			
114
            GroupType::value type;
115
116
117
118
119
			EMITTER_MANIP flow;
			int indent;
			
			SettingChanges modifiedSettings;
		};
120
121

		ptr_stack<Group> m_groups;
122
123
124
125
		unsigned m_curIndent;
	};

	template <typename T>
126
	void EmitterState::_Set(Setting<T>& fmt, T value, FmtScope::value scope) {
127
		switch(scope) {
128
			case FmtScope::Local:
129
130
				m_modifiedSettings.push(fmt.set(value));
				break;
131
			case FmtScope::Global:
132
133
134
135
136
137
138
139
140
				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);
		}
	}
}
141
142

#endif // EMITTERSTATE_H_62B23520_7C8E_11DE_8A39_0800200C9A66