Lightweight 0.20260625.0
Loading...
Searching...
No Matches
Lightweight::HasMany< OtherRecord, TheInverseSelector > Class Template Reference

This HasMany<OtherRecord> represents a simple one-to-many relationship between two records. More...

#include <HasMany.hpp>

Public Types

using ReferencedRecord = OtherRecord
 The record type of the "many" side of the relationship.
 
using ReferencedRecordList = std::vector< std::shared_ptr< OtherRecord > >
 The list of records on the "many" side of the relationship.
 
using value_type = OtherRecord
 Record type of the "many" side of the relationship.
 
using iterator = ReferencedRecordList::iterator
 Iterator type for the list of records.
 
using const_iterator = ReferencedRecordList::const_iterator
 Const iterator type for the list of records.
 

Public Member Functions

ReferencedRecordList const & All () const noexcept
 Retrieves the list of loaded records.
 
ReferencedRecordListAll () noexcept
 Retrieves the list of records as mutable reference.
 
template<typename Callable >
void Each (Callable const &callable)
 Iterates over the list of records and calls the given callable for each record.
 
ReferencedRecordListEmplace (ReferencedRecordList &&records) noexcept
 Emplaces the given list of records.
 
std::size_t Count () const noexcept
 Retrieves the number of records in this 1-to-many relationship.
 
bool IsEmpty () const noexcept
 Checks if this 1-to-many relationship is empty.
 
OtherRecord const & At (std::size_t index) const
 Retrieves the record at the given index.
 
OtherRecord & At (std::size_t index)
 Retrieves the record at the given index.
 
OtherRecord const & operator[] (std::size_t index) const
 Retrieves the record at the given index.
 
OtherRecord & operator[] (std::size_t index)
 Retrieves the record at the given index.
 
iterator begin () noexcept
 Returns an iterator to the beginning of the record list.
 
iterator end () noexcept
 Returns an iterator to the end of the record list.
 
const_iterator begin () const noexcept
 Returns a const iterator to the beginning of the record list.
 
const_iterator end () const noexcept
 Returns a const iterator to the end of the record list.
 
constexpr std::weak_ordering operator<=> (HasMany const &other) const noexcept=default
 Three-way comparison operator.
 
constexpr bool operator== (HasMany const &other) const noexcept=default
 Equality comparison operator.
 
constexpr bool operator!= (HasMany const &other) const noexcept=default
 Inequality comparison operator.
 
void SetAutoLoader (Loader loader) noexcept
 Used internally to configure on-demand loading of the records.
 

Static Public Attributes

static constexpr auto InverseSelector = TheInverseSelector
 Singles out the foreign key of OtherRecord that backs this relationship.
 

Detailed Description

template<typename OtherRecord, auto TheInverseSelector = AutoDetectRelation>
class Lightweight::HasMany< OtherRecord, TheInverseSelector >

This HasMany<OtherRecord> represents a simple one-to-many relationship between two records.

The HasMany<OtherRecord> is a member of the "one" side of the relationship.

OtherRecord must declare a BelongsTo member that points back to this "one" side. That member is located by matching the relationship type, not by its position in either record, so the two relationship members may be declared at any index. Declaring no such BelongsTo is a compile-time error.

When OtherRecord holds more than one foreign key into this record's table - say a meeting that references the same person table both as its organizer and as whoever writes the minutes - the inverse is ambiguous. Name the foreign key column through TheInverseSelector to single one out:

struct Meeting;
struct Human
{
HasMany<Meeting, SqlRealName { "organizer_id" }> organizedMeetings;
HasMany<Meeting, SqlRealName { "minute_taker_id" }> minutedMeetings;
};
struct Meeting
{
Field<int, PrimaryKey::AutoAssign> id;
BelongsTo<&Human::id, SqlRealName { "organizer_id" }> organizer;
BelongsTo<&Human::id, SqlRealName { "minute_taker_id" }, SqlNullable::Null> minuteTaker;
};
This HasMany<OtherRecord> represents a simple one-to-many relationship between two records.
Definition HasMany.hpp:64
Represents a single column in a table.
Definition Field.hpp:84
Helper class, used to represent a real SQL column names as template arguments.

