X-Git-Url: http://git.tdb.fi/?p=libs%2Fdatafile.git;a=blobdiff_plain;f=source%2Fvalue.h;h=fda83e494db884a228036fafdf3f6ba5c3ec9439;hp=f809c8fe4d3001cfba4660f45b9e9556a15d3ab8;hb=27630d44298cb67e075c166f4421288cc8ca117e;hpb=c4930d8d15a5a248ca921e0ed3f9bca8aa18b322 diff --git a/source/value.h b/source/value.h index f809c8f..fda83e4 100644 --- a/source/value.h +++ b/source/value.h @@ -7,54 +7,61 @@ Distributed under the LGPL #ifndef MSP_DATAFILE_VALUE_H_ #define MSP_DATAFILE_VALUE_H_ -#include -#include #include +#include #include "error.h" namespace Msp { namespace DataFile { +enum Type +{ + INTEGER, + FLOAT, + STRING, + BOOLEAN, + ENUM +}; + +template struct TypeResolver { static const Type type=ENUM; }; +template<> struct TypeResolver { static const Type type=INTEGER; }; +template<> struct TypeResolver { static const Type type=INTEGER; }; +template<> struct TypeResolver { static const Type type=INTEGER; }; +template<> struct TypeResolver { static const Type type=INTEGER; }; +template<> struct TypeResolver { static const Type type=INTEGER; }; +template<> struct TypeResolver { static const Type type=INTEGER; }; +template<> struct TypeResolver { static const Type type=INTEGER; }; +template<> struct TypeResolver { static const Type type=INTEGER; }; +template<> struct TypeResolver { static const Type type=FLOAT; }; +template<> struct TypeResolver { static const Type type=FLOAT; }; +template<> struct TypeResolver { static const Type type=BOOLEAN; }; +template<> struct TypeResolver { static const Type type=STRING; }; +template struct TypeResolver { static const Type type=TypeResolver::type; }; +template struct TypeResolver { static const Type type=TypeResolver::type; }; +template struct TypeResolver { static const Type type=TypeResolver::type; }; + class Value { public: - enum Type - { - INTEGER, - FLOAT, - STRING, - BOOLEAN, - ENUM - }; - Value(Type t, const std::string &d): type(t), data(d) { } + + template + Value(T d): type(TypeResolver::type), data(lexical_cast(d)) { } + template T get() const; + + Type get_type() const { return type; } + const std::string &get_raw() const { return data; } private: Type type; std::string data; }; -typedef std::vector ValueArray; - -template struct TypeResolver { static const Value::Type type=Value::ENUM; }; - -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; } +typedef std::vector ValueArray; -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 bool check_type(Type t) { return t==T; } +template<> inline bool check_type(Type t) { return t==INTEGER || t==FLOAT; } template inline T Value::get() const @@ -76,7 +83,7 @@ template<> inline std::string Value::get() const { if(type!=STRING) - throw TypeError("Value is not a string"); + throw TypeError("Type mismatch"); return data; } @@ -84,7 +91,7 @@ template<> inline const std::string &Value::get() const { if(type!=STRING) - throw TypeError("Value is not a string"); + throw TypeError("Type mismatch"); return data; }