3434#include < stddef.h>
3535#include < new>
3636#include < utility>
37- #include < type_traits>
3837#include " core-util/atomic_ops.h"
3938
4039namespace functional {
@@ -75,16 +74,31 @@ class Function <ReturnType(ArgTypes...)> {
7574 * Since ```Function``` only contains a single pointer, only a null assignment is necessary.
7675 */
7776 Function () : ref(nullptr ) {}
77+ /* *
78+ * Construct a Function from a FunctionInterface.
79+ *
80+ * This is an API that should only be used by Function and its helpers. It was specifically added to enable
81+ * bind_first and bind_last.
82+ *
83+ * @param[in] f
84+ */
7885 Function (detail::FunctionInterface<ReturnType(ArgTypes...)> *f) {
7986 ref = f;
8087 if (ref) {
8188 ref->inc ();
8289 }
8390 }
91+ /* *
92+ * Move constructor
93+ * This constructor steals the reference from the rvalue-reference Function
94+ * without incrementing the reference count.
95+ */
96+ Function (Function &&f): ref(f.ref) {}
8497
8598 Function (ReturnType (*f)(ArgTypes...)) {
8699 typedef detail::StaticContainer<ReturnType (ArgTypes...)> staticFP;
87100 staticFP * newf = reinterpret_cast <staticFP *>(detail::StaticFPAllocator.alloc ());
101+ CORE_UTIL_ASSERT_MSG (newf, " Function container memory allocation failed" );
88102 new (newf) staticFP (f);
89103 ref = newf;
90104 ref->inc ();
@@ -97,6 +111,7 @@ class Function <ReturnType(ArgTypes...)> {
97111 Function (C *o, ReturnType (C::*fp)(ArgTypes...)) {
98112 typedef detail::MemberContainer<C, ReturnType (ArgTypes...)> memberFP;
99113 memberFP * newf = reinterpret_cast <memberFP *>(detail::MemberFPAllocator.alloc ());
114+ CORE_UTIL_ASSERT_MSG (newf, " Function container memory allocation failed" );
100115 new (newf) memberFP (o, fp);
101116 ref = newf;
102117 ref->inc ();
@@ -113,6 +128,7 @@ class Function <ReturnType(ArgTypes...)> {
113128 Function (const F &f) {
114129 typedef detail::FunctorContainer<F, ReturnType (ArgTypes...), detail::FunctorFPAllocator> FunctorFP;
115130 FunctorFP * newf = reinterpret_cast <FunctorFP *>(detail::FunctorFPAllocator.alloc ());
131+ CORE_UTIL_ASSERT_MSG (newf, " Function container memory allocation failed" );
116132 new (newf) FunctorFP (f);
117133 ref = newf;
118134 ref->inc ();
@@ -133,6 +149,7 @@ class Function <ReturnType(ArgTypes...)> {
133149 typedef typename detail::CaptureFirst<ReturnType (ArgTypes...), detail::FunctorFPAllocator, CapturedTypes...> CaptureFP;
134150 static_assert (sizeof (CaptureFP) <= FUNCTOR_SIZE, " Size of bound arguments is too large" );
135151 CaptureFP * newf = reinterpret_cast <CaptureFP *>(detail::FunctorFPAllocator.alloc ());
152+ CORE_UTIL_ASSERT_MSG (newf, " Function container memory allocation failed" );
136153 new (newf) CaptureFP (t, f);
137154 ref = newf;
138155 ref->inc ();
0 commit comments