ScopedPlugin.hpp 768 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (c) 2021 Nicholas Corgan
  2. // SPDX-License-Identifier: BSL-1.0
  3. #pragma once
  4. #include <Pothos/Exception.hpp>
  5. #include <Pothos/Plugin.hpp>
  6. #include <Poco/Exception.h>
  7. #include <iostream>
  8. #include <string>
  9. template <typename ValueType>
  10. struct ScopedPlugin
  11. {
  12. ScopedPlugin(
  13. const std::string& path,
  14. ValueType&& value
  15. ):
  16. path(path)
  17. {
  18. Pothos::PluginRegistry::add(path, std::forward<ValueType>(value));
  19. }
  20. virtual ~ScopedPlugin()
  21. {
  22. POTHOS_EXCEPTION_TRY
  23. {
  24. Pothos::PluginRegistry::remove(path);
  25. }
  26. POTHOS_EXCEPTION_CATCH(const Pothos::Exception& ex)
  27. {
  28. std::cerr << ex.what() << std::endl;
  29. }
  30. }
  31. std::string path;
  32. };