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