new_node.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* new_node.h - new 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_NEW_NODE_H_C1EE08BB_8FA1_5CA3_9EF7_FCF458718F5E
  18. #define CORE_NODE_NEW_NODE_H_C1EE08BB_8FA1_5CA3_9EF7_FCF458718F5E
  19. #include "named_link_storage.h"
  20. namespace rainynite::core {
  21. template <class Self, typename Result, typename... Ts>
  22. class NewNode :
  23. public NamedLinkStorage<Self, Ts...>,
  24. public DocString,
  25. public BaseValue<Result>
  26. {
  27. using Storage = NamedLinkStorage<Self, Ts...>;
  28. public:
  29. bool can_set_source(shared_ptr<AbstractValue> src) const override {
  30. if (this->link_count() == 0)
  31. return false;
  32. return this->get_link_type(0).accept(src->get_type());
  33. }
  34. void set_source(shared_ptr<AbstractValue> src) override {
  35. this->set_link(0, src);
  36. }
  37. Result default_value(shared_ptr<Context> ctx) const noexcept override {
  38. if (this->link_count() == 0)
  39. return {};
  40. if (auto link = this->template get_link_as<Result>(0)) {
  41. return link->value(ctx);
  42. }
  43. return {};
  44. }
  45. size_t get_name_id(string const& name) const override {
  46. if (is_custom_property(name))
  47. return get_custom_property_id(name);
  48. return Storage::get_name_id(name);
  49. }
  50. AbstractReference get_property(string const& name) const override {
  51. if (is_custom_property(name))
  52. return get_custom_property(name);
  53. return Storage::get_property(name);
  54. }
  55. void set_property(string const& name, AbstractReference ref) override {
  56. if (is_custom_property(name))
  57. set_custom_property(name, ref);
  58. else
  59. Storage::set_property(name, ref);
  60. }
  61. bool remove_property(string const& name) override {
  62. if (is_custom_property(name))
  63. return remove_custom_property(name);
  64. else
  65. return Storage::remove_property(name);
  66. }
  67. size_t link_count() const override {
  68. return Storage::link_count()+cp_storage.size();
  69. }
  70. map<string, AbstractReference> get_link_map() const override {
  71. auto result = Storage::get_link_map();
  72. if (!has_custom_properties())
  73. return result;
  74. for (auto const& e : cp_names) {
  75. result.emplace(e.first, cp_storage[e.second]);
  76. }
  77. return result;
  78. }
  79. vector<AbstractReference> get_links() const override {
  80. auto result = Storage::get_links();
  81. if (!has_custom_properties())
  82. return result;
  83. result.insert(result.end(), cp_storage.begin(), cp_storage.end());
  84. return result;
  85. }
  86. string get_name_at(size_t id) const override {
  87. return apply_for_normal_or_custom<string>(
  88. id,
  89. [this](auto i) {
  90. return Storage::get_name_at(i);
  91. },
  92. [this](auto i) {
  93. return cp_name_list[i];
  94. }
  95. );
  96. }
  97. AbstractReference get_link(size_t i) const override {
  98. return apply_for_normal_or_custom<AbstractReference>(
  99. i,
  100. [this](auto i) {
  101. return Storage::get_link(i);
  102. },
  103. [this](auto i) {
  104. return cp_storage[i];
  105. }
  106. );
  107. }
  108. TypeConstraint get_link_type(size_t i) const override {
  109. return apply_for_normal_or_custom<TypeConstraint>(
  110. i,
  111. [this](auto i) {
  112. return Storage::get_link_type(i);
  113. },
  114. [](auto /*i*/) {
  115. return types::Any();
  116. }
  117. );
  118. }
  119. protected:
  120. void node_changed() override {
  121. this->changed();
  122. }
  123. void links_changed() override {
  124. this->changed();
  125. }
  126. private:
  127. template <typename T, class F, class G>
  128. T apply_for_normal_or_custom(size_t i, F normal, G custom) const {
  129. auto cp_id = (ptrdiff_t)i - (ptrdiff_t)Storage::link_count();
  130. if (cp_id < 0)
  131. return normal(i);
  132. size_t id = cp_id;
  133. if (id >= cp_storage.size())
  134. throw std::out_of_range("Link id out of range");
  135. return custom(id);
  136. }
  137. bool is_custom_property(string const& name) const {
  138. return !name.empty() && name[0] == '_';
  139. }
  140. size_t get_custom_property_id(string const& name) const {
  141. return Storage::link_count()+cp_names.at(name);
  142. }
  143. AbstractReference get_custom_property(string const& name) const {
  144. auto it = cp_names.find(name);
  145. if (it != cp_names.end())
  146. return cp_storage[it->second];
  147. throw NodeAccessError("Custom property '"+name+"' not found");
  148. }
  149. void set_custom_property(string const& name, AbstractReference ref) {
  150. auto it = cp_names.find(name);
  151. if (it != cp_names.end()) {
  152. cp_storage[it->second] = ref;
  153. } else {
  154. cp_names.emplace(name, cp_storage.size());
  155. cp_name_list.push_back(name);
  156. cp_storage.push_back(ref);
  157. }
  158. }
  159. bool remove_custom_property(string const& name) {
  160. auto it = cp_names.find(name);
  161. if (it != cp_names.end()) {
  162. auto id = it->second;
  163. cp_storage.erase(cp_storage.begin() + id);
  164. cp_name_list.erase(cp_name_list.begin() + id);
  165. cp_names.erase(it);
  166. for (auto& e : cp_names) {
  167. if (e.second > id)
  168. --e.second;
  169. }
  170. return true;
  171. }
  172. return false;
  173. }
  174. bool has_custom_properties() const {
  175. return cp_storage.size() != 0;
  176. }
  177. private:
  178. vector<AbstractReference> cp_storage;
  179. map<string, size_t> cp_names;
  180. vector<string> cp_name_list;
  181. };
  182. } // namespace rainynite::core
  183. #endif