BlockRegistry.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. ///
  2. /// \file Framework/BlockRegistry.hpp
  3. ///
  4. /// A BlockRegistry registers a block's factory function.
  5. ///
  6. /// \copyright
  7. /// Copyright (c) 2014-2016 Josh Blum
  8. /// 2021 Nicholas Corgan
  9. /// SPDX-License-Identifier: BSL-1.0
  10. ///
  11. #pragma once
  12. #include <Pothos/Config.hpp>
  13. #include <Pothos/Callable/Callable.hpp>
  14. #include <Pothos/Proxy/Proxy.hpp>
  15. #include <string>
  16. namespace Pothos {
  17. /*!
  18. * The BlockRegistry class registers factories for topological elements.
  19. * These elements include Blocks and sub-Topologies (hierarchies of elements).
  20. * A BlockRegistry can be created at static initialization time
  21. * so that modules providing blocks will automatically register.
  22. * Usage example (put this at the bottom of your c++ source file)
  23. * static Pothos::BlockRegistry registerMyBlock("/my/factory/path", &MyBlock::make);
  24. */
  25. class POTHOS_API BlockRegistry
  26. {
  27. public:
  28. /*!
  29. * Register a factory function into the plugin registry.
  30. * The resulting factory path will be /blocks/path.
  31. * Example: a path of /foo/bar will register to /blocks/foo/bar.
  32. *
  33. * Because this call is used at static initialization time,
  34. * it does not throw. However, registration errors are logged,
  35. * and the block will not be available at runtime.
  36. *
  37. * The return type of the call must be Block* or Topology*.
  38. *
  39. * \param path the factory path beginning with a slash ("/")
  40. * \param factory the Callable factory function
  41. */
  42. BlockRegistry(const std::string &path, const Callable &factory);
  43. /*!
  44. * Instantiate a block given the factory path and arguments.
  45. * \param path the factory path beginning with a slash ("/")
  46. * \param args a variable number of factory arguments
  47. * \return the newly created block instance as a Proxy
  48. */
  49. template <typename... ArgsType>
  50. static Proxy make(const std::string &path, ArgsType&&... args);
  51. /*!
  52. * Checks if block is registered at a given path.
  53. * \param path the factory path beginning with a slash ("/")
  54. * \return whether a block is registered at the given path
  55. */
  56. static bool doesBlockExist(const std::string &path);
  57. };
  58. } //namespace Pothos