]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/value.h
Cosmetic changes
[libs/datafile.git] / source / value.h
index dc117e0a7ff03207e73be05748849c0a182f3e0b..bcddd6ed3aebb579256cd0a11061a76312a4a95c 100644 (file)
@@ -1,47 +1,57 @@
-/*
-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 <sstream>
-#include <string>
+#ifndef MSP_DATAFILE_VALUE_H_
+#define MSP_DATAFILE_VALUE_H_
+
 #include <vector>
+#include <msp/core/attributes.h>
+#include <msp/core/meta.h>
+#include <msp/core/variant.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<typename T>
-       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<T>::signature),
+               data(static_cast<typename TypeInfo<T>::Store>(std::forward<T>(d)))
+       { }
+
+       Value(const Symbol &d): sig(TypeInfo<Symbol>::signature), data(d) { }
+       Value(Symbol &&d): sig(TypeInfo<Symbol>::signature), data(std::move(d)) { }
+
+       template<typename T>
+       typename TypeInfo<T>::Load get() const
+       { return get_<typename TypeInfo<T>::Store>(); }
+
+       char get_signature() const { return sig; }
 private:
-       Type type;
-       std::string data;
+       template<typename T>
+       T get_() const;
 };
-typedef std::vector<Value> ValueArray;
 
-} // namespace Parser
+template<typename T>
+inline T Value::get_() const
+{
+       return data.value<typename TypeInfo<T>::Store>();
+}
+
+template<>
+inline FloatType::Store Value::get_<FloatType::Store>() const
+{
+       if(sig==IntType::signature)
+               return data.value<IntType::Store>();
+       else
+               return data.value<FloatType::Store>();
+}
+
+} // namespace DataFile
 } // namespace Msp
 
 #endif