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