abstract_value.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * abstract_value.h - Node abstract Value
  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_VALUE_H__ACCED1BE
  19. #define __CORE__NODE__ABSTRACT_VALUE_H__ACCED1BE
  20. #include <boost/uuid/uuid.hpp>
  21. #include <boost/uuid/uuid_generators.hpp>
  22. #include <core/std/any.h>
  23. #include <core/log/simple_exception_log.h>
  24. #include "notify.h"
  25. #include "has_id.h"
  26. #include "abstract_list.h"
  27. namespace rainynite::core {
  28. using NodeId = boost::uuids::uuid;
  29. using NodeIdGenerator = boost::uuids::random_generator;
  30. class AbstractValue : public AbstractNotify, public HasId<NodeId, NodeIdGenerator> {
  31. public:
  32. virtual bool is_const() const {
  33. return false;
  34. }
  35. virtual Type get_type() const = 0;
  36. virtual any get_any(shared_ptr<Context> context) const noexcept = 0;
  37. virtual void set_any(any const& /*value*/) {
  38. throw NodeAccessError("Cannot set");
  39. }
  40. virtual bool can_set_any(any const& /*value*/) const {
  41. return false;
  42. }
  43. virtual any static_any() const {
  44. throw NodeAccessError("No static value");
  45. }
  46. virtual void set_source(shared_ptr<AbstractValue> /*source*/) {
  47. throw NodeAccessError("Cannot set source node");
  48. }
  49. virtual bool can_set_source(shared_ptr<AbstractValue> /*source*/) const {
  50. return false;
  51. }
  52. /**
  53. * Call arbitrary function with all dynamic list member nodes in context.
  54. *
  55. * If this node is not a list, throws NodeAccessError
  56. */
  57. virtual void step_into_list(shared_ptr<Context> /*context*/, std::function<void(NodeInContext)> /*f*/) const {
  58. throw NodeAccessError("This node is not a list");
  59. }
  60. virtual vector<NodeInContext> get_list_links(shared_ptr<Context> ctx) const {
  61. vector<NodeInContext> result;
  62. step_into_list(ctx, [&result](auto e) {
  63. result.push_back(std::move(e));
  64. });
  65. return result;
  66. }
  67. };
  68. template <typename T>
  69. constexpr bool is_vector = false;
  70. template <typename U>
  71. constexpr bool is_vector<vector<U>> = true;
  72. template <typename T>
  73. class BaseValue :
  74. public AbstractValue,
  75. public HasExceptionLogger
  76. {
  77. public:
  78. BaseValue() :
  79. HasExceptionLogger(make_shared<SimpleExceptionLogger>())
  80. {}
  81. public:
  82. /**
  83. * Get value of this node in given context
  84. *
  85. * Any exceptions that are thrown during node calculation should be
  86. * recorded in log (TODO; not yet implemented).
  87. */
  88. T value(shared_ptr<Context> context) const noexcept {
  89. try {
  90. return get(context);
  91. } catch (std::exception const& ex) {
  92. log_exception_from_this(ex);
  93. return {};
  94. } catch (...) {
  95. log_exception_from_this(NodeAccessError("Unknown error in BaseValue::value()"));
  96. return {};
  97. }
  98. }
  99. public:
  100. /**
  101. * Get value of this node in given context - may throw
  102. *
  103. * TODO: make protected
  104. */
  105. virtual T get(shared_ptr<Context> context) const {
  106. if constexpr (is_vector<T>) {
  107. using E = typename T::value_type;
  108. T result;
  109. step_into_list(
  110. context,
  111. [&result](NodeInContext nic) {
  112. if (auto value = dynamic_cast<BaseValue<E>*>(nic.node.get())) {
  113. result.push_back(value->get(nic.context));
  114. }
  115. }
  116. );
  117. return result;
  118. } else {
  119. (void) context;
  120. throw NodeAccessError("Get not implemented in node");
  121. }
  122. }
  123. virtual void set(T /*value*/) {
  124. throw NodeAccessError("Cannot set");
  125. }
  126. virtual T& mod() {
  127. throw NodeAccessError("Cannot set");
  128. }
  129. virtual bool can_set() const {
  130. return false;
  131. }
  132. public:
  133. Type get_type() const override {
  134. return typeid(T);
  135. }
  136. any get_any(shared_ptr<Context> context) const noexcept override {
  137. return value(context);
  138. }
  139. void set_any(any const& value_) override {
  140. set(any_cast<T>(value_));
  141. }
  142. bool can_set_any(any const& value_) const override {
  143. return can_set() && value_.type() == typeid(T);
  144. }
  145. void step_into_list(shared_ptr<Context> context, std::function<void(NodeInContext)> f) const override {
  146. if constexpr (is_vector<T>) {
  147. for (auto&& e : get_list_links(context))
  148. f(e);
  149. } else {
  150. AbstractValue::step_into_list(context, f);
  151. }
  152. }
  153. public:
  154. static Type static_type() {
  155. return typeid(T);
  156. }
  157. };
  158. } // namespace rainynite::core
  159. #endif