1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 Jochen Becher
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include <QList>
29 
30 namespace qmt {
31 
32 template<class T>
33 class Container
34 {
35 protected:
36     Container() = default;
Container(const Container<T> & rhs)37     Container(const Container<T> &rhs)
38         : m_elements(rhs.m_elements)
39     {
40         rhs.m_elements.clear();
41     }
42 
43 public:
~Container()44     ~Container()
45     {
46         qDeleteAll(m_elements);
47     }
48 
49     Container &operator=(const Container<T> &rhs)
50     {
51         if (this != &rhs) {
52             qDeleteAll(m_elements);
53             m_elements = rhs.m_elements;
54             rhs.m_elements.clear();
55         }
56         return *this;
57     }
58 
isEmpty()59     bool isEmpty() const { return m_elements.empty(); }
size()60     int size() const { return m_elements.size(); }
elements()61     QList<T *> elements() const { return m_elements; }
62 
takeElements()63     QList<T *> takeElements() const
64     {
65         QList<T *> elements = m_elements;
66         m_elements.clear();
67         return elements;
68     }
69 
submitElements(const QList<T * > & elements)70     void submitElements(const QList<T *> &elements)
71     {
72         qDeleteAll(m_elements);
73         m_elements = elements;
74     }
75 
reset()76     void reset()
77     {
78         qDeleteAll(m_elements);
79         m_elements.clear();
80     }
81 
submit(T * element)82     void submit(T *element)
83     {
84         m_elements.append(element);
85     }
86 
87 private:
88     mutable QList<T *> m_elements;
89 };
90 
91 } // namespace qmt
92