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