Loading...
Searching...
No Matches
Hierarchical.h
// Copyright (C) 2011 Anders Logg
//
// This file is part of DOLFIN.
//
// DOLFIN is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DOLFIN is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
//
// First added: 2011-01-30
// Last changed: 2013-03-11
#ifndef __HIERARCHICAL_H
#define __HIERARCHICAL_H
#include <memory>
#include <dolfin/log/log.h>
#include <dolfin/log/LogStream.h>
#include "NoDeleter.h"
namespace dolfin
{
template <typename T>
class Hierarchical
{
public:
Hierarchical(T& self) : _self(reference_to_no_delete_pointer(self)) {}
virtual ~Hierarchical() {}
std::size_t depth() const
{
std::size_t d = 1;
for (std::shared_ptr<const T> it = root_node_shared_ptr();
it->_child; it = it->_child)
d++;
return d;
}
bool has_parent() const
{ return _parent ? true : false; }
bool has_child() const
{ return _child ? true : false; }
T& parent()
{
if (!_parent)
dolfin_error("Hierarchical.h",
"extract parent of hierarchical object",
"Object has no parent in hierarchy");
return *_parent;
}
const T& parent() const
{
if (!_parent)
dolfin_error("Hierarchical.h",
"extract parent of hierarchical object",
"Object has no parent in hierarchy");
return *_parent;
}
std::shared_ptr<T> parent_shared_ptr()
{ return _parent; }
std::shared_ptr<const T> parent_shared_ptr() const
{ return _parent; }
T& child()
{
if (!_child)
dolfin_error("Hierarchical.h",
"extract child of hierarchical object",
"Object has no child in hierarchy");
return *_child;
}
const T& child() const
{
if (!_child)
dolfin_error("Hierarchical.h",
"extract child of hierarchical object",
"Object has no child in hierarchy");
return *_child;
}
std::shared_ptr<T> child_shared_ptr()
{ return _child; }
std::shared_ptr<const T> child_shared_ptr() const
{ return _child; }
T& root_node()
{
return *root_node_shared_ptr();
}
const T& root_node() const
{
return *root_node_shared_ptr();
}
std::shared_ptr<T> root_node_shared_ptr()
{
std::shared_ptr<T> it = _self;
for (; it->_parent; it = it->_parent);
return it;
}
std::shared_ptr<const T> root_node_shared_ptr() const
{
std::shared_ptr<const T> it = _self;
for (; it->_parent; it = it->_parent);
return it;
}
T& leaf_node()
{
return *leaf_node_shared_ptr();
}
const T& leaf_node() const
{
return *leaf_node_shared_ptr();
}
std::shared_ptr<T> leaf_node_shared_ptr()
{
std::shared_ptr<T> it = _self;
for (; it->_child; it = it->_child);
return it;
}
std::shared_ptr<const T> leaf_node_shared_ptr() const
{
std::shared_ptr<const T> it = _self;
for (; it->_child; it = it->_child);
return it;
}
void set_parent(std::shared_ptr<T> parent)
{ _parent = parent; }
void clear_child()
{
_child.reset();
}
void set_child(std::shared_ptr<T> child)
{ _child = child; }
const Hierarchical& operator= (const Hierarchical& hierarchical)
{
// Destroy any previous parent-child relations
_parent.reset();
_child.reset();
return *this;
}
void _debug() const
{
info("Debugging hierarchical object:");
cout << " depth = " << depth() << endl;
cout << " has_parent() = " << has_parent() << endl;
info(" _parent.get() = %x", _parent.get());
info(" _parent.count() = %d", _parent.use_count());
cout << " has_child() = " << has_parent() << endl;
info(" _child.get() = %x", _parent.get());
info(" _child.count() = %d", _parent.use_count());
}
private:
// The object itself
std::shared_ptr<T> _self;
// Parent and child in hierarchy
std::shared_ptr<T> _parent;
std::shared_ptr<T> _child;
};
}
#endif
dolfin::Hierarchical::operator=
const Hierarchical & operator=(const Hierarchical &hierarchical)
Assignment operator.
Definition Hierarchical.h:248
dolfin::Hierarchical::clear_child
void clear_child()
Clear child.
Definition Hierarchical.h:238
dolfin::Hierarchical::set_child
void set_child(std::shared_ptr< T > child)
Set child.
Definition Hierarchical.h:244
dolfin::Hierarchical::leaf_node
const T & leaf_node() const
Return leaf node object in hierarchy (const version).
Definition Hierarchical.h:208
dolfin::Hierarchical::root_node
T & root_node()
Definition Hierarchical.h:166
dolfin::Hierarchical::parent_shared_ptr
std::shared_ptr< T > parent_shared_ptr()
Definition Hierarchical.h:116
dolfin::Hierarchical::has_child
bool has_child() const
Definition Hierarchical.h:82
dolfin::Hierarchical::_debug
void _debug() const
Function useful for debugging the hierarchy.
Definition Hierarchical.h:258
dolfin::Hierarchical::leaf_node_shared_ptr
std::shared_ptr< T > leaf_node_shared_ptr()
Definition Hierarchical.h:218
dolfin::Hierarchical::Hierarchical
Hierarchical(T &self)
Constructor.
Definition Hierarchical.h:48
dolfin::Hierarchical::parent_shared_ptr
std::shared_ptr< const T > parent_shared_ptr() const
Return shared pointer to parent (const version).
Definition Hierarchical.h:120
dolfin::Hierarchical::root_node_shared_ptr
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
dolfin::Hierarchical::root_node
const T & root_node() const
Return root node object in hierarchy (const version).
Definition Hierarchical.h:172
dolfin::Hierarchical::~Hierarchical
virtual ~Hierarchical()
Destructor.
Definition Hierarchical.h:51
dolfin::Hierarchical::depth
std::size_t depth() const
Definition Hierarchical.h:60
dolfin::Hierarchical::has_parent
bool has_parent() const
Definition Hierarchical.h:74
dolfin::Hierarchical::child_shared_ptr
std::shared_ptr< T > child_shared_ptr()
Definition Hierarchical.h:154
dolfin::Hierarchical::child_shared_ptr
std::shared_ptr< const T > child_shared_ptr() const
Return shared pointer to child (const version).
Definition Hierarchical.h:158
dolfin::Hierarchical::root_node_shared_ptr
std::shared_ptr< T > root_node_shared_ptr()
Definition Hierarchical.h:182
dolfin::Hierarchical::parent
const T & parent() const
Return parent in hierarchy (const version).
Definition Hierarchical.h:101
dolfin::Hierarchical::leaf_node
T & leaf_node()
Definition Hierarchical.h:202
dolfin::Hierarchical::set_parent
void set_parent(std::shared_ptr< T > parent)
Set parent.
Definition Hierarchical.h:234
dolfin::Hierarchical::leaf_node_shared_ptr
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
dolfin::Hierarchical::child
T & child()
Definition Hierarchical.h:129
dolfin::Hierarchical::parent
T & parent()
Definition Hierarchical.h:91
dolfin::Hierarchical::child
const T & child() const
Return child in hierarchy (const version).
Definition Hierarchical.h:139
dolfin
Definition adapt.h:30
dolfin::dolfin_error
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition log.cpp:129
dolfin::cout
LogStream cout
dolfin::cout
dolfin::endl
LogStream endl
dolfin::endl;
dolfin::info
void info(std::string msg,...)
Print message.
Definition log.cpp:72
dolfin::reference_to_no_delete_pointer
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