-/* $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
- };
+private:
+ char sig;
+ Variant data;
- Value(Type t, const std::string &d): type(t), data(d) { }
+public:
template<typename T>
- T get() const;
-private:
- Type type;
- std::string data;
-};
-typedef std::vector<Value> ValueArray;
+ Value(T d):
+ sig(TypeInfo<T>::signature),
+ data(static_cast<typename TypeInfo<T>::Store>(d))
+ { }
-template<typename T> struct TypeResolver { };
+ Value(Symbol d): sig(TypeInfo<Symbol>::signature), data(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; };
+ template<typename T>
+ typename TypeInfo<T>::Load get() const
+ { return get_<typename TypeInfo<T>::Store>(); }
-template<Value::Type T> inline bool check_type(Value::Type) { return false; }
+ char get_signature() const { return sig; }
+private:
+ template<typename T>
+ T get_() const;
+};
-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; }
+typedef DEPRECATED std::vector<Value> ValueArray;
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