From: Mikko Rasa Date: Wed, 23 Aug 2006 20:48:52 +0000 (+0000) Subject: Type checking in value conversions X-Git-Tag: 1.0~41 X-Git-Url: http://git.tdb.fi/?p=libs%2Fdatafile.git;a=commitdiff_plain;h=98f563736e0837a429714b98656215503c607710 Type checking in value conversions Support for enum types --- diff --git a/source/constant.h b/source/constant.h index 1faba71..0ff3e32 100644 --- a/source/constant.h +++ b/source/constant.h @@ -1,6 +1,6 @@ /* 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_ diff --git a/source/input.cpp b/source/input.cpp index 5b2d63b..0f23bb2 100644 --- a/source/input.cpp +++ b/source/input.cpp @@ -1,6 +1,6 @@ /* 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" diff --git a/source/input.h b/source/input.h index 12d3eff..e1b2d87 100644 --- a/source/input.h +++ b/source/input.h @@ -1,6 +1,6 @@ /* 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_ diff --git a/source/loader.h b/source/loader.h index 5e8b2df..46c878b 100644 --- a/source/loader.h +++ b/source/loader.h @@ -1,6 +1,6 @@ /* 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_ diff --git a/source/parser.cpp b/source/parser.cpp index ef2daa3..c9ad188 100644 --- a/source/parser.cpp +++ b/source/parser.cpp @@ -1,6 +1,6 @@ /* This file is part of libmspparser -Copyright © 2006 Mikko Rasa, Mikkosoft Productions +Copyright © 2006 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ #include @@ -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) { + 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=="") diff --git a/source/parser.h b/source/parser.h index 107bb3f..c8b7ece 100644 --- a/source/parser.h +++ b/source/parser.h @@ -1,6 +1,6 @@ /* 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_ diff --git a/source/statement.h b/source/statement.h index 378ddab..3c843bf 100644 --- a/source/statement.h +++ b/source/statement.h @@ -1,6 +1,6 @@ /* 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_ diff --git a/source/token.h b/source/token.h index 050817b..4e25f46 100644 --- a/source/token.h +++ b/source/token.h @@ -1,6 +1,6 @@ /* 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_ diff --git a/source/value.cpp b/source/value.cpp deleted file mode 100644 index 6d5e032..0000000 --- a/source/value.cpp +++ /dev/null @@ -1,29 +0,0 @@ -/* -This file is part of libmspparser -Copyright © 2006 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ -#include -#include "value.h" - -namespace Msp { -namespace Parser { - -template<> -std::string Value::get() const -{ - if(type!=STRING) - throw TypeError("Value is not a string"); - return data; -} - -template<> -const std::string &Value::get() const -{ - if(type!=STRING) - throw TypeError("Value is not a string"); - return data; -} - -} // namespace Msp -} // namespace Parser diff --git a/source/value.h b/source/value.h index dc117e0..be6a7a4 100644 --- a/source/value.h +++ b/source/value.h @@ -1,6 +1,6 @@ /* 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_ @@ -21,26 +21,70 @@ public: INTEGER, FLOAT, STRING, - BOOLEAN + BOOLEAN, + ENUM }; Value(Type t, const std::string &d): type(t), data(d) { } template - 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 ValueArray; +template struct TypeResolver { }; + +template<> struct TypeResolver { static const Value::Type type=Value::INTEGER; }; +template<> struct TypeResolver { static const Value::Type type=Value::INTEGER; }; +template<> struct TypeResolver { static const Value::Type type=Value::INTEGER; }; +template<> struct TypeResolver { static const Value::Type type=Value::INTEGER; }; +template<> struct TypeResolver { static const Value::Type type=Value::INTEGER; }; +template<> struct TypeResolver { static const Value::Type type=Value::INTEGER; }; +template<> struct TypeResolver { static const Value::Type type=Value::FLOAT; }; +template<> struct TypeResolver { static const Value::Type type=Value::FLOAT; }; +template<> struct TypeResolver { static const Value::Type type=Value::BOOLEAN; }; + +template inline bool check_type(Value::Type) { return false; } + +template<> inline bool check_type(Value::Type t) { return t==Value::INTEGER; } +template<> inline bool check_type(Value::Type t) { return t==Value::INTEGER || t==Value::FLOAT; } +template<> inline bool check_type(Value::Type t) { return t==Value::BOOLEAN; } +template<> inline bool check_type(Value::Type t) { return t==Value::STRING; } +template<> inline bool check_type(Value::Type t) { return t==Value::ENUM; } + +template +inline T Value::get() const +{ + if(!check_type::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() const +{ + if(type!=STRING) + throw TypeError("Value is not a string"); + return data; +} + +template<> +inline const std::string &Value::get() const +{ + if(type!=STRING) + throw TypeError("Value is not a string"); + return data; +} + } // namespace Parser } // namespace Msp