GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 81.9% 59 / 0 / 72
Functions: 82.6% 19 / 0 / 23
Branches: 79.3% 23 / 0 / 29

src/scriptengine/ScriptEngine.hpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #ifndef SCRIPTENGINE_included
7 #define SCRIPTENGINE_included
8
9 #include "../utilities/core/ApplicationPathHelpers.hpp"
10 #include "../utilities/core/DynamicLibrary.hpp"
11 #include <algorithm>
12 #include <any>
13 #include <functional>
14 #include <iterator>
15 #include <map>
16 #include <memory>
17 #include <stdexcept>
18 #include <string>
19 #include <string_view>
20 #include <typeinfo>
21 #include <vector>
22
23 namespace openstudio {
24
25 namespace measure {
26 class ModelMeasure;
27 } // namespace measure
28
29 class ScriptEngine;
30
31 struct ScriptObject
32 {
33 std::any object;
34
35 51 bool empty() const {
36 51 return !object.has_value();
37 }
38 };
39 } // namespace openstudio
40
41 using ScriptEngineFactoryType = openstudio::ScriptEngine*(int, char**);
42
43 namespace openstudio {
44
45 namespace measure {
46 class OSMeasure;
47 }
48
49 class ScriptEngine
50 {
51 public:
52 280 ScriptEngine([[maybe_unused]] int argc, [[maybe_unused]] char* argv[]) {
53 // registerType<openstudio::measure::ModelMeasure*>("openstudio::measure::ModelMeasure *");
54 280 }
55
56 268 virtual ~ScriptEngine() = default;
57 ScriptEngine(const ScriptEngine&) = delete;
58 ScriptEngine(ScriptEngine&&) = delete;
59 ScriptEngine& operator=(const ScriptEngine&) = delete;
60 ScriptEngine& operator=(ScriptEngine&&) = delete;
61 2350 ScriptEngine* operator->() {
62 2350 return this;
63 }
64
65 virtual ScriptObject eval(std::string_view sv) = 0;
66
67 // execute string without expecting a return value
68 virtual void exec(std::string_view sv) = 0;
69
70 // TODO: this is totally the wrong place to put it, but I'm trying to see if it works
71 ✗ virtual void setupEmbeddedGems(const std::vector<openstudio::path>& includeDirs, const std::vector<openstudio::path>& gemPathDirs,
72 const openstudio::path& gemHomeDir, const openstudio::path& bundleGemFilePath,
73 ✗ const openstudio::path& bundleGemDirPath, const std::string& bundleWithoutGroups) {};
74
75 ✗ virtual void setupPythonPath(const std::vector<openstudio::path>& includeDirs) {};
76
77 virtual std::string inferMeasureClassName(const openstudio::path& measureScriptPath) = 0;
78
79 // Ideally this would return a openstudio::measure::OSMeasure* or a shared_ptr<openstudio::measure::OSMeasure> but this poses memory management
80 // issue for the underlying ScriptObject (and VALUE or PyObject), so just return the ScriptObject
81 virtual ScriptObject loadMeasure(const openstudio::path& measureScriptPath, std::string_view className) = 0;
82
83 // Returns number of arguments for methodName of the object methodObject
84 virtual int numberOfArguments(ScriptObject& methodObject, std::string_view methodName) = 0;
85
86 // Check if methodObject has a method called methodName. If overriden_only, will only look into the current subclass, not the parent ones
87 virtual bool hasMethod(ScriptObject& methodObject, std::string_view methodName, bool overriden_only = true) = 0;
88
89 template <typename T>
90 245 T getAs(ScriptObject& obj) {
91 if constexpr (std::is_same_v<T, bool>) {
92 return getAs_impl_bool(obj);
93 } else if constexpr (std::is_same_v<T, int>) {
94 187 return getAs_impl_int(obj);
95 } else if constexpr (std::is_same_v<T, double>) {
96 return getAs_impl_double(obj);
97 } else if constexpr (std::is_same_v<T, std::string>) {
98 ✗ return getAs_impl_string(obj);
99 } else {
100 58 void* result = getAs_impl(obj, typeid(T));
101
2/6
openstudio::measure::MeasureInfoBinding* openstudio::ScriptEngine::getAs<openstudio::measure::MeasureInfoBinding*>(openstudio::ScriptObject&):
✗ Branch 0 not taken.
✗ Branch 1 not taken.
openstudio::measure::ModelMeasure* openstudio::ScriptEngine::getAs<openstudio::measure::ModelMeasure*>(openstudio::ScriptObject&):
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
openstudio::measure::OSMeasure* openstudio::ScriptEngine::getAs<openstudio::measure::OSMeasure*>(openstudio::ScriptObject&):
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
58 if (result) {
102 58 return static_cast<T>(result);
103 } else {
104 ✗ throw std::bad_cast();
105 }
106 }
107 }
108
109 template <typename T>
110 1250 void registerType(std::string name) {
111
5/5
void openstudio::ScriptEngine::registerType<openstudio::measure::EnergyPlusMeasure*>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >):
✓ Branch 3 taken 251 times.
void openstudio::ScriptEngine::registerType<openstudio::measure::MeasureInfoBinding*>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >):
✓ Branch 3 taken 222 times.
void openstudio::ScriptEngine::registerType<openstudio::measure::ModelMeasure*>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >):
✓ Branch 3 taken 263 times.
void openstudio::ScriptEngine::registerType<openstudio::measure::OSMeasure*>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >):
✓ Branch 3 taken 263 times.
void openstudio::ScriptEngine::registerType<openstudio::measure::ReportingMeasure*>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >):
✓ Branch 3 taken 251 times.
1250 types.emplace(std::cref(typeid(T)), std::move(name));
112 1250 }
113
114 protected:
115 // convert the underlying object to the correct type, then return it as a void *
116 // so the above template function can provide it back to the caller.
117 virtual void* getAs_impl(ScriptObject& obj, const std::type_info&) = 0;
118
119 virtual bool getAs_impl_bool(ScriptObject& obj) = 0;
120 virtual int getAs_impl_int(ScriptObject& obj) = 0;
121 virtual double getAs_impl_double(ScriptObject& obj) = 0;
122 virtual std::string getAs_impl_string(ScriptObject& obj) = 0;
123
124 58 const std::string& getRegisteredTypeName(const std::type_info& type) {
125
1/1
✓ Branch 2 taken 58 times.
58 const auto& found_name = types.find(type);
126
127
1/2
✓ Branch 2 taken 58 times.
✗ Branch 3 not taken.
58 if (found_name != types.end()) {
128 116 return found_name->second;
129 }
130
131 ✗ throw std::runtime_error("unknown type requested");
132 }
133
134 private:
135 struct Compare
136 {
137 5122 bool operator()(const std::reference_wrapper<const std::type_info>& lhs, const std::reference_wrapper<const std::type_info>& rhs) const {
138 5122 return lhs.get().before(rhs.get());
139 }
140 };
141
142 std::map<std::reference_wrapper<const std::type_info>, std::string, Compare> types;
143 };
144
145 // The purpose of this class is to delay creating the scripting engine
146 // until it is needed, while still initializing with argc and argv
147 class ScriptEngineInstance
148 {
149 public:
150
1/1
✓ Branch 1 taken 566 times.
1698 ScriptEngineInstance(std::string_view libraryBaseName, std::vector<std::string> t_args) : libraryName(libraryBaseName), args(std::move(t_args)) {}
151
152 ScriptEngineInstance(const ScriptEngineInstance&) = delete;
153 ScriptEngineInstance(ScriptEngineInstance&&) = delete;
154 ScriptEngineInstance& operator=(const ScriptEngineInstance&) = delete;
155 ScriptEngineInstance& operator=(ScriptEngineInstance&&) = delete;
156
157 524 void registerInitializationFunction(const std::function<void()>& runSetupFun) {
158 524 m_runSetupFun = runSetupFun;
159 524 }
160
161 2351 ScriptEngine& operator->() {
162
2/2
✓ Branch 1 taken 2071 times.
✓ Branch 2 taken 280 times.
2351 if (instance) {
163 2071 return *(instance);
164 } else {
165 280 std::vector<char*> argv;
166
2/2
✓ Branch 1 taken 280 times.
✓ Branch 6 taken 280 times.
930 std::transform(args.begin(), args.end(), std::back_inserter(argv), [](const std::string& item) { return const_cast<char*>(item.c_str()); });
167
168
2/2
✓ Branch 2 taken 280 times.
✓ Branch 5 taken 280 times.
560 const auto enginePath = getOpenStudioModuleDirectory() / openstudio::getSharedModuleName(libraryName);
169 // DynamicLibrary will perform dlopen on construction and dlclose on destruction
170 // Don't create the DynamicLibrary until it is going to be used
171
1/1
✓ Branch 1 taken 280 times.
280 engineLib = std::make_unique<DynamicLibrary>(enginePath);
172
2/2
✓ Branch 2 taken 280 times.
✓ Branch 5 taken 280 times.
560 const auto factory = engineLib->load_symbol<ScriptEngineFactoryType>("makeScriptEngine");
173
1/1
✓ Branch 3 taken 280 times.
280 instance = std::unique_ptr<ScriptEngine>(factory(args.size(), argv.data()));
174
2/2
✓ Branch 1 taken 252 times.
✓ Branch 2 taken 28 times.
280 if (m_runSetupFun) {
175
1/1
✓ Branch 1 taken 251 times.
252 m_runSetupFun();
176 }
177 279 return *instance;
178 281 }
179 }
180 explicit operator bool() const {
181 return (bool)instance;
182 }
183 533 void reset() {
184 533 instance.reset();
185 525 engineLib.reset();
186 525 }
187
188 private:
189 std::string libraryName;
190 std::vector<std::string> args;
191 std::unique_ptr<ScriptEngine> instance;
192 std::unique_ptr<DynamicLibrary> engineLib;
193 std::function<void()> m_runSetupFun;
194 };
195
196 } // namespace openstudio
197
198 #endif
199