OpenLB 1.7
Loading...
Searching...
No Matches
functorPtr.hh
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2017 Adrian Kummerlaender
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
24#ifndef FUNCTOR_PTR_HH
25#define FUNCTOR_PTR_HH
26
27#include "functorPtr.h"
28
29#include "io/ostreamManager.h"
30
31namespace olb {
32
33template<typename F>
35 _ownF(),
36 _sharedF(),
37 _f(&f),
38 _owning(false)
39{ }
40
41template<typename F>
43 _ownF(),
44 _sharedF(),
45 _f(f),
46 _owning(false)
47{ }
48
49template<typename F>
50FunctorPtr<F>::FunctorPtr(const std::unique_ptr<F>& f):
51 FunctorPtr(f.get())
52{ }
53
54template<typename F>
55FunctorPtr<F>::FunctorPtr(std::unique_ptr<F>&& f):
56 _ownF(std::move(f)),
57 _f(_ownF.get()),
58 _owning(true)
59{ }
60
61template<typename F>
62FunctorPtr<F>::FunctorPtr(std::shared_ptr<F> f):
63 _sharedF(std::move(f)),
64 _f(_sharedF.get()),
65 _owning(true)
66{ }
67
68template<typename F>
72
73template<typename F>
74typename std::add_lvalue_reference<F>::type FunctorPtr<F>::operator*() const
76 return *_f;
77}
78
79template<typename F>
80typename std::add_pointer<F>::type FunctorPtr<F>::operator->() const noexcept
81{
82 return _f;
83}
85template<typename F>
87{
88 return _f != nullptr;
90
91template<typename F>
93{
94 return _owning;
95}
96
97template<typename F>
98template<typename G>
99auto FunctorPtr<F>::toShared() -> typename std::enable_if< util::has_identity_functor<G>::value, std::shared_ptr<F>>::type
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;
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}
123
124template<typename F>
125template<typename G>
126auto FunctorPtr<F>::toShared() -> typename std::enable_if<!util::has_identity_functor<G>::value, std::shared_ptr<F>>::type
127{
128 if ( _owning && _ownF ) {
129 // convert unique_ptr to shared_ptr
130 _sharedF = std::shared_ptr<F>(std::move(_ownF));
131 _f = _sharedF.get();
132 return _sharedF;
133 }
134 else if ( _sharedF ) {
135 return _sharedF;
136 }
137 else {
138 OstreamManager clerr(std::cerr,"FunctorPtr");
139 clerr << "ERROR: No identity functor defined. Required to lift functor reference into std::shared_ptr arithmetic." << std::endl;
140 std::exit(-1);
141 }
142}
143
144}
145
146#endif
Smart pointer for managing the various ways of passing functors around.
Definition functorPtr.h:60
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.
Definition functorPtr.hh:99
std::add_pointer< F >::type operator->() const noexcept
Enable pointer-like access to the exposed functor's members.
Definition functorPtr.hh:80
FunctorPtr()
Constructor for an empty instance.
Definition functorPtr.hh:69
bool isOwning() const
Indicates whether a functor instance is both exposed and owned.
Definition functorPtr.hh:92
std::add_lvalue_reference< F >::type operator*() const
Definition functorPtr.hh:74
Top level namespace for all of OpenLB.