proxy_node.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* proxy_node.h - node redirecting to other nodes
  2. * Copyright (C) 2017 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef CORE_NODE_PROXY_NODE_H_E46910AC_D319_5AB4_AB28_F69FAF7A6767
  18. #define CORE_NODE_PROXY_NODE_H_E46910AC_D319_5AB4_AB28_F69FAF7A6767
  19. #include <functional>
  20. #include "node.h"
  21. namespace rainynite::core {
  22. /**
  23. * ProxyNode is a node that returns value of another node, possibly with changed context.
  24. *
  25. * Examples include various switch (e.g. Animated) and time-manipulating nodes.
  26. */
  27. template <typename T>
  28. class ProxyNode : public Node<T> {
  29. public:
  30. /**
  31. * Get proxied NodeInContext
  32. */
  33. virtual NodeInContext get_proxy(shared_ptr<Context> ctx) const = 0;
  34. public:
  35. T get(shared_ptr<Context> ctx) const override {
  36. auto [node, nctx] = get_proxy(ctx);
  37. if (auto vnode = dynamic_cast<BaseValue<T>*>(node.get()))
  38. return vnode->get(nctx);
  39. throw NodeAccessError("Proxied node is of different type");
  40. }
  41. };
  42. } // namespace rainynite::core
  43. #endif