CppCommon  1.0.4.1
C++ Common Library
function.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_FUNCTION_H
10 #define CPPCOMMON_FUNCTION_H
11 
12 #include <functional>
13 #include <memory>
14 
15 namespace CppCommon {
16 
18 template <class, size_t Capacity = 1024>
19 class Function;
20 
22 
29 template <class R, class... Args, size_t Capacity>
30 class Function<R(Args...), Capacity>
31 {
32 public:
33  Function() noexcept;
34  Function(std::nullptr_t) noexcept;
35  Function(const Function& function) noexcept;
36  Function(Function&& function) noexcept;
37  template <class TFunction>
38  Function(TFunction&& function) noexcept;
39  ~Function() noexcept;
40 
41  Function& operator=(std::nullptr_t) noexcept;
42  Function& operator=(const Function& function) noexcept;
43  Function& operator=(Function&& function) noexcept;
44  template <typename TFunction>
45  Function& operator=(TFunction&& function) noexcept;
46  template <typename TFunction>
47  Function& operator=(std::reference_wrapper<TFunction> function) noexcept;
48 
50  explicit operator bool() const noexcept { return (_manager != nullptr); }
51 
53  R operator()(Args... args);
54 
56  void swap(Function& function) noexcept;
57  template <class UR, class... UArgs, size_t UCapacity>
58  friend void swap(Function<UR(UArgs...), UCapacity>& function1, Function<UR(UArgs...), UCapacity>& function2) noexcept;
59 
60 private:
61  enum class Operation { Clone, Destroy };
62 
63  using Invoker = R (*)(void*, Args&&...);
64  using Manager = void (*)(void*, void*, Operation);
65 
66  static const size_t StorageSize = Capacity - sizeof(Invoker) - sizeof(Manager);
67  static const size_t StorageAlign = 8;
68 
69  alignas(StorageAlign) std::byte _data[StorageSize];
70  Invoker _invoker;
71  Manager _manager;
72 
73  template <typename TFunction>
74  static R Invoke(void* data, Args&&... args) noexcept;
75 
76  template <typename TFunction>
77  static void Manage(void* dst, void* src, Operation op) noexcept;
78 };
79 
82 } // namespace CppCommon
83 
84 #include "function.inl"
85 
86 #endif // CPPCOMMON_FUNCTION_H
friend void swap(Function< UR(UArgs...), UCapacity > &function1, Function< UR(UArgs...), UCapacity > &function2) noexcept
Function & operator=(TFunction &&function) noexcept
Allocation free function stub.
Definition: function.h:19
Allocation free function inline implementation.
C++ Common project definitions.
Definition: token_bucket.h:15
void swap(FileCache &cache1, FileCache &cache2) noexcept
Definition: filecache.inl:23