BioDynaMo  v1.05.159-3be850bb
functor.h
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #ifndef CORE_FUNCTOR_H_
16 #define CORE_FUNCTOR_H_
17 
18 #include <utility>
19 
20 namespace bdm {
21 
22 // -----------------------------------------------------------------------------
23 template <typename TReturn, typename... TArgs>
24 class Functor {
25  public:
26  virtual ~Functor() = default;
27  virtual TReturn operator()(TArgs... args) = 0;
28 };
29 
30 // -----------------------------------------------------------------------------
36 template <typename TLambda>
37 struct LambdaFunctor {};
38 
40 template <typename TLambda, typename TReturn, typename... TArgs>
41 struct LambdaFunctor<TReturn (TLambda::*)(TArgs...) const> final
42  : public Functor<TReturn, TArgs...> {
43  TLambda lambda;
44 
45  LambdaFunctor(const TLambda& lambda) : lambda(lambda) {}
46  LambdaFunctor(const LambdaFunctor& other) : lambda(other.lambda) {}
47  virtual ~LambdaFunctor() = default;
48 
49  TReturn operator()(TArgs... args) override {
50  return lambda(std::forward<TArgs>(args)...);
51  }
52 };
53 
55 template <typename TLambda, typename TReturn, typename... TArgs>
56 struct LambdaFunctor<TReturn (TLambda::*)(TArgs...)> final
57  : public Functor<TReturn, TArgs...> {
58  TLambda lambda;
59 
60  LambdaFunctor(const TLambda& lambda) : lambda(lambda) {}
61  LambdaFunctor(const LambdaFunctor& other) : lambda(other.lambda) {}
62  virtual ~LambdaFunctor() = default;
63 
64  TReturn operator()(TArgs... args) override {
65  return lambda(std::forward<TArgs>(args)...);
66  }
67 };
68 
69 // -----------------------------------------------------------------------------
109 template <typename TLambda>
110 LambdaFunctor<decltype(&TLambda::operator())> L2F(const TLambda& l) {
112 }
113 
114 } // namespace bdm
115 
116 #endif // CORE_FUNCTOR_H_
virtual TReturn operator()(TArgs... args)=0
virtual ~Functor()=default
Definition: agent.cc:39
LambdaFunctor< decltype(&TLambda::operator())> L2F(const TLambda &l)
Definition: functor.h:110
TReturn operator()(TArgs... args) override
Definition: functor.h:49
LambdaFunctor(const LambdaFunctor &other)
Definition: functor.h:61
TReturn operator()(TArgs... args) override
Definition: functor.h:64