A meeting with many attendees is a many-to-many instead - see HasManyThrough. The worked example in docs/sql-to-lightweight.md combines both shapes.

Template Parameters
OtherRecordThe record type on the "many" side of the relationship.
TheInverseSelectorSingles out one of several foreign keys, see the RelationSelector concept.
See also
InverseBelongsToIndexOf, RelationSelector
DataMapper, Field, HasManyThrough

Definition at line 63 of file HasMany.hpp.

Member Typedef Documentation

◆ ReferencedRecord

template<typename OtherRecord , auto TheInverseSelector = AutoDetectRelation>
using Lightweight::HasMany< OtherRecord, TheInverseSelector >::ReferencedRecord = OtherRecord

The record type of the "many" side of the relationship.

Definition at line 71 of file HasMany.hpp.

◆ ReferencedRecordList

template<typename OtherRecord , auto TheInverseSelector = AutoDetectRelation>
using Lightweight::HasMany< OtherRecord, TheInverseSelector >::ReferencedRecordList = std::vector<std::shared_ptr<OtherRecord> >

The list of records on the "many" side of the relationship.

Definition at line 77 of file HasMany.hpp.

◆ value_type

template<typename OtherRecord , auto TheInverseSelector = AutoDetectRelation>
using Lightweight::HasMany< OtherRecord, TheInverseSelector >::value_type = OtherRecord

Record type of the "many" side of the relationship.

Definition at line 80 of file HasMany.hpp.

◆ iterator

template<typename OtherRecord , auto TheInverseSelector = AutoDetectRelation>
using Lightweight::HasMany< OtherRecord, TheInverseSelector >::iterator = ReferencedRecordList::iterator

Iterator type for the list of records.

Definition at line 83 of file HasMany.hpp.

◆ const_iterator

template<typename OtherRecord , auto TheInverseSelector = AutoDetectRelation>
using Lightweight::HasMany< OtherRecord, TheInverseSelector >::const_iterator = ReferencedRecordList::const_iterator

Const iterator type for the list of records.

Definition at line 86 of file HasMany.hpp.

Member Function Documentation

◆ All() [1/2]

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE HasMany< OtherRecord, InverseSelector >::ReferencedRecordList const & Lightweight::HasMany< OtherRecord, InverseSelector >::All ( ) const
inlinenoexcept

Retrieves the list of loaded records.

Definition at line 242 of file HasMany.hpp.

◆ All() [2/2]

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE HasMany< OtherRecord, InverseSelector >::ReferencedRecordList & Lightweight::HasMany< OtherRecord, InverseSelector >::All ( )
inlinenoexcept

Retrieves the list of records as mutable reference.

Definition at line 219 of file HasMany.hpp.

◆ Each()

template<typename OtherRecord , auto InverseSelector>
template<typename Callable >
void Lightweight::HasMany< OtherRecord, InverseSelector >::Each ( Callable const &  callable)

Iterates over the list of records and calls the given callable for each record.

Note
Use this method if you want to iterate over all records but do not need to store them all in memory, e.g. because the full data set wuold be too large.

Definition at line 227 of file HasMany.hpp.

◆ Emplace()

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE HasMany< OtherRecord, InverseSelector >::ReferencedRecordList & Lightweight::HasMany< OtherRecord, InverseSelector >::Emplace ( ReferencedRecordList &&  records)
inlinenoexcept

Emplaces the given list of records.

Definition at line 210 of file HasMany.hpp.

◆ Count()

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE std::size_t Lightweight::HasMany< OtherRecord, InverseSelector >::Count ( ) const
inlinenoexcept

Retrieves the number of records in this 1-to-many relationship.

Definition at line 249 of file HasMany.hpp.

◆ IsEmpty()

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE bool Lightweight::HasMany< OtherRecord, InverseSelector >::IsEmpty ( ) const
inlinenoexcept

Checks if this 1-to-many relationship is empty.

Definition at line 261 of file HasMany.hpp.

