From 7ea3632dfcd241f7572884e2dac240260a6fdb69 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 19 Sep 2007 15:29:57 +0000 Subject: [PATCH] Added support for bool in ArgumentParser::Parameter. Added support for using Input::read(...) methods using ArgumentParser::Paramter to adapter to multiple paramter types. --- include/osg/ArgumentParser | 7 ++ include/osgDB/Input | 13 ++ src/osg/ArgumentParser.cpp | 15 +++ src/osgDB/Input.cpp | 161 +++++++++++++++++++++++++ src/osgWrappers/osg/ArgumentParser.cpp | 10 ++ src/osgWrappers/osgDB/Input.cpp | 47 ++++++++ 6 files changed, 253 insertions(+) diff --git a/include/osg/ArgumentParser b/include/osg/ArgumentParser index 67ac7d250..9548dae08 100644 --- a/include/osg/ArgumentParser +++ b/include/osg/ArgumentParser @@ -33,6 +33,7 @@ class OSG_EXPORT ArgumentParser public: enum ParameterType { + BOOL_PARAMETER, FLOAT_PARAMETER, DOUBLE_PARAMETER, INT_PARAMETER, @@ -42,6 +43,7 @@ class OSG_EXPORT ArgumentParser union ValueUnion { + bool* _bool; float* _float; double* _double; int* _int; @@ -49,6 +51,8 @@ class OSG_EXPORT ArgumentParser std::string* _string; }; + Parameter(bool& value) { _type = BOOL_PARAMETER; _value._bool = &value; } + Parameter(float& value) { _type = FLOAT_PARAMETER; _value._float = &value; } Parameter(double& value) { _type = DOUBLE_PARAMETER; _value._double = &value; } @@ -83,6 +87,9 @@ class OSG_EXPORT ArgumentParser /** Return true if specified parameter is a number. */ static bool isNumber(const char* str); + /** Return true if specified parameter is a bool. */ + static bool isBool(const char* str); + public: ArgumentParser(int* argc,char **argv); diff --git a/include/osgDB/Input b/include/osgDB/Input index 9d10f85b8..f74e4dea5 100644 --- a/include/osgDB/Input +++ b/include/osgDB/Input @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -57,6 +58,18 @@ class OSGDB_EXPORT Input : public FieldReaderIterator virtual osg::Object* getObjectForUniqueID(const std::string& uniqueID); virtual void registerUniqueIDForObject(const std::string& uniqueID,osg::Object* obj); + typedef osg::ArgumentParser::Parameter Parameter; + + bool read(const char* str); + bool read(const char* str, Parameter value1); + bool read(const char* str, Parameter value1, Parameter value2); + bool read(const char* str, Parameter value1, Parameter value2, Parameter value3); + bool read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4); + bool read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5); + bool read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6); + bool read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7); + bool read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7, Parameter value8); + private: typedef std::map< std::string, osg::ref_ptr > UniqueIDToObjectMapping; diff --git a/src/osg/ArgumentParser.cpp b/src/osg/ArgumentParser.cpp index 1adbad44c..51c31e756 100644 --- a/src/osg/ArgumentParser.cpp +++ b/src/osg/ArgumentParser.cpp @@ -35,6 +35,15 @@ bool ArgumentParser::isString(const char* str) //return !isOption(str); } +bool ArgumentParser::isBool(const char* str) +{ + if (!str) return false; + + return (strcmp(str,"True")==0 || strcmp(str,"true")==0 || strcmp(str,"TRUE")==0 || + strcmp(str,"False")==0 || strcmp(str,"false")==0 || strcmp(str,"FALSE")==0 || + strcmp(str,"0")==0 || strcmp(str,"1")==0); +} + bool ArgumentParser::isNumber(const char* str) { if (!str) return false; @@ -133,6 +142,7 @@ bool ArgumentParser::Parameter::valid(const char* str) const { switch(_type) { + case Parameter::BOOL_PARAMETER: return isBool(str); break; case Parameter::FLOAT_PARAMETER: return isNumber(str); break; case Parameter::DOUBLE_PARAMETER: return isNumber(str); break; case Parameter::INT_PARAMETER: return isNumber(str); break; @@ -148,6 +158,11 @@ bool ArgumentParser::Parameter::assign(const char* str) { switch(_type) { + case Parameter::BOOL_PARAMETER: + { + *_value._bool = (strcmp(str,"True")==0 || strcmp(str,"true")==0 || strcmp(str,"TRUE")==0); + break; + } case Parameter::FLOAT_PARAMETER: *_value._float = atof(str); break; case Parameter::DOUBLE_PARAMETER: *_value._double = atof(str); break; case Parameter::INT_PARAMETER: *_value._int = atoi(str); break; diff --git a/src/osgDB/Input.cpp b/src/osgDB/Input.cpp index 696f97b7b..878acf474 100644 --- a/src/osgDB/Input.cpp +++ b/src/osgDB/Input.cpp @@ -97,3 +97,164 @@ osg::Node* Input::readNode(const std::string& fileName) { return readNodeFile(fileName,_options.get()); } + +bool Input::read(const char* str) +{ + if ((*this)[0].matchWord(str)) + { + (*this) += 1; + return true; + } + else return false; +} + +bool Input::read(const char* str, Parameter value1) +{ + if ((*this)[0].matchWord(str) && value1.valid((*this)[1].getStr())) + { + value1.assign((*this)[1].getStr()); + (*this) += 2; + return true; + } + else return false; +} + +bool Input::read(const char* str, Parameter value1, Parameter value2) +{ + if ((*this)[0].matchWord(str) && + value1.valid((*this)[1].getStr()) && + value2.valid((*this)[2].getStr())) + { + value1.assign((*this)[1].getStr()); + value2.assign((*this)[2].getStr()); + (*this) += 3; + return true; + } + else return false; +} + +bool Input::read(const char* str, Parameter value1, Parameter value2, Parameter value3) +{ + if ((*this)[0].matchWord(str) && + value1.valid((*this)[1].getStr()) && + value2.valid((*this)[2].getStr()) && + value3.valid((*this)[3].getStr())) + { + value1.assign((*this)[1].getStr()); + value2.assign((*this)[2].getStr()); + value3.assign((*this)[3].getStr()); + (*this) += 4; + return true; + } + else return false; +} + +bool Input::read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4) +{ + if ((*this)[0].matchWord(str) && + value1.valid((*this)[1].getStr()) && + value2.valid((*this)[2].getStr()) && + value3.valid((*this)[3].getStr()) && + value4.valid((*this)[4].getStr())) + { + value1.assign((*this)[1].getStr()); + value2.assign((*this)[2].getStr()); + value3.assign((*this)[3].getStr()); + value4.assign((*this)[4].getStr()); + (*this) += 5; + return true; + } + else return false; +} + +bool Input::read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5) +{ + if ((*this)[0].matchWord(str) && + value1.valid((*this)[1].getStr()) && + value2.valid((*this)[2].getStr()) && + value3.valid((*this)[3].getStr()) && + value4.valid((*this)[4].getStr()) && + value5.valid((*this)[5].getStr())) + { + value1.assign((*this)[1].getStr()); + value2.assign((*this)[2].getStr()); + value3.assign((*this)[3].getStr()); + value4.assign((*this)[4].getStr()); + value5.assign((*this)[5].getStr()); + (*this) += 6; + return true; + } + else return false; +} + +bool Input::read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6) +{ + if ((*this)[0].matchWord(str) && + value1.valid((*this)[1].getStr()) && + value2.valid((*this)[2].getStr()) && + value3.valid((*this)[3].getStr()) && + value4.valid((*this)[4].getStr()) && + value5.valid((*this)[5].getStr()) && + value6.valid((*this)[6].getStr())) + { + value1.assign((*this)[1].getStr()); + value2.assign((*this)[2].getStr()); + value3.assign((*this)[3].getStr()); + value4.assign((*this)[4].getStr()); + value5.assign((*this)[5].getStr()); + value6.assign((*this)[6].getStr()); + (*this) += 7; + return true; + } + else return false; +} + +bool Input::read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7) +{ + if ((*this)[0].matchWord(str) && + value1.valid((*this)[1].getStr()) && + value2.valid((*this)[2].getStr()) && + value3.valid((*this)[3].getStr()) && + value4.valid((*this)[4].getStr()) && + value5.valid((*this)[5].getStr()) && + value6.valid((*this)[6].getStr()) && + value7.valid((*this)[7].getStr())) + { + value1.assign((*this)[1].getStr()); + value2.assign((*this)[2].getStr()); + value3.assign((*this)[3].getStr()); + value4.assign((*this)[4].getStr()); + value5.assign((*this)[5].getStr()); + value6.assign((*this)[6].getStr()); + value7.assign((*this)[7].getStr()); + (*this) += 8; + return true; + } + else return false; +} + +bool Input::read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7, Parameter value8) +{ + if ((*this)[0].matchWord(str) && + value1.valid((*this)[1].getStr()) && + value2.valid((*this)[2].getStr()) && + value3.valid((*this)[3].getStr()) && + value4.valid((*this)[4].getStr()) && + value5.valid((*this)[5].getStr()) && + value6.valid((*this)[6].getStr()) && + value7.valid((*this)[7].getStr()) && + value8.valid((*this)[8].getStr())) + { + value1.assign((*this)[1].getStr()); + value2.assign((*this)[2].getStr()); + value3.assign((*this)[3].getStr()); + value4.assign((*this)[4].getStr()); + value5.assign((*this)[5].getStr()); + value6.assign((*this)[6].getStr()); + value7.assign((*this)[7].getStr()); + value8.assign((*this)[8].getStr()); + (*this) += 9; + return true; + } + else return false; +} diff --git a/src/osgWrappers/osg/ArgumentParser.cpp b/src/osgWrappers/osg/ArgumentParser.cpp index 8392bedd5..ee0354778 100644 --- a/src/osgWrappers/osg/ArgumentParser.cpp +++ b/src/osgWrappers/osg/ArgumentParser.cpp @@ -232,6 +232,10 @@ BEGIN_VALUE_REFLECTOR(osg::ArgumentParser) __bool__isNumber__C5_char_P1_S, "Return true if specified parameter is a number. ", ""); + I_StaticMethod1(bool, isBool, IN, const char *, str, + __bool__isBool__C5_char_P1_S, + "Return true if specified parameter is a bool. ", + ""); I_SimpleProperty(std::string, ApplicationName, __std_string__getApplicationName, 0); @@ -245,6 +249,7 @@ END_REFLECTOR BEGIN_ENUM_REFLECTOR(osg::ArgumentParser::Parameter::ParameterType) I_DeclaringFile("osg/ArgumentParser"); + I_EnumLabel(osg::ArgumentParser::Parameter::BOOL_PARAMETER); I_EnumLabel(osg::ArgumentParser::Parameter::FLOAT_PARAMETER); I_EnumLabel(osg::ArgumentParser::Parameter::DOUBLE_PARAMETER); I_EnumLabel(osg::ArgumentParser::Parameter::INT_PARAMETER); @@ -254,6 +259,11 @@ END_REFLECTOR BEGIN_VALUE_REFLECTOR(osg::ArgumentParser::Parameter) I_DeclaringFile("osg/ArgumentParser"); + I_Constructor1(IN, bool &, value, + Properties::NON_EXPLICIT, + ____Parameter__bool_R1, + "", + ""); I_Constructor1(IN, float &, value, Properties::NON_EXPLICIT, ____Parameter__float_R1, diff --git a/src/osgWrappers/osgDB/Input.cpp b/src/osgWrappers/osgDB/Input.cpp index 3d596446c..3692aadbf 100644 --- a/src/osgWrappers/osgDB/Input.cpp +++ b/src/osgWrappers/osgDB/Input.cpp @@ -28,6 +28,8 @@ #undef OUT #endif +TYPE_NAME_ALIAS(osg::ArgumentParser::Parameter, osgDB::Input::Parameter) + BEGIN_OBJECT_REFLECTOR(osgDB::Input) I_DeclaringFile("osgDB/Input"); I_BaseType(osgDB::FieldReaderIterator); @@ -109,6 +111,51 @@ BEGIN_OBJECT_REFLECTOR(osgDB::Input) __void__registerUniqueIDForObject__C5_std_string_R1__osg_Object_P1, "", ""); + I_Method1(bool, read, IN, const char *, str, + Properties::NON_VIRTUAL, + __bool__read__C5_char_P1, + "", + ""); + I_Method2(bool, read, IN, const char *, str, IN, osgDB::Input::Parameter, value1, + Properties::NON_VIRTUAL, + __bool__read__C5_char_P1__Parameter, + "", + ""); + I_Method3(bool, read, IN, const char *, str, IN, osgDB::Input::Parameter, value1, IN, osgDB::Input::Parameter, value2, + Properties::NON_VIRTUAL, + __bool__read__C5_char_P1__Parameter__Parameter, + "", + ""); + I_Method4(bool, read, IN, const char *, str, IN, osgDB::Input::Parameter, value1, IN, osgDB::Input::Parameter, value2, IN, osgDB::Input::Parameter, value3, + Properties::NON_VIRTUAL, + __bool__read__C5_char_P1__Parameter__Parameter__Parameter, + "", + ""); + I_Method5(bool, read, IN, const char *, str, IN, osgDB::Input::Parameter, value1, IN, osgDB::Input::Parameter, value2, IN, osgDB::Input::Parameter, value3, IN, osgDB::Input::Parameter, value4, + Properties::NON_VIRTUAL, + __bool__read__C5_char_P1__Parameter__Parameter__Parameter__Parameter, + "", + ""); + I_Method6(bool, read, IN, const char *, str, IN, osgDB::Input::Parameter, value1, IN, osgDB::Input::Parameter, value2, IN, osgDB::Input::Parameter, value3, IN, osgDB::Input::Parameter, value4, IN, osgDB::Input::Parameter, value5, + Properties::NON_VIRTUAL, + __bool__read__C5_char_P1__Parameter__Parameter__Parameter__Parameter__Parameter, + "", + ""); + I_Method7(bool, read, IN, const char *, str, IN, osgDB::Input::Parameter, value1, IN, osgDB::Input::Parameter, value2, IN, osgDB::Input::Parameter, value3, IN, osgDB::Input::Parameter, value4, IN, osgDB::Input::Parameter, value5, IN, osgDB::Input::Parameter, value6, + Properties::NON_VIRTUAL, + __bool__read__C5_char_P1__Parameter__Parameter__Parameter__Parameter__Parameter__Parameter, + "", + ""); + I_Method8(bool, read, IN, const char *, str, IN, osgDB::Input::Parameter, value1, IN, osgDB::Input::Parameter, value2, IN, osgDB::Input::Parameter, value3, IN, osgDB::Input::Parameter, value4, IN, osgDB::Input::Parameter, value5, IN, osgDB::Input::Parameter, value6, IN, osgDB::Input::Parameter, value7, + Properties::NON_VIRTUAL, + __bool__read__C5_char_P1__Parameter__Parameter__Parameter__Parameter__Parameter__Parameter__Parameter, + "", + ""); + I_Method9(bool, read, IN, const char *, str, IN, osgDB::Input::Parameter, value1, IN, osgDB::Input::Parameter, value2, IN, osgDB::Input::Parameter, value3, IN, osgDB::Input::Parameter, value4, IN, osgDB::Input::Parameter, value5, IN, osgDB::Input::Parameter, value6, IN, osgDB::Input::Parameter, value7, IN, osgDB::Input::Parameter, value8, + Properties::NON_VIRTUAL, + __bool__read__C5_char_P1__Parameter__Parameter__Parameter__Parameter__Parameter__Parameter__Parameter__Parameter, + "", + ""); I_SimpleProperty(const osgDB::ReaderWriter::Options *, Options, __C5_ReaderWriter_Options_P1__getOptions, __void__setOptions__C5_ReaderWriter_Options_P1);