Calico
 All Classes Namespaces Files Functions Variables Typedefs Friends Groups
cal::container_base< Derived, ConstIterator, Iterator, Size > Struct Template Reference

CRTP base type for defining an mutable, iterable container. More...

#include <iterator.hpp>

Public Types

typedef ConstIterator const_iterator
 Const iterator type.
 
typedef std::iterator_traits
< const_iterator >::pointer 
const_pointer
 Const pointer type.
 
typedef std::iterator_traits
< const_iterator >::reference 
const_reference
 Const reference type.
 
typedef std::reverse_iterator
< const_iterator
const_reverse_iterator
 Const reverse iterator.
 
typedef std::common_type
< std::iterator_traits
< const_iterator >
::difference_type,
std::iterator_traits< iterator >
::difference_type >::type 
difference_type
 Difference type.
 
typedef Iterator iterator
 Iterator type.
 
typedef std::iterator_traits
< iterator >::pointer 
pointer
 Pointer type.
 
typedef std::iterator_traits
< iterator >::reference 
reference
 Reference type.
 
typedef std::reverse_iterator
< iterator
reverse_iterator
 Reverse iterator.
 
typedef Size size_type
 Size type.
 
typedef std::common_type
< std::iterator_traits
< const_iterator >::value_type,
std::iterator_traits< iterator >
::value_type >::type 
value_type
 Value type.
 

Public Member Functions

const_reference at (size_type index) const
 Accesses the element at a given index with bounds-checking. More...
 
reference at (size_type index)
 Accesses the element at a given index with bounds-checking. More...
 
const_reference back () const
 Returns a const_reference to the last element in the container. More...
 
reference back ()
 Returns a reference to the last element in the container. More...
 
iterator begin ()
 Returns an iterator to the beginning of the container. More...
 
const_iterator begin () const
 Returns a const_iterator to the beginning of the container. More...
 
const_iterator cbegin () const
 Returns a const_iterator to the beginning of the container. More...
 
const_iterator cend () const
 Returns a const_iterator to the end of the container. More...
 
const_reverse_iterator crbegin () const
 Returns a const_reverse_iterator to the beginning of the container. More...
 
const_reverse_iterator crend () const
 Returns a const_reverse_iterator to the end of the container. More...
 
bool empty () const
 Returns whether the container is empty. More...
 
iterator end ()
 Returns an iterator to the end of the container. More...
 
const_iterator end () const
 Returns a const_iterator to the end of the container. More...
 
const_reference front () const
 Returns a const_reference to the first element in the container. More...
 
reference front ()
 Returns a reference to the first element in the container. More...
 
const_reference operator[] (size_type index) const
 Accesses the element at a given index. More...
 
reference operator[] (size_type index)
 Accesses the element at a given index. More...
 
reverse_iterator rbegin ()
 Returns a const_reverse_iterator to the beginning of the container. More...
 
const_reverse_iterator rbegin () const
 Returns a const_reverse_iterator to the beginning of the container. More...
 
reverse_iterator rend ()
 Returns a const_reverse_iterator to the end of the container. More...
 
const_reverse_iterator rend () const
 Returns a const_reverse_iterator to the end of the container. More...
 
size_type size () const
 Returns the number of elements in the container. More...
 

Protected Member Functions

iterator _begin ()
 Returns an iterator to the beginning of the container. More...
 
iterator _end ()
 Returns an iterator to the end of the container. More...
 

Detailed Description

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
struct cal::container_base< Derived, ConstIterator, Iterator, Size >

CRTP base type for defining an mutable, iterable container.

Template Parameters
DerivedThe derived type.
ConstIteratorAn iterator type for read-only access.
IteratorAn iterator type that is convertible to ConstIterator. If the container is immutable, it must be the same as ConstIterator.
SizeThe size type. Defaults to the common_type of the unsigned difference_types of the iterators.

The Derived type must override certain methods in order for this base type to work correctly. It must provide each of the following at minimum:

  • Either one of the following (starting with the lowest precedence):
    • data (equivalent to _begin)
    • _begin
    • begin const and begin (the latter is optional if the container is immutable).
  • Either one of the following (starting with the lowest precedence):
    • size
    • _end
    • end const and end (the latter is optional if the container is immutable).

The listing below shows the dependencies of each function defined by the base type. The arrow (<==) may be read as "depends on". Any of these functions may be overridden to customize the behavior of the container.

Note: Keep in mind that if the Derived type defines a function, the name of the function becomes shadowed so all overloads of this function provided by the base type become unavailable unless explicitly imported via a using declaration.

size const <== begin const && end const
_begin <== data /* not defined */
_end <== begin && size
begin <== _begin
end <== _end
// special cases for consts
begin const <== begin
end const <== end
cbegin const <== begin const
cend const <== end const
// queries
empty <== begin const && end const
[] const <== begin const
[] <== begin
at const <== [] const
at <== []
// front & back
front const <== begin const
front <== begin
back const <== end const
back <== end
// reverse iterators
rbegin const <== end const
rend const <== begin const
rbegin <== end
rend <== begin
crbegin <== rbegin const
crend <== rend const

