OpenLB 1.7
Loading...
Searching...
No Matches
functorPtr.h
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_H
25#define FUNCTOR_PTR_H
26
27#include <memory>
28#include <type_traits>
29
30namespace olb {
31
32namespace util {
33
35template<class F, class U = void>
36struct has_identity_functor : std::false_type { };
38template<class F>
39struct has_identity_functor<F, std::void_t<typename F::identity_functor_type>> : std::true_type { };
40
41}
42
44
59template<typename F>
61private:
63 std::unique_ptr<F> _ownF;
65 std::shared_ptr<F> _sharedF;
67 F* _f;
69 const bool _owning;
70
71public:
73 FunctorPtr(F& f);
75 FunctorPtr(F* f);
77
80 FunctorPtr(const std::unique_ptr<F>& f);
82 FunctorPtr(std::unique_ptr<F>&& f);
84 FunctorPtr(std::shared_ptr<F> f);
86
89 FunctorPtr();
90
94 FunctorPtr(FunctorPtr&&) = default;
95
97 template<typename... Args>
98 bool operator()(Args... args)
99 {
100 return _f->operator()(std::forward<Args>(args)...);
101 }
102
104 typename std::add_lvalue_reference<F>::type operator*() const;
106 typename std::add_pointer<F>::type operator->() const noexcept;
107
109
115 operator bool() const;
116
118
124 bool isOwning() const;
125
127 template<typename G = F>
128 auto toShared() -> typename std::enable_if< util::has_identity_functor<G>::value, std::shared_ptr<F>>::type;
130 template<typename G = F>
131 auto toShared() -> typename std::enable_if<!util::has_identity_functor<G>::value, std::shared_ptr<F>>::type;
132
133};
134
135}
136
137#endif
Smart pointer for managing the various ways of passing functors around.
Definition functorPtr.h:60
bool operator()(Args... args)
Perfect forwarding functor operator.
Definition functorPtr.h:98
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
FunctorPtr(FunctorPtr &&)=default
Move constructor.
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
FunctorPtr(FunctorPtr &)=delete
Copy construction is disabled as it is incompatible with unique ownership.
std::add_lvalue_reference< F >::type operator*() const
Definition functorPtr.hh:74
Top level namespace for all of OpenLB.
Indicates existence of F::identity_functor_type typedef.
Definition functorPtr.h:36