LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
LC::Util::ModelItemBase< T > Class Template Reference

Base class for model items for tree-like models. More...

#include "modelitembase.h"

+ Inheritance diagram for LC::Util::ModelItemBase< T >:
+ Collaboration diagram for LC::Util::ModelItemBase< T >:

Public Types

using iterator = typename TList_t::iterator
 A non-const iterator for the list of children. More...
 
using const_iterator = typename TList_t::const_iterator
 A const iterator for the list of children. More...
 

Public Member Functions

iterator begin ()
 Returns a non-const iterator pointing to the beginning of the child items list. More...
 
iterator end ()
 Returns a non-const iterator pointing past the last child item. More...
 
const_iterator begin () const
 Returns a const iterator pointing to the beginning of the child items list. More...
 
const_iterator end () const
 Returns a const iterator pointing past the last child item. More...
 
T_ptr GetChild (int row) const
 Returns a child at the given row. More...
 
const TList_tGetChildren () const
 Returns a constant reference to the list of children. More...
 
TList_tGetChildren ()
 Returns a non-constant reference to the list of children. More...
 
int GetRowCount () const
 Returns the children count. More...
 
bool IsEmpty () const
 Returns whether there are any children at all. More...
 
iterator EraseChild (iterator it)
 Erases a child item at the position defined by it. More...
 
iterator EraseChildren (iterator begin, iterator end)
 Erases all child items in the given range. More...
 
void AppendExisting (const T_ptr &t)
 Appends a child item t to the list of child items. More...
 
void AppendExisting (const TList_t &items)
 Appends a list of items to the list of child items. More...
 
template<typename... Args>
T_ptrAppendChild (Args &&... args)
 Creates a new child item, appends it and returns it. More...
 
template<typename... Args>
T_ptrInsertChild (int pos, Args &&... args)
 Creates a new child item, inserts it at the given position and returns it. More...
 
T_ptr GetParent () const
 Returns the pointer to the parent item. More...
 
int GetRow (const T_ptr &item) const
 Returns the index of the item in the children list. More...
 
int GetRow (const T_cptr &item) const
 Returns the index of the item in the children list. More...
 
int GetRow () const
 Returns the index of this item in the parent's children list. More...
 

Protected Types

using T_wptr = std::weak_ptr< T >
 
using T_ptr = std::shared_ptr< T >
 
using T_cptr = std::shared_ptr< const T >
 
using TList_t = QVector< T_ptr >
 

Protected Member Functions

 ModelItemBase ()=default
 Constructs a default ModelItemBase with no parent. More...
 
 ModelItemBase (const T_wptr &parent)
 Constructs a ModelItemBase with a given parent item. More...
 

Protected Attributes

T_wptr Parent_
 
TList_t Children_
 

Detailed Description

template<typename T>
class LC::Util::ModelItemBase< T >

Base class for model items for tree-like models.

A ModelItemBase only manages its children items and keeps track of its parent item. It does not contain any data itself.

This class is intended to be a base class for some concreted model item implementation (see ModelItem class for an example). In particular it cannot be instantiated itself since its constructors are protected.

The lifetime of the items is also managed. Shared pointers are used for this: the ModelItemBase stores a list of shared pointers to its children and a weak pointer to its parent.

In particular, child items are automatically deleted when they are removed from the children list of an item via EraseChild() or EraseChildren() functions, unless a shared pointer reference is kept to a child. A new child can be added via AppendChild(), InsertChild() or AppendExisting().

An STL iterator-like interface to the children is also provided, so a ModelItemBase can also be used in a range-for loop.

Template Parameters
TThe type of the class derived from ModelItemBase.
See also
ModelItem

Definition at line 52 of file modelitembase.h.

Member Typedef Documentation

◆ const_iterator

template<typename T >
using LC::Util::ModelItemBase< T >::const_iterator = typename TList_t::const_iterator

A const iterator for the list of children.

Definition at line 88 of file modelitembase.h.

◆ iterator

template<typename T >
using LC::Util::ModelItemBase< T >::iterator = typename TList_t::iterator

A non-const iterator for the list of children.

Definition at line 84 of file modelitembase.h.

◆ T_cptr

template<typename T >
using LC::Util::ModelItemBase< T >::T_cptr = std::shared_ptr<const T>
protected

