plugify  1.0.0.0
plugin.hpp
1 #pragma once
2 
3 #include <cstdint>
4 #include <optional>
5 #include <plugify/mem_addr.hpp>
6 #include <plugify/path.hpp>
7 #include <plugify/reference_wrapper.hpp>
8 #include <plugify_export.h>
9 #include <span>
10 #include <string>
11 
12 namespace plugify {
13  class Plugin;
14  class PluginDescriptorRef;
15  class MethodRef;
16 
24  enum class PluginState : uint8_t {
25  NotLoaded,
26  Error,
27  Loaded,
28  Running,
29  Terminating,
30  Unknown,
31  };
32 
37  using UniqueId = std::ptrdiff_t;
38 
46  using MethodData = std::pair<MethodRef, MemAddr>;
47 
52  class PLUGIFY_API PluginRef : public Ref<const Plugin> {
53  using Ref::Ref;
54  public:
59  UniqueId GetId() const noexcept;
60 
65  std::string_view GetName() const noexcept;
66 
71  std::string_view GetFriendlyName() const noexcept;
72 
77  std::filesystem::path_view GetBaseDir() const noexcept;
78 
83  PluginDescriptorRef GetDescriptor() const noexcept;
84 
89  PluginState GetState() const noexcept;
90 
95  std::string_view GetError() const noexcept;
96 
101  std::span<const MethodData> GetMethods() const noexcept;
102 
124  std::optional<std::filesystem::path_view> FindResource(std::filesystem::path_view path) const;
125  };
126  static_assert(is_ref_v<PluginRef>);
127 
131  namespace PluginUtils {
137  [[maybe_unused]] constexpr std::string_view ToString(PluginState state) {
138  switch (state) {
139  case PluginState::NotLoaded: return "NotLoaded";
140  case PluginState::Error: return "Error";
141  case PluginState::Loaded: return "Loaded";
142  case PluginState::Running: return "Running";
143  case PluginState::Terminating: return "Terminating";
144  default: return "Unknown";
145  }
146  }
147 
153  [[maybe_unused]] constexpr PluginState FromString(std::string_view state) {
154  if (state == "NotLoaded") {
155  return PluginState::NotLoaded;
156  } else if (state == "Error") {
157  return PluginState::Error;
158  } else if (state == "Loaded") {
159  return PluginState::Loaded;
160  } else if (state == "Running") {
161  return PluginState::Running;
162  } else if (state == "Terminating") {
163  return PluginState::Terminating;
164  }
165  return PluginState::Unknown;
166  }
167  } // namespace PluginUtils
168 
169 } // namespace plugify
A reference class for the PluginDescriptor structure.
Reference wrapper to access for plugin's information.
Definition: plugin.hpp:52
UniqueId GetId() const noexcept
Get the unique identifier of the plugin.
A lightweight reference wrapper for objects of type T.