rvargas@google.com | e106ef6 | 2011-03-26 03:48:03 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
hbono@chromium.org | 83bc0b6 | 2010-03-10 15:23:35 +0900 | [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 | |
thakis@chromium.org | 927d728 | 2011-05-20 08:34:17 +0900 | [diff] [blame] | 5 | #ifndef BASE_SCOPED_NATIVE_LIBRARY_H_ |
| 6 | #define BASE_SCOPED_NATIVE_LIBRARY_H_ |
hbono@chromium.org | 83bc0b6 | 2010-03-10 15:23:35 +0900 | [diff] [blame] | 7 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 8 | #include "base/base_export.h" |
hbono@chromium.org | 83bc0b6 | 2010-03-10 15:23:35 +0900 | [diff] [blame] | 9 | #include "base/native_library.h" |
| 10 | |
| 11 | namespace base { |
| 12 | |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 13 | class FilePath; |
| 14 | |
hbono@chromium.org | 83bc0b6 | 2010-03-10 15:23:35 +0900 | [diff] [blame] | 15 | // A class which encapsulates a base::NativeLibrary object available only in a |
| 16 | // scope. |
| 17 | // This class automatically unloads the loaded library in its destructor. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 18 | class BASE_EXPORT ScopedNativeLibrary { |
hbono@chromium.org | 83bc0b6 | 2010-03-10 15:23:35 +0900 | [diff] [blame] | 19 | public: |
brettw@chromium.org | 3cd7170 | 2010-11-10 10:16:11 +0900 | [diff] [blame] | 20 | // Initializes with a NULL library. |
| 21 | ScopedNativeLibrary(); |
hbono@chromium.org | 83bc0b6 | 2010-03-10 15:23:35 +0900 | [diff] [blame] | 22 | |
brettw@chromium.org | 3cd7170 | 2010-11-10 10:16:11 +0900 | [diff] [blame] | 23 | // Takes ownership of the given library handle. |
| 24 | explicit ScopedNativeLibrary(NativeLibrary library); |
hbono@chromium.org | 83bc0b6 | 2010-03-10 15:23:35 +0900 | [diff] [blame] | 25 | |
brettw@chromium.org | 3cd7170 | 2010-11-10 10:16:11 +0900 | [diff] [blame] | 26 | // Opens the given library and manages its lifetime. |
| 27 | explicit ScopedNativeLibrary(const FilePath& library_path); |
| 28 | |
| 29 | ~ScopedNativeLibrary(); |
| 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(); |
hbono@chromium.org | 83bc0b6 | 2010-03-10 15:23:35 +0900 | [diff] [blame] | 43 | |
| 44 | private: |
brettw@chromium.org | 3cd7170 | 2010-11-10 10:16:11 +0900 | [diff] [blame] | 45 | NativeLibrary library_; |
| 46 | |
apatrick@chromium.org | 064bd53 | 2010-07-13 02:58:14 +0900 | [diff] [blame] | 47 | DISALLOW_COPY_AND_ASSIGN(ScopedNativeLibrary); |
hbono@chromium.org | 83bc0b6 | 2010-03-10 15:23:35 +0900 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | } // namespace base |
| 51 | |
danakj | 0393da3 | 2015-03-10 09:31:16 +0900 | [diff] [blame] | 52 | #endif // BASE_SCOPED_NATIVE_LIBRARY_H_ |