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_
11 #include <msp/strings/lexicalcast.h>
26 template<typename T> struct TypeResolver { static const Type type=ENUM; };
27 template<> struct TypeResolver<short> { static const Type type=INTEGER; };
28 template<> struct TypeResolver<unsigned short> { static const Type type=INTEGER; };
29 template<> struct TypeResolver<int> { static const Type type=INTEGER; };
30 template<> struct TypeResolver<unsigned> { static const Type type=INTEGER; };
31 template<> struct TypeResolver<long> { static const Type type=INTEGER; };
32 template<> struct TypeResolver<unsigned long> { static const Type type=INTEGER; };
33 template<> struct TypeResolver<long long> { static const Type type=INTEGER; };
34 template<> struct TypeResolver<unsigned long long> { static const Type type=INTEGER; };
35 template<> struct TypeResolver<float> { static const Type type=FLOAT; };
36 template<> struct TypeResolver<double> { static const Type type=FLOAT; };
37 template<> struct TypeResolver<bool> { static const Type type=BOOLEAN; };
38 template<> struct TypeResolver<std::string> { static const Type type=STRING; };
39 template<typename T> struct TypeResolver<const T> { static const Type type=TypeResolver<T>::type; };
40 template<typename T> struct TypeResolver<T &> { static const Type type=TypeResolver<T>::type; };
41 template<typename T> struct TypeResolver<const T &> { static const Type type=TypeResolver<T>::type; };
46 Value(Type t, const std::string &d): type(t), data(d) { }
49 Value(T d): type(TypeResolver<T>::type), data(lexical_cast(d)) { }
54 Type get_type() const { return type; }
55 const std::string &get_raw() const { return data; }
61 typedef std::vector<Value> ValueArray;
63 template<Type T> inline bool check_type(Type t) { return t==T; }
64 template<> inline bool check_type<FLOAT>(Type t) { return t==INTEGER || t==FLOAT; }
67 inline T Value::get() const
69 if(!check_type<TypeResolver<T>::type>(type))
70 throw TypeError("Type mismatch");
72 std::istringstream ss(data);
77 throw Exception("Invalid value");
83 inline std::string Value::get<std::string>() const
86 throw TypeError("Type mismatch");
91 inline const std::string &Value::get<const std::string&>() const
94 throw TypeError("Type mismatch");
98 } // namespace DataFile