Flutter Engine
native_library.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 FLUTTER_FML_NATIVE_LIBRARY_H_
6 #define FLUTTER_FML_NATIVE_LIBRARY_H_
7 
8 #include <optional>
9 
10 #include "flutter/fml/build_config.h"
11 #include "flutter/fml/macros.h"
12 #include "flutter/fml/memory/ref_counted.h"
13 #include "flutter/fml/memory/ref_ptr.h"
14 
15 #if defined(OS_WIN)
16 #include <windows.h>
17 #endif // defined(OS_WIN)
18 
19 namespace fml {
20 class NativeLibrary : public fml::RefCountedThreadSafe<NativeLibrary> {
21  public:
22 #if OS_WIN
23  using Handle = HMODULE;
24  using SymbolHandle = FARPROC;
25 #else // OS_WIN
26  using Handle = void*;
27  using SymbolHandle = void*;
28 #endif // OS_WIN
29 
30  static fml::RefPtr<NativeLibrary> Create(const char* path);
31 
33  Handle handle,
34  bool close_handle_when_done);
35 
37 
38  template <typename T>
39  const std::optional<T> ResolveFunction(const char* symbol) {
40  auto* resolved_symbol = Resolve(symbol);
41  if (!resolved_symbol) {
42  return std::nullopt;
43  }
44  return std::optional<T>(reinterpret_cast<T>(resolved_symbol));
45  }
46 
47  const uint8_t* ResolveSymbol(const char* symbol) {
48  auto* resolved_symbol = reinterpret_cast<const uint8_t*>(Resolve(symbol));
49  if (resolved_symbol == nullptr) {
50  FML_DLOG(INFO) << "Could not resolve symbol in library: " << symbol;
51  }
52  return resolved_symbol;
53  }
54 
55  private:
56  Handle handle_ = nullptr;
57  bool close_handle_ = true;
58 
59  explicit NativeLibrary(const char* path);
60 
61  NativeLibrary(Handle handle, bool close_handle);
62 
63  ~NativeLibrary();
64 
65  Handle GetHandle() const;
66  SymbolHandle Resolve(const char* symbol) const;
67 
68  FML_DISALLOW_COPY_AND_ASSIGN(NativeLibrary);
69  FML_FRIEND_REF_COUNTED_THREAD_SAFE(NativeLibrary);
70  FML_FRIEND_MAKE_REF_COUNTED(NativeLibrary);
71 };
72 
73 } // namespace fml
74 
75 #endif // FLUTTER_FML_NATIVE_LIBRARY_H_
static fml::RefPtr< NativeLibrary > CreateForCurrentProcess()
static fml::RefPtr< NativeLibrary > CreateWithHandle(Handle handle, bool close_handle_when_done)
const uint8_t * ResolveSymbol(const char *symbol)
Definition: ascii_trie.cc:9
static fml::RefPtr< NativeLibrary > Create(const char *path)
HINSTANCE HMODULE
Definition: windows_types.h:96
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
#define FML_DLOG(severity)
Definition: logging.h:85
const std::optional< T > ResolveFunction(const char *symbol)