Definition at line 63 of file modelitembase.h.

◆ T_ptr

template<typename T >
using LC::Util::ModelItemBase< T >::T_ptr = std::shared_ptr<T>
protected

Definition at line 62 of file modelitembase.h.

◆ T_wptr

template<typename T >
using LC::Util::ModelItemBase< T >::T_wptr = std::weak_ptr<T>
protected

Definition at line 61 of file modelitembase.h.

◆ TList_t

template<typename T >
using LC::Util::ModelItemBase< T >::TList_t = QVector<T_ptr>
protected

Definition at line 64 of file modelitembase.h.

Constructor & Destructor Documentation

◆ ModelItemBase() [1/2]

template<typename T >
LC::Util::ModelItemBase< T >::ModelItemBase ( )
protecteddefault

Constructs a default ModelItemBase with no parent.

◆ ModelItemBase() [2/2]

template<typename T >
LC::Util::ModelItemBase< T >::ModelItemBase ( const T_wptr parent)
inlineexplicitprotected

Constructs a ModelItemBase with a given parent item.

Parameters
[in]parentThe weak reference to the parent item.

Definition at line 77 of file modelitembase.h.

Member Function Documentation

◆ AppendChild()

template<typename T >
template<typename... Args>
T_ptr& LC::Util::ModelItemBase< T >::AppendChild ( Args &&...  args)
inline

Creates a new child item, appends it and returns it.

Template Parameters
ArgsThe types of the arguments to the constructor of of the model.
Parameters
[in]argsThe arguments to the constructor of the model item type T.
Returns
The pointer to the newly created child item.
See also
AppendExisting()
InsertChild()

Definition at line 264 of file modelitembase.h.

◆ AppendExisting() [1/2]

template<typename T >
void LC::Util::ModelItemBase< T >::AppendExisting ( const T_ptr t)
inline

Appends a child item t to the list of child items.

This function invalidates all iterators.

Parameters
[in]tThe child item to append.
See also
AppendChild()
InsertChild()

Definition at line 233 of file modelitembase.h.

◆ AppendExisting() [2/2]

template<typename T >
void LC::Util::ModelItemBase< T >::AppendExisting ( const TList_t items)
inline

Appends a list of items to the list of child items.

This function invalidates all iterators.

Parameters
[in]itemsThe list of items to append.
See also
AppendChild()
InsertChild()

Definition at line 247 of file modelitembase.h.

◆ begin() [1/2]

template<typename T >
iterator LC::Util::ModelItemBase< T >::begin ( )
inline

Returns a non-const iterator pointing to the beginning of the child items list.

Returns
A non-const iterator pointing to the beginning of the children list.

Definition at line 96 of file modelitembase.h.

◆ begin() [2/2]

template<typename T >
const_iterator LC::Util::ModelItemBase< T >::begin ( ) const
inline

Returns a const iterator pointing to the beginning of the child items list.

Returns
A const iterator pointing to the beginning of the children list.

Definition at line 117 of file modelitembase.h.

◆ end() [1/2]

template<typename T >
iterator LC::Util::ModelItemBase< T >::end ( )
inline

Returns a non-const iterator pointing past the last child item.

Returns
A non-const iterator pointing to the past the last child item.

Definition at line 106 of file modelitembase.h.

◆ end() [2/2]

template<typename T >
const_iterator LC::Util::ModelItemBase< T >::end ( ) const
inline

Returns a const iterator pointing past the last child item.

Returns
A const iterator pointing to the past the last child item.

Definition at line 127 of file modelitembase.h.

◆ EraseChild()

template<typename T >
iterator LC::Util::ModelItemBase< T >::EraseChild ( iterator  it)
inline

Erases a child item at the position defined by it.

If it is invalid iterator, the behavior is undefined.

This function invalidates all iterators.

Parameters
[in]itThe iterator pointing to the child to be erased.
Returns
The iterator pointing to the item right next to the erased one.
See also
EraseChildren()

Definition at line 195 of file modelitembase.h.

◆ EraseChildren()

template<typename T >
iterator LC::Util::ModelItemBase< T >::EraseChildren ( iterator  begin,
iterator  end 
)
inline

