list.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* list.cpp - list utils
  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. #include <core/std/algorithm.h>
  18. #include <core/node_info.h>
  19. #include <core/node/proxy_node.h>
  20. #include <core/node/property.h>
  21. #include <core/all_types.h>
  22. namespace rainynite::core {
  23. class ToUntypedList : public Node<Nothing> {
  24. public:
  25. ToUntypedList() {
  26. init_property("source", {}, make_value<Nothing>());
  27. }
  28. public:
  29. vector<NodeInContext> get_list_links(shared_ptr<Context> ctx) const override {
  30. if (auto list = get_property("source"))
  31. return list->get_list_links(ctx);
  32. return {};
  33. }
  34. Nothing get(shared_ptr<Context> /*ctx*/) const override {
  35. return {};
  36. }
  37. };
  38. REGISTER_NODE(ToUntypedList);
  39. /**
  40. * Convert UntypedListValue to typed list.
  41. *
  42. * NOTE: currently doesn't do any type-checking!
  43. */
  44. template <typename T>
  45. class ToTypedList : public Node<vector<T>> {
  46. public:
  47. ToTypedList() {
  48. auto src = make_shared<UntypedListValue>();
  49. src->new_id();
  50. this->template init_property("source", Type(typeid(Nothing)), std::move(src));
  51. }
  52. vector<NodeInContext> get_list_links(shared_ptr<Context> ctx) const override {
  53. if (auto list = this->get_property("source"))
  54. return list->get_list_links(ctx);
  55. return {};
  56. }
  57. };
  58. NODE_INFO_TEMPLATE(ToTypedList, ToTypedList<T>, vector<T>);
  59. TYPE_INSTANCES(ToTypedListNodeInfo)
  60. template <typename T>
  61. class ListElement : public ProxyNode<T> {
  62. public:
  63. ListElement() {
  64. this->init_property("source", {}, make_value<Nothing>());
  65. this->template init<double>(n, 0);
  66. }
  67. NodeInContext get_proxy(shared_ptr<Context> ctx) const override {
  68. auto list = this->get_property("source");
  69. auto l = list->get_list_links(ctx);
  70. if (l.size() == 0)
  71. throw NodeAccessError("Requested element of empty list");
  72. size_t n = clamp(get_n()->get(ctx), 0.0, l.size()-1.0);
  73. return l[n];
  74. }
  75. private:
  76. NODE_PROPERTY(n, double);
  77. };
  78. NODE_INFO_TEMPLATE(ListElement, ListElement<T>, T);
  79. TYPE_INSTANCES(ListElementNodeInfo)
  80. } // namespace rainynite::core