-/*
-This file is part of libmspparser
+/* $Id$
+
+This file is part of libmspdatafile
Copyright © 2006 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
-#ifndef MSP_PARSER_VALUE_H_
-#define MSP_PARSER_VALUE_H_
+#ifndef MSP_DATAFILE_VALUE_H_
+#define MSP_DATAFILE_VALUE_H_
-#include <sstream>
-#include <string>
#include <vector>
-#include "error.h"
+#include <msp/strings/lexicalcast.h>
+#include "except.h"
namespace Msp {
-namespace Parser {
+namespace DataFile {
+
+enum Type
+{
+ INTEGER,
+ FLOAT,
+ STRING,
+ BOOLEAN,
+ ENUM
+};
+
+template<typename T> struct TypeResolver { static const Type type=ENUM; };
+template<> struct TypeResolver<short> { static const Type type=INTEGER; };
+template<> struct TypeResolver<unsigned short> { static const Type type=INTEGER; };
+template<> struct TypeResolver<int> { static const Type type=INTEGER; };
+template<> struct TypeResolver<unsigned> { static const Type type=INTEGER; };
+template<> struct TypeResolver<long> { static const Type type=INTEGER; };
+template<> struct TypeResolver<unsigned long> { static const Type type=INTEGER; };
+template<> struct TypeResolver<long long> { static const Type type=INTEGER; };
+template<> struct TypeResolver<unsigned long long> { static const Type type=INTEGER; };
+template<> struct TypeResolver<float> { static const Type type=FLOAT; };
+template<> struct TypeResolver<double> { static const Type type=FLOAT; };
+template<> struct TypeResolver<bool> { static const Type type=BOOLEAN; };
+template<> struct TypeResolver<std::string> { static const Type type=STRING; };
+template<typename T> struct TypeResolver<const T> { static const Type type=TypeResolver<T>::type; };
+template<typename T> struct TypeResolver<T &> { static const Type type=TypeResolver<T>::type; };
+template<typename T> struct TypeResolver<const T &> { static const Type type=TypeResolver<T>::type; };
class Value
{
public:
- enum Type
- {
- INTEGER,
- FLOAT,
- STRING,
- BOOLEAN,
- ENUM
- };
-
Value(Type t, const std::string &d): type(t), data(d) { }
+
+ template<typename T>
+ Value(T d): type(TypeResolver<T>::type), data(lexical_cast(d)) { }
+
template<typename T>
T get() const;
+
+ Type get_type() const { return type; }
+ const std::string &get_raw() const { return data; }
private:
Type type;
std::string data;
};
-typedef std::vector<Value> ValueArray;
-
-template<typename T> struct TypeResolver { };
-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<Value::Type T> inline bool check_type(Value::Type) { return false; }
+typedef std::vector<Value> ValueArray;
-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; }
+template<Type T> inline bool check_type(Type t) { return t==T; }
+template<> inline bool check_type<FLOAT>(Type t) { return t==INTEGER || t==FLOAT; }
template<typename T>
inline T Value::get() const
inline std::string Value::get<std::string>() const
{
if(type!=STRING)
- throw TypeError("Value is not a string");
+ throw TypeError("Type mismatch");
return data;
}
inline const std::string &Value::get<const std::string&>() const
{
if(type!=STRING)
- throw TypeError("Value is not a string");
+ throw TypeError("Type mismatch");
return data;
}
-} // namespace Parser
+} // namespace DataFile
} // namespace Msp
#endif