Registry.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ///
  2. /// \file Plugin/Registry.hpp
  3. ///
  4. /// PluginRegistry is a collection of plugins organized into a tree structure.
  5. ///
  6. /// \copyright
  7. /// Copyright (c) 2013-2018 Josh Blum
  8. /// SPDX-License-Identifier: BSL-1.0
  9. ///
  10. #pragma once
  11. #include <Pothos/Config.hpp>
  12. #include <Pothos/Plugin/Plugin.hpp>
  13. #include <Pothos/Plugin/Path.hpp>
  14. #include <string>
  15. #include <vector>
  16. namespace Pothos {
  17. /*!
  18. * Structure for holding a dump of the registry info.
  19. */
  20. struct POTHOS_API PluginRegistryInfoDump
  21. {
  22. PluginRegistryInfoDump(void);
  23. std::string pluginPath;
  24. std::string objectType;
  25. std::string modulePath;
  26. std::string moduleVersion;
  27. std::vector<PluginRegistryInfoDump> subInfo;
  28. };
  29. /*!
  30. * PluginRegistry is a singleton class with static access methods.
  31. */
  32. class POTHOS_API PluginRegistry
  33. {
  34. public:
  35. /*!
  36. * Add a plugin to the registry at the specified path.
  37. * \param plugin the plugin to register
  38. */
  39. static void add(const Plugin &plugin);
  40. /*!
  41. * Convenience method to register a new plugin from components.
  42. */
  43. template <typename ValueType>
  44. static void add(const PluginPath &path, ValueType &&value);
  45. /*!
  46. * Convenience method to register a new callable plugin from components.
  47. * The second parameter must be a function pointer type.
  48. * The resulting plugin will contain a Pothos::Callable.
  49. */
  50. template <typename CallType>
  51. static void addCall(const PluginPath &path, const CallType &call);
  52. /*!
  53. * Remove a plugin from the registry at the specified path.
  54. * \throws PluginRegistryError if the path is not in the registry
  55. * \param path the path to the plugin in the registry
  56. * \return the removed Plugin
  57. */
  58. static Plugin remove(const PluginPath &path);
  59. /*!
  60. * Get a plugin from the registry at the specified path.
  61. * \throws PluginRegistryError if the path is not in the registry
  62. * \param path the path to the plugin in the registry
  63. * \return the Plugin at the specified path
  64. */
  65. static Plugin get(const PluginPath &path);
  66. /*!
  67. * Is there nothing located at the given path node?
  68. * Subtrees may have plugins, but this direct node may not.
  69. * \return true if the path node is empty
  70. */
  71. static bool empty(const PluginPath &path);
  72. /*!
  73. * Does the path exist in the registry?
  74. * \return true if the path exists
  75. */
  76. static bool exists(const PluginPath &path);
  77. /*!
  78. * List the PluginPath names at the given path.
  79. * The result will be in the order that the plugins were added.
  80. * \param path the base or directory path
  81. * \return a list of names in this path
  82. */
  83. static std::vector<std::string> list(const PluginPath &path);
  84. /*!
  85. * Create a dump structure of the entire registry.
  86. */
  87. static PluginRegistryInfoDump dump(void);
  88. private:
  89. //! private constructor: we don't make PluginRegistry instances
  90. PluginRegistry(void){}
  91. };
  92. } //namespace Pothos
  93. #include <utility> //std::forward
  94. template <typename ValueType>
  95. void Pothos::PluginRegistry::add(const PluginPath &path, ValueType &&value)
  96. {
  97. Pothos::PluginRegistry::add(Pothos::Plugin(path, std::forward<ValueType>(value)));
  98. }
  99. #include <Pothos/Callable/CallableImpl.hpp> //used in template definition below:
  100. template <typename CallType>
  101. void Pothos::PluginRegistry::addCall(const PluginPath &path, const CallType &call)
  102. {
  103. Pothos::PluginRegistry::add(Pothos::Plugin(path, Pothos::Callable(call)));
  104. }