X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fvalue.h;h=219b0e42dd5174cf8b5eabecf55df7f5b03b17d5;hb=7df5e45c7f414f6a07681dc4ec2abb63b091a309;hp=dc117e0a7ff03207e73be05748849c0a182f3e0b;hpb=5453824394771ca21de32088a2842486b63e6f6d;p=libs%2Fdatafile.git diff --git a/source/value.h b/source/value.h index dc117e0..219b0e4 100644 --- a/source/value.h +++ b/source/value.h @@ -1,47 +1,63 @@ -/* -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_ - -#include -#include +#ifndef MSP_DATAFILE_VALUE_H_ +#define MSP_DATAFILE_VALUE_H_ + #include +#include +#include +#include "except.h" +#include "type.h" namespace Msp { -namespace Parser { +namespace DataFile { class Value { +private: + char sig; + Variant data; + public: - enum Type - { - INTEGER, - FLOAT, - STRING, - BOOLEAN - }; - - 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; - } + Value(T d): + sig(TypeInfo::signature), + data(static_cast::Store>(d)) + { } + + Value(Symbol d): sig(TypeInfo::signature), data(d) { } + + template + typename RemoveReference::Type get() const + { return get_::Store>(); } + + char get_signature() const { return sig; } private: - Type type; - std::string data; + template + T get_() const; }; -typedef std::vector ValueArray; -} // namespace Parser +typedef std::vector ValueArray __attribute__((deprecated)); + +template +inline T Value::get_() const +{ + if(sig!=TypeInfo::signature) + throw TypeError("Type mismatch"); + + return data.value::Store>(); +} + +template<> +inline FloatType::Store Value::get_() const +{ + if(sig==IntType::signature) + return data.value(); + else if(sig!=FloatType::signature) + throw TypeError("Type mismatch"); + + return data.value(); +} + +} // namespace DataFile } // namespace Msp #endif