QuantumLibrary
quantum_capture.h
1 /*
2 ** Copyright 2018 Bloomberg Finance L.P.
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16 #ifndef BLOOMBERG_QUANTUM_CAPTURE_H
17 #define BLOOMBERG_QUANTUM_CAPTURE_H
18 
19 #include <quantum/impl/quantum_stl_impl.h>
20 #include <quantum/quantum_traits.h>
21 #include <quantum/quantum_allocator_traits.h>
22 #include <type_traits>
23 #include <assert.h>
24 #include <utility>
25 #include <tuple>
26 #include <array>
27 
28 namespace Bloomberg {
29 namespace quantum {
30 
31 //==============================================================================================
32 // class Capture
33 //==============================================================================================
37 template <typename FUNC, typename ... ARGS>
38 class Capture
39 {
40 public:
41  Capture(FUNC&& func, ARGS&&...args);
42 
43  template <typename ... T>
44  int operator()(T&&...t);
45 private:
46  FUNC _func;
47  std::tuple<ARGS...> _args;
48 };
49 
50 //Helper function
51 template <typename FUNC, typename ... ARGS>
52 Capture<FUNC, ARGS...>
53 makeCapture(FUNC&& func, ARGS&& ... args);
54 
55 //==============================================================================================
56 // class Function
57 //==============================================================================================
61 template <typename RET, typename ... ARGS>
62 class Function;
63 
64 template <typename RET, typename ... ARGS>
65 class Function<RET(ARGS...)>
66 {
67  static constexpr size_t size{__QUANTUM_FUNCTION_ALLOC_SIZE};
68  using Func = RET(*)(ARGS...);
69  using Callback = RET(*)(void*, ARGS...);
70  using Deleter = void(*)(void*);
71 
72 public:
73  // Ctors
74  Function(RET(*ptr)(ARGS...)); //construct with function pointer
75  template <typename FUNCTOR>
76  Function(FUNCTOR&& functor); //construct with functor
77  Function(const Function<RET(ARGS...)>& other);
78  Function(Function<RET(ARGS...)>&& other);
79  Function& operator=(const Function<RET(ARGS...)>& other);
80  Function& operator=(Function<RET(ARGS...)>&& other);
81  ~Function();
82 
83  // Methods
84  RET operator()(ARGS...args);
85  explicit operator bool() const;
86 
87 private:
88  static void dummyDeleter(void*) {}
89  static void deleter(void* p) { delete[] static_cast<char*>(p); }
90 
91  template <typename FUNCTOR>
92  void initFunctor(FUNCTOR&& functor, std::true_type);
93 
94  template <typename FUNCTOR>
95  void initFunctor(FUNCTOR&& functor, std::false_type);
96 
97  std::array<char, size> _storage;
98  void* _callable;
99  Callback _callback;
100  Deleter _deleter;
101 };
102 
103 }}
104 
105 #include <quantum/impl/quantum_capture_impl.h>
106 
107 #endif //BLOOMBERG_QUANTUM_CAPTURE_H
Definition: quantum_buffer_impl.h:22
Capture(FUNC &&func, ARGS &&...args)
Definition: quantum_capture_impl.h:29
int operator()(T &&...t)
Definition: quantum_capture_impl.h:36
Similar implementation to std::function except that it allows capture of non-copyable types.
Definition: quantum_capture.h:62
Capture< FUNC, ARGS... > makeCapture(FUNC &&func, ARGS &&... args)
Definition: quantum_capture_impl.h:42
Class allowing lambda parameter captures.
Definition: quantum_capture.h:38