time_list.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. template <typename T>
  28. class TimeList : public ProxyListNode<T> {
  29. public:
  30. TimeList() {
  31. this->template init<T>(source, {});
  32. this->template init<TimePeriod>(period, {});
  33. this->template init<Time>(step, {});
  34. }
  35. public:
  36. void step_into_list(shared_ptr<Context> ctx, std::function<void(NodeInContext)> f) const override {
  37. try {
  38. auto source = get_source();
  39. auto period = get_period()->get(ctx);
  40. auto step = get_step()->get(ctx);
  41. for (auto t = period.get_first(); t < period.get_last(); t += step) {
  42. auto nctx = make_shared<Context>(*ctx);
  43. nctx->set_time(t);
  44. f({source, nctx});
  45. }
  46. } catch (...) {
  47. }
  48. }
  49. private:
  50. NODE_PROPERTY(source, T);
  51. NODE_PROPERTY(period, TimePeriod);
  52. NODE_PROPERTY(step, Time);
  53. };
  54. NODE_INFO_TEMPLATE(TimeList, TimeList<T>, vector<T>);
  55. TYPE_INSTANCES(TimeListNodeInfo)
  56. } // namespace nodes
  57. } // namespace rainynite::core