MySQL 9.6.0
Source Code Documentation
mysql::ranges::View_source< Source_tp, owns_source_tp > Class Template Reference

Wrapper around an object that is the source for a view: the wrapped object is owned by this object if the wrapped object's type satisfies std::ranges::view, and not owned otherwise. More...

#include <view_sources.h>

Public Types

using Source_t = Source_tp
 
using Raw_source_t = Raw_view_source< Source_t, owns_source >
 Source_t if owns_source, otherwise Source_t &. More...
 
using Source_ref_t = std::conditional_t< owns_source, Source_t, const Source_t * >
 Internal representation of the source. More...
 

Public Member Functions

 View_source ()=default
 Default-construct an object. More...
 
 View_source (const Source_t &source)
 Construct from a const reference to the source. More...
 
const Raw_source_tget () const
 Return a copy of the stored object if it is owned; otherwise a reference to it. More...
 
const Source_treference () const
 Return a reference to the stored object. More...
 
const Source_toperator-> () const
 Arrow operator to access members of the source. More...
 
auto begin () const
 Return begin iterator to the source. More...
 
auto end () const
 Return end iterator to the source. More...
 

Static Public Attributes

static constexpr bool owns_source = owns_source_tp
 True if this object owns a copy of its source; false if it holds a reference to its source. More...
 

Static Private Member Functions

static decltype(auto) from_ref (const Source_t &object)
 Return an Source_ref_t from the given pointer, holding no object if the pointer is nullptr, and holding the pointed-to object otherwise. More...
 

Private Attributes

Source_ref_t m_source {}
 The source. More...
 

Detailed Description

template<class Source_tp, bool owns_source_tp = std::ranges::view<Source_tp>>
class mysql::ranges::View_source< Source_tp, owns_source_tp >

Wrapper around an object that is the source for a view: the wrapped object is owned by this object if the wrapped object's type satisfies std::ranges::view, and not owned otherwise.

This is meant to be used by member variables of the view class, or the view's iterator class, that need to reference the source.

This intends to prevent dangling references by enforcing the following rule:

"Views and their iterators shall represent sources that are views by-value, and sources that are containers by-reference."

For full justification, see readme.md.

Internally, this class stores a source of type T using a member variable of type T if std::ranges::view<T>, or type const T * otherwise. It provides an API similar to std::optional, except this object is not optional.

This object is default-constructible if T is default-constructible (although the default-constructed object is in a singular state and can't be used for anything else than as the target of an assignment operation). If default-constructibility is not important, you may use Raw_view_source instead.

Note that types need to be declared explicitly as views, using either std::ranges::view_base or std::ranges::enable_range.

Template Parameters
Source_tpThe source type.
owns_source_tpDetermines if the object owns its source. By default, this is deduced from std::ranges::view<Source_tp>.

Member Typedef Documentation

◆ Raw_source_t

template<class Source_tp , bool owns_source_tp = std::ranges::view<Source_tp>>
using mysql::ranges::View_source< Source_tp, owns_source_tp >::Raw_source_t = Raw_view_source<Source_t, owns_source>

Source_t if owns_source, otherwise Source_t &.

◆ Source_ref_t

template<class Source_tp , bool owns_source_tp = std::ranges::view<Source_tp>>
using mysql::ranges::View_source< Source_tp, owns_source_tp >::Source_ref_t = std::conditional_t<owns_source, Source_t, const Source_t *>

Internal representation of the source.

◆ Source_t

template<class Source_tp , bool owns_source_tp = std::ranges::view<Source_tp>>
using mysql::ranges::View_source< Source_tp, owns_source_tp >::Source_t = Source_tp

Constructor & Destructor Documentation

◆ View_source() [1/2]

template<class Source_tp , bool owns_source_tp = std::ranges::view<Source_tp>>
mysql::ranges::View_source< Source_tp, owns_source_tp >::View_source ( )
default

Default-construct an object.

If the source is not owned, this holds a nullptr internally, so then the only things you can do with the object is assign to it.

◆ View_source() [2/2]

template<class Source_tp , bool owns_source_tp = std::ranges::view<Source_tp>>
mysql::ranges::View_source< Source_tp, owns_source_tp >::View_source ( const Source_t source)
inlineexplicit

Construct from a const reference to the source.

Member Function Documentation

◆ begin()

template<class Source_tp , bool owns_source_tp = std::ranges::view<Source_tp>>
auto mysql::ranges::View_source< Source_tp, owns_source_tp >::begin ( ) const
inline

Return begin iterator to the source.

The behavior is undefined for default-constructed objects.

x.begin() is equivalent to x->begin(); this function is provided for API compatibility with Optional_view_source.

◆ end()

template<class Source_tp , bool owns_source_tp = std::ranges::view<Source_tp>>
auto mysql::ranges::View_source< Source_tp, owns_source_tp >::end ( ) const
inline

Return end iterator to the source.

The behavior is undefined for default-constructed non-owning objects.

x.end() is equivalent to x->end(); this function is provided for API compatibility with Optional_view_source.

◆ from_ref()

template<class Source_tp , bool owns_source_tp = std::ranges::view<Source_tp>>
static decltype(auto) mysql::ranges::View_source< Source_tp, owns_source_tp >::from_ref ( const Source_t object)
inlinestaticprivate

Return an Source_ref_t from the given pointer, holding no object if the pointer is nullptr, and holding the pointed-to object otherwise.

◆ get()

template<class Source_tp , bool owns_source_tp = std::ranges::view<Source_tp>>
const Raw_source_t & mysql::ranges::View_source< Source_tp, owns_source_tp >::get ( ) const
inline

Return a copy of the stored object if it is owned; otherwise a reference to it.

The behavior is undefined for default-constructed objects.

◆ operator->()

template<class Source_tp , bool owns_source_tp = std::ranges::view<Source_tp>>
const Source_t * mysql::ranges::View_source< Source_tp, owns_source_tp >::operator-> ( ) const
inline

Arrow operator to access members of the source.

The behavior is undefined for default-constructed objects.

Note
To access begin/end iterators from the source when there is a source, and objects that behave like end iterators when there is no source, use o.begin() and o.end() instead of o->begin() and o->end().

◆ reference()

template<class Source_tp , bool owns_source_tp = std::ranges::view<Source_tp>>
const Source_t & mysql::ranges::View_source< Source_tp, owns_source_tp >::reference ( ) const
inline

Return a reference to the stored object.

The behavior is undefined for default-constructed objects. Note that the reference is only valid as long as this object exists.

Member Data Documentation

◆ m_source

template<class Source_tp , bool owns_source_tp = std::ranges::view<Source_tp>>
Source_ref_t mysql::ranges::View_source< Source_tp, owns_source_tp >::m_source {}
private

The source.

◆ owns_source

template<class Source_tp , bool owns_source_tp = std::ranges::view<Source_tp>>
constexpr bool mysql::ranges::View_source< Source_tp, owns_source_tp >::owns_source = owns_source_tp
staticconstexpr

True if this object owns a copy of its source; false if it holds a reference to its source.


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