2 This file is part of libmspparser
3 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
6 #ifndef MSP_PARSER_VALUE_H_
7 #define MSP_PARSER_VALUE_H_
29 Value(Type t, const std::string &d): type(t), data(d) { }
36 typedef std::vector<Value> ValueArray;
38 template<typename T> struct TypeResolver { };
40 template<> struct TypeResolver<short> { static const Value::Type type=Value::INTEGER; };
41 template<> struct TypeResolver<unsigned short> { static const Value::Type type=Value::INTEGER; };
42 template<> struct TypeResolver<int> { static const Value::Type type=Value::INTEGER; };
43 template<> struct TypeResolver<unsigned> { static const Value::Type type=Value::INTEGER; };
44 template<> struct TypeResolver<long> { static const Value::Type type=Value::INTEGER; };
45 template<> struct TypeResolver<unsigned long> { static const Value::Type type=Value::INTEGER; };
46 template<> struct TypeResolver<float> { static const Value::Type type=Value::FLOAT; };
47 template<> struct TypeResolver<double> { static const Value::Type type=Value::FLOAT; };
48 template<> struct TypeResolver<bool> { static const Value::Type type=Value::BOOLEAN; };
50 template<Value::Type T> inline bool check_type(Value::Type) { return false; }
52 template<> inline bool check_type<Value::INTEGER>(Value::Type t) { return t==Value::INTEGER; }
53 template<> inline bool check_type<Value::FLOAT>(Value::Type t) { return t==Value::INTEGER || t==Value::FLOAT; }
54 template<> inline bool check_type<Value::BOOLEAN>(Value::Type t) { return t==Value::BOOLEAN; }
55 template<> inline bool check_type<Value::STRING>(Value::Type t) { return t==Value::STRING; }
56 template<> inline bool check_type<Value::ENUM>(Value::Type t) { return t==Value::ENUM; }
59 inline T Value::get() const
61 if(!check_type<TypeResolver<T>::type>(type))
62 throw TypeError("Type mismatch");
64 std::istringstream ss(data);
69 throw Exception("Invalid value");
75 inline std::string Value::get<std::string>() const
78 throw TypeError("Value is not a string");
83 inline const std::string &Value::get<const std::string&>() const
86 throw TypeError("Value is not a string");