Fcitx
library.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2016-2016 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 #ifndef _FCITX_UTILS_LIBRARY_H_
8 #define _FCITX_UTILS_LIBRARY_H_
9 
10 /// \addtogroup FcitxUtils
11 /// \{
12 /// \file
13 /// \brief Class to handler dynamic library.
14 
15 #include <cstddef>
16 #include <filesystem>
17 #include <functional>
18 #include <memory>
19 #include <string>
20 #include <fcitx-utils/fcitxutils_export.h>
21 #include <fcitx-utils/flags.h>
22 #include <fcitx-utils/macros.h>
23 
24 namespace fcitx {
25 
26 enum class LibraryLoadHint {
27  NoHint = 0,
28  ResolveAllSymbolsHint = 0x1,
29  PreventUnloadHint = 0x2,
30  ExportExternalSymbolsHint = 0x4,
31  NewNameSpace = 0x8,
32  DefaultHint = PreventUnloadHint,
33 };
34 
35 class LibraryPrivate;
36 
37 class FCITXUTILS_EXPORT Library {
38 public:
39  explicit Library(const std::string &path);
40  explicit Library(const char *path);
41  explicit Library(const std::filesystem::path &path = {});
42  FCITX_DECLARE_VIRTUAL_DTOR_MOVE(Library);
43 
44  bool loaded() const;
45  bool load(Flags<LibraryLoadHint> hint = LibraryLoadHint::DefaultHint);
46  bool unload();
47  void *resolve(const char *name);
48  bool findData(const char *slug, const char *magic, size_t lenOfMagic,
49  const std::function<void(const char *data)> &parser);
50  std::string error();
51  const std::string &path() const;
52  const std::filesystem::path &fspath() const;
53 
54  template <typename Func>
55  static auto toFunction(void *ptr) {
56  return reinterpret_cast<Func *>(ptr);
57  }
58 
59  static bool isNewNamespaceSupported();
60 
61 private:
62  std::unique_ptr<LibraryPrivate> d_ptr;
63  FCITX_DECLARE_PRIVATE(Library);
64 };
65 } // namespace fcitx
66 
67 #endif // _FCITX_UTILS_LIBRARY_H_
Definition: action.cpp:17
Class provides bit flag support for Enum.
Definition: flags.h:33
Helper template class to make easier to use type safe enum flags.