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_
28 Value(Type t, const std::string &d): type(t), data(d) { }
35 typedef std::vector<Value> ValueArray;
37 template<typename T> struct TypeResolver { };
39 template<> struct TypeResolver<short> { static const Value::Type type=Value::INTEGER; };
40 template<> struct TypeResolver<unsigned short> { static const Value::Type type=Value::INTEGER; };
41 template<> struct TypeResolver<int> { static const Value::Type type=Value::INTEGER; };
42 template<> struct TypeResolver<unsigned> { static const Value::Type type=Value::INTEGER; };
43 template<> struct TypeResolver<long> { static const Value::Type type=Value::INTEGER; };
44 template<> struct TypeResolver<unsigned long> { static const Value::Type type=Value::INTEGER; };
45 template<> struct TypeResolver<float> { static const Value::Type type=Value::FLOAT; };
46 template<> struct TypeResolver<double> { static const Value::Type type=Value::FLOAT; };
47 template<> struct TypeResolver<bool> { static const Value::Type type=Value::BOOLEAN; };
49 template<Value::Type T> inline bool check_type(Value::Type) { return false; }
51 template<> inline bool check_type<Value::INTEGER>(Value::Type t) { return t==Value::INTEGER; }
52 template<> inline bool check_type<Value::FLOAT>(Value::Type t) { return t==Value::INTEGER || t==Value::FLOAT; }
53 template<> inline bool check_type<Value::BOOLEAN>(Value::Type t) { return t==Value::BOOLEAN; }
54 template<> inline bool check_type<Value::STRING>(Value::Type t) { return t==Value::STRING; }
55 template<> inline bool check_type<Value::ENUM>(Value::Type t) { return t==Value::ENUM; }
58 inline T Value::get() const
60 if(!check_type<TypeResolver<T>::type>(type))
61 throw TypeError("Type mismatch");
63 std::istringstream ss(data);
67 throw ValueError("Invalid value");
73 inline std::string Value::get<std::string>() const
76 throw TypeError("Value is not a string");
81 inline const std::string &Value::get<const std::string&>() const
84 throw TypeError("Value is not a string");