new_node.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* new_node.h - new node
  2. * Copyright (C) 2017-2018 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. void remove(size_t i) override {
  68. auto name = get_name_at(i);
  69. if (is_custom_property(name))
  70. remove_custom_property(name);
  71. else
  72. Storage::remove(i);
  73. }
  74. size_t link_count() const override {
  75. return Storage::link_count()+cp_storage.size();
  76. }
  77. map<string, AbstractReference> get_link_map() const override {
  78. auto result = Storage::get_link_map();
  79. if (!has_custom_properties())
  80. return result;
  81. for (auto const& e : cp_names) {
  82. result.emplace(e.first, cp_storage[e.second]);
  83. }
  84. return result;
  85. }
  86. vector<AbstractReference> get_links() const override {
  87. auto result = Storage::get_links();
  88. if (!has_custom_properties())
  89. return result;
  90. result.insert(result.end(), cp_storage.begin(), cp_storage.end());
  91. return result;
  92. }
  93. string get_name_at(size_t id) const override {
  94. return apply_for_normal_or_custom<string>(
  95. id,
  96. [this](auto i) {
  97. return Storage::get_name_at(i);
  98. },
  99. [this](auto i) {
  100. return cp_name_list[i];
  101. }
  102. );
  103. }
  104. AbstractReference get_link(size_t i) const override {
  105. return apply_for_normal_or_custom<AbstractReference>(
  106. i,
  107. [this](auto i) {
  108. return Storage::get_link(i);
  109. },
  110. [this](auto i) {
  111. return cp_storage[i];
  112. }
  113. );
  114. }
  115. TypeConstraint get_link_type(size_t i) const override {
  116. return apply_for_normal_or_custom<TypeConstraint>(
  117. i,
  118. [this](auto i) {
  119. return Storage::get_link_type(i);
  120. },
  121. [](auto /*i*/) {
  122. return types::Any();
  123. }
  124. );
  125. }
  126. void set_link(size_t i, AbstractReference value) override {
  127. return apply_for_normal_or_custom<void>(
  128. i,
  129. [this, value](auto ii) {
  130. Storage::set_link(ii, value);
  131. },
  132. [this, value](auto ii) {
  133. cp_storage[ii] = value;
  134. }
  135. );
  136. }
  137. protected:
  138. void node_changed() override {
  139. this->changed();
  140. }
  141. private:
  142. template <typename T, class F, class G>
  143. T apply_for_normal_or_custom(size_t i, F normal, G custom) const {
  144. auto cp_id = (ptrdiff_t)i - (ptrdiff_t)Storage::link_count();
  145. if (cp_id < 0)
  146. return normal(i);
  147. size_t id = cp_id;
  148. if (id >= cp_storage.size())
  149. throw std::out_of_range("Link id out of range");
  150. return custom(id);
  151. }
  152. bool is_custom_property(string const& name) const {
  153. return !name.empty() && name[0] == '_';
  154. }
  155. size_t get_custom_property_id(string const& name) const {
  156. return Storage::link_count()+cp_names.at(name);
  157. }
  158. AbstractReference get_custom_property(string const& name) const {
  159. auto it = cp_names.find(name);
  160. if (it != cp_names.end())
  161. return cp_storage[it->second];
  162. throw NodeAccessError("Custom property '"+name+"' not found");
  163. }
  164. void set_custom_property(string const& name, AbstractReference ref) {
  165. auto it = cp_names.find(name);
  166. if (it != cp_names.end()) {
  167. cp_storage[it->second] = ref;
  168. this->changed();
  169. } else {
  170. cp_names.emplace(name, cp_storage.size());
  171. cp_name_list.push_back(name);
  172. cp_storage.push_back(ref);
  173. this->links_changed();
  174. }
  175. }
  176. bool remove_custom_property(string const& name) {
  177. auto it = cp_names.find(name);
  178. if (it != cp_names.end()) {
  179. auto id = it->second;
  180. cp_storage.erase(cp_storage.begin() + id);
  181. cp_name_list.erase(cp_name_list.begin() + id);
  182. cp_names.erase(it);
  183. for (auto& e : cp_names) {
  184. if (e.second > id)
  185. --e.second;
  186. }
  187. return true;
  188. }
  189. return false;
  190. }
  191. bool has_custom_properties() const {
  192. return cp_storage.size() != 0;
  193. }
  194. private:
  195. vector<AbstractReference> cp_storage;
  196. map<string, size_t> cp_names;
  197. vector<string> cp_name_list;
  198. };
  199. } // namespace rainynite::core
  200. #endif