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

std::optional-like wrapper around an object that is the source for a view: this may hold an object or not; and 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>

Inheritance diagram for mysql::ranges::Optional_view_source< Source_tp, owns_source_tp >:
[legend]

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 Optional_source_t = std::conditional_t< owns_source, std::optional< Source_t >, const Source_t * >
 Internal representation of the source. More...
 

Public Member Functions

 Optional_view_source ()=default
 Construct an objec that does not hold a source. More...
 
 Optional_view_source (const Source_t &source)
 Construct from a const reference to the source. More...
 
 Optional_view_source (const Source_t *source)
 Construct from a const pointer to the source. More...
 
bool has_object () const
 Return true if this object holds a 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 Raw_source_treference () const
 Return a reference to the stored object. More...
 
const Source_tpointer () const
 Return a pointer to the source if there is one, or nullptr otherwise. More...
 
const Source_toperator-> () const
 Arrow operator to access members of the source. More...
 
auto begin () const
 Return a valid begin iterator, even if !has_object(). More...
 
auto end () const
 Return a valid end iterator, even if !has_object(). 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 Protected Member Functions

static auto null_iterator ()
 

Static Private Member Functions

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

Private Attributes

Optional_source_t m_source {}
 The source. More...
 

Detailed Description

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

std::optional-like wrapper around an object that is the source for a view: this may hold an object or not; and 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 std::optional<T> if std::ranges::view<T>, or type const T * otherwise. It provides an API similar to std::optional.

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

◆ Optional_source_t

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

Internal representation of the source.

◆ Raw_source_t

template<class Source_tp , bool owns_source_tp = std::ranges::view<Source_tp>>
using mysql::ranges::Optional_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_t

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

Constructor & Destructor Documentation

◆ Optional_view_source() [1/3]

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

Construct an objec that does not hold a source.

◆ Optional_view_source() [2/3]

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

Construct from a const reference to the source.

◆ Optional_view_source() [3/3]

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

Construct from a const pointer to the source.

The source may be nullptr, in which case this object will not hold any sourcd.

Member Function Documentation

◆ begin()

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

Return a valid begin iterator, even if !has_object().

If !has_object(), it is defined that the source is an empty range, so then this function returns the end iterator to a default-constructed source.

This requires std::ranges::common_range<Source_t>, i.e., begin() and end() must return the same type.

◆ end()

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

Return a valid end iterator, even if !has_object().

If !has_object(), this returns the end iterator to a default-constructed source.

◆ from_ptr()

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

Return an Optional_source_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::Optional_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 if !has_object().

◆ has_object()

template<class Source_tp , bool owns_source_tp = std::ranges::view<Source_tp>>
bool mysql::ranges::Optional_view_source< Source_tp, owns_source_tp >::has_object ( ) const
inline

Return true if this object holds a source.

◆ null_iterator()

template<class Source_tp , bool owns_source_tp = std::ranges::view<Source_tp>>
static auto mysql::ranges::Optional_view_source< Source_tp, owns_source_tp >::null_iterator ( )
inlinestaticprotected

◆ operator->()

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

Arrow operator to access members of the source.

The behavior is undefined if !has_object().

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().

◆ pointer()

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

Return a pointer to the source if there is one, or nullptr otherwise.

Note that the pointer is only valid as long as this object exists.

◆ reference()

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

Return a reference to the stored object.

The behavior is undefined if !has_object(). 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>>
Optional_source_t mysql::ranges::Optional_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::Optional_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: