X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Fvalue.h;h=f809c8fe4d3001cfba4660f45b9e9556a15d3ab8;hb=3e2ee845788985e552d2a09f4c524e08fada1fdf;hp=dc117e0a7ff03207e73be05748849c0a182f3e0b;hpb=5453824394771ca21de32088a2842486b63e6f6d;p=libs%2Fdatafile.git diff --git a/source/value.h b/source/value.h index dc117e0..f809c8f 100644 --- a/source/value.h +++ b/source/value.h @@ -1,17 +1,19 @@ -/* -This file is part of libmspparser -Copyright © 2006 Mikko Rasa, Mikkosoft Productions +/* $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 "error.h" namespace Msp { -namespace Parser { +namespace DataFile { class Value { @@ -21,27 +23,72 @@ 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; -} // namespace Parser +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; } + +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()) + //XXX + throw Exception("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 DataFile } // namespace Msp #endif