CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
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
15namespace CppCommon {
16
18template <class, size_t Capacity = 1024>
20
22
29template <class R, class... Args, size_t Capacity>
30class Function<R(Args...), Capacity>
31{
32public:
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
60private:
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=(std::reference_wrapper< TFunction > function) noexcept
Function & operator=(TFunction &&function) noexcept
Allocation free function stub.
Definition function.h:19
Allocation free function inline implementation.
C++ Common project definitions.
void swap(FileCache &cache1, FileCache &cache2) noexcept
Definition filecache.inl:23