Observable Reference
Generic observable objects for C++
Public Types | Public Member Functions | List of all members
observable::detail::collection< ValueType > Class Template Referencefinal

Description

template<typename ValueType>
class observable::detail::collection< ValueType >

Thread-safe collection that can store arbitrary items and apply a functor over them.

All methods of the collection can be safely called in parallel, from multiple threads.

Warning
The order of elements inside the collection is unspecified.
Template Parameters
ValueTypeType of the elements that will be stored inside the collection. This type must be at least move constructible.

Public Types

using id = std::size_t
 Identifier for an element that has been inserted. More...
 

Public Member Functions

 collection () noexcept=default
 Create an empty collection. More...
 
template<typename ValueType_ >
auto insert (ValueType_ &&element)
 Insert a new element into the collection. More...
 
auto remove (id const &element_id) noexcept
 Remove a previously inserted element from the collection. More...
 
template<typename UnaryFunctor >
void apply (UnaryFunctor &&fun) const noexcept(noexcept(fun(std::declval< ValueType >())))
 Apply a unary functor over all elements of the collection. More...
 
auto empty () const noexcept
 Return true if the collection has no elements. More...
 
 ~collection () noexcept
 Destructor. More...
 
 collection (collection const &)=delete
 Collections are not copy-constructible. More...
 
auto operator= (collection const &) -> collection &=delete
 Collections are not copy-assignable. More...
 
 collection (collection &&)=delete
 Collections are move-constructible. More...
 
auto operator= (collection &&) -> collection &=delete
 Collections are move-assignable. More...
 

Member Typedef Documentation

◆ id

template<typename ValueType >
using observable::detail::collection< ValueType >::id = std::size_t

Identifier for an element that has been inserted.

You can use this id to remove a previously inserted element.

Constructor & Destructor Documentation

◆ collection() [1/3]

template<typename ValueType >
observable::detail::collection< ValueType >::collection ( )
defaultnoexcept

Create an empty collection.

◆ ~collection()

template<typename ValueType >
observable::detail::collection< ValueType >::~collection ( )
inlinenoexcept

Destructor.

◆ collection() [2/3]

template<typename ValueType >
observable::detail::collection< ValueType >::collection ( collection< ValueType > const &  )
delete

Collections are not copy-constructible.

◆ collection() [3/3]

template<typename ValueType >
observable::detail::collection< ValueType >::collection ( collection< ValueType > &&  )
delete

Collections are move-constructible.

Member Function Documentation

◆ insert()

template<typename ValueType >
template<typename ValueType_ >
auto observable::detail::collection< ValueType >::insert ( ValueType_ &&  element)
inline

Insert a new element into the collection.

Parameters
[in]elementThe object to be inserted.
Template Parameters
ValueType_Type of the inserted element. Must be convertible to the collection's ValueType.
Returns
An id that can be used to remove the inserted element. This id is stable; you can use it after modifying the collection.
Note
Any apply() call running concurrently with an insert() that has already called its functor for at least one element, is guaranteed to not call the functor for this newly inserted element.

◆ remove()

template<typename ValueType >
auto observable::detail::collection< ValueType >::remove ( id const &  element_id)
inlinenoexcept

Remove a previously inserted element from the collection.

If no element with the provided id exists, this method does nothing.

Parameters
[in]element_idId of the element to remove. This is returned by insert.
Returns
True if an element of the collection was removed, false if no element has been removed.
Note
Any apply() call running concurrently with a remove() call that has not already called its functor with the removed element, is guaranteed to not call the functor with the removed element.

◆ apply()

template<typename ValueType >
template<typename UnaryFunctor >
void observable::detail::collection< ValueType >::apply ( UnaryFunctor &&  fun) const
inlinenoexcept

Apply a unary functor over all elements of the collection.

The functor will be called multiple times, with each element, in an unspecified order.

Note
This method is reentrant; you can call insert() and remove() on the collection from inside the functor.
It is well defined and supported to remove() the element passed to the functor, even before the functor returns.
Parameters
[in]funA functor that will be called with each element of the collection. The functor must be assignable to a std::function<void(ValueType const &)>.
Template Parameters
UnaryFunctorType of the fun parameter.

◆ empty()

template<typename ValueType >
auto observable::detail::collection< ValueType >::empty ( ) const
inlinenoexcept

Return true if the collection has no elements.

◆ operator=() [1/2]

template<typename ValueType >
auto observable::detail::collection< ValueType >::operator= ( collection< ValueType > const &  ) -> collection &=delete
delete

Collections are not copy-assignable.

◆ operator=() [2/2]

template<typename ValueType >
auto observable::detail::collection< ValueType >::operator= ( collection< ValueType > &&  ) -> collection &=delete
delete

Collections are move-assignable.