OpenLB 1.7
Loading...
Searching...
No Matches
Public Member Functions | List of all members
olb::FunctorPtr< F > Class Template Reference

Smart pointer for managing the various ways of passing functors around. More...

#include <functorPtr.h>

+ Inheritance diagram for olb::FunctorPtr< F >:
+ Collaboration diagram for olb::FunctorPtr< F >:

Public Member Functions

 FunctorPtr (F &f)
 Constructor for transparently accepting a functor reference.
 
 FunctorPtr (F *f)
 Constructor for transparently accepting a non-owning functor pointer.
 
 FunctorPtr (const std::unique_ptr< F > &f)
 Constructor for transparently accepting a owning functor pointer.
 
 FunctorPtr (std::unique_ptr< F > &&f)
 Constructor for transparently taking ownership of a owning functor pointer.
 
 FunctorPtr (std::shared_ptr< F > f)
 Constructor for transparently sharing ownership of a functor.
 
 FunctorPtr ()
 Constructor for an empty instance.
 
 FunctorPtr (FunctorPtr &)=delete
 Copy construction is disabled as it is incompatible with unique ownership.
 
 FunctorPtr (FunctorPtr &&)=default
 Move constructor.
 
template<typename... Args>
bool operator() (Args... args)
 Perfect forwarding functor operator.
 
std::add_lvalue_reference< F >::type operator* () const
 
std::add_pointer< F >::type operator-> () const noexcept
 Enable pointer-like access to the exposed functor's members.
 
 operator bool () const
 Indicates whether a functor instance is exposed.
 
bool isOwning () const
 Indicates whether a functor instance is both exposed and owned.
 
template<typename G = F>
auto toShared () -> typename std::enable_if< util::has_identity_functor< G >::value, std::shared_ptr< F > >::type
 Lifts managed functor reference for usage in std::shared_ptr<F> arithmetic.
 
template<typename G = F>
auto toShared () -> typename std::enable_if<!util::has_identity_functor< G >::value, std::shared_ptr< F > >::type
 Lifts managed functor reference for usage in std::shared_ptr<F> arithmetic (limited)
 

Detailed Description

template<typename F>
class olb::FunctorPtr< F >

Smart pointer for managing the various ways of passing functors around.

There is a rich set of functors that compose other functors by accepting them as constructor arguments. e.g. SuperLpNorm3D, SuperPlaneIntegralF3D

Previously all of these functors only accepted other functors by reference which prevented e.g. the use case of accepting a std::unique_ptr to a indicator freshly created by SuperGeometry3D::getMaterialIndicator.

This class hides the memory management required to support accepting either functor references, non-owning pointers or owning pointers in a single argument.

Note that if this class is constructed by reference or raw pointer the caller is responsible for assuring that the passed functor is available for the full lifetime. This is not the case when constructed by a smart pointer.

Definition at line 60 of file functorPtr.h.

Constructor & Destructor Documentation

◆ FunctorPtr() [1/8]

template<typename F >
olb::FunctorPtr< F >::FunctorPtr ( F & f)

Constructor for transparently accepting a functor reference.

Definition at line 34 of file functorPtr.hh.

34 :
35 _ownF(),
36 _sharedF(),
37 _f(&f),
38 _owning(false)
39{ }

◆ FunctorPtr() [2/8]

template<typename F >
olb::FunctorPtr< F >::FunctorPtr ( F * f)

Constructor for transparently accepting a non-owning functor pointer.

Definition at line 42 of file functorPtr.hh.

42 :
43 _ownF(),
44 _sharedF(),
45 _f(f),
46 _owning(false)
47{ }

◆ FunctorPtr() [3/8]

template<typename F >
olb::FunctorPtr< F >::FunctorPtr ( const std::unique_ptr< F > & f)

Constructor for transparently accepting a owning functor pointer.

Equivalent to construction using a non-owning functor pointer

Definition at line 50 of file functorPtr.hh.

50 :
51 FunctorPtr(f.get())
52{ }
FunctorPtr()
Constructor for an empty instance.
Definition functorPtr.hh:69

◆ FunctorPtr() [4/8]

template<typename F >
olb::FunctorPtr< F >::FunctorPtr ( std::unique_ptr< F > && f)

Constructor for transparently taking ownership of a owning functor pointer.

Definition at line 55 of file functorPtr.hh.

55 :
56 _ownF(std::move(f)),
57 _f(_ownF.get()),
58 _owning(true)
59{ }

