ORM C++
Loading...
Searching...
No Matches
relation_collections.cppm
Go to the documentation of this file.
1module;
2
3#include <memory>
4#include <optional>
5#include <type_traits>
6#include <utility>
7#include <vector>
8
9export module orm:relation_collections;
10
11// relations.hpp
12namespace orm
13{
14export
15{
16
17 class Database;
18 template <typename SchemaType>
19 class OrmContext;
20}
21namespace detail
22{
23template <typename T>
24class RelationCollection
25{
26public:
27 using value_type = T;
28 using container_type = std::vector<T>;
29 using size_type = typename container_type::size_type;
30 using iterator = typename container_type::iterator;
31 using const_iterator = typename container_type::const_iterator;
32
33 RelationCollection() = default;
34
35 RelationCollection(const RelationCollection& other)
36 : values_(not other.values_ ? nullptr : std::make_shared<container_type>(*other.values_)),
37 loaded_(other.loaded_)
38 {
39 }
40
41 auto operator=(const RelationCollection& other) -> RelationCollection&
42 {
43 if (this == &other)
44 {
45 return *this;
46 }
47
48 auto copiedValues =
49 not other.values_ ? std::shared_ptr<container_type>{} : std::make_shared<container_type>(*other.values_);
50 values_ = std::move(copiedValues);
51 loaded_ = other.loaded_;
52 return *this;
53 }
54
55 RelationCollection(RelationCollection&&) noexcept = default;
56 auto operator=(RelationCollection&&) noexcept -> RelationCollection& = default;
57
58 explicit RelationCollection(container_type values)
59 : values_(std::make_shared<container_type>(std::move(values))), loaded_(true)
60 {
61 }
62
63 [[nodiscard]] auto isLoaded() const noexcept -> bool
64 {
65 return loaded_;
66 }
67
68 auto values() -> container_type&
69 {
70 return mutableValues();
71 }
72
73 auto values() const -> const container_type&
74 {
75 return readableValues();
76 }
77
78 auto begin() -> iterator
79 {
80 return values().begin();
81 }
82
83 auto begin() const -> const_iterator
84 {
85 return values().begin();
86 }
87
88 auto cbegin() const -> const_iterator
89 {
90 return values().cbegin();
91 }
92
93 auto end() -> iterator
94 {
95 return values().end();
96 }
97
98 auto end() const -> const_iterator
99 {
100 return values().end();
101 }
102
103 auto cend() const -> const_iterator
104 {
105 return values().cend();
106 }
107
108 [[nodiscard]] auto size() const noexcept -> size_type
109 {
110 return not values_ ? 0 : values_->size();
111 }
112
113 [[nodiscard]] auto empty() const noexcept -> bool
114 {
115 return not values_ or values_->empty();
116 }
117
118 auto operator[](size_type index) -> T&
119 {
120 return values()[index];
121 }
122
123 auto operator[](size_type index) const -> const T&
124 {
125 return values()[index];
126 }
127
128private:
129 friend class orm::Database;
130 template <typename>
131 friend class orm::OrmContext;
132
133 auto setLoaded(container_type values) -> void
134 {
135 values_ = std::make_shared<container_type>(std::move(values));
136 loaded_ = true;
137 }
138
139 auto mutableValues() -> container_type&
140 {
141 if (not values_)
142 {
143 values_ = std::make_shared<container_type>();
144 }
145
146 return *values_;
147 }
148
149 auto readableValues() const -> const container_type&
150 {
151 if (not values_)
152 {
153 values_ = std::make_shared<container_type>();
154 }
155
156 return *values_;
157 }
158
159 mutable std::shared_ptr<container_type> values_;
160 bool loaded_{};
161};
162}
163export
164{
165 // namespace detail
167 template <typename T>
168 class OneToMany : public detail::RelationCollection<T>
169 {
170 public:
171 using detail::RelationCollection<T>::RelationCollection;
172 };
174 template <typename T>
175 class ManyToMany : public detail::RelationCollection<T>
176 {
177 public:
178 using detail::RelationCollection<T>::RelationCollection;
179 };
180}
181namespace detail
182{
183template <typename T>
184struct RelationCollectionTraits
185{
186 inline static constexpr bool isCollection = false;
187 inline static constexpr bool isOneToMany = false;
188 using Target = void;
189};
190
191template <typename T>
192struct RelationCollectionTraits<OneToMany<T>>
193{
194 inline static constexpr bool isCollection = true;
195 inline static constexpr bool isOneToMany = true;
196 using Target = T;
197};
198
199template <typename T>
200struct RelationCollectionTraits<ManyToMany<T>>
201{
202 inline static constexpr bool isCollection = true;
203 inline static constexpr bool isOneToMany = false;
204 using Target = T;
205};
206
207template <typename T>
208inline constexpr bool isRelationCollection = RelationCollectionTraits<std::remove_cv_t<T>>::isCollection;
209
210template <typename T>
211struct OptionalRelationCollectionTraits
212{
213 inline static constexpr bool value = false;
214};
215
216template <typename T>
217struct OptionalRelationCollectionTraits<std::optional<T>>
218{
219 inline static constexpr bool value = isRelationCollection<T>;
220};
221
222template <typename T>
223inline constexpr bool isOptionalRelationCollection = OptionalRelationCollectionTraits<std::remove_cv_t<T>>::value;
224}
225export
226{
227 // namespace detail
228
229 template <typename T>
230 inline constexpr bool is_relation_collection_v = detail::isRelationCollection<std::remove_cvref_t<T>>;
231
232 template <typename T>
233 inline constexpr bool is_optional_relation_collection_v =
234 detail::isOptionalRelationCollection<std::remove_cvref_t<T>>;
235
236 template <typename T>
237 using relation_target_t = typename detail::RelationCollectionTraits<std::remove_cvref_t<T>>::Target;
238
239 template <typename T>
240 inline constexpr bool is_one_to_many_v =
241 is_relation_collection_v<T> && detail::RelationCollectionTraits<std::remove_cvref_t<T>>::isOneToMany;
242}
243
244} // namespace orm
typename detail::RelationCollectionTraits< std::remove_cvref_t< T > >::Target relation_target_t
constexpr bool is_one_to_many_v
constexpr bool is_relation_collection_v
constexpr bool is_optional_relation_collection_v