X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fvalue.h;h=e2b9fb1c569782fcbc96dc9d687d9b48eb83388d;hb=b39ce68f12c30eedb272b65fe78baec5864d89ca;hp=4b3ac4192b400f58388716d5baeaee7f243cb759;hpb=aa8bfd3c13848dc27947d3947038bd66c258d288;p=libs%2Fdatafile.git diff --git a/source/value.h b/source/value.h index 4b3ac41..e2b9fb1 100644 --- a/source/value.h +++ b/source/value.h @@ -1,98 +1,54 @@ -/* $Id$ - -This file is part of libmspdatafile -Copyright © 2006 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ #ifndef MSP_DATAFILE_VALUE_H_ #define MSP_DATAFILE_VALUE_H_ #include -#include -#include "except.h" +#include +#include +#include "type.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: - Value(Type t, const std::string &d): type(t), data(d) { } +private: + char sig; + Variant data; +public: template - Value(T d): type(TypeResolver::type), data(lexical_cast(d)) { } + Value(T d): + sig(TypeInfo::signature), + data(static_cast::Store>(d)) + { } + + Value(Symbol d): sig(TypeInfo::signature), data(d) { } template - T get() const; + typename TypeInfo::Load get() const + { return get_::Store>(); } - Type get_type() const { return type; } - const std::string &get_raw() const { return data; } + char get_signature() const { return sig; } private: - Type type; - std::string data; + template + T get_() const; }; -typedef std::vector ValueArray; - -template inline bool check_type(Type t) { return t==T; } -template<> inline bool check_type(Type t) { return t==INTEGER || t==FLOAT; } +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("Type mismatch"); - 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("Type mismatch"); - return data; + if(sig==IntType::signature) + return data.value(); + else + return data.value(); } } // namespace DataFile