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. 4 5 #ifndef BASE_SCOPED_NATIVE_LIBRARY_H_ 6 #define BASE_SCOPED_NATIVE_LIBRARY_H_ 7 8 #include "base/base_export.h" 9 #include "base/macros.h" 10 #include "base/native_library.h" 11 12 namespace base { 13 14 class FilePath; 15 16 // A class which encapsulates a base::NativeLibrary object available only in a 17 // scope. 18 // This class automatically unloads the loaded library in its destructor. 19 class BASE_EXPORT ScopedNativeLibrary { 20 public: 21 // Initializes with a NULL library. 22 ScopedNativeLibrary(); 23 24 // Takes ownership of the given library handle. 25 explicit ScopedNativeLibrary(NativeLibrary library); 26 27 // Opens the given library and manages its lifetime. 28 explicit ScopedNativeLibrary(const FilePath& library_path); 29 30 ~ScopedNativeLibrary(); 31 32 // Returns true if there's a valid library loaded. is_valid()33 bool is_valid() const { return !!library_; } 34 get()35 NativeLibrary get() const { return library_; } 36 37 void* GetFunctionPointer(const char* function_name) const; 38 39 // Takes ownership of the given library handle. Any existing handle will 40 // be freed. 41 void Reset(NativeLibrary library); 42 43 // Returns the native library handle and removes it from this object. The 44 // caller must manage the lifetime of the handle. 45 NativeLibrary Release(); 46 47 private: 48 NativeLibrary library_; 49 50 DISALLOW_COPY_AND_ASSIGN(ScopedNativeLibrary); 51 }; 52 53 } // namespace base 54 55 #endif // BASE_SCOPED_NATIVE_LIBRARY_H_ 56