emitterstate.h 4.95 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
		GroupType::value CurGroupType() const;
        FlowType::value CurGroupFlowType() const;
Jesse Beder's avatar
Jesse Beder committed
42
        int CurGroupIndent() const;
Jesse Beder's avatar
Jesse Beder committed
43
44
45
		int CurIndent() const { return m_curIndent; }
        bool HasAnchor() const { return m_hasAnchor; }
        bool HasTag() const { return m_hasTag; }
46
47
48
49

		void ClearModifiedSettings();

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

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

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

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

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

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

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

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

	template <typename T>
136
	void EmitterState::_Set(Setting<T>& fmt, T value, FmtScope::value scope) {
137
		switch(scope) {
138
			case FmtScope::Local:
139
140
				m_modifiedSettings.push(fmt.set(value));
				break;
141
			case FmtScope::Global:
142
143
144
145
146
147
148
149
150
				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);
		}
	}
}
151
152

#endif // EMITTERSTATE_H_62B23520_7C8E_11DE_8A39_0800200C9A66