]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/value.h
Cosmetic changes
[libs/datafile.git] / source / value.h
index 5ebbb5e1e52a101f475e7914089ed21b6febf051..bcddd6ed3aebb579256cd0a11061a76312a4a95c 100644 (file)
@@ -1,91 +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 <sstream>
-#include <string>
 #include <vector>
-#include "error.h"
+#include <msp/core/attributes.h>
+#include <msp/core/meta.h>
+#include <msp/core/variant.h>
+#include "type.h"
 
 namespace Msp {
 namespace DataFile {
 
 class Value
 {
-public:
-       enum Type
-       {
-               INTEGER,
-               FLOAT,
-               STRING,
-               BOOLEAN,
-               ENUM
-       };
-
-       Value(Type t, const std::string &d): type(t), data(d) { }
-       template<typename T>
-       T get() const;
 private:
-       Type type;
-       std::string data;
-};
-typedef std::vector<Value> ValueArray;
+       char sig;
+       Variant data;
 
-template<typename T> struct TypeResolver { };
+public:
+       template<typename T>
+       Value(T &&d):
+               sig(TypeInfo<T>::signature),
+               data(static_cast<typename TypeInfo<T>::Store>(std::forward<T>(d)))
+       { }
 
-template<> struct TypeResolver<short>          { static const Value::Type type=Value::INTEGER; };
-template<> struct TypeResolver<unsigned short> { static const Value::Type type=Value::INTEGER; };
-template<> struct TypeResolver<int>            { static const Value::Type type=Value::INTEGER; };
-template<> struct TypeResolver<unsigned>       { static const Value::Type type=Value::INTEGER; };
-template<> struct TypeResolver<long>           { static const Value::Type type=Value::INTEGER; };
-template<> struct TypeResolver<unsigned long>  { static const Value::Type type=Value::INTEGER; };
-template<> struct TypeResolver<float>          { static const Value::Type type=Value::FLOAT; };
-template<> struct TypeResolver<double>         { static const Value::Type type=Value::FLOAT; };
-template<> struct TypeResolver<bool>           { static const Value::Type type=Value::BOOLEAN; };
+       Value(const Symbol &d): sig(TypeInfo<Symbol>::signature), data(d) { }
+       Value(Symbol &&d): sig(TypeInfo<Symbol>::signature), data(std::move(d)) { }
 
-template<Value::Type T> inline bool check_type(Value::Type) { return false; }
+       template<typename T>
+       typename TypeInfo<T>::Load get() const
+       { return get_<typename TypeInfo<T>::Store>(); }
 
-template<> inline bool check_type<Value::INTEGER>(Value::Type t) { return t==Value::INTEGER; }
-template<> inline bool check_type<Value::FLOAT>(Value::Type t)   { return t==Value::INTEGER || t==Value::FLOAT; }
-template<> inline bool check_type<Value::BOOLEAN>(Value::Type t) { return t==Value::BOOLEAN; }
-template<> inline bool check_type<Value::STRING>(Value::Type t)  { return t==Value::STRING; }
-template<> inline bool check_type<Value::ENUM>(Value::Type t)    { return t==Value::ENUM; }
+       char get_signature() const { return sig; }
+private:
+       template<typename T>
+       T get_() const;
+};
 
 template<typename T>
-inline T Value::get() const
-{
-       if(!check_type<TypeResolver<T>::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<std::string>() const
+inline T Value::get_() const
 {
-       if(type!=STRING)
-               throw TypeError("Value is not a string");
-       return data;
+       return data.value<typename TypeInfo<T>::Store>();
 }
 
 template<>
-inline const std::string &Value::get<const std::string&>() const
+inline FloatType::Store Value::get_<FloatType::Store>() const
 {
-       if(type!=STRING)
-               throw TypeError("Value is not a string");
-       return data;
+       if(sig==IntType::signature)
+               return data.value<IntType::Store>();
+       else
+               return data.value<FloatType::Store>();
 }
 
 } // namespace DataFile