summaryrefslogtreecommitdiff
path: root/lib/collection.hpp
blob: 6802bcb005795d8b45b73b14bd9e0cfd87f4fb53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#pragma once

#include <algorithm>
#include <functional>
#include <memory>
#include <type_traits>
#include <vector>

template<typename Object, bool shared = true> class Collection {
public:
	virtual ~Collection() = default;

	using Ptr = std::conditional_t<shared, std::shared_ptr<Object>, std::unique_ptr<Object>>;
	using Objects = std::vector<Ptr>;
	Objects objects;

	template<typename T = Object, typename... Params>
	auto
	create(Params &&... params)
		requires std::is_base_of_v<Object, T>
	{
		if constexpr (shared) {
			auto obj = std::make_shared<T>(std::forward<Params>(params)...);
			objects.emplace_back(obj);
			return obj;
		}
		else {
			return static_cast<T *>(objects.emplace_back(std::make_unique<T>(std::forward<Params>(params)...)).get());
		}
	}

	template<typename T = Object>
	T *
	find()
	{
		if (auto i = std::find_if(objects.begin(), objects.end(),
					[](auto && o) {
						return (dynamic_cast<T *>(o.get()));
					});
				i != objects.end()) {
			return static_cast<T *>(i->get());
		}
		return nullptr;
	}

	template<typename T = Object, typename... Params>
	auto
	findOrCreate(Params &&... params)
		requires std::is_base_of_v<Object, T>
	{
		if (auto o = find<T>()) {
			return o;
		}
		return create<T>(std::forward<Params>(params)...).get();
	}

	template<typename T = Object, typename... Params>
	auto
	apply(const auto & m, Params &&... params) const
	{
		return apply_internal<T>(objects.begin(), objects.end(), m, std::forward<Params>(params)...);
	}

	template<typename T = Object, typename... Params>
	auto
	rapply(const auto & m, Params &&... params) const
	{
		return apply_internal<T>(objects.rbegin(), objects.rend(), m, std::forward<Params>(params)...);
	}

	template<typename T = Object, typename... Params>
	auto
	applyOne(const auto & m, Params &&... params) const
	{
		return applyOne_internal<T>(objects.begin(), objects.end(), m, std::forward<Params>(params)...);
	}

	template<typename T = Object, typename... Params>
	auto
	rapplyOne(const auto & m, Params &&... params) const
	{
		return applyOne_internal<T>(objects.rbegin(), objects.rend(), m, std::forward<Params>(params)...);
	}

	template<typename T = Object>
	auto
	removeAll()
	{
		return std::erase_if(objects, [](auto && op) {
			return dynamic_cast<T *>(op.get());
		});
	}

	auto
	end() const
	{
		return objects.end();
	}

	auto
	rend() const
	{
		return objects.rend();
	}

protected:
	template<typename T = Object, typename... Params>
	auto
	apply_internal(const auto begin, const auto end, const auto & m, Params &&... params) const
	{
		return std::count_if(begin, end, [&m, &params...](auto && op) {
			if (auto o = dynamic_cast<T *>(op.get())) {
				std::invoke(m, o, std::forward<Params>(params)...);
				return true;
			}
			return false;
		});
	}

	template<typename T = Object, typename... Params>
	auto
	applyOne_internal(const auto begin, const auto end, const auto & m, Params &&... params) const
	{
		return std::find_if(begin, end, [&m, &params...](auto && op) {
			if (auto o = dynamic_cast<T *>(op.get())) {
				return std::invoke(m, o, std::forward<Params>(params)...);
			}
			return false;
		});
	}
};