Loading...
Searching...
No Matches
proxynode.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-GPL-2.0-only-with-PDELab-exception
#ifndef DUNE_TYPETREE_PROXYNODE_HH
#define DUNE_TYPETREE_PROXYNODE_HH
#include <type_traits>
#include <dune/typetree/nodeinterface.hh>
#include <dune/typetree/nodetags.hh>
#include <dune/common/shared_ptr.hh>
#include <dune/common/indices.hh>
#include <dune/common/std/type_traits.hh>
namespace Dune {
namespace TypeTree {
template<typename Node>
class ProxyNode;
template<typename ProxiedNode>
class StaticChildAccessors
{
static const bool proxiedNodeIsConst = std::is_const<typename std::remove_reference<ProxiedNode>::type>::value;
template<std::size_t k>
struct lazy_enabled
{
static const bool value = !proxiedNodeIsConst;
};
typedef ProxyNode<ProxiedNode> Node;
template<bool enabled = !proxiedNodeIsConst>
typename std::enable_if<enabled,Node&>::type
node ()
{
return static_cast<Node&>(*this);
}
const Node& node () const
{
return static_cast<const Node&>(*this);
}
public:
template<std::size_t k>
struct Child
: public ProxiedNode::template Child<k>
{};
template<std::size_t k,
typename std::enable_if<lazy_enabled<k>::value, int>::type = 0>
auto& child (index_constant<k> = {})
{
return node().proxiedNode().template child<k>();
}
template<std::size_t k>
const auto& child (index_constant<k> = {}) const
{
return node().proxiedNode().template child<k>();
}
template<std::size_t k,
typename std::enable_if<lazy_enabled<k>::value, int>::type = 0>
auto childStorage (index_constant<k> = {})
{
return node().proxiedNode().template childStorage<k>();
}
template<std::size_t k>
auto childStorage (index_constant<k> = {}) const
{
return node().proxiedNode().template childStorage<k>();
}
template<std::size_t k, class ProxyChild>
void setChild (ProxyChild&& child, typename std::enable_if<lazy_enabled<k>::value,void*>::type = 0)
{
node().proxiedNode().template setChild<k>(std::forward<ProxyChild>(child));
}
const typename ProxiedNode::NodeStorage& nodeStorage () const
{
return node().proxiedNode().nodeStorage();
}
};
template<typename ProxiedNode>
class DynamicChildAccessors
: public StaticChildAccessors<ProxiedNode>
{
typedef ProxyNode<ProxiedNode> Node;
static const bool proxiedNodeIsConst = std::is_const<typename std::remove_reference<ProxiedNode>::type>::value;
template<bool enabled = !proxiedNodeIsConst>
typename std::enable_if<enabled,Node&>::type
node ()
{
return static_cast<Node&>(*this);
}
const Node& node () const
{
return static_cast<const Node&>(*this);
}
public:
template<bool enabled = !proxiedNodeIsConst,
typename std::enable_if<enabled, int>::type = 0>
auto& child (std::size_t i)
{
return node().proxiedNode().child(i);
}
const auto& child (std::size_t i) const
{
return node().proxiedNode().child(i);
}
template<bool enabled = !proxiedNodeIsConst,
typename std::enable_if<enabled, int>::type = 0>
auto childStorage (std::size_t i)
{
return node().proxiedNode().childStorage(i);
}
auto childStorage (std::size_t i) const
{
return node().proxiedNode().childStorage(i);
}
template<class ProxyChild, bool enabled = !proxiedNodeIsConst>
void setChild (std::size_t i, ProxyChild&& child, typename std::enable_if<enabled,void*>::type = 0)
{
node().proxiedNode().setChild(i, std::forward<ProxyChild>(child));
}
};
template<typename Node, typename NodeTag>
struct ProxyNodeBase;
template<typename Node>
struct ProxyNodeBase<Node,LeafNodeTag>
{
};
template<typename Node>
struct ProxyNodeBase<Node,CompositeNodeTag>
: public StaticChildAccessors<Node>
{
typedef typename Node::ChildTypes ChildTypes;
typedef typename Node::NodeStorage NodeStorage;
};
template<typename Node>
struct ProxyNodeBase<Node,PowerNodeTag>
: public DynamicChildAccessors<Node>
{
typedef typename Node::ChildType ChildType;
typedef typename Node::NodeStorage NodeStorage;
};
template<typename Node>
struct ProxyNodeBase<Node,DynamicPowerNodeTag>
: public DynamicChildAccessors<Node>
{
typedef typename Node::ChildType ChildType;
typedef typename Node::NodeStorage NodeStorage;
};
template<typename Node>
class ProxyNode
: public ProxyNodeBase<Node,NodeTag<Node>>
{
static const bool proxiedNodeIsConst = std::is_const<typename std::remove_reference<Node>::type>::value;
template <class N>
using HasStaticDegree = index_constant<N::degree()>;
template <class N>
static constexpr bool hasStaticDegree = Std::is_detected<HasStaticDegree, N>::value;
// accessor mixins need to be friends for access to proxiedNode()
friend class StaticChildAccessors<Node>;
friend class DynamicChildAccessors<Node>;
public:
typedef Node ProxiedNode;
typedef Dune::TypeTree::NodeTag<Node> NodeTag;
static const bool isLeaf = Node::isLeaf;
static const bool isPower = Node::isPower;
static const bool isComposite = Node::isComposite;
template <class N = Node,
std::enable_if_t<hasStaticDegree<N>, int> = 0>
static constexpr auto degree ()
{
return N::degree();
}
template <class N = Node,
std::enable_if_t<not hasStaticDegree<N>, int> = 0>
auto degree () const
{
return proxiedNode().degree();
}
protected:
template<bool enabled = !proxiedNodeIsConst>
typename std::enable_if<enabled,Node&>::type
proxiedNode ()
{
return *_node;
}
const Node& proxiedNode () const
{
return *_node;
}
template<bool enabled = !proxiedNodeIsConst>
typename std::enable_if<enabled,std::shared_ptr<Node> >::type
proxiedNodeStorage ()
{
return _node;
}
std::shared_ptr<const Node> proxiedNodeStorage () const
{
return _node;
}
ProxyNode (Node& node)
: _node(stackobject_to_shared_ptr(node))
{}
ProxyNode (std::shared_ptr<Node> node)
: _node(std::move(node))
{}
private:
std::shared_ptr<Node> _node;
};
} // namespace TypeTree
} //namespace Dune
#endif // DUNE_TYPETREE_PROXYNODE_HH
nodetags.hh
nodeinterface.hh
Dune::TypeTree::NodeTag
typename std::decay_t< Node >::NodeTag NodeTag
Returns the node tag of the given Node.
Definition nodeinterface.hh:70
Dune
Definition accumulate_static.hh:17
Dune::TypeTree
Definition accumulate_static.hh:18
Dune::TypeTree::LeafNodeTag
Tag designating a leaf node.
Definition nodetags.hh:18
Dune::TypeTree::PowerNodeTag
Tag designating a power node.
Definition nodetags.hh:21
Dune::TypeTree::DynamicPowerNodeTag
Tag designating a power node with runtime degree.
Definition nodetags.hh:24
Dune::TypeTree::CompositeNodeTag
Tag designating a composite node.
Definition nodetags.hh:27
Dune::TypeTree::ProxyNode
Base class for nodes acting as a proxy for an existing node.
Definition proxynode.hh:255
Dune::TypeTree::ProxyNode::ProxyNode
ProxyNode(Node &node)
Definition proxynode.hh:336
Dune::TypeTree::ProxyNode< ProxiedNode >::NodeTag
Dune::TypeTree::NodeTag< ProxiedNode > NodeTag
Definition proxynode.hh:272
Dune::TypeTree::ProxyNode< ProxiedNode >::isComposite
static const bool isComposite
Definition proxynode.hh:281
Dune::TypeTree::ProxyNode< ProxiedNode >::isLeaf
static const bool isLeaf
Definition proxynode.hh:275
Dune::TypeTree::ProxyNode< ProxiedNode >::isPower
static const bool isPower
Definition proxynode.hh:278
Dune::TypeTree::ProxyNode::degree
auto degree() const
Definition proxynode.hh:292
Dune::TypeTree::ProxyNode::proxiedNode
std::enable_if< enabled, Node & >::type proxiedNode()
Returns the proxied node.
Definition proxynode.hh:306
Dune::TypeTree::ProxyNode::proxiedNodeStorage
std::shared_ptr< const Node > proxiedNodeStorage() const
Returns the storage of the proxied node (const version).
Definition proxynode.hh:326
Dune::TypeTree::ProxyNode< ProxiedNode >::ProxiedNode
ProxiedNode ProxiedNode
Definition proxynode.hh:270
Dune::TypeTree::ProxyNode::proxiedNodeStorage
std::enable_if< enabled, std::shared_ptr< Node > >::type proxiedNodeStorage()
Returns the storage of the proxied node.
Definition proxynode.hh:320
Dune::TypeTree::ProxyNode::degree
static constexpr auto degree()
Definition proxynode.hh:285
Dune::TypeTree::ProxyNode::ProxyNode
ProxyNode(std::shared_ptr< Node > node)
Definition proxynode.hh:340
Dune::TypeTree::ProxyNode::proxiedNode
const Node & proxiedNode() const
Returns the proxied node (const version).
Definition proxynode.hh:312
Dune::TypeTree::StaticChildAccessors
Mixin class providing methods for child access with compile-time parameter.
Definition proxynode.hh:30
Dune::TypeTree::StaticChildAccessors::child
auto & child(index_constant< k >={})
Returns the i-th child.
Definition proxynode.hh:71
Dune::TypeTree::StaticChildAccessors::setChild
void setChild(ProxyChild &&child, typename std::enable_if< lazy_enabled< k >::value, void * >::type=0)
Sets the i-th child to the passed-in value.
Definition proxynode.hh:112
Dune::TypeTree::StaticChildAccessors::nodeStorage
const ProxiedNode::NodeStorage & nodeStorage() const
Definition proxynode.hh:117
Dune::TypeTree::StaticChildAccessors::childStorage
auto childStorage(index_constant< k >={})
Returns the storage of the i-th child.
Definition proxynode.hh:92
Dune::TypeTree::StaticChildAccessors::child
const auto & child(index_constant< k >={}) const
Returns the i-th child (const version).
Definition proxynode.hh:81
Dune::TypeTree::StaticChildAccessors::childStorage
auto childStorage(index_constant< k >={}) const
Returns the storage of the i-th child (const version).
Definition proxynode.hh:105
Dune::TypeTree::StaticChildAccessors::Child
Access to the type and storage type of the i-th child.
Definition proxynode.hh:60
Dune::TypeTree::DynamicChildAccessors
Mixin class providing methods for child access with run-time parameter.
Definition proxynode.hh:133
Dune::TypeTree::DynamicChildAccessors::child
auto & child(std::size_t i)
Returns the i-th child.
Definition proxynode.hh:162
Dune::TypeTree::DynamicChildAccessors::childStorage
auto childStorage(std::size_t i) const
Returns the storage of the i-th child (const version).
Definition proxynode.hh:194
Dune::TypeTree::DynamicChildAccessors::setChild
void setChild(std::size_t i, ProxyChild &&child, typename std::enable_if< enabled, void * >::type=0)
Sets the i-th child to the passed-in value.
Definition proxynode.hh:201
Dune::TypeTree::DynamicChildAccessors::child
const auto & child(std::size_t i) const
Returns the i-th child (const version).
Definition proxynode.hh:171
Dune::TypeTree::DynamicChildAccessors::childStorage
auto childStorage(std::size_t i)
Returns the storage of the i-th child.
Definition proxynode.hh:182
Dune::TypeTree::ProxyNodeBase
Tag-based dispatch to appropriate base class that provides necessary functionality.
Definition proxynode.hh:210
Dune::TypeTree::ProxyNodeBase< Node, CompositeNodeTag >::NodeStorage
Node::NodeStorage NodeStorage
Definition proxynode.hh:224
Dune::TypeTree::ProxyNodeBase< Node, CompositeNodeTag >::ChildTypes
Node::ChildTypes ChildTypes
Definition proxynode.hh:223
Dune::TypeTree::ProxyNodeBase< Node, PowerNodeTag >::NodeStorage
Node::NodeStorage NodeStorage
Definition proxynode.hh:233
Dune::TypeTree::ProxyNodeBase< Node, PowerNodeTag >::ChildType
Node::ChildType ChildType
Definition proxynode.hh:232
Dune::TypeTree::ProxyNodeBase< Node, DynamicPowerNodeTag >::NodeStorage
Node::NodeStorage NodeStorage
Definition proxynode.hh:242
Dune::TypeTree::ProxyNodeBase< Node, DynamicPowerNodeTag >::ChildType
Node::ChildType ChildType
Definition proxynode.hh:241