X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fvalue.h;h=ace18945cbc2ea39eb4bc395cc116c0016b8e7e0;hb=9527722cbf433e25dcdf9210271af876a85910c7;hp=be6a7a483f7768849f845051dbe563e9cea97ac9;hpb=98f563736e0837a429714b98656215503c607710;p=libs%2Fdatafile.git diff --git a/source/value.h b/source/value.h index be6a7a4..ace1894 100644 --- a/source/value.h +++ b/source/value.h @@ -1,58 +1,67 @@ -/* -This file is part of libmspparser +/* $Id$ + +This file is part of libmspdatafile Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ -#ifndef MSP_PARSER_VALUE_H_ -#define MSP_PARSER_VALUE_H_ +#ifndef MSP_DATAFILE_VALUE_H_ +#define MSP_DATAFILE_VALUE_H_ -#include -#include #include +#include +#include "except.h" namespace Msp { -namespace Parser { +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 { }; - -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 @@ -60,20 +69,14 @@ 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; + return lexical_cast(data); } template<> inline std::string Value::get() const { if(type!=STRING) - throw TypeError("Value is not a string"); + throw TypeError("Type mismatch"); return data; } @@ -81,11 +84,11 @@ template<> inline const std::string &Value::get() const { if(type!=STRING) - throw TypeError("Value is not a string"); + throw TypeError("Type mismatch"); return data; } -} // namespace Parser +} // namespace DataFile } // namespace Msp #endif