Ocean
Ocean::SharedPointerConstArrayAccessor< T > Class Template Reference

This class implements an accessor providing direct access to std::shared_ptr<T> elements returned as const T* pointers. More...

Inheritance diagram for Ocean::SharedPointerConstArrayAccessor< T >:

Public Types

using SharedPointer = std::shared_ptr< T >
 Definition of the shared pointer object. More...
 
using SharedPointers = std::vector< SharedPointer >
 Definition of a vector holding the shared pointer objects. More...
 
- Public Types inherited from Ocean::ConstAccessor< T, TKey >
typedef T Type
 Definition of the element type of this accessor. More...
 
typedef TKey KeyType
 Definition of the key (or e.g., index) type of this accessor. More...
 

Public Member Functions

 SharedPointerConstArrayAccessor ()=default
 Creates a new empty accessor. More...
 
 SharedPointerConstArrayAccessor (SharedPointerConstArrayAccessor< T > &&accessor)=default
 Move constructor. More...
 
 SharedPointerConstArrayAccessor (const SharedPointer *elements, const size_t size)
 Creates a new accessor object. More...
 
 SharedPointerConstArrayAccessor (const SharedPointers &elements)
 Creates a new accessor object. More...
 
virtual size_t size () const
 Returns the number of accessible elements of this accessor object. More...
 
virtual const T *const & operator[] (const size_t &index) const
 Returns one element of this accessor object. More...
 
SharedPointerConstArrayAccessor< T > & operator= (SharedPointerConstArrayAccessor< T > &&accessor)=default
 Move operator. More...
 
- Public Member Functions inherited from Ocean::ConstIndexedAccessor< const T * >
virtual bool canAccess (const size_t &index) const
 Returns whether this accessor has a specific element. More...
 
virtual bool firstElement (const T * &element, size_t &index) const
 Returns the first element of this accessor. More...
 
virtual bool nextElement (const size_t &previousIndex, const T * &nextElement, size_t &nextIndex) const
 Returns the next element which follows a given key of the previous element. More...
 
- Public Member Functions inherited from Ocean::ConstAccessor< T, TKey >
virtual const T * data () const
 Returns a pointer to the elements of this accessor if the data exists within one memory block without gaps. More...
 
virtual bool canAccess (const TKey &key) const =0
 Returns whether this accessor has a specific element. More...
 
virtual bool firstElement (T &element, TKey &key) const =0
 Returns the first element of this accessor. More...
 
virtual bool nextElement (const TKey &previousKey, T &nextElement, TKey &nextKey) const =0
 Returns the next element which follows a given key of the previous element. More...
 
virtual const T & operator[] (const TKey &key) const =0
 Returns one element of this accessor object by a given key. More...
 
- Public Member Functions inherited from Ocean::Accessor
virtual ~Accessor ()=default
 Default destructor. More...
 
bool isEmpty () const
 Returns whether this accessor provides no elements. More...
 

Protected Attributes

std::vector< const T * > elements_
 The pointers to the actual elements wrapped in the shared pointers. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from Ocean::Accessor
template<typename TAccessor >
static std::vector< typename TAccessor::Type > accessor2elements (const TAccessor &accessor)
 Returns all elements of a given accessor (as a block). More...
 
template<typename TAccessor >
static std::unordered_map< typename TAccessor::KeyType, typename TAccessor::Type > accessor2map (const TAccessor &accessor)
 Returns all elements of a given accessor as a map with key and elements. More...
 
template<typename TAccessor , typename TIndex >
static std::vector< typename TAccessor::Type > accessor2subsetElements (const TAccessor &accessor, const std::vector< TIndex > &subset)
 Returns a subset of all elements of a given accessor (as a block). More...
 
- Protected Member Functions inherited from Ocean::ConstIndexedAccessor< const T * >
 ConstIndexedAccessor ()=default
 Creates a new indexed-based accessor object. More...
 
- Protected Member Functions inherited from Ocean::ConstAccessor< T, TKey >
 ConstAccessor ()=default
 Protected default constructor. More...
 
