emitterstate.h 5.82 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/emitterdef.h"
12
#include "yaml-cpp/emittermanip.h"
13
14
15
16
#include <cassert>
#include <vector>
#include <stack>
#include <memory>
Jesse Beder's avatar
Jesse Beder committed
17
#include <stdexcept>
18
19
20

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

25
26
27
28
29
30
31
32
33
	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
34
		void SetError(const std::string& error) { throw std::runtime_error(error); m_isGood = false; m_lastError = error; }
35
		
Jesse Beder's avatar
Jesse Beder committed
36
37
		// node handling
        void BeginScalar();
38
39
		void BeginGroup(GroupType::value type);
		void EndGroup(GroupType::value type);
40
41
42
43

        EmitterNodeType::value NextGroupType(GroupType::value type) const;
        EmitterNodeType::value CurGroupNodeType() const;

Jesse Beder's avatar
Jesse Beder committed
44
45
		GroupType::value CurGroupType() const;
        FlowType::value CurGroupFlowType() const;
Jesse Beder's avatar
Jesse Beder committed
46
        int CurGroupIndent() const;
47
        std::size_t CurGroupChildCount() const;
48

Jesse Beder's avatar
Jesse Beder committed
49
50
51
		int CurIndent() const { return m_curIndent; }
        bool HasAnchor() const { return m_hasAnchor; }
        bool HasTag() const { return m_hasTag; }
52
53
54
55

		void ClearModifiedSettings();

		// formatters
56
57
		void SetLocalValue(EMITTER_MANIP value);

58
		bool SetOutputCharset(EMITTER_MANIP value, FmtScope::value scope);
59
60
		EMITTER_MANIP GetOutputCharset() const { return m_charset.get(); }

61
		bool SetStringFormat(EMITTER_MANIP value, FmtScope::value scope);
62
63
		EMITTER_MANIP GetStringFormat() const { return m_strFmt.get(); }
		
64
		bool SetBoolFormat(EMITTER_MANIP value, FmtScope::value scope);
65
66
		EMITTER_MANIP GetBoolFormat() const { return m_boolFmt.get(); }

67
		bool SetBoolLengthFormat(EMITTER_MANIP value, FmtScope::value scope);
68
69
		EMITTER_MANIP GetBoolLengthFormat() const { return m_boolLengthFmt.get(); }

70
		bool SetBoolCaseFormat(EMITTER_MANIP value, FmtScope::value scope);
71
72
		EMITTER_MANIP GetBoolCaseFormat() const { return m_boolCaseFmt.get(); }

73
		bool SetIntFormat(EMITTER_MANIP value, FmtScope::value scope);
74
75
		EMITTER_MANIP GetIntFormat() const { return m_intFmt.get(); }

76
		bool SetIndent(unsigned value, FmtScope::value scope);
77
78
		int GetIndent() const { return m_indent.get(); }
		
79
		bool SetPreCommentIndent(unsigned value, FmtScope::value scope);
80
		int GetPreCommentIndent() const { return m_preCommentIndent.get(); }
81
		bool SetPostCommentIndent(unsigned value, FmtScope::value scope);
82
83
		int GetPostCommentIndent() const { return m_postCommentIndent.get(); }
		
84
85
		bool SetFlowType(GroupType::value groupType, EMITTER_MANIP value, FmtScope::value scope);
		EMITTER_MANIP GetFlowType(GroupType::value groupType) const;
86
		
87
		bool SetMapKeyFormat(EMITTER_MANIP value, FmtScope::value scope);
88
		EMITTER_MANIP GetMapKeyFormat() const { return m_mapKeyFmt.get(); }
89
        
90
        bool SetFloatPrecision(int value, FmtScope::value scope);
91
        unsigned GetFloatPrecision() const { return m_floatPrecision.get(); }
92
        bool SetDoublePrecision(int value, FmtScope::value scope);
93
        unsigned GetDoublePrecision() const { return m_doublePrecision.get(); }
94
95
96
		
	private:
		template <typename T>
97
		void _Set(Setting<T>& fmt, T value, FmtScope::value scope);
Jesse Beder's avatar
Jesse Beder committed
98
99
        
        void BeginNode();
100
101
102
103
104
105
106
		
	private:
		// basic state ok?
		bool m_isGood;
		std::string m_lastError;
		
		// other state
107
108
109
110
111
112
113
114
115
116
117
		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;
118
119
        Setting<int> m_floatPrecision;
        Setting<int> m_doublePrecision;
120
121
122
123
124
		
		SettingChanges m_modifiedSettings;
		SettingChanges m_globalModifiedSettings;
		
		struct Group {
Jesse Beder's avatar
Jesse Beder committed
125
			explicit Group(GroupType::value type_): type(type_), indent(0), childCount(0) {}
126
			
127
            GroupType::value type;
128
            FlowType::value flowType;
129
			int indent;
Jesse Beder's avatar
Jesse Beder committed
130
            std::size_t childCount;
131
132
			
			SettingChanges modifiedSettings;
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
            
            EmitterNodeType::value NodeType() const {
                if(type == GroupType::Seq) {
                    if(flowType == FlowType::Flow)
                        return EmitterNodeType::FlowSeq;
                    else
                        return EmitterNodeType::BlockSeq;
                } else {
                    if(flowType == FlowType::Flow)
                        return EmitterNodeType::FlowMap;
                    else
                        return EmitterNodeType::BlockMap;
                }

                // can't get here
                assert(false);
                return EmitterNodeType::None;
            }
151
		};
152
153

		ptr_stack<Group> m_groups;
154
		unsigned m_curIndent;
Jesse Beder's avatar
Jesse Beder committed
155
156
        bool m_hasAnchor;
        bool m_hasTag;
157
158
159
	};

	template <typename T>
160
	void EmitterState::_Set(Setting<T>& fmt, T value, FmtScope::value scope) {
161
		switch(scope) {
162
			case FmtScope::Local:
163
164
				m_modifiedSettings.push(fmt.set(value));
				break;
165
			case FmtScope::Global:
166
167
168
169
170
171
172
173
174
				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);
		}
	}
}
175
176

#endif // EMITTERSTATE_H_62B23520_7C8E_11DE_8A39_0800200C9A66