OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_SCOPED_NATIVE_LIBRARY_H_ | 5 #ifndef BASE_SCOPED_NATIVE_LIBRARY_H_ |
6 #define BASE_SCOPED_NATIVE_LIBRARY_H_ | 6 #define BASE_SCOPED_NATIVE_LIBRARY_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/native_library.h" | 9 #include "base/native_library.h" |
10 | 10 |
11 class FilePath; | 11 class FilePath; |
12 | 12 |
13 namespace base { | 13 namespace base { |
14 | 14 |
15 // A class which encapsulates a base::NativeLibrary object available only in a | 15 // A class which encapsulates a base::NativeLibrary object available only in a |
16 // scope. | 16 // scope. |
17 // This class automatically unloads the loaded library in its destructor. | 17 // This class automatically unloads the loaded library in its destructor. |
18 class ScopedNativeLibrary { | 18 class ScopedNativeLibrary { |
19 public: | 19 public: |
20 explicit ScopedNativeLibrary(const FilePath& library_path) { | 20 // Initializes with a NULL library. |
21 library_ = base::LoadNativeLibrary(library_path); | 21 ScopedNativeLibrary(); |
22 } | |
23 | 22 |
24 ~ScopedNativeLibrary() { | 23 // Takes ownership of the given library handle. |
25 if (library_) | 24 explicit ScopedNativeLibrary(NativeLibrary library); |
26 base::UnloadNativeLibrary(library_); | |
27 } | |
28 | 25 |
29 void* GetFunctionPointer(const char* function_name) { | 26 // Opens the given library and manages its lifetime. |
30 if (!library_) | 27 explicit ScopedNativeLibrary(const FilePath& library_path); |
31 return NULL; | 28 |
32 return base::GetFunctionPointerFromNativeLibrary(library_, function_name); | 29 ~ScopedNativeLibrary(); |
33 } | 30 |
| 31 // Returns true if there's a valid library loaded. |
| 32 bool is_valid() const { return !!library_; } |
| 33 |
| 34 void* GetFunctionPointer(const char* function_name) const; |
| 35 |
| 36 // Takes ownership of the given library handle. Any existing handle will |
| 37 // be freed. |
| 38 void Reset(NativeLibrary library); |
| 39 |
| 40 // Returns the native library handle and removes it from this object. The |
| 41 // caller must manage the lifetime of the handle. |
| 42 NativeLibrary Release(); |
34 | 43 |
35 private: | 44 private: |
36 base::NativeLibrary library_; | 45 NativeLibrary library_; |
| 46 |
37 DISALLOW_COPY_AND_ASSIGN(ScopedNativeLibrary); | 47 DISALLOW_COPY_AND_ASSIGN(ScopedNativeLibrary); |
38 }; | 48 }; |
39 | 49 |
40 } // namespace base | 50 } // namespace base |
41 | 51 |
42 #endif // BASE_SCOPED_NATIVE_LIBRARY_H_ | 52 #endif // BASE_SCOPED_NATIVE_LIBRARY_H_ |
OLD | NEW |