]> git.tdb.fi Git - libs/datafile.git/commitdiff
Type checking in value conversions
authorMikko Rasa <tdb@tdb.fi>
Wed, 23 Aug 2006 20:48:52 +0000 (20:48 +0000)
committerMikko Rasa <tdb@tdb.fi>
Wed, 23 Aug 2006 20:48:52 +0000 (20:48 +0000)
Support for enum types

source/constant.h
source/input.cpp
source/input.h
source/loader.h
source/parser.cpp
source/parser.h
source/statement.h
source/token.h
source/value.cpp [deleted file]
source/value.h

index 1faba71985afc27a4b21a7ef4cdb9f43cf25312d..0ff3e32a222283c69080b2b5d36b131a500626bc 100644 (file)
@@ -1,6 +1,6 @@
 /*
 This file is part of libmspparser
 /*
 This file is part of libmspparser
-Copyright © 2006 Mikko Rasa, Mikkosoft Productions
+Copyright © 2006  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 #ifndef MSP_PARSER_CONSTANT_H_
 Distributed under the LGPL
 */
 #ifndef MSP_PARSER_CONSTANT_H_
index 5b2d63b7ba8becdc20232a8933b815b3c5d5f288..0f23bb29b9797bff09fc738dc52be6e7aeddf8e7 100644 (file)
@@ -1,6 +1,6 @@
 /*
 This file is part of libmspparser
 /*
 This file is part of libmspparser
-Copyright © 2006 Mikko Rasa, Mikkosoft Productions
+Copyright © 2006  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 #include "input.h"
 Distributed under the LGPL
 */
 #include "input.h"
index 12d3eff08dff23156fe3f43a59037157e21aface..e1b2d87e0e9ea1b8e3d997012d1ef08ee1417da5 100644 (file)
@@ -1,6 +1,6 @@
 /*
 This file is part of libmspparser
 /*
 This file is part of libmspparser
-Copyright © 2006 Mikko Rasa, Mikkosoft Productions
+Copyright © 2006  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 #ifndef MSP_PARSER_INPUT_H_
 Distributed under the LGPL
 */
 #ifndef MSP_PARSER_INPUT_H_
index 5e8b2dfe0a1a5accfa94669ddd1fee5625fae3c1..46c878b09ef6a65c63fad2a266d28befa358f2ef 100644 (file)
@@ -1,6 +1,6 @@
 /*
 This file is part of libmspparser
 /*
 This file is part of libmspparser
-Copyright © 2006 Mikko Rasa, Mikkosoft Productions
+Copyright © 2006  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 #ifndef MSP_PARSER_LOADER_H_
 Distributed under the LGPL
 */
 #ifndef MSP_PARSER_LOADER_H_
index ef2daa3e0ab8bd5dbb75ce8696a64c789c8417ab..c9ad188868a0bed69087879472b2741b67e07f46 100644 (file)
@@ -1,6 +1,6 @@
 /*
 This file is part of libmspparser
 /*
 This file is part of libmspparser
-Copyright © 2006 Mikko Rasa, Mikkosoft Productions
+Copyright © 2006  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 #include <cctype>
 Distributed under the LGPL
 */
 #include <cctype>
@@ -96,6 +96,12 @@ Statement Parser::parse_(const Token *t)
                        result.args.push_back(Value(Value::STRING, token.str));
                else if(token.type==Token::IDENTIFIER)
                {
                        result.args.push_back(Value(Value::STRING, token.str));
                else if(token.type==Token::IDENTIFIER)
                {
+                       if(token.str=="true")
+                               result.args.push_back(Value(Value::BOOLEAN, "1"));
+                       else if(token.str=="false")
+                               result.args.push_back(Value(Value::BOOLEAN, "0"));
+                       else
+                               result.args.push_back(Value(Value::ENUM, token.str));
                        //result.args.push_back(resolve_identifiertoken.str);
                }
                else if(token.str=="")
                        //result.args.push_back(resolve_identifiertoken.str);
                }
                else if(token.str=="")
