From: <hug...@li...> - 2010-06-06 13:08:45
|
details: http://hugin.hg.sourceforge.net/hgweb/hugin/hugin/hgrepo/h/hu/hugin/hugin/rev/92cd7da1f7c8 changeset: 4066:92cd7da1f7c8 user: Florian Achleitner <flo...@gm...> date: Sat Jun 05 15:48:03 2010 +0200 description: - add AutoVariable. - Filter Variable names. diffstat: src/hugin_base/makefilelib/AutoVariable.cpp | 28 ++++++++++++++++++ src/hugin_base/makefilelib/AutoVariable.h | 44 +++++++++++++++++++++++++++++ src/hugin_base/makefilelib/CMakeLists.txt | 4 +- src/hugin_base/makefilelib/Variable.cpp | 14 ++++++++- src/hugin_base/makefilelib/Variable.h | 18 ++++++++--- src/hugin_base/makefilelib/main.cpp | 22 +++++++++++++- 6 files changed, 120 insertions(+), 10 deletions(-) diffs (224 lines): diff -r 6c0c9fa6aee2 -r 92cd7da1f7c8 src/hugin_base/makefilelib/AutoVariable.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/hugin_base/makefilelib/AutoVariable.cpp Sat Jun 05 15:48:03 2010 +0200 @@ -0,0 +1,28 @@ +/** + * @file AutoVariable.cpp + * @brief + * Created on: Jun 5, 2010 + * @author Florian Achleitner <flo...@gm...> + */ + +#include "AutoVariable.h" +#include <stdexcept> + +namespace makefile +{ +int AutoVariable::checkStrings() +{ + return 0; +} + +std::string& AutoVariable::getValue() +{ + throw(std::runtime_error("AutoVariables have no predefined value.")); +} + +VariableDef& AutoVariable::getDef() +{ + throw(std::runtime_error("AutoVariables can not be defined.")); +} + +} diff -r 6c0c9fa6aee2 -r 92cd7da1f7c8 src/hugin_base/makefilelib/AutoVariable.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/hugin_base/makefilelib/AutoVariable.h Sat Jun 05 15:48:03 2010 +0200 @@ -0,0 +1,44 @@ +/** + * @file AutoVariable.h + * @brief + * Created on: Jun 5, 2010 + * @author Florian Achleitner <flo...@gm...> + */ + +#ifndef AUTOVARIABLE_H_ +#define AUTOVARIABLE_H_ + + +#include "Variable.h" + +namespace makefile +{ +/** + * GNU Make has automatic Variables, which can not be defined and have names + * consisting of one special character. + * Use this class to refer to such variables. + * http://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html#Automatic-Variables + */ +class AutoVariable: public makefile::Variable +{ + /// Automatic Variables have special names, so we omit checking. + virtual int checkStrings(); +public: + AutoVariable(std::string name) + : Variable(name) + { + checkStrings(); + } + virtual ~AutoVariable() + {}; + + /// Has no value, exception. + virtual std::string& getValue(); + + /// Has no definition, exception. + virtual VariableDef& getDef(); +}; + +} + +#endif /* AUTOVARIABLE_H_ */ diff -r 6c0c9fa6aee2 -r 92cd7da1f7c8 src/hugin_base/makefilelib/CMakeLists.txt --- a/src/hugin_base/makefilelib/CMakeLists.txt Fri Jun 04 23:03:31 2010 +0200 +++ b/src/hugin_base/makefilelib/CMakeLists.txt Sat Jun 05 15:48:03 2010 +0200 @@ -1,7 +1,7 @@ SET(MAKEFILELIB_SRC Comment.cpp MakefileItem.cpp Variable.cpp VariableRef.cpp - Filename.cpp Makefile.cpp Rule.cpp VariableDef.cpp ) + Filename.cpp Makefile.cpp Rule.cpp VariableDef.cpp AutoVariable.cpp) SET(MAKEFILELIB_HEADER Comment.h Makefile.h Rule.h Variable.h Filename.h - MakefileItem.h VariableDef.h VariableRef.h) + MakefileItem.h VariableDef.h VariableRef.h AutoVariable.h) IF (${HUGIN_SHARED_LIBS}) diff -r 6c0c9fa6aee2 -r 92cd7da1f7c8 src/hugin_base/makefilelib/Variable.cpp --- a/src/hugin_base/makefilelib/Variable.cpp Fri Jun 04 23:03:31 2010 +0200 +++ b/src/hugin_base/makefilelib/Variable.cpp Sat Jun 05 15:48:03 2010 +0200 @@ -6,15 +6,27 @@ */ #include "Variable.h" - +#include <boost/regex.hpp> +#include <stdexcept> namespace makefile { /// @todo Checks nothing yet. +/** + * According to Gnu Make Manual http://www.gnu.org/software/make/manual/html_node/Using-Variables.html#Using-Variables + * it's recommended to use only alphanumerics and _ in Variable name. + * + * @return + */ int Variable::checkStrings() { + static const boost::regex validname("[[:alnum:]_]*"); + if( !boost::regex_match(name, validname)) + throw std::invalid_argument("Bad Variable name: " + name); + + return 0; } } diff -r 6c0c9fa6aee2 -r 92cd7da1f7c8 src/hugin_base/makefilelib/Variable.h --- a/src/hugin_base/makefilelib/Variable.h Fri Jun 04 23:03:31 2010 +0200 +++ b/src/hugin_base/makefilelib/Variable.h Sat Jun 05 15:48:03 2010 +0200 @@ -21,6 +21,7 @@ */ class Variable { +protected: std::string name, value; /// A VariableDef connected to the Variable. VariableDef def; @@ -31,8 +32,15 @@ * Checks the name and value Strings and replaces forbidden characters. * @return Number of replaced chars. */ - int checkStrings(); + virtual int checkStrings(); + /// To be used only by subclasses, like AutoVariable. + Variable(std::string name_) + : name(name_), value(""), def(*this), ref(*this) + { + + } +private: Variable(const Variable&); // no implicite copies! public: Variable(std::string name_, std::string value_) @@ -41,22 +49,22 @@ checkStrings(); } - std::string& getName() + virtual std::string& getName() { return name; } - std::string& getValue() + virtual std::string& getValue() { return value; } - VariableDef& getDef() + virtual VariableDef& getDef() { return def; } - VariableRef& getRef() + virtual VariableRef& getRef() { return ref; } diff -r 6c0c9fa6aee2 -r 92cd7da1f7c8 src/hugin_base/makefilelib/main.cpp --- a/src/hugin_base/makefilelib/main.cpp Fri Jun 04 23:03:31 2010 +0200 +++ b/src/hugin_base/makefilelib/main.cpp Sat Jun 05 15:48:03 2010 +0200 @@ -6,12 +6,15 @@ */ #include <iostream> +#include <stdexcept> #include "Comment.h" #include "Variable.h" #include "VariableDef.h" #include "VariableRef.h" #include "MakefileItem.h" #include "Makefile.h" +#include "AutoVariable.h" + using namespace std; using namespace makefile; @@ -29,7 +32,22 @@ cout << myname.getRef() << endl; Variable myfullname("MYFULLNAME", myname.getRef().toString() + " Achleitner"); - cout << myfullname.getDef(); - cout << myfullname.getRef() << endl; + cout << myfullname.getDef() << myfullname.getRef() << endl; + + try + { + Variable namesucks("This name sucks", "anyvalue"); + cout << namesucks.getDef(); + } + catch(std::exception& e) + { + cerr << e.what() << endl; + } + Variable namesucksless("This_name_sucks_less", "badvalue"); + cout << namesucksless.getDef(); + + AutoVariable autovar("@"); +// cout << autovar.getDef(); causes an exception as it should. + cout << autovar.getRef() << endl; return 0; } |