Member Function Documentation

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
iterator cal::container_base< Derived, ConstIterator, Iterator, Size >::_begin ( )
inlineprotected

Returns an iterator to the beginning of the container.

Depends on data(). This function is provided so that the non-const begin() can be overridden without shadowing the const begin() provided by this base type.

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
iterator cal::container_base< Derived, ConstIterator, Iterator, Size >::_end ( )
inlineprotected

Returns an iterator to the end of the container.

Depends on begin() and size(). This function is provided so that the non-const end() can be overridden without shadowing the const end() provided by this base type.

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
const_reference cal::container_base< Derived, ConstIterator, Iterator, Size >::at ( size_type  index) const
inline

Accesses the element at a given index with bounds-checking.

Depends on operator[] const.

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
reference cal::container_base< Derived, ConstIterator, Iterator, Size >::at ( size_type  index)
inline

Accesses the element at a given index with bounds-checking.

Depends on operator[].

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
const_reference cal::container_base< Derived, ConstIterator, Iterator, Size >::back ( ) const
inline

Returns a const_reference to the last element in the container.

Depends on end() const and is only defined if the iterator is bidirectional. If the container is empty, the result is undefined.

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
reference cal::container_base< Derived, ConstIterator, Iterator, Size >::back ( )
inline

Returns a reference to the last element in the container.

Depends on end() and is only defined if the iterator is bidirectional. If the container is empty, the result is undefined.

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
iterator cal::container_base< Derived, ConstIterator, Iterator, Size >::begin ( )
inline

Returns an iterator to the beginning of the container.

Depends on _begin().

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
const_iterator cal::container_base< Derived, ConstIterator, Iterator, Size >::begin ( ) const
inline

Returns a const_iterator to the beginning of the container.

Depends on the non-const begin() and uses a const_cast.

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
const_iterator cal::container_base< Derived, ConstIterator, Iterator, Size >::cbegin ( ) const
inline

Returns a const_iterator to the beginning of the container.

Depends on begin() const.

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
const_iterator cal::container_base< Derived, ConstIterator, Iterator, Size >::cend ( ) const
inline

Returns a const_iterator to the end of the container.

Depends on end() const.

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
const_reverse_iterator cal::container_base< Derived, ConstIterator, Iterator, Size >::crbegin ( ) const
inline

Returns a const_reverse_iterator to the beginning of the container.

Depends on rbegin() const.

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
const_reverse_iterator cal::container_base< Derived, ConstIterator, Iterator, Size >::crend ( ) const
inline

Returns a const_reverse_iterator to the end of the container.

Depends on rend() const.

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
bool cal::container_base< Derived, ConstIterator, Iterator, Size >::empty ( ) const
inline

Returns whether the container is empty.

Depends on begin() const and end() const.

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
iterator cal::container_base< Derived, ConstIterator, Iterator, Size >::end ( )
inline

Returns an iterator to the end of the container.

Depends on _end().

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
const_iterator cal::container_base< Derived, ConstIterator, Iterator, Size >::end ( ) const
inline

Returns a const_iterator to the end of the container.

Depends on the non-const end() and uses a const_cast.

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
const_reference cal::container_base< Derived, ConstIterator, Iterator, Size >::front ( ) const
inline

Returns a const_reference to the first element in the container.

Depends on begin() const. If the container is empty, the result is undefined.

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
reference cal::container_base< Derived, ConstIterator, Iterator, Size >::front ( )
inline

Returns a reference to the first element in the container.

Depends on begin(). If the container is empty, the result is undefined.

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
const_reference cal::container_base< Derived, ConstIterator, Iterator, Size >::operator[] ( size_type  index) const
inline

Accesses the element at a given index.

Depends on begin() const and calls std::advance with ADL.

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
reference cal::container_base< Derived, ConstIterator, Iterator, Size >::operator[] ( size_type  index)
inline

Accesses the element at a given index.

Depends on begin() and calls std::advance with ADL.

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
reverse_iterator cal::container_base< Derived, ConstIterator, Iterator, Size >::rbegin ( )
inline

Returns a const_reverse_iterator to the beginning of the container.

Depends on end().

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
const_reverse_iterator cal::container_base< Derived, ConstIterator, Iterator, Size >::rbegin ( ) const
inline

Returns a const_reverse_iterator to the beginning of the container.

Depends on end() const.

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
reverse_iterator cal::container_base< Derived, ConstIterator, Iterator, Size >::rend ( )
inline

Returns a const_reverse_iterator to the end of the container.

Depends on begin().

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
const_reverse_iterator cal::container_base< Derived, ConstIterator, Iterator, Size >::rend ( ) const
inline

Returns a const_reverse_iterator to the end of the container.

Depends on begin() const.

template<class Derived, class ConstIterator, class Iterator = ConstIterator, class Size = auto>
size_type cal::container_base< Derived, ConstIterator, Iterator, Size >::size ( ) const
inline

Returns the number of elements in the container.

Depends on begin() const and end() const and calls std::distance() with ADL.