| 6 | #define BASE_SCOPED_NATIVE_LIBRARY_H_ |
| 7 |
| 8 | #include "base/base_export.h" |
| 9 | #include "base/macros.h" |
| 10 | #include "base/native_library.h" |
| 11 | #include "base/scoped_generic.h" |
| 12 |
| 13 | namespace base { |
| 14 |
| 15 | class FilePath; |
| 16 |
| 17 | struct BASE_EXPORT NativeLibraryTraits { |
| 18 | // It's assumed that this is a fast inline function with little-to-no |
| 19 | // penalty for duplicate calls. This must be a static function even |
| 20 | // for stateful traits. |
| 21 | static NativeLibrary InvalidValue() { return nullptr; } |
| 22 |
| 23 | // This free function will not be called if library == InvalidValue()! |
| 24 | static void Free(NativeLibrary library); |
| 25 | }; |
| 26 |
| 27 | // A class which encapsulates a base::NativeLibrary object available only in a |
| 28 | // scope. |
| 29 | // This class automatically unloads the loaded library in its destructor. |
| 30 | class BASE_EXPORT ScopedNativeLibrary |
| 31 | : public ScopedGeneric<NativeLibrary, NativeLibraryTraits> { |
| 32 | public: |
| 33 | // Initializes with a NULL library. |
| 34 | ScopedNativeLibrary(); |
| 35 |
| 36 | ~ScopedNativeLibrary() override; |
| 37 |
| 38 | // Takes ownership of the given library handle. |
| 39 | explicit ScopedNativeLibrary(NativeLibrary library); |
| 40 |
| 41 | // Opens the given library and manages its lifetime. |
| 42 | explicit ScopedNativeLibrary(const FilePath& library_path); |
| 43 |
| 44 | // Move constructor. Takes ownership of handle stored in |scoped_library| |
| 45 | ScopedNativeLibrary(ScopedNativeLibrary&& scoped_library); |
| 46 |
| 47 | // Move assignment operator. Takes ownership of handle stored in |
| 48 | // |scoped_library|. |
| 49 | ScopedNativeLibrary& operator=(ScopedNativeLibrary&& scoped_library) = |
| 50 | default; |
| 51 |
| 52 | void* GetFunctionPointer(const char* function_name) const; |
| 53 |
| 54 | const NativeLibraryLoadError* GetError() const; |
| 55 |
| 56 | private: |
| 57 | NativeLibraryLoadError error_; |
| 58 |
| 59 | DISALLOW_COPY_AND_ASSIGN(ScopedNativeLibrary); |
| 60 | }; |
| 61 |
| 62 | } // namespace base |
| 63 |
| 64 | #endif // BASE_SCOPED_NATIVE_LIBRARY_H_ |
| 65 |