diff --git a/include/engine/api/route_parameters.hpp b/include/engine/api/route_parameters.hpp index e6f63c5e3..6db16a723 100644 --- a/include/engine/api/route_parameters.hpp +++ b/include/engine/api/route_parameters.hpp @@ -60,7 +60,7 @@ struct RouteParameters : public BaseParameters const bool alternatives_, const GeometriesType geometries_, const OverviewType overview_, - boost::optional uturns_, + const boost::optional uturns_, Args... args_) : BaseParameters{std::forward(args_)...}, steps{steps_}, alternatives{alternatives_}, geometries{geometries_}, overview{overview_}, uturns{uturns_} diff --git a/include/extractor/extractor.hpp b/include/extractor/extractor.hpp index 4ee27d06a..1b61513c1 100644 --- a/include/extractor/extractor.hpp +++ b/include/extractor/extractor.hpp @@ -40,7 +40,7 @@ namespace osrm namespace extractor { -class ProfileProperties; +struct ProfileProperties; class Extractor { diff --git a/include/extractor/graph_compressor.hpp b/include/extractor/graph_compressor.hpp index d1a5be524..78f3eaab3 100644 --- a/include/extractor/graph_compressor.hpp +++ b/include/extractor/graph_compressor.hpp @@ -21,8 +21,6 @@ class GraphCompressor using EdgeData = util::NodeBasedDynamicGraph::EdgeData; public: - GraphCompressor(); - void Compress(const std::unordered_set &barrier_nodes, const std::unordered_set &traffic_lights, RestrictionMap &restriction_map, diff --git a/include/extractor/profile_properties.hpp b/include/extractor/profile_properties.hpp index 8e2253a37..859034c91 100644 --- a/include/extractor/profile_properties.hpp +++ b/include/extractor/profile_properties.hpp @@ -1,6 +1,8 @@ #ifndef PROFILE_PROPERTIES_HPP #define PROFILE_PROPERTIES_HPP +#include + namespace osrm { namespace extractor @@ -20,7 +22,7 @@ struct ProfileProperties void SetUturnPenalty(const double u_turn_penalty_) { - u_turn_penalty = static_cast(u_turn_penalty_ * 10.); + u_turn_penalty = boost::numeric_cast(u_turn_penalty_ * 10.); } double GetTrafficSignalPenalty() const @@ -30,7 +32,7 @@ struct ProfileProperties void SetTrafficSignalPenalty(const double traffic_signal_penalty_) { - traffic_signal_penalty = static_cast(traffic_signal_penalty_ * 10.); + traffic_signal_penalty = boost::numeric_cast(traffic_signal_penalty_ * 10.); } //! penalty to cross a traffic light in deci-seconds diff --git a/include/extractor/scripting_environment.hpp b/include/extractor/scripting_environment.hpp index e26383ac7..5494c1268 100644 --- a/include/extractor/scripting_environment.hpp +++ b/include/extractor/scripting_environment.hpp @@ -4,6 +4,8 @@ #include "extractor/profile_properties.hpp" #include "extractor/raster_source.hpp" +#include "util/lua_util.hpp" + #include #include #include @@ -28,12 +30,9 @@ class ScriptingEnvironment public: struct Context { - Context(); - ~Context(); - ProfileProperties properties; SourceContainer sources; - lua_State *state; + util::LuaState state; }; explicit ScriptingEnvironment(const std::string &file_name); @@ -47,7 +46,7 @@ class ScriptingEnvironment void InitContext(Context &context); std::mutex init_mutex; std::string file_name; - tbb::enumerable_thread_specific> script_contexts; + tbb::enumerable_thread_specific> script_contexts; }; } } diff --git a/include/util/lua_util.hpp b/include/util/lua_util.hpp index 4eb799e04..4d4fe17be 100644 --- a/include/util/lua_util.hpp +++ b/include/util/lua_util.hpp @@ -18,10 +18,19 @@ namespace osrm namespace util { -template void LUA_print(T output) { std::cout << "[LUA] " << output << std::endl; } +struct LuaState +{ + LuaState() : handle{::luaL_newstate(), &::lua_close} { luaL_openlibs(*this); } + + operator lua_State *() { return handle.get(); } + operator lua_State const *() const { return handle.get(); } + + using handle_type = std::unique_ptr; + handle_type handle; +}; // Check if the lua function is defined -inline bool lua_function_exists(lua_State *lua_state, const char *name) +inline bool luaFunctionExists(lua_State *lua_state, const char *name) { luabind::object globals_table = luabind::globals(lua_state); luabind::object lua_function = globals_table[name]; diff --git a/src/extractor/edge_based_graph_factory.cpp b/src/extractor/edge_based_graph_factory.cpp index 62a004e7e..bab868e26 100644 --- a/src/extractor/edge_based_graph_factory.cpp +++ b/src/extractor/edge_based_graph_factory.cpp @@ -278,7 +278,7 @@ void EdgeBasedGraphFactory::GenerateEdgeExpandedEdges( util::SimpleLogger().Write() << "generating edge-expanded edges"; BOOST_ASSERT(lua_state != nullptr); - const bool use_turn_function = util::lua_function_exists(lua_state, "turn_function"); + const bool use_turn_function = util::luaFunctionExists(lua_state, "turn_function"); std::size_t node_based_edge_counter = 0; std::size_t original_edges_counter = 0; @@ -454,7 +454,7 @@ int EdgeBasedGraphFactory::GetTurnPenalty(double angle, lua_State *lua_state) co { // call lua profile to compute turn penalty double penalty = luabind::call_function(lua_state, "turn_function", 180. - angle); - return static_cast(penalty); + return boost::numeric_cast(penalty); } catch (const luabind::error &er) { diff --git a/src/extractor/extraction_containers.cpp b/src/extractor/extraction_containers.cpp index 5c86eee6e..5106ca982 100644 --- a/src/extractor/extraction_containers.cpp +++ b/src/extractor/extraction_containers.cpp @@ -288,7 +288,7 @@ void ExtractionContainers::PrepareEdges(lua_State *segment_state) const auto all_edges_list_end_ = all_edges_list.end(); const auto all_nodes_list_end_ = all_nodes_list.end(); - auto has_segment_function = util::lua_function_exists(segment_state, "segment_function"); + const auto has_segment_function = util::luaFunctionExists(segment_state, "segment_function"); while (edge_iterator != all_edges_list_end_ && node_iterator != all_nodes_list_end_) { diff --git a/src/extractor/extractor.cpp b/src/extractor/extractor.cpp index 515d0f782..50f75aaa1 100644 --- a/src/extractor/extractor.cpp +++ b/src/extractor/extractor.cpp @@ -111,7 +111,7 @@ int Extractor::run() auto& main_context = scripting_environment.GetContex(); // setup raster sources - if (util::lua_function_exists(main_context.state, "source_function")) + if (util::luaFunctionExists(main_context.state, "source_function")) { luabind::call_function(main_context.state, "source_function"); } diff --git a/src/extractor/graph_compressor.cpp b/src/extractor/graph_compressor.cpp index 6df9d485e..6df9a38cc 100644 --- a/src/extractor/graph_compressor.cpp +++ b/src/extractor/graph_compressor.cpp @@ -13,10 +13,6 @@ namespace osrm namespace extractor { -GraphCompressor::GraphCompressor() -{ -} - void GraphCompressor::Compress(const std::unordered_set &barrier_nodes, const std::unordered_set &traffic_lights, RestrictionMap &restriction_map, diff --git a/src/extractor/restriction_parser.cpp b/src/extractor/restriction_parser.cpp index 68aa8d622..66cd25903 100644 --- a/src/extractor/restriction_parser.cpp +++ b/src/extractor/restriction_parser.cpp @@ -44,7 +44,7 @@ RestrictionParser::RestrictionParser(lua_State *lua_state, const ProfileProperti void RestrictionParser::ReadRestrictionExceptions(lua_State *lua_state) { - if (util::lua_function_exists(lua_state, "get_exceptions")) + if (util::luaFunctionExists(lua_state, "get_exceptions")) { luabind::set_pcall_callback(&luaErrorCallback); // get list of turn restriction exceptions diff --git a/src/extractor/scripting_environment.cpp b/src/extractor/scripting_environment.cpp index 09f524c8f..156bc1c37 100644 --- a/src/extractor/scripting_environment.cpp +++ b/src/extractor/scripting_environment.cpp @@ -8,6 +8,7 @@ #include "extractor/raster_source.hpp" #include "extractor/profile_properties.hpp" #include "util/lua_util.hpp" +#include "util/make_unique.hpp" #include "util/exception.hpp" #include "util/simple_logger.hpp" #include "util/typedefs.hpp" @@ -52,10 +53,6 @@ int luaErrorCallback(lua_State *state) } } -ScriptingEnvironment::Context::Context() : state(luaL_newstate()) {} - -ScriptingEnvironment::Context::~Context() { lua_close(state); } - ScriptingEnvironment::ScriptingEnvironment(const std::string &file_name) : file_name(file_name) { util::SimpleLogger().Write() << "Using script " << file_name; @@ -73,8 +70,7 @@ void ScriptingEnvironment::InitContext(ScriptingEnvironment::Context &context) // Add our function to the state's global scope luabind::module(context.state) - [luabind::def("print", util::LUA_print), - luabind::def("durationIsValid", durationIsValid), + [luabind::def("durationIsValid", durationIsValid), luabind::def("parseDuration", parseDuration), luabind::class_("mode") .enum_("enums")[luabind::value("inaccessible", TRAVEL_MODE_INACCESSIBLE), @@ -176,8 +172,7 @@ ScriptingEnvironment::Context &ScriptingEnvironment::GetContex() auto &ref = script_contexts.local(initialized); if (!initialized) { - std::shared_ptr state = std::make_shared(); - ref = state; + ref = util::make_unique(); InitContext(*ref); } luabind::set_pcall_callback(&luaErrorCallback); diff --git a/unit_tests/mocks/mock_datafacade.hpp b/unit_tests/mocks/mock_datafacade.hpp index 833520015..720b07607 100644 --- a/unit_tests/mocks/mock_datafacade.hpp +++ b/unit_tests/mocks/mock_datafacade.hpp @@ -103,6 +103,7 @@ class MockDataFacadeT final : public osrm::engine::datafacade::BaseDataFacade;