[email protected] | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | da2566e1 | 2010-03-10 06:23:35 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | e078590 | 2011-05-19 23:34:17 | [diff] [blame] | 5 | #ifndef BASE_SCOPED_NATIVE_LIBRARY_H_ |
6 | #define BASE_SCOPED_NATIVE_LIBRARY_H_ | ||||
[email protected] | da2566e1 | 2010-03-10 06:23:35 | [diff] [blame] | 7 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 8 | #include "base/base_export.h" |
[email protected] | da2566e1 | 2010-03-10 06:23:35 | [diff] [blame] | 9 | #include "base/native_library.h" |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 10 | #include "base/scoped_generic.h" |
[email protected] | da2566e1 | 2010-03-10 06:23:35 | [diff] [blame] | 11 | |
12 | namespace base { | ||||
13 | |||||
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 14 | class FilePath; |
15 | |||||
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 16 | struct BASE_EXPORT NativeLibraryTraits { |
17 | // It's assumed that this is a fast inline function with little-to-no | ||||
18 | // penalty for duplicate calls. This must be a static function even | ||||
19 | // for stateful traits. | ||||
20 | static NativeLibrary InvalidValue() { return nullptr; } | ||||
21 | |||||
22 | // This free function will not be called if library == InvalidValue()! | ||||
23 | static void Free(NativeLibrary library); | ||||
24 | }; | ||||
25 | |||||
[email protected] | da2566e1 | 2010-03-10 06:23:35 | [diff] [blame] | 26 | // A class which encapsulates a base::NativeLibrary object available only in a |
27 | // scope. | ||||
28 | // This class automatically unloads the loaded library in its destructor. | ||||
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 29 | class BASE_EXPORT ScopedNativeLibrary |
30 | : public ScopedGeneric<NativeLibrary, NativeLibraryTraits> { | ||||
[email protected] | da2566e1 | 2010-03-10 06:23:35 | [diff] [blame] | 31 | public: |
[email protected] | 709a847e | 2010-11-10 01:16:11 | [diff] [blame] | 32 | // Initializes with a NULL library. |
33 | ScopedNativeLibrary(); | ||||
[email protected] | da2566e1 | 2010-03-10 06:23:35 | [diff] [blame] | 34 | |
[email protected] | 709a847e | 2010-11-10 01:16:11 | [diff] [blame] | 35 | // Takes ownership of the given library handle. |
36 | explicit ScopedNativeLibrary(NativeLibrary library); | ||||
[email protected] | da2566e1 | 2010-03-10 06:23:35 | [diff] [blame] | 37 | |
[email protected] | 709a847e | 2010-11-10 01:16:11 | [diff] [blame] | 38 | // Opens the given library and manages its lifetime. |
39 | explicit ScopedNativeLibrary(const FilePath& library_path); | ||||
40 | |||||
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 41 | // Move constructor. Takes ownership of handle stored in |scoped_library| |
42 | ScopedNativeLibrary(ScopedNativeLibrary&& scoped_library); | ||||
[email protected] | 709a847e | 2010-11-10 01:16:11 | [diff] [blame] | 43 | |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 44 | // Move assignment operator. Takes ownership of handle stored in |
45 | // |scoped_library|. | ||||
46 | ScopedNativeLibrary& operator=(ScopedNativeLibrary&& scoped_library) = | ||||
47 | default; | ||||
wittman | f2d5644 | 2015-11-02 22:38:40 | [diff] [blame] | 48 | |
David Bienvenu | 5f4d4f0 | 2020-09-27 16:55:03 | [diff] [blame] | 49 | ScopedNativeLibrary(const ScopedNativeLibrary&) = delete; |
50 | ScopedNativeLibrary& operator=(const ScopedNativeLibrary&) = delete; | ||||
51 | |||||
52 | ~ScopedNativeLibrary() override; | ||||
53 | |||||
[email protected] | 709a847e | 2010-11-10 01:16:11 | [diff] [blame] | 54 | void* GetFunctionPointer(const char* function_name) const; |
55 | |||||
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 56 | const NativeLibraryLoadError* GetError() const; |
[email protected] | da2566e1 | 2010-03-10 06:23:35 | [diff] [blame] | 57 | |
58 | private: | ||||
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 59 | NativeLibraryLoadError error_; |
[email protected] | da2566e1 | 2010-03-10 06:23:35 | [diff] [blame] | 60 | }; |
61 | |||||
62 | } // namespace base | ||||
63 | |||||
danakj | 0a44860 | 2015-03-10 00:31:16 | [diff] [blame] | 64 | #endif // BASE_SCOPED_NATIVE_LIBRARY_H_ |