◆ At() [1/2]

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE OtherRecord const & Lightweight::HasMany< OtherRecord, InverseSelector >::At ( std::size_t  index) const
inline

Retrieves the record at the given index.

Parameters
indexThe index of the record to retrieve.
Note
This method will on-demand load the records if they are not already loaded.
This method will throw if the index is out of bounds.

Definition at line 267 of file HasMany.hpp.

◆ At() [2/2]

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE OtherRecord & Lightweight::HasMany< OtherRecord, InverseSelector >::At ( std::size_t  index)
inline

Retrieves the record at the given index.

Parameters
indexThe index of the record to retrieve.
Note
This method will on-demand load the records if they are not already loaded.
This method will throw if the index is out of bounds.

Definition at line 274 of file HasMany.hpp.

◆ operator[]() [1/2]

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE OtherRecord const & Lightweight::HasMany< OtherRecord, InverseSelector >::operator[] ( std::size_t  index) const
inline

Retrieves the record at the given index.

Parameters
indexThe index of the record to retrieve.
Note
This method will on-demand load the records if they are not already loaded.
This method will NOT throw if the index is out of bounds. The behaviour is undefined.

Definition at line 281 of file HasMany.hpp.

◆ operator[]() [2/2]

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE OtherRecord & Lightweight::HasMany< OtherRecord, InverseSelector >::operator[] ( std::size_t  index)
inline

Retrieves the record at the given index.

Parameters
indexThe index of the record to retrieve.
Note
This method will on-demand load the records if they are not already loaded.
This method will NOT throw if the index is out of bounds. The behaviour is undefined.

Definition at line 288 of file HasMany.hpp.

◆ begin() [1/2]

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE HasMany< OtherRecord, InverseSelector >::iterator Lightweight::HasMany< OtherRecord, InverseSelector >::begin ( )
inlinenoexcept

Returns an iterator to the beginning of the record list.

Definition at line 296 of file HasMany.hpp.

Referenced by Lightweight::HasMany< OtherRecord, TheInverseSelector >::begin().

◆ end() [1/2]

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE HasMany< OtherRecord, InverseSelector >::iterator Lightweight::HasMany< OtherRecord, InverseSelector >::end ( )
inlinenoexcept

Returns an iterator to the end of the record list.

Definition at line 307 of file HasMany.hpp.

Referenced by Lightweight::HasMany< OtherRecord, TheInverseSelector >::end().

◆ begin() [2/2]

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE HasMany< OtherRecord, InverseSelector >::const_iterator Lightweight::HasMany< OtherRecord, InverseSelector >::begin ( ) const
inlinenoexcept

Returns a const iterator to the beginning of the record list.

Definition at line 318 of file HasMany.hpp.

References Lightweight::HasMany< OtherRecord, TheInverseSelector >::begin().

◆ end() [2/2]

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE HasMany< OtherRecord, InverseSelector >::const_iterator Lightweight::HasMany< OtherRecord, InverseSelector >::end ( ) const
inlinenoexcept

Returns a const iterator to the end of the record list.

Definition at line 330 of file HasMany.hpp.

References Lightweight::HasMany< OtherRecord, TheInverseSelector >::end().

◆ SetAutoLoader()

template<typename OtherRecord , auto InverseSelector>
LIGHTWEIGHT_FORCE_INLINE void Lightweight::HasMany< OtherRecord, InverseSelector >::SetAutoLoader ( Loader  loader)
inlinenoexcept

Used internally to configure on-demand loading of the records.

Definition at line 195 of file HasMany.hpp.

Referenced by Lightweight::DataMapper::ConfigureRelationAutoLoading().

Member Data Documentation

◆ InverseSelector

template<typename OtherRecord , auto TheInverseSelector = AutoDetectRelation>
constexpr auto Lightweight::HasMany< OtherRecord, TheInverseSelector >::InverseSelector = TheInverseSelector
staticconstexpr

Singles out the foreign key of OtherRecord that backs this relationship.

Definition at line 74 of file HasMany.hpp.


The documentation for this class was generated from the following file: