5#include "../DataBinder/Core.hpp"
6#include "../DataBinder/SqlNullValue.hpp"
7#include "../SqlStatement.hpp"
8#include "BelongsTo.hpp"
12#include <reflection-cpp/reflection.hpp>
32template <
typename OtherRecord>
46 using iterator =
typename ReferencedRecordList::iterator;
61 template <typename Callable>
62 void Each(Callable const& callable);
68 [[nodiscard]] std::
size_t Count() const noexcept;
71 [[nodiscard]]
bool IsEmpty() const noexcept;
78 [[nodiscard]] OtherRecord const&
At(std::
size_t index) const;
85 [[nodiscard]] OtherRecord&
At(std::
size_t index);
92 [[nodiscard]] OtherRecord const& operator[](std::
size_t index) const;
99 [[nodiscard]] OtherRecord& operator[](std::
size_t index);
101 [[nodiscard]]
iterator begin() noexcept;
102 [[nodiscard]]
iterator end() noexcept;
106 constexpr std::weak_ordering operator<=>(
HasMany const& other) const noexcept = default;
107 constexpr
bool operator==(
HasMany const& other) const noexcept = default;
108 constexpr
bool operator!=(
HasMany const& other) const noexcept = default;
112 std::function<size_t()> count {};
113 std::function<void()> all {};
116 std::weak_ordering operator<=>(Loader
const& )
const noexcept
118 return std::weak_ordering::equivalent;
126 void RequireLoaded();
129 std::optional<ReferencedRecordList> _records;
130 std::optional<size_t> _count;
134constexpr bool IsHasMany = IsSpecializationOf<HasMany, T>;
136template <
typename OtherRecord>
139 _loader = std::move(loader);
142template <
typename OtherRecord>
149template <
typename OtherRecord>
153 _records = { std::move(records) };
157template <
typename OtherRecord>
164template <
typename OtherRecord>
165template <
typename Callable>
168 if (!_records && _loader.each)
170 _loader.each(callable);
174 for (
auto const& record: All())
178template <
typename OtherRecord>
185template <
typename OtherRecord>
189 return _records->size();
194 return _count.value_or(0);
197template <
typename OtherRecord>
203template <
typename OtherRecord>
207 return *_records->at(index);
210template <
typename OtherRecord>
214 return *_records->at(index);
217template <
typename OtherRecord>
221 return *(*_records)[index];
224template <
typename OtherRecord>
228 return *(*_records)[index];
231template <
typename OtherRecord>
235 return _records->begin();
238template <
typename OtherRecord>
242 return _records->end();
245template <
typename OtherRecord>
249 return _records->begin();
252template <
typename OtherRecord>
256 return _records->end();
This HasMany<OtherRecord> represents a simple one-to-many relationship between two records.
OtherRecord value_type
Record type of the "many" side of the relationship.
void Each(Callable const &callable)
Iterates over the list of records and calls the given callable for each record.
std::size_t Count() const noexcept
Retrieves the number of records in this 1-to-many relationship.
std::vector< std::shared_ptr< OtherRecord > > ReferencedRecordList
The list of records on the "many" side of the relationship.
bool IsEmpty() const noexcept
Checks if this 1-to-many relationship is empty.
ReferencedRecordList & Emplace(ReferencedRecordList &&records) noexcept
Emplaces the given list of records.
OtherRecord ReferencedRecord
The record type of the "many" side of the relationship.
typename ReferencedRecordList::const_iterator const_iterator
Const iterator type for the list of records.
void SetAutoLoader(Loader loader) noexcept
Used internally to configure on-demand loading of the records.
ReferencedRecordList const & All() const noexcept
Retrieves the list of loaded records.
OtherRecord const & operator[](std::size_t index) const
Retrieves the record at the given index.
OtherRecord const & At(std::size_t index) const
Retrieves the record at the given index.
typename ReferencedRecordList::iterator iterator
Iterator type for the list of records.