DOLFIN
DOLFIN C++ interface
Loading...
Searching...
No Matches
Hierarchical.h
1// Copyright (C) 2011 Anders Logg
2//
3// This file is part of DOLFIN.
4//
5// DOLFIN 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, or
8// (at your option) any later version.
9//
10// DOLFIN 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 DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17//
18// First added: 2011-01-30
19// Last changed: 2013-03-11
20
21#ifndef __HIERARCHICAL_H
22#define __HIERARCHICAL_H
23
24#include <memory>
25
26#include <dolfin/log/log.h>
27#include <dolfin/log/LogStream.h>
28#include "NoDeleter.h"
29
30namespace dolfin
31{
32
41
42 template <typename T>
44 {
45 public:
46
49
51 virtual ~Hierarchical() {}
52
60 std::size_t depth() const
61 {
62 std::size_t d = 1;
63 for (std::shared_ptr<const T> it = root_node_shared_ptr();
64 it->_child; it = it->_child)
65 d++;
66 return d;
67 }
68
74 bool has_parent() const
75 { return _parent ? true : false; }
76
82 bool has_child() const
83 { return _child ? true : false; }
84
91 T& parent()
92 {
93 if (!_parent)
94 dolfin_error("Hierarchical.h",
95 "extract parent of hierarchical object",
96 "Object has no parent in hierarchy");
97 return *_parent;
98 }
99
101 const T& parent() const
102 {
103 if (!_parent)
104 dolfin_error("Hierarchical.h",
105 "extract parent of hierarchical object",
106 "Object has no parent in hierarchy");
107 return *_parent;
108 }
109
116 std::shared_ptr<T> parent_shared_ptr()
117 { return _parent; }
118
120 std::shared_ptr<const T> parent_shared_ptr() const
121 { return _parent; }
122
129 T& child()
130 {
131 if (!_child)
132 dolfin_error("Hierarchical.h",
133 "extract child of hierarchical object",
134 "Object has no child in hierarchy");
135 return *_child;
136 }
137
139 const T& child() const
140 {
141 if (!_child)
142 dolfin_error("Hierarchical.h",
143 "extract child of hierarchical object",
144 "Object has no child in hierarchy");
145 return *_child;
146 }
147
154 std::shared_ptr<T> child_shared_ptr()
155 { return _child; }
156
158 std::shared_ptr<const T> child_shared_ptr() const
159 { return _child; }
160
167 {
168 return *root_node_shared_ptr();
169 }
170
172 const T& root_node() const
173 {
174 return *root_node_shared_ptr();
175 }
176
182 std::shared_ptr<T> root_node_shared_ptr()
183 {
184 std::shared_ptr<T> it = _self;
185 for (; it->_parent; it = it->_parent);
186 return it;
187 }
188
190 std::shared_ptr<const T> root_node_shared_ptr() const
191 {
192 std::shared_ptr<const T> it = _self;
193 for (; it->_parent; it = it->_parent);
194 return it;
195 }
196
203 {
204 return *leaf_node_shared_ptr();
205 }
206
208 const T& leaf_node() const
209 {
210 return *leaf_node_shared_ptr();
211 }
212
218 std::shared_ptr<T> leaf_node_shared_ptr()
219 {
220 std::shared_ptr<T> it = _self;
221 for (; it->_child; it = it->_child);
222 return it;
223 }
224
226 std::shared_ptr<const T> leaf_node_shared_ptr() const
227 {
228 std::shared_ptr<const T> it = _self;
229 for (; it->_child; it = it->_child);
230 return it;
231 }
232
234 void set_parent(std::shared_ptr<T> parent)
235 { _parent = parent; }
236
239 {
240 _child.reset();
241 }
242
244 void set_child(std::shared_ptr<T> child)
245 { _child = child; }
246
248 const Hierarchical& operator= (const Hierarchical& hierarchical)
249 {
250 // Destroy any previous parent-child relations
251 _parent.reset();
252 _child.reset();
253
254 return *this;
255 }
256
258 void _debug() const
259 {
260 info("Debugging hierarchical object:");
261 cout << " depth = " << depth() << endl;
262 cout << " has_parent() = " << has_parent() << endl;
263 info(" _parent.get() = %x", _parent.get());
264 info(" _parent.count() = %d", _parent.use_count());
265 cout << " has_child() = " << has_parent() << endl;
266 info(" _child.get() = %x", _parent.get());
267 info(" _child.count() = %d", _parent.use_count());
268 }
269
270 private:
271
272 // The object itself
273 std::shared_ptr<T> _self;
274
275 // Parent and child in hierarchy
276 std::shared_ptr<T> _parent;
277 std::shared_ptr<T> _child;
278
279 };
280
281}
282
283#endif
Definition Hierarchical.h:44
const Hierarchical & operator=(const Hierarchical &hierarchical)
Assignment operator.
Definition Hierarchical.h:248
void clear_child()
Clear child.
Definition Hierarchical.h:238
void set_child(std::shared_ptr< T > child)
Set child.
Definition Hierarchical.h:244
const T & leaf_node() const
Return leaf node object in hierarchy (const version).
Definition Hierarchical.h:208
T & root_node()
Definition Hierarchical.h:166
std::shared_ptr< T > parent_shared_ptr()
Definition Hierarchical.h:116
bool has_child() const
Definition Hierarchical.h:82
void _debug() const
Function useful for debugging the hierarchy.
Definition Hierarchical.h:258
std::shared_ptr< T > leaf_node_shared_ptr()
Definition Hierarchical.h:218
Hierarchical(T &self)
Constructor.
Definition Hierarchical.h:48
std::shared_ptr< const T > parent_shared_ptr() const
Return shared pointer to parent (const version).
Definition Hierarchical.h:120
std::shared_ptr< const T > root_node_shared_ptr() const
Return shared pointer to root node object in hierarchy (const version).
Definition Hierarchical.h:190
const T & root_node() const
Return root node object in hierarchy (const version).
Definition Hierarchical.h:172
virtual ~Hierarchical()
Destructor.
Definition Hierarchical.h:51
std::size_t depth() const
Definition Hierarchical.h:60
bool has_parent() const
Definition Hierarchical.h:74
std::shared_ptr< T > child_shared_ptr()
Definition Hierarchical.h:154
std::shared_ptr< const T > child_shared_ptr() const
Return shared pointer to child (const version).
Definition Hierarchical.h:158
std::shared_ptr< T > root_node_shared_ptr()
Definition Hierarchical.h:182
const T & parent() const
Return parent in hierarchy (const version).
Definition Hierarchical.h:101
T & leaf_node()
Definition Hierarchical.h:202
void set_parent(std::shared_ptr< T > parent)
Set parent.
Definition Hierarchical.h:234
std::shared_ptr< const T > leaf_node_shared_ptr() const
Return shared pointer to leaf node object in hierarchy (const version).
Definition Hierarchical.h:226
T & child()
Definition Hierarchical.h:129
T & parent()
Definition Hierarchical.h:91
const T & child() const
Return child in hierarchy (const version).
Definition Hierarchical.h:139
Definition adapt.h:30
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition log.cpp:129
LogStream cout
dolfin::cout
LogStream endl
dolfin::endl;
void info(std::string msg,...)
Print message.
Definition log.cpp:72
std::shared_ptr< T > reference_to_no_delete_pointer(T &r)
Helper function to construct shared pointer with NoDeleter with cleaner syntax.
Definition NoDeleter.h:43