GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 88.2% 15 / 0 / 17
Functions: 100.0% 3 / 0 / 3
Branches: 58.3% 7 / 0 / 12

src/utilities/core/DynamicLibraryPOSIX.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 dynamiclibrary_posix_hpp_INCLUDED
7 #define dynamiclibrary_posix_hpp_INCLUDED
8
9 #include "Path.hpp"
10
11 #include <memory>
12 #include <stdexcept>
13 #include <string>
14
15 #include <dlfcn.h>
16
17 #include <fmt/format.h>
18
19 namespace openstudio {
20
21 struct DynamicLibrary
22 {
23 template <typename Signature>
24 280 [[nodiscard]] Signature* load_symbol(const std::string& name) {
25 // reinterpret_cast is necessary here
26 280 const auto symbol = reinterpret_cast<Signature*>(dlsym(m_handle.get(), name.c_str())); // NOLINT
27
28
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 280 times.
280 if (symbol == nullptr) {
29 ✗ throw std::runtime_error(fmt::format("Unable to load symbol: '{}', reason: '{}'", name, dlerror()));
30 }
31
32 280 return symbol;
33 }
34
35 280 explicit DynamicLibrary(openstudio::path location) : m_location{std::move(location)} {
36 280 int flags = RTLD_LAZY | RTLD_LOCAL; // NOLINT(misc-const-correctness, hicpp-signed-bitwise)
37
38 // This seems to work on Mac without RTLD_GLOBAL...
39 #ifdef __linux__
40
4/4
✓ Branch 1 taken 280 times.
✓ Branch 4 taken 280 times.
✓ Branch 9 taken 35 times.
✓ Branch 10 taken 245 times.
560 if (m_location.filename().generic_string().find("python") != std::string::npos) {
41 // https://stackoverflow.com/questions/67891197/ctypes-cpython-39-x86-64-linux-gnu-so-undefined-symbol-pyfloat-type-in-embedd
42 35 flags = RTLD_LAZY | RTLD_GLOBAL;
43 }
44 #endif
45 280 m_handle = {dlopen(m_location.c_str(), flags), m_handle_deleter};
46
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 280 times.
280 if (!m_handle) {
47 ✗ throw std::runtime_error(fmt::format("Unable to load library '{}', reason: '{}'", m_location.string(), dlerror()));
48 }
49 280 }
50
51 268 static void m_handle_deleter(void* h) {
52
1/2
✓ Branch 0 taken 268 times.
✗ Branch 1 not taken.
268 if (h) {
53 268 dlclose(h);
54 }
55 268 }
56
57 openstudio::path m_location{};
58 std::unique_ptr<void, decltype(&m_handle_deleter)> m_handle{nullptr, m_handle_deleter};
59 };
60 } // namespace openstudio
61
62 #endif // dynamiclibrary_posix_hpp_INCLUDED
63