X-Git-Url: http://git.tdb.fi/?p=libs%2Fdatafile.git;a=blobdiff_plain;f=source%2Fvalue.h;h=be6a7a483f7768849f845051dbe563e9cea97ac9;hp=dc117e0a7ff03207e73be05748849c0a182f3e0b;hb=98f563736e0837a429714b98656215503c607710;hpb=52b69f7841d3b2ca0bb83c04214f30c7d1c6a615 diff --git a/source/value.h b/source/value.h index dc117e0..be6a7a4 100644 --- a/source/value.h +++ b/source/value.h @@ -1,6 +1,6 @@ /* This file is part of libmspparser -Copyright © 2006 Mikko Rasa, Mikkosoft Productions +Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ #ifndef MSP_PARSER_VALUE_H_ @@ -21,26 +21,70 @@ public: INTEGER, FLOAT, STRING, - BOOLEAN + BOOLEAN, + ENUM }; Value(Type t, const std::string &d): type(t), data(d) { } template - T get() const - { - std::istringstream ss(data); - T result; - ss>>result; - if(ss.fail()) - throw TypeError("Type mismatch"); - return result; - } + T get() const; private: Type type; std::string data; }; typedef std::vector ValueArray; +template struct TypeResolver { }; + +template<> struct TypeResolver { static const Value::Type type=Value::INTEGER; }; +template<> struct TypeResolver { static const Value::Type type=Value::INTEGER; }; +template<> struct TypeResolver { static const Value::Type type=Value::INTEGER; }; +template<> struct TypeResolver { static const Value::Type type=Value::INTEGER; }; +template<> struct TypeResolver { static const Value::Type type=Value::INTEGER; }; +template<> struct TypeResolver { static const Value::Type type=Value::INTEGER; }; +template<> struct TypeResolver { static const Value::Type type=Value::FLOAT; }; +template<> struct TypeResolver { static const Value::Type type=Value::FLOAT; }; +template<> struct TypeResolver { static const Value::Type type=Value::BOOLEAN; }; + +template inline bool check_type(Value::Type) { return false; } + +template<> inline bool check_type(Value::Type t) { return t==Value::INTEGER; } +template<> inline bool check_type(Value::Type t) { return t==Value::INTEGER || t==Value::FLOAT; } +template<> inline bool check_type(Value::Type t) { return t==Value::BOOLEAN; } +template<> inline bool check_type(Value::Type t) { return t==Value::STRING; } +template<> inline bool check_type(Value::Type t) { return t==Value::ENUM; } + +template +inline T Value::get() const +{ + if(!check_type::type>(type)) + throw TypeError("Type mismatch"); + + std::istringstream ss(data); + T result; + ss>>result; + if(ss.fail()) + throw ValueError("Invalid value"); + + return result; +} + +template<> +inline std::string Value::get() const +{ + if(type!=STRING) + throw TypeError("Value is not a string"); + return data; +} + +template<> +inline const std::string &Value::get() const +{ + if(type!=STRING) + throw TypeError("Value is not a string"); + return data; +} + } // namespace Parser } // namespace Msp