blob: e9923f43424a6383c80bc299bcb3d9968dc52670 [file] [log] [blame]
rvargas@google.come106ef62011-03-26 03:48:03 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
hbono@chromium.org83bc0b62010-03-10 15:23:35 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
thakis@chromium.org927d7282011-05-20 08:34:17 +09005#ifndef BASE_SCOPED_NATIVE_LIBRARY_H_
6#define BASE_SCOPED_NATIVE_LIBRARY_H_
hbono@chromium.org83bc0b62010-03-10 15:23:35 +09007
darin@chromium.orge585bed2011-08-06 00:34:00 +09008#include "base/base_export.h"
hbono@chromium.org83bc0b62010-03-10 15:23:35 +09009#include "base/native_library.h"
10
11namespace base {
12
brettw@chromium.orga7086942013-02-02 14:12:33 +090013class FilePath;
14
hbono@chromium.org83bc0b62010-03-10 15:23:35 +090015// 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.orge585bed2011-08-06 00:34:00 +090018class BASE_EXPORT ScopedNativeLibrary {
hbono@chromium.org83bc0b62010-03-10 15:23:35 +090019 public:
brettw@chromium.org3cd71702010-11-10 10:16:11 +090020 // Initializes with a NULL library.
21 ScopedNativeLibrary();
hbono@chromium.org83bc0b62010-03-10 15:23:35 +090022
brettw@chromium.org3cd71702010-11-10 10:16:11 +090023 // Takes ownership of the given library handle.
24 explicit ScopedNativeLibrary(NativeLibrary library);
hbono@chromium.org83bc0b62010-03-10 15:23:35 +090025
brettw@chromium.org3cd71702010-11-10 10:16:11 +090026 // 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.org83bc0b62010-03-10 15:23:35 +090043
44 private:
brettw@chromium.org3cd71702010-11-10 10:16:11 +090045 NativeLibrary library_;
46
apatrick@chromium.org064bd532010-07-13 02:58:14 +090047 DISALLOW_COPY_AND_ASSIGN(ScopedNativeLibrary);
hbono@chromium.org83bc0b62010-03-10 15:23:35 +090048};
49
50} // namespace base
51
thakis@chromium.org927d7282011-05-20 08:34:17 +090052#endif // BASE_MEMORY_NATIVE_LIBRARY_H_