◆ FunctorPtr() [5/8]

template<typename F >
olb::FunctorPtr< F >::FunctorPtr ( std::shared_ptr< F > f)

Constructor for transparently sharing ownership of a functor.

Definition at line 62 of file functorPtr.hh.

62 :
63 _sharedF(std::move(f)),
64 _f(_sharedF.get()),
65 _owning(true)
66{ }

◆ FunctorPtr() [6/8]

template<typename F >
olb::FunctorPtr< F >::FunctorPtr ( )

Constructor for an empty instance.

Equivalent to construction by nullptr.

Definition at line 69 of file functorPtr.hh.

69 :
70 FunctorPtr(nullptr)
71{ }

◆ FunctorPtr() [7/8]

template<typename F >
olb::FunctorPtr< F >::FunctorPtr ( FunctorPtr< F > & )
delete

Copy construction is disabled as it is incompatible with unique ownership.

◆ FunctorPtr() [8/8]

template<typename F >
olb::FunctorPtr< F >::FunctorPtr ( FunctorPtr< F > && )
default

Move constructor.

Member Function Documentation

◆ isOwning()

template<typename F >
bool olb::FunctorPtr< F >::isOwning ( ) const

Indicates whether a functor instance is both exposed and owned.

Returns
true iff FunctorPtr took ownership of managed functor during construction

It is not enough to check _ownF and _sharedF for non-nullptr contents as _sharedF is also used to store F::identity_functor_type instances created by toShared.

Definition at line 92 of file functorPtr.hh.

93{
94 return _owning;
95}

◆ operator bool()

template<typename F >
olb::FunctorPtr< F >::operator bool ( ) const

Indicates whether a functor instance is exposed.

Returns
true iff a functor is exposed

This is useful for supporting optional functors in constructor interfaces by constructing FunctorPtr using a nullptr

Definition at line 86 of file functorPtr.hh.

87{
88 return _f != nullptr;
89}

◆ operator()()

template<typename F >
template<typename... Args>
bool olb::FunctorPtr< F >::operator() ( Args... args)
inline

Perfect forwarding functor operator.

Definition at line 98 of file functorPtr.h.

99 {
100 return _f->operator()(std::forward<Args>(args)...);
101 }

◆ operator*()

template<typename F >
std::add_lvalue_reference< F >::type olb::FunctorPtr< F >::operator* ( ) const
Returns
reference to the exposed functor

Definition at line 74 of file functorPtr.hh.

75{
76 return *_f;
77}

◆ operator->()

template<typename F >
std::add_pointer< F >::type olb::FunctorPtr< F >::operator-> ( ) const
noexcept

Enable pointer-like access to the exposed functor's members.

Definition at line 80 of file functorPtr.hh.

81{
82 return _f;
83}

◆ toShared() [1/2]

template<typename F >
template<typename G >
auto olb::FunctorPtr< F >::toShared ( ) -> typename std::enable_if< util::has_identity_functor<G>::value, std::shared_ptr<F>>::type

Lifts managed functor reference for usage in std::shared_ptr<F> arithmetic.

Definition at line 99 of file functorPtr.hh.

100{
101 if ( _owning && _ownF ) {
102 // convert unique_ptr to shared_ptr
103 _sharedF = std::shared_ptr<F>(std::move(_ownF));
104 _f = _sharedF.get();
105 return _sharedF;
106 }
107 else if ( _sharedF ) {
108 // either we are owning or toShared was called previously and
109 // prepared a F::identity_functor_type we can reuse here:
110 return _sharedF;
111 }
112 else {
113#ifdef OLB_DEBUG_UNSAFE_FUNCTOR_ARITHMETIC
114 OstreamManager clerr(std::cerr,"FunctorPtr");
115 clerr << "WARNING: Non-owning functor wrapped in std::shared_ptr. Lifetime of composed functor is not guaranteed." << std::endl;
116#endif
117
118 // wrap non-owning pointer in identity functor
119 _sharedF = std::shared_ptr<F>(new typename F::identity_functor_type(*_f));
120 return _sharedF;
121 }
122}

◆ toShared() [2/2]

template<typename F >
template<typename G = F>
auto olb::FunctorPtr< F >::toShared ( ) -> typename std::enable_if<!util::has_identity_functor< G >::value, std::shared_ptr< F > >::type

Lifts managed functor reference for usage in std::shared_ptr<F> arithmetic (limited)


The documentation for this class was generated from the following files: