time_list.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * time_list.cpp - TimeList node
  3. * Copyright (C) 2017 caryoscelus
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <core/node_info.h>
  19. #include <core/node/node.h>
  20. #include <core/node/property.h>
  21. #include <core/node/proxy_node.h>
  22. #include <core/time/time_period.h>
  23. #include <core/all_types.h>
  24. #include <core/context.h>
  25. namespace rainynite::core {
  26. namespace nodes {
  27. /**
  28. * Node that makes a list of its source at different times.
  29. *
  30. * TODO: use List<Time> instead of TimePeriod and Time parameters
  31. */
  32. template <typename T>
  33. class TimeList : public ProxyListNode<T> {
  34. public:
  35. TimeList() {
  36. this->template init<T>(source, {});
  37. this->template init<TimePeriod>(period, {});
  38. this->template init<Time>(step, {});
  39. }
  40. public:
  41. void step_into_list(shared_ptr<Context> ctx, std::function<void(NodeInContext)> f) const override {
  42. try {
  43. auto source = get_source();
  44. auto period = get_period()->get(ctx);
  45. auto step = get_step()->get(ctx);
  46. for (auto t = period.get_first(); t < period.get_last(); t += step) {
  47. auto nctx = make_shared<Context>(*ctx);
  48. nctx->set_time(t);
  49. f({source, nctx});
  50. }
  51. } catch (...) {
  52. }
  53. }
  54. private:
  55. NODE_PROPERTY(source, T);
  56. NODE_PROPERTY(period, TimePeriod);
  57. NODE_PROPERTY(step, Time);
  58. };
  59. NODE_INFO_TEMPLATE(TimeList, TimeList<T>, vector<T>);
  60. TYPE_INSTANCES(TimeListNodeInfo)
  61. } // namespace nodes
  62. } // namespace rainynite::core