scoped_native_library.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #ifndef BASE_SCOPED_NATIVE_LIBRARY_H_
  5. #define BASE_SCOPED_NATIVE_LIBRARY_H_
  6. #include "base/base_export.h"
  7. #include "base/native_library.h"
  8. #include "base/scoped_generic.h"
  9. namespace base {
  10. class FilePath;
  11. struct BASE_EXPORT NativeLibraryTraits {
  12. // It's assumed that this is a fast inline function with little-to-no
  13. // penalty for duplicate calls. This must be a static function even
  14. // for stateful traits.
  15. static NativeLibrary InvalidValue() { return nullptr; }
  16. // This free function will not be called if library == InvalidValue()!
  17. static void Free(NativeLibrary library);
  18. };
  19. // A class which encapsulates a base::NativeLibrary object available only in a
  20. // scope.
  21. // This class automatically unloads the loaded library in its destructor.
  22. class BASE_EXPORT ScopedNativeLibrary
  23. : public ScopedGeneric<NativeLibrary, NativeLibraryTraits> {
  24. public:
  25. // Initializes with a NULL library.
  26. ScopedNativeLibrary();
  27. // Takes ownership of the given library handle.
  28. explicit ScopedNativeLibrary(NativeLibrary library);
  29. // Opens the given library and manages its lifetime.
  30. explicit ScopedNativeLibrary(const FilePath& library_path);
  31. // Move constructor. Takes ownership of handle stored in |scoped_library|
  32. ScopedNativeLibrary(ScopedNativeLibrary&& scoped_library);
  33. // Move assignment operator. Takes ownership of handle stored in
  34. // |scoped_library|.
  35. ScopedNativeLibrary& operator=(ScopedNativeLibrary&& scoped_library) =
  36. default;
  37. ScopedNativeLibrary(const ScopedNativeLibrary&) = delete;
  38. ScopedNativeLibrary& operator=(const ScopedNativeLibrary&) = delete;
  39. ~ScopedNativeLibrary() override;
  40. void* GetFunctionPointer(const char* function_name) const;
  41. const NativeLibraryLoadError* GetError() const;
  42. private:
  43. NativeLibraryLoadError error_;
  44. };
  45. } // namespace base
  46. #endif // BASE_SCOPED_NATIVE_LIBRARY_H_