From 8b3c8f70930481d2e488b9c45a3a57c6dd674802 Mon Sep 17 00:00:00 2001 From: Dennis Luxen Date: Mon, 23 Dec 2013 17:55:29 +0100 Subject: [PATCH] refactoring variable names and layout to be more readable --- Extractor/BaseParser.cpp | 74 ++++++++++++++++++++---------- Extractor/BaseParser.h | 30 ++++++------ Extractor/PBFParser.cpp | 12 +++-- Extractor/ScriptingEnvironment.cpp | 1 + Extractor/ScriptingEnvironment.h | 4 +- Extractor/XMLParser.cpp | 4 +- 6 files changed, 81 insertions(+), 44 deletions(-) diff --git a/Extractor/BaseParser.cpp b/Extractor/BaseParser.cpp index 636d89d6d..65f3b78bf 100644 --- a/Extractor/BaseParser.cpp +++ b/Extractor/BaseParser.cpp @@ -27,24 +27,32 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "BaseParser.h" #include "ExtractorStructs.h" +#include "ScriptingEnvironment.h" + +#include "../Util/LuaUtil.h" +#include "../Util/OSRMException.h" +#include "../Util/SimpleLogger.h" #include -BaseParser::BaseParser(ExtractorCallbacks* ec, ScriptingEnvironment& se) : -extractor_callbacks(ec), scriptingEnvironment(se), luaState(NULL), use_turn_restrictions(true) { - luaState = se.getLuaStateForThreadID(0); +BaseParser::BaseParser( + ExtractorCallbacks * extractor_callbacks, + ScriptingEnvironment & scripting_environment +) : extractor_callbacks(extractor_callbacks), + lua_state(scripting_environment.getLuaStateForThreadID(0)), + scripting_environment(scripting_environment), + use_turn_restrictions(true) +{ ReadUseRestrictionsSetting(); ReadRestrictionExceptions(); } void BaseParser::ReadUseRestrictionsSetting() { - if( 0 != luaL_dostring( luaState, "return use_turn_restrictions\n") ) { - throw OSRMException( - /*lua_tostring( luaState, -1 ) + */"ERROR occured in scripting block" - ); + if( 0 != luaL_dostring( lua_state, "return use_turn_restrictions\n") ) { + throw OSRMException("ERROR occured in scripting block"); } - if( lua_isboolean( luaState, -1) ) { - use_turn_restrictions = lua_toboolean(luaState, -1); + if( lua_isboolean( lua_state, -1) ) { + use_turn_restrictions = lua_toboolean(lua_state, -1); } if( use_turn_restrictions ) { SimpleLogger().Write() << "Using turn restrictions"; @@ -54,16 +62,18 @@ void BaseParser::ReadUseRestrictionsSetting() { } void BaseParser::ReadRestrictionExceptions() { - if(lua_function_exists(luaState, "get_exceptions" )) { + if(lua_function_exists(lua_state, "get_exceptions" )) { //get list of turn restriction exceptions luabind::call_function( - luaState, + lua_state, "get_exceptions", boost::ref(restriction_exceptions) ); - SimpleLogger().Write() << "Found " << restriction_exceptions.size() << " exceptions to turn restriction"; + const unsigned exception_count = restriction_exceptions.size(); + SimpleLogger().Write() << + "Found " << exception_count << " exceptions to turn restrictions:"; BOOST_FOREACH(const std::string & str, restriction_exceptions) { - SimpleLogger().Write() << " " << str; + SimpleLogger().Write() << " " << str; } } else { SimpleLogger().Write() << "Found no exceptions to turn restrictions"; @@ -77,20 +87,32 @@ void BaseParser::report_errors(lua_State *L, const int status) const { } } -void BaseParser::ParseNodeInLua(ImportNode& n, lua_State* localLuaState) { - luabind::call_function( localLuaState, "node_function", boost::ref(n) ); +void BaseParser::ParseNodeInLua(ImportNode& n, lua_State* local_lua_state) { + luabind::call_function( + local_lua_state, + "node_function", + boost::ref(n) + ); } -void BaseParser::ParseWayInLua(ExtractionWay& w, lua_State* localLuaState) { - luabind::call_function( localLuaState, "way_function", boost::ref(w) ); +void BaseParser::ParseWayInLua(ExtractionWay& w, lua_State* local_lua_state) { + luabind::call_function( + local_lua_state, + "way_function", + boost::ref(w) + ); } -bool BaseParser::ShouldIgnoreRestriction(const std::string & except_tag_string) const { +bool BaseParser::ShouldIgnoreRestriction( + const std::string & except_tag_string +) const { //should this restriction be ignored? yes if there's an overlap between: - //a) the list of modes in the except tag of the restriction (except_tag_string), ex: except=bus;bicycle - //b) the lua profile defines a hierachy of modes, ex: [access, vehicle, bicycle] + // a) the list of modes in the except tag of the restriction + // (except_tag_string), eg: except=bus;bicycle + // b) the lua profile defines a hierachy of modes, + // eg: [access, vehicle, bicycle] - if( "" == except_tag_string ) { + if( except_tag_string.empty() ) { return false; } @@ -98,8 +120,14 @@ bool BaseParser::ShouldIgnoreRestriction(const std::string & except_tag_string) //only a few exceptions are actually defined. std::vector exceptions; boost::algorithm::split_regex(exceptions, except_tag_string, boost::regex("[;][ ]*")); - BOOST_FOREACH(std::string& str, exceptions) { - if( restriction_exceptions.end() != std::find(restriction_exceptions.begin(), restriction_exceptions.end(), str) ) { + BOOST_FOREACH(std::string& current_string, exceptions) { + std::vector::const_iterator string_iterator; + string_iterator = std::find( + restriction_exceptions.begin(), + restriction_exceptions.end(), + current_string + ); + if( restriction_exceptions.end() != string_iterator ) { return true; } } diff --git a/Extractor/BaseParser.h b/Extractor/BaseParser.h index abfb3bb1f..98c2aeef2 100644 --- a/Extractor/BaseParser.h +++ b/Extractor/BaseParser.h @@ -28,11 +28,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef BASEPARSER_H_ #define BASEPARSER_H_ -// #include "ExtractorCallbacks.h" -#include "ScriptingEnvironment.h" -#include "../Util/OSRMException.h" -#include "../Util/SimpleLogger.h" - extern "C" { #include #include @@ -40,33 +35,40 @@ extern "C" { } #include +#include +#include class ExtractorCallbacks; +class ScriptingEnvironment; struct ExtractionWay; struct ImportNode; class BaseParser : boost::noncopyable { public: - BaseParser(ExtractorCallbacks* ec, ScriptingEnvironment& se); + BaseParser( + ExtractorCallbacks * extractor_callbacks, + ScriptingEnvironment & scripting_environment + ); virtual ~BaseParser() {} virtual bool ReadHeader() = 0; virtual bool Parse() = 0; - virtual void ParseNodeInLua(ImportNode& n, lua_State* luaStateForThread); - virtual void ParseWayInLua(ExtractionWay& n, lua_State* luaStateForThread); - virtual void report_errors(lua_State *L, const int status) const; + virtual void ParseNodeInLua(ImportNode & n, lua_State* thread_lua_state); + virtual void ParseWayInLua(ExtractionWay & n, lua_State* thread_lua_state); + virtual void report_errors(lua_State * lua_state, const int status) const; protected: virtual void ReadUseRestrictionsSetting(); virtual void ReadRestrictionExceptions(); - virtual bool ShouldIgnoreRestriction(const std::string& except_tag_string) const; + virtual bool ShouldIgnoreRestriction( + const std::string & except_tag_string + ) const; - ExtractorCallbacks* extractor_callbacks; - ScriptingEnvironment& scriptingEnvironment; - lua_State* luaState; + ExtractorCallbacks * extractor_callbacks; + lua_State * lua_state; + ScriptingEnvironment & scripting_environment; std::vector restriction_exceptions; bool use_turn_restrictions; - }; #endif /* BASEPARSER_H_ */ diff --git a/Extractor/PBFParser.cpp b/Extractor/PBFParser.cpp index c46b561b0..b8e998e7d 100644 --- a/Extractor/PBFParser.cpp +++ b/Extractor/PBFParser.cpp @@ -28,6 +28,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "ExtractorCallbacks.h" #include "ExtractorStructs.h" #include "PBFParser.h" +#include "ScriptingEnvironment.h" + #include "../DataStructures/HashTable.h" #include "../Util/MachineInfo.h" #include "../Util/OpenMPWrapper.h" @@ -43,7 +45,11 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include -PBFParser::PBFParser(const char * fileName, ExtractorCallbacks* ec, ScriptingEnvironment& se) : BaseParser( ec, se ) { +PBFParser::PBFParser( + const char * fileName, + ExtractorCallbacks* ec, + ScriptingEnvironment& se +) : BaseParser( ec, se ) { GOOGLE_PROTOBUF_VERIFY_VERSION; //TODO: What is the bottleneck here? Filling the queue or reading the stuff from disk? //NOTE: With Lua scripting, it is parsing the stuff. I/O is virtually for free. @@ -207,7 +213,7 @@ inline void PBFParser::parseDenseNode(_ThreadData * threadData) { #pragma omp parallel for schedule ( guided ) for(int i = 0; i < number_of_nodes; ++i) { ImportNode &n = extracted_nodes_vector[i]; - ParseNodeInLua( n, scriptingEnvironment.getLuaStateForThreadID(omp_get_thread_num()) ); + ParseNodeInLua( n, scripting_environment.getLuaStateForThreadID(omp_get_thread_num()) ); } BOOST_FOREACH(const ImportNode &n, extracted_nodes_vector) { @@ -342,7 +348,7 @@ inline void PBFParser::parseWay(_ThreadData * threadData) { if(2 > w.path.size()) { continue; } - ParseWayInLua( w, scriptingEnvironment.getLuaStateForThreadID( omp_get_thread_num()) ); + ParseWayInLua( w, scripting_environment.getLuaStateForThreadID( omp_get_thread_num()) ); } BOOST_FOREACH(ExtractionWay & w, parsed_way_vector) { diff --git a/Extractor/ScriptingEnvironment.cpp b/Extractor/ScriptingEnvironment.cpp index 5d0d469fa..baafdb10d 100644 --- a/Extractor/ScriptingEnvironment.cpp +++ b/Extractor/ScriptingEnvironment.cpp @@ -29,6 +29,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "ExtractorStructs.h" #include "ScriptingEnvironment.h" #include "../DataStructures/ImportNode.h" +#include "../Util/LuaUtil.h" #include "../Util/OpenMPWrapper.h" #include "../Util/OSRMException.h" #include "../Util/SimpleLogger.h" diff --git a/Extractor/ScriptingEnvironment.h b/Extractor/ScriptingEnvironment.h index 81ac4568a..de3e96e8d 100644 --- a/Extractor/ScriptingEnvironment.h +++ b/Extractor/ScriptingEnvironment.h @@ -28,10 +28,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef SCRIPTINGENVIRONMENT_H_ #define SCRIPTINGENVIRONMENT_H_ -#include "../Util/LuaUtil.h" - #include +struct lua_State; + class ScriptingEnvironment { public: ScriptingEnvironment(); diff --git a/Extractor/XMLParser.cpp b/Extractor/XMLParser.cpp index b54a64b30..5da7e52d7 100644 --- a/Extractor/XMLParser.cpp +++ b/Extractor/XMLParser.cpp @@ -64,7 +64,7 @@ bool XMLParser::Parse() { if ( xmlStrEqual( currentName, ( const xmlChar* ) "node" ) == 1 ) { ImportNode n = _ReadXMLNode(); - ParseNodeInLua( n, luaState ); + ParseNodeInLua( n, lua_state ); extractor_callbacks->nodeFunction(n); // if(!extractor_callbacks->nodeFunction(n)) // std::cerr << "[XMLParser] dense node not parsed" << std::endl; @@ -72,7 +72,7 @@ bool XMLParser::Parse() { if ( xmlStrEqual( currentName, ( const xmlChar* ) "way" ) == 1 ) { ExtractionWay way = _ReadXMLWay( ); - ParseWayInLua( way, luaState ); + ParseWayInLua( way, lua_state ); extractor_callbacks->wayFunction(way); // if(!extractor_callbacks->wayFunction(way)) // std::cerr << "[PBFParser] way not parsed" << std::endl;