X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fvalue.h;h=e2b9fb1c569782fcbc96dc9d687d9b48eb83388d;hb=4edbe0eb078c4e480682862ccb68ebc8cb284045;hp=5fc3540f763ee9a2a8d4de12f623d5b2f77eccb3;hpb=ec2bce20b19a20b4ebc17e4cb82a0bfbcda15c50;p=libs%2Fdatafile.git diff --git a/source/value.h b/source/value.h index 5fc3540..e2b9fb1 100644 --- a/source/value.h +++ b/source/value.h @@ -1,93 +1,57 @@ -/* -This file is part of libmspparser -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" +#include +#include +#include "type.h" namespace Msp { -namespace Parser { +namespace DataFile { class Value { -public: - enum Type - { - INTEGER, - FLOAT, - STRING, - BOOLEAN, - ENUM - }; +private: + char sig; + Variant data; - Value(Type t, const std::string &d): type(t), data(d) { } +public: template - T get() const; -private: - Type type; - std::string data; -}; -typedef std::vector ValueArray; + Value(T d): + sig(TypeInfo::signature), + data(static_cast::Store>(d)) + { } -template struct TypeResolver { }; + Value(Symbol d): sig(TypeInfo::signature), data(d) { } -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 + typename TypeInfo::Load get() const + { return get_::Store>(); } -template inline bool check_type(Value::Type) { return false; } + char get_signature() const { return sig; } +private: + template + T get_() const; +}; -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; } +typedef std::vector ValueArray __attribute__((deprecated)); 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 +inline T Value::get_() const { - if(type!=STRING) - throw TypeError("Value is not a string"); - return data; + return data.value::Store>(); } template<> -inline const std::string &Value::get() const +inline FloatType::Store Value::get_() const { - if(type!=STRING) - throw TypeError("Value is not a string"); - return data; + if(sig==IntType::signature) + return data.value(); + else + return data.value(); } -} // namespace Parser +} // namespace DataFile } // namespace Msp #endif