index 107bb3f9c53b83256e441f6ac6f4de5446e8658c..c8b7ecef4e72f1c168980ca3b130bdc7f5a4f713 100644 (file)
@@ -1,6 +1,6 @@
 /*
 This file is part of libmspparser
 /*
 This file is part of libmspparser
-Copyright © 2006 Mikko Rasa, Mikkosoft Productions
+Copyright © 2006  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 #ifndef MSP_PARSER_PARSER_H_
 Distributed under the LGPL
 */
 #ifndef MSP_PARSER_PARSER_H_
index 378ddabdb4ab1c8efc5d19a2853998197d703920..3c843bfeb0c74d06093f7c3051bb8ff44165bd37 100644 (file)
@@ -1,6 +1,6 @@
 /*
 This file is part of libmspparser
 /*
 This file is part of libmspparser
-Copyright © 2006 Mikko Rasa, Mikkosoft Productions
+Copyright © 2006  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 #ifndef MSP_PARSER_STATEMENT_H_
 Distributed under the LGPL
 */
 #ifndef MSP_PARSER_STATEMENT_H_
index 050817b153fb010567e2732889895a91b4a06bde..4e25f46f27d56cf06e0617d25fde6b23865e238f 100644 (file)
@@ -1,6 +1,6 @@
 /*
 This file is part of libmspparser
 /*
 This file is part of libmspparser
-Copyright © 2006 Mikko Rasa, Mikkosoft Productions
+Copyright © 2006  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 #ifndef MSP_PARSER_TOKEN_H_
 Distributed under the LGPL
 */
 #ifndef MSP_PARSER_TOKEN_H_
diff --git a/source/value.cpp b/source/value.cpp
deleted file mode 100644 (file)
index 6d5e032..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
-This file is part of libmspparser
-Copyright © 2006 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-#include <msp/error.h>
-#include "value.h"
-
-namespace Msp {
-namespace Parser {
-
-template<>
-std::string Value::get<std::string>() const
-{
-       if(type!=STRING)
-               throw TypeError("Value is not a string");
-       return data;
-}
-
-template<>
-const std::string &Value::get<const std::string&>() const
-{
-       if(type!=STRING)
-               throw TypeError("Value is not a string");
-       return data;
-}
-
-} // namespace Msp
-} // namespace Parser
index dc117e0a7ff03207e73be05748849c0a182f3e0b..be6a7a483f7768849f845051dbe563e9cea97ac9 100644 (file)
@@ -1,6 +1,6 @@
 /*
 This file is part of libmspparser
 /*
 This file is part of libmspparser
-Copyright © 2006 Mikko Rasa, Mikkosoft Productions
+Copyright © 2006  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 #ifndef MSP_PARSER_VALUE_H_
 Distributed under the LGPL
 */
 #ifndef MSP_PARSER_VALUE_H_
@@ -21,26 +21,70 @@ public:
                INTEGER,
                FLOAT,
                STRING,
                INTEGER,
                FLOAT,
                STRING,
-               BOOLEAN
+               BOOLEAN,
+               ENUM
        };
 
        Value(Type t, const std::string &d): type(t), data(d) { }
        template<typename T>
        };
 
        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;
-       }
+       T get() const;
 private:
        Type type;
        std::string data;
 };
 typedef std::vector<Value> ValueArray;
 
 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; }
+
+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<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())
+               throw ValueError("Invalid value");
+
+       return result;
+}
+
+template<>
+inline std::string Value::get<std::string>() const
+{
+       if(type!=STRING)
+               throw TypeError("Value is not a string");
+       return data;
+}
+
+template<>
+inline const std::string &Value::get<const std::string&>() const
+{
+       if(type!=STRING)
+               throw TypeError("Value is not a string");
+       return data;
+}
+
 } // namespace Parser
 } // namespace Msp
 
 } // namespace Parser
 } // namespace Msp