abstract_node.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* abstract_node.h - abstract Node
  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. #ifndef CORE_NODE_ABSTRACT_NODE_H_EE148DA4_6D84_5EDA_A5B0_C192A55C5B09
  18. #define CORE_NODE_ABSTRACT_NODE_H_EE148DA4_6D84_5EDA_A5B0_C192A55C5B09
  19. #include <boost/signals2/signal.hpp>
  20. #include <core/std/map.h>
  21. #include "abstract_list.h"
  22. #include "doc_string.h"
  23. namespace rainynite::core {
  24. class AbstractNode {
  25. public:
  26. virtual AbstractReference get_property(string const& name) const = 0;
  27. virtual void set_property(string const& name, AbstractReference ref) = 0;
  28. virtual bool remove_property(string const& /*name*/) {
  29. return false;
  30. }
  31. virtual string get_name_at(size_t id) const = 0;
  32. virtual size_t get_name_id(string const& name) const = 0;
  33. virtual map<string, AbstractReference> get_link_map() const = 0;
  34. protected:
  35. /**
  36. * This function should be called when node has changed.
  37. *
  38. * In practice, it exists solely due to class hierarchy and lack of
  39. * AbstractValue inheritance in AbstractNode.
  40. */
  41. virtual void node_changed() = 0;
  42. public:
  43. template <typename T>
  44. shared_ptr<BaseValue<T>> get_property_as(string const& name) const {
  45. return base_value_cast<T>(get_property(name));
  46. }
  47. template <typename T>
  48. optional<T> get_property_value(string const& name, shared_ptr<Context> context) const {
  49. try {
  50. if (auto node = get_property_as<T>(name))
  51. return node->value(context);
  52. return {};
  53. } catch (NodeAccessError const&) {
  54. return {};
  55. } catch (...) {
  56. return {};
  57. }
  58. }
  59. };
  60. class AbstractNodeBase : public AbstractListLinked, public AbstractNode {
  61. };
  62. /**
  63. * Abstract node: entity with links to AbstractValues
  64. */
  65. class BaseOldNode : public AbstractNodeBase, public DocString {
  66. public:
  67. virtual ~BaseOldNode();
  68. public:
  69. AbstractReference get_property(string const& name) const override;
  70. void set_property(string const& name, AbstractReference ref) override;
  71. bool remove_property(string const& name) override;
  72. size_t init_property(string const& name, TypeConstraint type, AbstractReference value);
  73. map<string, AbstractReference> get_link_map() const override;
  74. string get_name_at(size_t id) const override {
  75. return names_list[id];
  76. }
  77. /// Get id of property name (throws on error)
  78. size_t get_name_id(string const& name) const override {
  79. return named_storage.at(name);
  80. }
  81. public:
  82. vector<AbstractReference> get_links() const override {
  83. return numbered_storage;
  84. }
  85. AbstractReference get_link(size_t i) const override {
  86. return get_by_id(i);
  87. }
  88. TypeConstraint get_link_type(size_t i) const override {
  89. return types[i];
  90. }
  91. void set_link(size_t i, AbstractReference value) override;
  92. size_t link_count() const override {
  93. return numbered_storage.size();
  94. }
  95. private:
  96. AbstractReference const& get_by_id(size_t index) const {
  97. return numbered_storage[index];
  98. }
  99. AbstractReference& get_by_id(size_t index) {
  100. return numbered_storage[index];
  101. }
  102. private:
  103. map<string, size_t> named_storage;
  104. vector<string> names_list;
  105. vector<AbstractReference> numbered_storage;
  106. vector<boost::signals2::connection> signal_connections;
  107. vector<TypeConstraint> types;
  108. };
  109. } // namespace rainynite::core
  110. #endif