1 | #ifndef __YAMLPROVIDERYAMLEXTENSIONS_H__
|
---|
2 | #define __YAMLPROVIDERYAMLEXTENSIONS_H__
|
---|
3 |
|
---|
4 | #include <yaml.h>
|
---|
5 | #include <mars_util.h>
|
---|
6 | #include <stdarg.h>
|
---|
7 | #include <map>
|
---|
8 | #include <string>
|
---|
9 |
|
---|
10 | namespace YAML
|
---|
11 | {
|
---|
12 | template< typename T >
|
---|
13 | static void operator >> (const Node & nd, T & target)
|
---|
14 | {
|
---|
15 | target = nd.as<T>();
|
---|
16 | }
|
---|
17 |
|
---|
18 | template< typename T >
|
---|
19 | void operator >> (const Node & nd, mars::Range< T > & rng)
|
---|
20 | {
|
---|
21 | if (nd.Type() == NodeType::Sequence)
|
---|
22 | {
|
---|
23 | nd[0] >> rng.minimum;
|
---|
24 | nd[1] >> rng.maximum;
|
---|
25 | rng.update();
|
---|
26 | } else
|
---|
27 | {
|
---|
28 | nd >> rng.minimum;
|
---|
29 | nd >> rng.maximum;
|
---|
30 | rng.update();
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | template< typename T >
|
---|
35 | void operator >> (const Node & nd, mars::RangeX< T > & rng)
|
---|
36 | {
|
---|
37 | if (nd.Type() == NodeType::Sequence)
|
---|
38 | {
|
---|
39 | nd[0] >> rng.minimum;
|
---|
40 | nd[1] >> rng.maximum;
|
---|
41 | rng.update();
|
---|
42 | } else
|
---|
43 | {
|
---|
44 | nd >> rng.minimum;
|
---|
45 | nd >> rng.maximum;
|
---|
46 | rng.update();
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | template <typename L, typename E>
|
---|
51 | static void operator >> (const Node & nd, L & l)
|
---|
52 | {
|
---|
53 | E e;
|
---|
54 |
|
---|
55 | if (nd.Type() == NodeType::Sequence)
|
---|
56 | {
|
---|
57 | for (const_iterator it = nd.begin(); it != nd.end(); ++it)
|
---|
58 | {
|
---|
59 | *it >> e;
|
---|
60 | l.push_back(e);
|
---|
61 | }
|
---|
62 | } else
|
---|
63 | {
|
---|
64 | nd >> e;
|
---|
65 | l.push_back(e);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | template< typename E >
|
---|
70 | class StringMapSchema
|
---|
71 | {
|
---|
72 | private:
|
---|
73 | typedef std::map< std::string, E > MapType;
|
---|
74 |
|
---|
75 | MapType _map;
|
---|
76 |
|
---|
77 | public:
|
---|
78 | StringMapSchema (const char * szFirst, /*const E & eFirst, */...)
|
---|
79 | {
|
---|
80 | va_list vl;
|
---|
81 | const char * szIdent = szFirst;
|
---|
82 |
|
---|
83 | va_start(vl, szFirst);
|
---|
84 | while(szIdent != NULL)
|
---|
85 | {
|
---|
86 | _map[szIdent] = static_cast< E > (va_arg(vl, int));
|
---|
87 | szIdent = va_arg(vl, const char *);
|
---|
88 | }
|
---|
89 | va_end(vl);
|
---|
90 | }
|
---|
91 |
|
---|
92 | bool setValue (E & e, const std::string & s) const
|
---|
93 | {
|
---|
94 | typename MapType::const_iterator i = _map.find(s);
|
---|
95 |
|
---|
96 | if (i != _map.end())
|
---|
97 | {
|
---|
98 | e = i->second;
|
---|
99 | return true;
|
---|
100 | }
|
---|
101 |
|
---|
102 | return false;
|
---|
103 | }
|
---|
104 | };
|
---|
105 |
|
---|
106 | template< typename E >
|
---|
107 | class MappedValue
|
---|
108 | {
|
---|
109 | private:
|
---|
110 | E & _e;
|
---|
111 | const StringMapSchema< E > & _schema;
|
---|
112 |
|
---|
113 | public:
|
---|
114 | MappedValue (E & e, const StringMapSchema< E > & schema)
|
---|
115 | : _e(e), _schema(schema) {}
|
---|
116 |
|
---|
117 | inline bool operator << (const std::string & s)
|
---|
118 | { return _schema.setValue(_e, s); }
|
---|
119 | };
|
---|
120 |
|
---|
121 | template< typename E >
|
---|
122 | class MappedRange
|
---|
123 | {
|
---|
124 | private:
|
---|
125 | mars::Range< E > & _rng;
|
---|
126 | const StringMapSchema< E > & _schema;
|
---|
127 |
|
---|
128 | public:
|
---|
129 | MappedRange (mars::Range< E > & rng, const StringMapSchema< E > & schema)
|
---|
130 | : _rng(rng), _schema(schema) {}
|
---|
131 |
|
---|
132 | bool operator << (const std::string & s)
|
---|
133 | {
|
---|
134 | E e;
|
---|
135 |
|
---|
136 | if (_schema.setValue(e, s))
|
---|
137 | {
|
---|
138 | _rng.minimum = _rng.maximum = e;
|
---|
139 | _rng.update();
|
---|
140 | return true;
|
---|
141 | }
|
---|
142 |
|
---|
143 | return false;
|
---|
144 | }
|
---|
145 |
|
---|
146 | bool setRange (const std::string & sMin, const std::string & sMax)
|
---|
147 | {
|
---|
148 | E eMin, eMax;
|
---|
149 |
|
---|
150 | if (_schema.setValue(eMin, sMin) && _schema.setValue(eMax, sMax))
|
---|
151 | {
|
---|
152 | _rng.minimum = eMin;
|
---|
153 | _rng.maximum = eMax;
|
---|
154 | _rng.update();
|
---|
155 | return true;
|
---|
156 | }
|
---|
157 |
|
---|
158 | return false;
|
---|
159 | }
|
---|
160 | };
|
---|
161 |
|
---|
162 | template< typename E >
|
---|
163 | void operator >> (const Node & nd, MappedValue< E > mapped)
|
---|
164 | {
|
---|
165 | std::string s;
|
---|
166 |
|
---|
167 | nd >> s;
|
---|
168 | if (!(mapped << s))
|
---|
169 | throw RepresentationException(nd.Mark(), "Unknown identifier for mapped value: " + s);
|
---|
170 | }
|
---|
171 |
|
---|
172 | template< typename E >
|
---|
173 | void operator >> (const Node & nd, MappedRange< E > mapped)
|
---|
174 | {
|
---|
175 | if (nd.Type() == NodeType::Sequence)
|
---|
176 | {
|
---|
177 | std::string sMin, sMax;
|
---|
178 |
|
---|
179 | nd[0] >> sMin;
|
---|
180 | nd[1] >> sMax;
|
---|
181 | if (!mapped.setRange(sMin, sMax))
|
---|
182 | throw RepresentationException(nd.Mark(), "Unknown identifier(s) for mapped range: " + sMin + "/" + sMax);
|
---|
183 | } else
|
---|
184 | {
|
---|
185 | std::string s;
|
---|
186 | nd >> s;
|
---|
187 | if (!(mapped << s))
|
---|
188 | throw RepresentationException(nd.Mark(), "Unknown identifier for mapped range: " + s);
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | #endif
|
---|