123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- /* abstract_node.h - abstract Node
- * Copyright (C) 2017 caryoscelus
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #ifndef CORE_NODE_ABSTRACT_NODE_H_EE148DA4_6D84_5EDA_A5B0_C192A55C5B09
- #define CORE_NODE_ABSTRACT_NODE_H_EE148DA4_6D84_5EDA_A5B0_C192A55C5B09
- #include <boost/signals2/signal.hpp>
- #include <core/std/map.h>
- #include "abstract_list.h"
- #include "doc_string.h"
- namespace rainynite::core {
- class AbstractNode {
- public:
- virtual AbstractReference get_property(string const& name) const = 0;
- virtual void set_property(string const& name, AbstractReference ref) = 0;
- virtual bool remove_property(string const& /*name*/) {
- return false;
- }
- virtual string get_name_at(size_t id) const = 0;
- virtual size_t get_name_id(string const& name) const = 0;
- virtual map<string, AbstractReference> get_link_map() const = 0;
- protected:
- /**
- * This function should be called when node has changed.
- *
- * In practice, it exists solely due to class hierarchy and lack of
- * AbstractValue inheritance in AbstractNode.
- */
- virtual void node_changed() = 0;
- public:
- template <typename T>
- shared_ptr<BaseValue<T>> get_property_as(string const& name) const {
- return base_value_cast<T>(get_property(name));
- }
- template <typename T>
- optional<T> get_property_value(string const& name, shared_ptr<Context> context) const {
- try {
- if (auto node = get_property_as<T>(name))
- return node->value(context);
- return {};
- } catch (NodeAccessError const&) {
- return {};
- } catch (...) {
- return {};
- }
- }
- };
- class AbstractNodeBase : public AbstractListLinked, public AbstractNode {
- };
- /**
- * Abstract node: entity with links to AbstractValues
- */
- class BaseOldNode : public AbstractNodeBase, public DocString {
- public:
- virtual ~BaseOldNode();
- public:
- AbstractReference get_property(string const& name) const override;
- void set_property(string const& name, AbstractReference ref) override;
- bool remove_property(string const& name) override;
- size_t init_property(string const& name, TypeConstraint type, AbstractReference value);
- map<string, AbstractReference> get_link_map() const override;
- string get_name_at(size_t id) const override {
- return names_list[id];
- }
- /// Get id of property name (throws on error)
- size_t get_name_id(string const& name) const override {
- return named_storage.at(name);
- }
- public:
- vector<AbstractReference> get_links() const override {
- return numbered_storage;
- }
- AbstractReference get_link(size_t i) const override {
- return get_by_id(i);
- }
- TypeConstraint get_link_type(size_t i) const override {
- return types[i];
- }
- void set_link(size_t i, AbstractReference value) override;
- size_t link_count() const override {
- return numbered_storage.size();
- }
- private:
- AbstractReference const& get_by_id(size_t index) const {
- return numbered_storage[index];
- }
- AbstractReference& get_by_id(size_t index) {
- return numbered_storage[index];
- }
- private:
- map<string, size_t> named_storage;
- vector<string> names_list;
- vector<AbstractReference> numbered_storage;
- vector<boost::signals2::connection> signal_connections;
- vector<TypeConstraint> types;
- };
- } // namespace rainynite::core
- #endif
|