3 This file is part of libmspdatafile
4 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
7 #ifndef MSP_DATAFILE_VALUE_H_
8 #define MSP_DATAFILE_VALUE_H_
30 Value(Type t, const std::string &d): type(t), data(d) { }
37 typedef std::vector<Value> ValueArray;
39 template<typename T> struct TypeResolver { static const Value::Type type=Value::ENUM; };
41 template<> struct TypeResolver<short> { static const Value::Type type=Value::INTEGER; };
42 template<> struct TypeResolver<unsigned short> { static const Value::Type type=Value::INTEGER; };
43 template<> struct TypeResolver<int> { static const Value::Type type=Value::INTEGER; };
44 template<> struct TypeResolver<unsigned> { static const Value::Type type=Value::INTEGER; };
45 template<> struct TypeResolver<long> { static const Value::Type type=Value::INTEGER; };
46 template<> struct TypeResolver<unsigned long> { static const Value::Type type=Value::INTEGER; };
47 template<> struct TypeResolver<float> { static const Value::Type type=Value::FLOAT; };
48 template<> struct TypeResolver<double> { static const Value::Type type=Value::FLOAT; };
49 template<> struct TypeResolver<bool> { static const Value::Type type=Value::BOOLEAN; };
51 template<Value::Type T> inline bool check_type(Value::Type) { return false; }
53 template<> inline bool check_type<Value::INTEGER>(Value::Type t) { return t==Value::INTEGER; }
54 template<> inline bool check_type<Value::FLOAT>(Value::Type t) { return t==Value::INTEGER || t==Value::FLOAT; }
55 template<> inline bool check_type<Value::BOOLEAN>(Value::Type t) { return t==Value::BOOLEAN; }
56 template<> inline bool check_type<Value::STRING>(Value::Type t) { return t==Value::STRING; }
57 template<> inline bool check_type<Value::ENUM>(Value::Type t) { return t==Value::ENUM; }
60 inline T Value::get() const
62 if(!check_type<TypeResolver<T>::type>(type))
63 throw TypeError("Type mismatch");
65 std::istringstream ss(data);
70 throw Exception("Invalid value");
76 inline std::string Value::get<std::string>() const
79 throw TypeError("Value is not a string");
84 inline const std::string &Value::get<const std::string&>() const
87 throw TypeError("Value is not a string");
91 } // namespace DataFile