Erases all child items in the given range.

The range is half-open, comprised of items in the [begin; end) range of iterators.

If the range contains invalid iterators, the behavior is undefined.

This function invalidates all iterators.

Parameters
[in]beginThe iterator pointing to the first item to be erased.
[in]endThe iterator pointing past the last item to be erased.
Returns
The iterator pointing to the item right next to the last erased one.
See also
EraseChild()

Definition at line 219 of file modelitembase.h.

◆ GetChild()

template<typename T >
T_ptr LC::Util::ModelItemBase< T >::GetChild ( int  row) const
inline

Returns a child at the given row.

If there is no child at the given row, returns a null shared_ptr.

Parameters
[in]rowThe index of the child to be returned.
Returns
A pointer to the child in the row, or a null pointer if there is no child at that row.

Definition at line 141 of file modelitembase.h.

◆ GetChildren() [1/2]

template<typename T >
TList_t& LC::Util::ModelItemBase< T >::GetChildren ( )
inline

Returns a non-constant reference to the list of children.

Returns
The non-constant reference to the list of children.

Definition at line 159 of file modelitembase.h.

◆ GetChildren() [2/2]

template<typename T >
const TList_t& LC::Util::ModelItemBase< T >::GetChildren ( ) const
inline

Returns a constant reference to the list of children.

Returns
The constant reference to the list of children.

Definition at line 150 of file modelitembase.h.

◆ GetParent()

template<typename T >
T_ptr LC::Util::ModelItemBase< T >::GetParent ( ) const
inline

Returns the pointer to the parent item.

Returns
The pointer to the parent item or a null pointer if the parent item is deleted already or if there never was a parent.

Definition at line 297 of file modelitembase.h.

◆ GetRow() [1/3]

template<typename T >
int LC::Util::ModelItemBase< T >::GetRow ( ) const
inline

Returns the index of this item in the parent's children list.

If the parent is already deleted or there is no parent, -1 is returned.

Returns
The index of this item in the parent's list, or -1 if there is no parent.

Definition at line 334 of file modelitembase.h.

◆ GetRow() [2/3]

template<typename T >
int LC::Util::ModelItemBase< T >::GetRow ( const T_cptr item) const
inline

Returns the index of the item in the children list.

Returns
The index of the item in the children list, of -1 if item is not a child of this model item.

Definition at line 317 of file modelitembase.h.

◆ GetRow() [3/3]

template<typename T >
int LC::Util::ModelItemBase< T >::GetRow ( const T_ptr item) const
inline

Returns the index of the item in the children list.

Returns
The index of the item in the children list, of -1 if item is not a child of this model item.

Definition at line 307 of file modelitembase.h.

◆ GetRowCount()

template<typename T >
int LC::Util::ModelItemBase< T >::GetRowCount ( ) const
inline

Returns the children count.

Returns
The children count.

Definition at line 168 of file modelitembase.h.

◆ InsertChild()

template<typename T >
template<typename... Args>
T_ptr& LC::Util::ModelItemBase< T >::InsertChild ( int  pos,
Args &&...  args 
)
inline

Creates a new child item, inserts it at the given position and returns it.

Template Parameters
ArgsThe types of the arguments to the constructor of of the model.
Parameters
[in]posThe index where the newly created item should be inserted.
[in]argsThe arguments to the constructor of the model item type T.
Returns
The pointer to the newly created child item.
See also
AppendExisting()
AppendChild()

Definition at line 285 of file modelitembase.h.

◆ IsEmpty()

template<typename T >
bool LC::Util::ModelItemBase< T >::IsEmpty ( ) const
inline

Returns whether there are any children at all.

Returns
Returns true if there are no child items, or false otherwise.

Definition at line 178 of file modelitembase.h.

Member Data Documentation

◆ Children_

template<typename T >
TList_t LC::Util::ModelItemBase< T >::Children_
protected

Definition at line 67 of file modelitembase.h.

Referenced by LC::Util::ModelItemBase< ModelItem >::end().

◆ Parent_

template<typename T >
T_wptr LC::Util::ModelItemBase< T >::Parent_
protected

Definition at line 66 of file modelitembase.h.

Referenced by LC::Util::ModelItemBase< ModelItem >::InsertChild().


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