blob: 96765659a294794fc9f1d2d6a8cdbb4876e48e5c [file] [log] [blame]
[email protected]26fbf802011-03-25 18:48:031// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]da2566e12010-03-10 06:23:352// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e0785902011-05-19 23:34:175#ifndef BASE_SCOPED_NATIVE_LIBRARY_H_
6#define BASE_SCOPED_NATIVE_LIBRARY_H_
[email protected]da2566e12010-03-10 06:23:357
[email protected]0bea7252011-08-05 15:34:008#include "base/base_export.h"
[email protected]da2566e12010-03-10 06:23:359#include "base/native_library.h"
Cliff Smolinskyf395bef2019-04-12 23:45:4410#include "base/scoped_generic.h"
[email protected]da2566e12010-03-10 06:23:3511
12namespace base {
13
[email protected]a3ef4832013-02-02 05:12:3314class FilePath;
15
Cliff Smolinskyf395bef2019-04-12 23:45:4416struct 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; }
21
22 // This free function will not be called if library == InvalidValue()!
23 static void Free(NativeLibrary library);
24};
25
[email protected]da2566e12010-03-10 06:23:3526// A class which encapsulates a base::NativeLibrary object available only in a
27// scope.
28// This class automatically unloads the loaded library in its destructor.
Cliff Smolinskyf395bef2019-04-12 23:45:4429class BASE_EXPORT ScopedNativeLibrary
30 : public ScopedGeneric<NativeLibrary, NativeLibraryTraits> {
[email protected]da2566e12010-03-10 06:23:3531 public:
[email protected]709a847e2010-11-10 01:16:1132 // Initializes with a NULL library.
33 ScopedNativeLibrary();
[email protected]da2566e12010-03-10 06:23:3534
[email protected]709a847e2010-11-10 01:16:1135 // Takes ownership of the given library handle.
36 explicit ScopedNativeLibrary(NativeLibrary library);
[email protected]da2566e12010-03-10 06:23:3537
[email protected]709a847e2010-11-10 01:16:1138 // Opens the given library and manages its lifetime.
39 explicit ScopedNativeLibrary(const FilePath& library_path);
40
Cliff Smolinskyf395bef2019-04-12 23:45:4441 // Move constructor. Takes ownership of handle stored in |scoped_library|
42 ScopedNativeLibrary(ScopedNativeLibrary&& scoped_library);
[email protected]709a847e2010-11-10 01:16:1143
Cliff Smolinskyf395bef2019-04-12 23:45:4444 // Move assignment operator. Takes ownership of handle stored in
45 // |scoped_library|.
46 ScopedNativeLibrary& operator=(ScopedNativeLibrary&& scoped_library) =
47 default;
wittmanf2d56442015-11-02 22:38:4048
David Bienvenu5f4d4f02020-09-27 16:55:0349 ScopedNativeLibrary(const ScopedNativeLibrary&) = delete;
50 ScopedNativeLibrary& operator=(const ScopedNativeLibrary&) = delete;
51
52 ~ScopedNativeLibrary() override;
53
[email protected]709a847e2010-11-10 01:16:1154 void* GetFunctionPointer(const char* function_name) const;
55
Cliff Smolinskyf395bef2019-04-12 23:45:4456 const NativeLibraryLoadError* GetError() const;
[email protected]da2566e12010-03-10 06:23:3557
58 private:
Cliff Smolinskyf395bef2019-04-12 23:45:4459 NativeLibraryLoadError error_;
[email protected]da2566e12010-03-10 06:23:3560};
61
62} // namespace base
63
danakj0a448602015-03-10 00:31:1664#endif // BASE_SCOPED_NATIVE_LIBRARY_H_