1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
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/macros.h"
10 #include "base/native_library.h"
11 #include "base/scoped_generic.h"
17 struct BASE_EXPORT NativeLibraryTraits {
18 // It's assumed that this is a fast inline function with little-to-no
19 // penalty for duplicate calls. This must be a static function even
20 // for stateful traits.
21 static NativeLibrary InvalidValue() { return nullptr; }
23 // This free function will not be called if library == InvalidValue()!
24 static void Free(NativeLibrary library);
27 // A class which encapsulates a base::NativeLibrary object available only in a
29 // This class automatically unloads the loaded library in its destructor.
30 class BASE_EXPORT ScopedNativeLibrary
31 : public ScopedGeneric<NativeLibrary, NativeLibraryTraits> {
33 // Initializes with a NULL library.
34 ScopedNativeLibrary();
36 ~ScopedNativeLibrary() override;
38 // Takes ownership of the given library handle.
39 explicit ScopedNativeLibrary(NativeLibrary library);
41 // Opens the given library and manages its lifetime.
42 explicit ScopedNativeLibrary(const FilePath& library_path);
44 // Move constructor. Takes ownership of handle stored in |scoped_library|
45 ScopedNativeLibrary(ScopedNativeLibrary&& scoped_library);
47 // Move assignment operator. Takes ownership of handle stored in
49 ScopedNativeLibrary& operator=(ScopedNativeLibrary&& scoped_library) =
52 void* GetFunctionPointer(const char* function_name) const;
54 const NativeLibraryLoadError* GetError() const;
57 NativeLibraryLoadError error_;
59 DISALLOW_COPY_AND_ASSIGN(ScopedNativeLibrary);
64 #endif // BASE_SCOPED_NATIVE_LIBRARY_H_