- Protected Member Functions inherited from Ocean::Accessor
 Accessor ()=default
 Protected default constructor. More...
 
 Accessor (const Accessor &accessor)=default
 Protected copy constructor. More...
 
Accessoroperator= (const Accessor &accessor)=delete
 Deleted assign operator. More...
 

Detailed Description

template<typename T>
class Ocean::SharedPointerConstArrayAccessor< T >

This class implements an accessor providing direct access to std::shared_ptr<T> elements returned as const T* pointers.

An instance of this accessor does not copy the elements, thus the caller has to ensure that the actual elements exist as long as the instance of the accessor exists.
This class is mainly a helper class to avoid extracting pointer from a shared_ptr objects and using a normal ConstArrayAccessor.

The application of the this class is demonstrated in the following code example:

void iterate(const ConstIndexedAccessor<const Object*>& accessor)
{
for (size_t n = 0; n < accessor.size(); ++n)
{
// access the object by application of the !virtual! index operator
const Object* object = accessor[n];
// ... do something with the object ...
}
}
void main()
{
std::vector<std::shared_ptr<Object>> objects;
// ... add some objects ...
// iterate over all objects
iterate(SharedPointerConstArrayAccessor<Object>(objects));
}
Template Parameters
TThe data type of the shared_ptr's element_type
See also
ConstArrayAccessor.

Member Typedef Documentation

◆ SharedPointer

template<typename T >
using Ocean::SharedPointerConstArrayAccessor< T >::SharedPointer = std::shared_ptr<T>

Definition of the shared pointer object.

◆ SharedPointers

template<typename T >
using Ocean::SharedPointerConstArrayAccessor< T >::SharedPointers = std::vector<SharedPointer>

Definition of a vector holding the shared pointer objects.

Constructor & Destructor Documentation

◆ SharedPointerConstArrayAccessor() [1/4]

Creates a new empty accessor.

◆ SharedPointerConstArrayAccessor() [2/4]

Move constructor.

Parameters
accessorAccessor to be moved

◆ SharedPointerConstArrayAccessor() [3/4]

template<typename T >
Ocean::SharedPointerConstArrayAccessor< T >::SharedPointerConstArrayAccessor ( const SharedPointer elements,
const size_t  size 
)
inline

Creates a new accessor object.

Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.

Parameters
elementsThe elements that can be accessed, may be nullptr if size is equal 0
sizeThe number of elements that can be accessed, may be 0 if elements is nullptr

◆ SharedPointerConstArrayAccessor() [4/4]

template<typename T >
Ocean::SharedPointerConstArrayAccessor< T >::SharedPointerConstArrayAccessor ( const SharedPointers elements)
inlineexplicit

Creates a new accessor object.

Beware: The given elements are not copied, they must not be deleted before the accessor is disposed.

Parameters
elementsA vector holding all elements

Member Function Documentation

◆ operator=()

template<typename T >
SharedPointerConstArrayAccessor<T>& Ocean::SharedPointerConstArrayAccessor< T >::operator= ( SharedPointerConstArrayAccessor< T > &&  accessor)
default

Move operator.

Parameters
accessorAccessor to be moved
Returns
Reference to this accessor

◆ operator[]()

template<typename T >
const T *const & Ocean::SharedPointerConstArrayAccessor< T >::operator[] ( const size_t &  index) const
virtual

Returns one element of this accessor object.

See also
ConstAccessor::operator[].

Implements Ocean::ConstIndexedAccessor< const T * >.

◆ size()

template<typename T >
size_t Ocean::SharedPointerConstArrayAccessor< T >::size
virtual

Returns the number of accessible elements of this accessor object.

See also
ConstAccessor::size().

Implements Ocean::Accessor.

Field Documentation

◆ elements_

template<typename T >
std::vector<const T*> Ocean::SharedPointerConstArrayAccessor< T >::elements_
protected

The pointers to the actual elements wrapped in the shared pointers.


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