node.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * node.h - 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. #ifndef __CORE__NODE__NODE_H__245D750C
  19. #define __CORE__NODE__NODE_H__245D750C
  20. #include "abstract_node.h"
  21. #include "abstract_value.h"
  22. #include "list.h"
  23. namespace rainynite::core {
  24. /**
  25. * Basic representation of any time-changeable Node
  26. */
  27. template <typename T>
  28. class Node : public BaseValue<T>, public AbstractNode {
  29. public:
  30. template <typename U>
  31. void init(string const& name, U value) {
  32. init_property(name, make_optional(Type(typeid(U))), make_value<U>(value));
  33. }
  34. template <typename U>
  35. void init_list(string const& name) {
  36. init_property(name, make_optional(Type(typeid(vector<U>))), make_shared<ListValue<U>>());
  37. }
  38. public:
  39. bool can_set_source(shared_ptr<AbstractValue> src) const override {
  40. if (link_count() == 0)
  41. return false;
  42. if (auto type = get_link_type(0))
  43. return src->get_type() == *type;
  44. return true;
  45. }
  46. void set_source(shared_ptr<AbstractValue> src) override {
  47. set_link(0, src);
  48. }
  49. protected:
  50. void node_changed() override {
  51. this->changed();
  52. }
  53. };
  54. } // namespace rainynite::core
  55. #endif