query/include/matador/object/object_info.hpp

69 lines
1.7 KiB
C++

#ifndef OBJECT_INFO_HPP
#define OBJECT_INFO_HPP
#include "matador/object/basic_object_info.hpp"
#include "matador/object/observer.hpp"
#include <functional>
namespace matador::object {
class repository_node;
template<typename Type>
class object_info final : public basic_object_info {
public:
using create_func = std::function<std::unique_ptr<Type>()>;
object_info(const repository_node& node,
const std::shared_ptr<class object> &obj,
std::vector<std::unique_ptr<observer<Type>>>&& observers,
create_func&& creator)
: basic_object_info(node, obj)
, creator_(std::move(creator))
, observers_(std::move(observers)){}
explicit object_info(const repository_node& node)
: basic_object_info(node, {}) {
}
const Type &prototype() const { return prototype_; }
std::unique_ptr<Type> create() const { return creator_(); }
void on_attach() const override {
for (auto &observer : observers_) {
observer->on_attach(node_, prototype_);
}
}
void on_detach() const override {
for (auto &observer : observers_) {
observer->on_detach(node_, prototype_);
}
}
void register_observer(std::unique_ptr<observer<Type>>&& observer) {
observers_.push_back(std::move(observer));
}
const std::vector<std::unique_ptr<observer<Type>>>& observers() const {
return observers_;
}
private:
Type prototype_;
create_func creator_{[]{ return std::make_unique<Type>(); }};
std::vector<std::unique_ptr<observer<Type>>> observers_;
};
template<typename Type>
using object_info_ref = std::reference_wrapper<const object_info<Type>>;
namespace detail {
struct null_type {};
}
using null_info = object_info<detail::null_type>;
}
#endif //OBJECT_INFO_HPP