Motorcortex Core  version: 2.7.6
utl_fncwrapper.h
1 /*
2  * Developer : Alexey Zakharov (alexey.zakharov@vectioneer.com)
3  * All rights reserved. Copyright (c) 2020 VECTIONEER.
4  */
5 
6 #ifndef MOTORCORTEX_CORE_UTL_FNCWRAPPER_H
7 #define MOTORCORTEX_CORE_UTL_FNCWRAPPER_H
8 
9 #include <memory>
10 
12  struct ImplBase {
13  virtual void call() = 0;
14 
15  virtual ~ImplBase() {}
16  };
17 
18  std::unique_ptr<ImplBase> impl;
19 
20  template <typename F>
21  struct ImplType : ImplBase {
22  F f;
23 
24  ImplType(F&& f_) : f(std::move(f_)) {}
25 
26  void call() { f(); }
27  };
28 
29 public:
30  template <typename F>
31  FunctionWrapper(F&& f) : impl(new ImplType<F>(std::move(f))) {}
32 
33  void operator()() { impl->call(); }
34 
35  FunctionWrapper() = default;
36 
37  FunctionWrapper(FunctionWrapper&& other) : impl(std::move(other.impl)) {}
38 
39  FunctionWrapper& operator=(FunctionWrapper&& other) {
40  impl = std::move(other.impl);
41  return *this;
42  }
43 
44  FunctionWrapper(const FunctionWrapper&) = delete;
45 
47 
48  FunctionWrapper& operator=(const FunctionWrapper&) = delete;
49 };
50 
51 #endif // MOTORCORTEX_CORE_UTL_FNCWRAPPER_H
FunctionWrapper
Definition: utl_fncwrapper.h:11