1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_SCOPED_NATIVE_LIBRARY_H_
6 #define BASE_SCOPED_NATIVE_LIBRARY_H_
8 #include "base/base_export.h"
9 #include "base/native_library.h"
10 #include "base/scoped_generic.h"
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; }
22 // This free function will not be called if library == InvalidValue()!
23 static void Free(NativeLibrary library);
26 // A class which encapsulates a base::NativeLibrary object available only in a
28 // This class automatically unloads the loaded library in its destructor.
29 class BASE_EXPORT ScopedNativeLibrary
30 : public ScopedGeneric<NativeLibrary, NativeLibraryTraits> {
32 // Initializes with a NULL library.
33 ScopedNativeLibrary();
35 // Takes ownership of the given library handle.
36 explicit ScopedNativeLibrary(NativeLibrary library);
38 // Opens the given library and manages its lifetime.
39 explicit ScopedNativeLibrary(const FilePath& library_path);
41 // Move constructor. Takes ownership of handle stored in |scoped_library|
42 ScopedNativeLibrary(ScopedNativeLibrary&& scoped_library);
44 // Move assignment operator. Takes ownership of handle stored in
46 ScopedNativeLibrary& operator=(ScopedNativeLibrary&& scoped_library) =
49 ScopedNativeLibrary(const ScopedNativeLibrary&) = delete;
50 ScopedNativeLibrary& operator=(const ScopedNativeLibrary&) = delete;
52 ~ScopedNativeLibrary() override;
54 void* GetFunctionPointer(const char* function_name) const;
56 const NativeLibraryLoadError* GetError() const;
59 NativeLibraryLoadError error_;
64 #endif // BASE_SCOPED_NATIVE_LIBRARY_H_