]> git.tdb.fi Git - libs/datafile.git/blob - source/value.h
Remove old build info
[libs/datafile.git] / source / value.h
1 /*
2 This file is part of libmspparser
3 Copyright © 2006  Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
5 */
6 #ifndef MSP_PARSER_VALUE_H_
7 #define MSP_PARSER_VALUE_H_
8
9 #include <sstream>
10 #include <string>
11 #include <vector>
12 #include "error.h"
13
14 namespace Msp {
15 namespace Parser {
16
17 class Value
18 {
19 public:
20         enum Type
21         {
22                 INTEGER,
23                 FLOAT,
24                 STRING,
25                 BOOLEAN,
26                 ENUM
27         };
28
29         Value(Type t, const std::string &d): type(t), data(d) { }
30         template<typename T>
31         T get() const;
32 private:
33         Type type;
34         std::string data;
35 };
36 typedef std::vector<Value> ValueArray;
37
38 template<typename T> struct TypeResolver { };
39
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; };
49
50 template<Value::Type T> inline bool check_type(Value::Type) { return false; }
51
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; }
57
58 template<typename T>
59 inline T Value::get() const
60 {
61         if(!check_type<TypeResolver<T>::type>(type))
62                 throw TypeError("Type mismatch");
63
64         std::istringstream ss(data);
65         T result;
66         ss>>result;
67         if(ss.fail())
68                 //XXX
69                 throw Exception("Invalid value");
70
71         return result;
72 }
73
74 template<>
75 inline std::string Value::get<std::string>() const
76 {
77         if(type!=STRING)
78                 throw TypeError("Value is not a string");
79         return data;
80 }
81
82 template<>
83 inline const std::string &Value::get<const std::string&>() const
84 {
85         if(type!=STRING)
86                 throw TypeError("Value is not a string");
87         return data;
88 }
89
90 } // namespace Parser
91 } // namespace Msp
92
93 #endif