-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
scripting_environment_lua.hpp
113 lines (91 loc) · 4.15 KB
/
scripting_environment_lua.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#ifndef SCRIPTING_ENVIRONMENT_LUA_HPP
#define SCRIPTING_ENVIRONMENT_LUA_HPP
#include "extractor/extraction_relation.hpp"
#include "extractor/location_dependent_data.hpp"
#include "extractor/raster_source.hpp"
#include "extractor/scripting_environment.hpp"
#include <tbb/enumerable_thread_specific.h>
#include <memory>
#include <mutex>
#include <string>
#include <sol/sol.hpp>
namespace osrm::extractor
{
struct LuaScriptingContext final
{
LuaScriptingContext(const LocationDependentData &location_dependent_data)
: location_dependent_data(location_dependent_data),
last_location_point(0., 180.) // assume (0,180) is invalid coordinate
{
}
void ProcessNode(const osmium::Node &,
ExtractionNode &result,
const ExtractionRelationContainer &relations);
void ProcessWay(const osmium::Way &,
ExtractionWay &result,
const ExtractionRelationContainer &relations);
ProfileProperties properties;
RasterContainer raster_sources;
sol::state state;
bool has_turn_penalty_function = false;
bool has_node_function = false;
bool has_way_function = false;
bool has_segment_function = false;
sol::protected_function turn_function;
sol::protected_function way_function;
sol::protected_function node_function;
sol::protected_function segment_function;
int api_version = 4;
sol::table profile_table;
// Reference to immutable location dependent data and locations memo
const LocationDependentData &location_dependent_data;
LocationDependentData::point_t last_location_point;
std::vector<std::size_t> last_location_indexes;
};
/**
* Creates a lua context and binds osmium way, node and relation objects and
* ExtractionWay and ExtractionNode to lua objects.
*
* Each thread has its own lua state which is implemented with thread specific
* storage from TBB.
*/
class Sol2ScriptingEnvironment final : public ScriptingEnvironment
{
public:
static const constexpr int SUPPORTED_MIN_API_VERSION = 0;
static const constexpr int SUPPORTED_MAX_API_VERSION = 4;
explicit Sol2ScriptingEnvironment(
const std::string &file_name,
const std::vector<std::filesystem::path> &location_dependent_data_paths);
~Sol2ScriptingEnvironment() override = default;
const ProfileProperties &GetProfileProperties() override;
std::vector<std::vector<std::string>> GetExcludableClasses() override;
std::vector<std::string> GetNameSuffixList() override;
std::vector<std::string> GetClassNames() override;
std::vector<std::string> GetRestrictions() override;
std::vector<std::string> GetRelations() override;
void ProcessTurn(ExtractionTurn &turn) override;
void ProcessSegment(ExtractionSegment &segment) override;
void
ProcessElements(const osmium::memory::Buffer &buffer,
const RestrictionParser &restriction_parser,
const ManeuverOverrideRelationParser &maneuver_override_parser,
const ExtractionRelationContainer &relations,
std::vector<std::pair<const osmium::Node &, ExtractionNode>> &resulting_nodes,
std::vector<std::pair<const osmium::Way &, ExtractionWay>> &resulting_ways,
std::vector<InputTurnRestriction> &resulting_restrictions,
std::vector<InputManeuverOverride> &resulting_maneuver_overrides) override;
bool HasLocationDependentData() const override { return !location_dependent_data.empty(); }
private:
LuaScriptingContext &GetSol2Context();
std::vector<std::string> GetStringListFromTable(const std::string &table_name);
std::vector<std::vector<std::string>> GetStringListsFromTable(const std::string &table_name);
std::vector<std::string> GetStringListFromFunction(const std::string &function_name);
void InitContext(LuaScriptingContext &context);
std::mutex init_mutex;
std::string file_name;
tbb::enumerable_thread_specific<std::unique_ptr<LuaScriptingContext>> script_contexts;
const LocationDependentData location_dependent_data;
};
} // namespace osrm::extractor
#endif /* SCRIPTING_ENVIRONMENT_LUA_HPP */