Loading...
Searching...
No Matches
TreeItem.h
Go to the documentation of this file.
1/* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2
3 This file is part of the TerraLib - a Framework for building GIS enabled applications.
4
5 TerraLib is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 TerraLib is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with TerraLib. See COPYING. If not, write to
17 TerraLib Team at <terralib-team@terralib.org>.
18 */
19
20/*!
21 \file terralib/common/TreeItem.h
22
23 \brief A base class for data organized as a tree.
24*/
25
26#ifndef __TERRALIB_COMMON_INTERNAL_COUNTEDTREEITEM_H
27#define __TERRALIB_COMMON_INTERNAL_COUNTEDTREEITEM_H
28
29// TerraLib
30#include "Config.h"
31#include "Counted.h"
32
33// STL
34#include <list>
35
36// Boost
37#include <boost/intrusive_ptr.hpp>
38
39namespace te
40{
41 namespace common
42 {
43// Forward declaration
44 class TreeItem;
45
46 typedef boost::intrusive_ptr<TreeItem> TreeItemPtr;
47
48 /*!
49 \class TreeItem
50
51 \brief This abstract class describes a basic item to be organized in a tree-oriented way.
52
53 A tree item can have several descendants (children) and only one parent.
54 This data structure will clear all the memory used by the items.
55
56 This implementation is based in the counted pattern, allowing to share the ownership of the items.
57
58 \ingroup common
59
60 \sa AbstractLayer
61 */
63 {
64 public:
65
66 typedef std::list<TreeItemPtr>::iterator iterator;
67 typedef std::list<TreeItemPtr>::const_iterator const_iterator;
68
69 /*!
70 \brief It initializes a new item having a parent.
71
72 Its parent will have this new item attached as its child automatically.
73
74 \param parent The parent item of this new item.
75 */
76 explicit TreeItem(TreeItem* parent = 0);
77
78 /*!
79 \brief It initializes a new item having a parent.
80
81 Its parent will have this new item attached as its child automatically.
82
83 \param parent The parent item of this new item.
84 */
85 explicit TreeItem(const TreeItemPtr& parent);
86
87 /*! \brief Virtual destructor. */
88 virtual ~TreeItem();
89
90 /*!
91 \brief It returns true if the item has descendants.
92
93 \return True if the item has descendants.
94 */
95 bool hasChildren() const;
96
97 /*!
98 \brief It returns a pointer to the parent of this node.
99
100 \return It returns a pointer to the parent node.
101
102 \note You canīt free the memory pointed by the returned pointer; otherwise, the result will be unpredictable.
103 */
105
106 /*!
107 \brief It returns the children of this tree item.
108
109 \return It returns the children list of this tree item.
110 */
111 const std::list<TreeItemPtr>& getChildren() const;
112
113 /*!
114 \brief It returns the n-th child.
115
116 \param i The child index.
117
118 \return It returns a pointer to the specified child node.
119 */
120 const TreeItemPtr& getChild(std::size_t i) const;
121
122 /*!
123 \brief It returns the n-th child.
124
125 \param i The child index.
126
127 \return It returns a pointer to the specified child node.
128 */
129 const TreeItemPtr& operator[](std::size_t i) const;
130
131 /*!
132 \brief It adds (appends) the item to the end of the children's list.
133
134 If the child item already has a parent,
135 it will be disconnected from it and will be attached to this one.
136
137 \param childItem The item to be added as a child of this item.
138
139 \pos The children's list will be increased with one element.
140 */
141 void add(const TreeItemPtr& childItem);
142
143 /*!
144 \brief It inserts an item in the informed position.
145
146 If the item has a parent,
147 it will be disconnected from it and will be attached to this one.
148
149 \param i The position where the item will be inserted.
150 \param item The item to be inserted.
151
152 \pos The children's list will be increased with one element.
153 */
154 void insert(std::size_t i, const TreeItemPtr& childItem);
155
156 /*!
157 \brief It removes the i-th child.
158
159 \param i The position of the item to be removed.
160
161 \return The removed item.
162
163 \pos The children's list will be decreased by one element.
164 */
165 TreeItemPtr remove(std::size_t i);
166
167 /*!
168 \brief It removes from the given position a certain number of items as children of this item.
169 The items to be removed will be disconnected from this item.
170
171 \param i The position from where the items will be removed.
172 \param count The number of items to be removed (> 0).
173
174 \return The list of removed items.
175 */
176 std::list<TreeItemPtr> remove(std::size_t i, std::size_t count);
177
178 /*!
179 \brief It replaces the child item at the given position by the new one.
180 The replaced item will be disconnected from this item.
181
182 \param i The item position where the replace operation will be taken.
183 \param childItem The new child item that will replace the item in the i-th position.
184
185 \return The item that was replaced.
186 */
187 TreeItemPtr replace(std::size_t i, const TreeItemPtr& childItem);
188
189 /*!
190 \brief It checks if the given layer is sibling of this one.
191
192 \param layer The layer to be compared to.
193
194 \return It returns true if the layers are siblings.
195 */
196 bool isSibling(const TreeItem* item) const;
197
198 /*!
199 \brief It returns the number of children of this node.
200
201 \return The number of direct descendants of this node.
202 */
203 std::size_t getChildrenCount() const;
204
205 /*!
206 \brief It returns the number of nodes that descends from this node.
207
208 \param count The number of nodes that descends from this node.
209
210 \note It will return the number of nodes in a sub-tree. So, if you want to count
211 how many elements there are in a tree, just call this method for the root item.
212
213 \note The count parameter must be initialized with zero.
214 */
215 void getDescendantsCount(std::size_t& count) const;
216
217 /*! \brief It disconnects this item from its parent, if it has one. */
219
220 /*!
221 \brief It returns the index of this item in the list of children of its parent item.
222
223 \return The index of this item in the list of children of its parent item. It returns
224 npos, if it has no parent.
225 */
226 std::size_t getIndex() const;
227
228 /*!
229 \brief It swaps the position of the given children.
230
231 \param firstChild One of the children.
232 \param secondChild The other child.
233 */
234 void swap(const TreeItemPtr& firstChild, const TreeItemPtr& secondChild);
235
236 /*!
237 \brief It swaps this item with its sibling in their parent node.
238
239 \param sibling The sibling to swap.
240 */
241 void swap(const TreeItemPtr& sibling);
242
243 /*!
244 \brief It returns the constant iterator associated to the first child of this item.
245
246 \return The constant iterator associated to the first child of this item.
247 */
249
250 /*!
251 \brief It returns the iterator associated to the first child of this item.
252
253 \return The iterator associated to the first child of this item.
254 */
256
257 /*!
258 \brief It returns the constant iterator that refers to one past the end of the children of this item.
259
260 \return The constant iterator that refers to one past the end of the children of this item.
261 */
263
264 /*!
265 \brief It returns the iterator that refers to one past the children of this item.
266
267 \return The iterator that refers to one past the children of this item.
268 */
270
271 protected:
272
273 /*!
274 \brief It sets the parent of this item to the specified one. Don't use this method if
275 you are not sure how to handle all the pointers involved in this operation.
276
277 \param parent The item to be set as parent of this item.
278
279 \note Avoid calling this method; instead, use add or the constructors.
280 */
281 void setParent(TreeItem* parent);
282
283 public:
284
285 static const std::size_t npos; //!< A value to indicate no match.
286
287 protected:
288
289 TreeItem* m_parent; //!< A pointer to the parent node.
290 std::list<TreeItemPtr> m_children; //!< Pointers to the items that are children.
291 };
292
293 } // end namespace common
294} // end namespace te
295
296#endif // __TERRALIB_COMMON_INTERNAL_COUNTEDTREEITEM_H
A counted class keeps track of how many abstract instances are pointing to it.
A counted class keeps track of how many abstract instances are pointing to it.
Definition: Counted.h:44
This abstract class describes a basic item to be organized in a tree-oriented way.
Definition: TreeItem.h:63
void getDescendantsCount(std::size_t &count) const
It returns the number of nodes that descends from this node.
bool isSibling(const TreeItem *item) const
It checks if the given layer is sibling of this one.
void disconnect()
It disconnects this item from its parent, if it has one.
void swap(const TreeItemPtr &firstChild, const TreeItemPtr &secondChild)
It swaps the position of the given children.
bool hasChildren() const
It returns true if the item has descendants.
const_iterator begin() const
It returns the constant iterator associated to the first child of this item.
TreeItem * m_parent
A pointer to the parent node.
Definition: TreeItem.h:289
TreeItemPtr replace(std::size_t i, const TreeItemPtr &childItem)
It replaces the child item at the given position by the new one. The replaced item will be disconnect...
const_iterator end() const
It returns the constant iterator that refers to one past the end of the children of this item.
const std::list< TreeItemPtr > & getChildren() const
It returns the children of this tree item.
void setParent(TreeItem *parent)
It sets the parent of this item to the specified one. Don't use this method if you are not sure how t...
const TreeItemPtr & getChild(std::size_t i) const
It returns the n-th child.
std::size_t getIndex() const
It returns the index of this item in the list of children of its parent item.
void insert(std::size_t i, const TreeItemPtr &childItem)
It inserts an item in the informed position.
void swap(const TreeItemPtr &sibling)
It swaps this item with its sibling in their parent node.
std::list< TreeItemPtr >::const_iterator const_iterator
Definition: TreeItem.h:67
std::list< TreeItemPtr > m_children
Pointers to the items that are children.
Definition: TreeItem.h:290
std::list< TreeItemPtr >::iterator iterator
Definition: TreeItem.h:66
const TreeItemPtr & operator[](std::size_t i) const
It returns the n-th child.
TreeItem(TreeItem *parent=0)
It initializes a new item having a parent.
void add(const TreeItemPtr &childItem)
It adds (appends) the item to the end of the children's list.
std::size_t getChildrenCount() const
It returns the number of children of this node.
std::list< TreeItemPtr > remove(std::size_t i, std::size_t count)
It removes from the given position a certain number of items as children of this item....
TreeItem * getParent() const
It returns a pointer to the parent of this node.
iterator begin()
It returns the iterator associated to the first child of this item.
TreeItemPtr remove(std::size_t i)
It removes the i-th child.
virtual ~TreeItem()
Virtual destructor.
TreeItem(const TreeItemPtr &parent)
It initializes a new item having a parent.
iterator end()
It returns the iterator that refers to one past the children of this item.
static const std::size_t npos
A value to indicate no match.
Definition: TreeItem.h:285
boost::intrusive_ptr< TreeItem > TreeItemPtr
Definition: TreeItem.h:46
TerraLib.
#define TECOMMONEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:66
Proxy configuration file for TerraView (see terraview_config.h).