Line data Source code
1 : /** 2 : Copyright (c) 2024 Stappler LLC <admin@stappler.dev> 3 : 4 : Permission is hereby granted, free of charge, to any person obtaining a copy 5 : of this software and associated documentation files (the "Software"), to deal 6 : in the Software without restriction, including without limitation the rights 7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 : copies of the Software, and to permit persons to whom the Software is 9 : furnished to do so, subject to the following conditions: 10 : 11 : The above copyright notice and this permission notice shall be included in 12 : all copies or substantial portions of the Software. 13 : 14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 : THE SOFTWARE. 21 : **/ 22 : 23 : #ifndef CORE_CORE_UTILS_SPDSO_H_ 24 : #define CORE_CORE_UTILS_SPDSO_H_ 25 : 26 : #include "SPStringView.h" 27 : 28 : namespace STAPPLER_VERSIONIZED stappler { 29 : 30 : enum class DsoFlags : uint32_t { 31 : None = 0, 32 : Self = 1 << 0, // open caller app itself instead of target library ( Dso(StringView(), DsoFlags::Self) ) 33 : Lazy = 1 << 1, // use lazy binding if available (default) 34 : Global = 1 << 2, 35 : }; 36 : 37 : SP_DEFINE_ENUM_AS_MASK(DsoFlags) 38 : 39 : class Dso { 40 : public: 41 : ~Dso(); 42 : 43 : Dso(); 44 : Dso(StringView); // Lazy | Local by default 45 : Dso(StringView, DsoFlags); 46 : 47 : Dso(const Dso &) = delete; 48 : Dso & operator=(const Dso &) = delete; 49 : 50 : Dso(Dso &&); 51 : Dso & operator=(Dso &&); 52 : 53 : template <typename T = void *> 54 5692 : T sym(StringView name) { 55 : static_assert(std::is_pointer<T>::value, "Pointer type required to load from DSO"); 56 5692 : return reinterpret_cast<T>(loadSym(name)); 57 : } 58 : 59 138395 : explicit operator bool() const { return _handle != nullptr; } 60 : 61 : DsoFlags getFlags() const { return _flags; } 62 : 63 25 : StringView getError() const { return _error; } 64 : 65 : void close(); 66 : 67 : bool isSelf() const; 68 : bool isLazy() const; 69 : 70 : protected: 71 : void *loadSym(StringView); 72 : 73 : DsoFlags _flags = DsoFlags::None; 74 : void *_handle = nullptr; 75 : const char *_error = nullptr; 76 : }; 77 : 78 : } 79 : 80 : #endif /* CORE_CORE_UTILS_SPDSO_H_ */