BioDynaMo  v1.05.120-25dc9790
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_
bdm::Functor::~Functor
virtual ~Functor()=default
bdm::LambdaFunctor< TReturn(TLambda::*)(TArgs...) const >::LambdaFunctor
LambdaFunctor(const TLambda &lambda)
Definition: functor.h:45
bdm
Definition: agent.cc:39
bdm::LambdaFunctor< TReturn(TLambda::*)(TArgs...)>::lambda
TLambda lambda
Definition: functor.h:58
bdm::LambdaFunctor
Definition: functor.h:37
bdm::L2F
LambdaFunctor< decltype(&TLambda::operator())> L2F(const TLambda &l)
Definition: functor.h:110
bdm::Functor
Definition: functor.h:24
bdm::Functor::operator()
virtual TReturn operator()(TArgs... args)=0
bdm::LambdaFunctor< TReturn(TLambda::*)(TArgs...)>::LambdaFunctor
LambdaFunctor(const TLambda &lambda)
Definition: functor.h:60
bdm::LambdaFunctor< TReturn(TLambda::*)(TArgs...)>::operator()
TReturn operator()(TArgs... args) override
Definition: functor.h:64
bdm::LambdaFunctor< TReturn(TLambda::*)(TArgs...) const >::lambda
TLambda lambda
Definition: functor.h:43
bdm::LambdaFunctor< TReturn(TLambda::*)(TArgs...) const >::operator()
TReturn operator()(TArgs... args) override
Definition: functor.h:49
bdm::LambdaFunctor< TReturn(TLambda::*)(TArgs...)>::LambdaFunctor
LambdaFunctor(const LambdaFunctor &other)
Definition: functor.h:61
bdm::LambdaFunctor< TReturn(TLambda::*)(TArgs...) const >::LambdaFunctor
LambdaFunctor(const LambdaFunctor &other)
Definition: functor.h:46