LeechCraft  0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
modelitembase.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #pragma once
10 
11 #include <memory>
12 #include <QVector>
13 
14 namespace LC::Util
15 {
45  template<typename T>
46  class ModelItemBase : public std::enable_shared_from_this<T>
47  {
48  protected:
49  using T_wptr = std::weak_ptr<T>;
50  using T_ptr = std::shared_ptr<T>;
51  using T_cptr = std::shared_ptr<const T>;
52  using TList_t = QVector<T_ptr>;
53 
56 
59  ModelItemBase () = default;
60 
65  explicit ModelItemBase (const T_wptr& parent)
66  : Parent_ { parent }
67  {
68  }
69  public:
72  using iterator = typename TList_t::iterator;
73 
76  using const_iterator = typename TList_t::const_iterator;
77 
85  {
86  return Children_.begin ();
87  }
88 
95  {
96  return Children_.end ();
97  }
98 
106  {
107  return Children_.begin ();
108  }
109 
116  {
117  return Children_.end ();
118  }
119 
129  T_ptr GetChild (int row) const
130  {
131  return Children_.value (row);
132  }
133 
138  const TList_t& GetChildren () const
139  {
140  return Children_;
141  }
142 
148  {
149  return Children_;
150  }
151 
156  int GetRowCount () const
157  {
158  return Children_.size ();
159  }
160 
166  bool IsEmpty () const
167  {
168  return Children_.isEmpty ();
169  }
170 
184  {
185  return Children_.erase (it);
186  }
187 
208  {
209  return Children_.erase (begin, end);
210  }
211 
221  void AppendExisting (const T_ptr& t)
222  {
223  Children_ << t;
224  }
225 
235  void AppendExisting (const TList_t& items)
236  {
237  Children_ += items;
238  }
239 
251  template<typename... Args>
252  T_ptr& AppendChild (Args&&... args)
253  {
254  Children_.append (std::make_shared<T> (std::forward<Args> (args)...));
255  return Children_.last ();
256  }
257 
272  template<typename... Args>
273  T_ptr& InsertChild (int pos, Args&&... args)
274  {
275  Children_.insert (pos, std::make_shared<T> (std::forward<Args> (args)...));
276  return Children_ [pos];
277  }
278 
285  T_ptr GetParent () const
286  {
287  return Parent_.lock ();
288  }
289 
295  int GetRow (const T_ptr& item) const
296  {
297  return Children_.indexOf (item);
298  }
299 
305  int GetRow (const T_cptr& item) const
306  {
307  const auto pos = std::find (Children_.begin (), Children_.end (), item);
308  return pos == Children_.end () ?
309  -1 :
310  std::distance (Children_.begin (), pos);
311  }
312 
322  int GetRow () const
323  {
324  const auto parent = GetParent ();
325  if (!parent)
326  return -1;
327  return parent->GetRow (this->shared_from_this ());
328  }
329  };
330 }
Base class for model items for tree-like models.
Definition: modelitembase.h:46
T_ptr GetChild(int row) const
Returns a child at the given row.
std::shared_ptr< const ModelItem > T_cptr
Definition: modelitembase.h:51
int GetRow(const T_cptr &item) const
Returns the index of the item in the children list.
ModelItemBase()=default
Constructs a default ModelItemBase with no parent.
bool IsEmpty() const
Returns whether there are any children at all.
const_iterator end() const
Returns a const iterator pointing past the last child item.
const TList_t & GetChildren() const
Returns a constant reference to the list of children.
T_ptr & InsertChild(int pos, Args &&... args)
Creates a new child item, inserts it at the given position and returns it.
iterator end()
Returns a non-const iterator pointing past the last child item.
Definition: modelitembase.h:94
typename TList_t::const_iterator const_iterator
A const iterator for the list of children.
Definition: modelitembase.h:76
void AppendExisting(const T_ptr &t)
Appends a child item t to the list of child items.
T_ptr & AppendChild(Args &&... args)
Creates a new child item, appends it and returns it.
T_ptr GetParent() const
Returns the pointer to the parent item.
ModelItemBase(const T_wptr &parent)
Constructs a ModelItemBase with a given parent item.
Definition: modelitembase.h:65
std::shared_ptr< ModelItem > T_ptr
Definition: modelitembase.h:50
iterator EraseChildren(iterator begin, iterator end)
Erases all child items in the given range.
int GetRow() const
Returns the index of this item in the parent&#39;s children list.
typename TList_t::iterator iterator
A non-const iterator for the list of children.
Definition: modelitembase.h:72
void AppendExisting(const TList_t &items)
Appends a list of items to the list of child items.
iterator EraseChild(iterator it)
Erases a child item at the position defined by it.
const_iterator begin() const
Returns a const iterator pointing to the beginning of the child items list.
int GetRowCount() const
Returns the children count.
int GetRow(const T_ptr &item) const
Returns the index of the item in the children list.
TList_t & GetChildren()
Returns a non-constant reference to the list of children.
iterator begin()
Returns a non-const iterator pointing to the beginning of the child items list.
Definition: modelitembase.h:84
std::weak_ptr< ModelItem > T_wptr
Definition: modelitembase.h:49