4 #include <unordered_map>
13 class SchemaAttribute;
16 class SchemaAttributeFloat;
17 class SchemaAttributeInt;
18 class SchemaAttributeString;
19 class SchemaAttributeComponent;
20 class SchemaAttributeDefault;
25 typedef std::unordered_map<QByteArray, std::shared_ptr<SchemaAttribute>> SchemaAttributeMap;
34 Range(
bool boundedBelow,
bool boundedAbove, T val);
36 bool inside(T val)
const;
41 static Range<T> parseRange(
const QVariantMap& map);
45 bool operator==(
const Range<T>& other)
const;
46 bool operator<(const Range<T>& other)
const;
63 void setAttributeName(
const QByteArray& name);
64 const QByteArray& attributeName()
const;
66 void setDescription(
const QString& descr);
67 const QString& description()
const;
69 virtual QString type()
const = 0;
71 virtual void parseAttributes(
const QVariantMap& map);
73 static std::shared_ptr<SchemaAttribute> parse(
const QVariantMap& map);
76 QByteArray m_attributeName;
77 QString m_description;
87 typedef std::shared_ptr<SchemaAttribute> SchemaAttributePtr;
96 virtual ~SchemaAttributeT() {}
98 void setDefaultValue(
const T& value);
99 const T& valueRef()
const;
102 bool hasDefaultValue()
const;
104 virtual void parseAttributes(
const QVariantMap& map)
override;
113 template <
typename Numeric>
114 class SchemaAttributeNumericT :
public SchemaAttributeT<Numeric>
117 SchemaAttributeNumericT() {}
118 virtual ~SchemaAttributeNumericT() {}
120 Range<Numeric> range()
const;
121 void setRange(Range<Numeric> range);
123 virtual void parseAttributes(
const QVariantMap& map);
126 Range<Numeric> m_range;
131 class SchemaAttributeFloat :
public SchemaAttributeNumericT<float>
134 SchemaAttributeFloat();
135 virtual ~SchemaAttributeFloat();
137 virtual QString type()
const override;
140 class SchemaAttributeInt :
public SchemaAttributeNumericT<int>
143 SchemaAttributeInt();
144 virtual ~SchemaAttributeInt();
146 virtual QString type()
const override;
149 class SchemaAttributeString :
public SchemaAttributeT<QString>
152 SchemaAttributeString();
153 virtual ~SchemaAttributeString();
155 virtual QString type()
const override;
158 class SchemaAttributeComponent :
public SchemaAttributeT<QString>
160 typedef std::map<QByteArray, Range<unsigned> > SlotRanges;
165 SchemaAttributeComponent();
166 virtual ~SchemaAttributeComponent();
168 virtual QString type()
const override;
170 const QString& componentRole()
const;
171 Range<unsigned> range()
const;
172 Range<unsigned> range(
const QByteArray & keyword)
const;
173 const SlotRanges & allRanges()
const;
175 virtual void parseAttributes(
const QVariantMap& map)
override;
180 Range<unsigned> m_defaultRange;
183 class SchemaAttributeDefault :
public SchemaAttributeT<QString>
186 SchemaAttributeDefault(
const QString & type);
187 virtual ~SchemaAttributeDefault();
189 virtual QString type()
const override;
197 template <
typename T>
198 void SchemaAttributeT<T>::setDefaultValue(
const T &value)
200 m_defaultValue = value;
204 template <
typename T>
205 const T& SchemaAttributeT<T>::valueRef()
const
207 return m_defaultValue;
210 template <
typename T>
211 T SchemaAttributeT<T>::value()
const
213 return m_defaultValue;
216 template <
typename T>
217 bool SchemaAttributeT<T>::hasDefaultValue()
const
222 template <
typename T>
223 void SchemaAttributeT<T>::parseAttributes(
const QVariantMap &map)
225 SchemaAttribute::parseAttributes(map);
226 auto it = map.find(
"default");
227 if(it != map.end()) {
228 setDefaultValue(it.value().value<T>());
234 template <
typename T>
235 Range<T> SchemaAttributeNumericT<T>::range()
const
240 template <
typename T>
241 void SchemaAttributeNumericT<T>::setRange(Range<T> range)
246 template <
typename T>
247 void SchemaAttributeNumericT<T>::parseAttributes(
const QVariantMap &map)
249 SchemaAttributeT<T>::parseAttributes(map);
250 setRange(Range<T>::parseRange(map));
255 template <
typename T>
257 : isBoundedBelow(false),
258 isBoundedAbove(false)
261 template <
typename T>
262 Range<T>::Range(T pmin, T pmax)
263 : min(pmin), max(pmax),
264 isBoundedBelow(true),
268 template <
typename T>
269 Range<T>::Range(
bool boundedBelow,
bool boundedAbove, T val)
270 : isBoundedBelow(boundedBelow),
271 isBoundedAbove(boundedAbove)
273 if(boundedAbove && boundedBelow)
275 else if(boundedAbove)
277 else if(boundedBelow)
281 template <
typename T>
282 bool Range<T>::inside(T val)
const
284 if(!isBoundedAbove && !isBoundedBelow) {
286 }
else if(!isBoundedAbove) {
288 }
else if(!isBoundedBelow) {
291 return val <= max && val >= min;
295 template <
typename T>
296 bool Range<T>::operator==(
const Range<T>& other)
const
298 return !(*
this < other) && !(other < *
this);
301 template <
typename T>
302 bool Range<T>::operator<(
const Range<T>& other)
const
304 return std::make_tuple(isBoundedBelow, min, !isBoundedAbove, max)
305 < std::make_tuple(other.isBoundedBelow, other.min, !other.isBoundedAbove, other.max);
308 template <
typename T>
309 void Range<T>::setMin(T val)
312 isBoundedBelow =
true;
315 template <
typename T>
316 void Range<T>::setMax(T val)
319 isBoundedAbove =
true;
322 template <
typename T>
323 Range<T> Range<T>::parseRange(
const QVariantMap& map)
326 auto it = map.find(
"min");
327 if(it != map.end()) {
328 ran.setMin(it.value().value<T>());
331 it = map.find(
"max");
332 if(it != map.end()) {
333 ran.setMax(it.value().value<T>());