abstract_node.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * abstract_node.h - abstract 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__ABSTRACT_NODE_H__24ECBF82
  19. #define __CORE__NODE__ABSTRACT_NODE_H__24ECBF82
  20. #include <boost/signals2/signal.hpp>
  21. #include <core/std/map.h>
  22. #include "abstract_list.h"
  23. namespace rainynite::core {
  24. class AbstractNode : public AbstractListLinked {
  25. public:
  26. virtual ~AbstractNode();
  27. public:
  28. AbstractReference get_property(string const& name) const;
  29. template <typename T>
  30. BaseReference<T> get_property_as(string const& name) const {
  31. return dynamic_pointer_cast<BaseValue<T>>(get_property(name));
  32. }
  33. template <typename T>
  34. optional<T> get_property_value(string const& name, shared_ptr<Context> context) const {
  35. try {
  36. return get_property_as<T>(name)->get(context);
  37. } catch (NodeAccessError const&) {
  38. return {};
  39. } catch (...) {
  40. return {};
  41. }
  42. }
  43. void set_property(string const& name, AbstractReference ref);
  44. bool remove_property(string const& name);
  45. size_t init_property(string const& name, optional<Type> type, AbstractReference value);
  46. map<string, AbstractReference> get_link_map() const;
  47. string get_name_at(size_t id) {
  48. return names_list[id];
  49. }
  50. public:
  51. vector<AbstractReference> get_links() const override {
  52. return numbered_storage;
  53. }
  54. AbstractReference get_link(size_t i) const override {
  55. return get_by_id(i);
  56. }
  57. optional<Type> get_link_type(size_t i) const override {
  58. return types[i];
  59. }
  60. void set_link(size_t i, AbstractReference value) override;
  61. size_t link_count() const override {
  62. return numbered_storage.size();
  63. }
  64. protected:
  65. /**
  66. * This function should be called when node has changed.
  67. *
  68. * In practice, it exists solely due to class hierarchy and lack of
  69. * AbstractValue inheritance in AbstractNode.
  70. */
  71. virtual void node_changed() = 0;
  72. private:
  73. AbstractReference const& get_by_id(size_t index) const {
  74. return numbered_storage[index];
  75. }
  76. AbstractReference& get_by_id(size_t index) {
  77. return numbered_storage[index];
  78. }
  79. private:
  80. map<string, size_t> named_storage;
  81. vector<string> names_list;
  82. vector<AbstractReference> numbered_storage;
  83. vector<boost::signals2::connection> signal_connections;
  84. vector<optional<Type>> types;
  85. };
  86. } // namespace rainynite::core
  87. #endif