X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fvalue.h;h=1b12db9a22db5f40e77578199bdfd7817fff4093;hb=a582163d380833b1370ba067a1fd0ad5c2984723;hp=f809c8fe4d3001cfba4660f45b9e9556a15d3ab8;hpb=de02b5618273df1b94085934f699371b4be31783;p=libs%2Fdatafile.git diff --git a/source/value.h b/source/value.h index f809c8f..1b12db9 100644 --- a/source/value.h +++ b/source/value.h @@ -1,91 +1,67 @@ /* $Id$ This file is part of libmspdatafile -Copyright © 2006 Mikko Rasa, Mikkosoft Productions +Copyright © 2006-2008, 2010 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ + #ifndef MSP_DATAFILE_VALUE_H_ #define MSP_DATAFILE_VALUE_H_ -#include -#include #include -#include "error.h" +#include +#include +#include "except.h" +#include "type.h" namespace Msp { namespace DataFile { class Value { +private: + char sig; + Variant data; + public: - enum Type - { - INTEGER, - FLOAT, - STRING, - BOOLEAN, - ENUM - }; - - Value(Type t, const std::string &d): type(t), data(d) { } 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 { static const Value::Type type=Value::ENUM; }; + 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 RemoveReference::Type 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 +inline T Value::get_() const { - if(!check_type::type>(type)) + if(sig!=TypeInfo::signature) throw TypeError("Type mismatch"); - std::istringstream ss(data); - T result; - ss>>result; - if(ss.fail()) - //XXX - throw Exception("Invalid value"); - - return result; + return data.value::Store>(); } template<> -inline 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 if(sig!=FloatType::signature) + throw TypeError("Type mismatch"); -template<> -inline const std::string &Value::get() const -{ - if(type!=STRING) - throw TypeError("Value is not a string"); - return data; + return data.value(); } } // namespace DataFile