1 // Boost.Function library
3 // Copyright (C) 2001, 2002 Doug Gregor (gregod@cs.rpi.edu)
5 // Permission to copy, use, sell and distribute this software is granted
6 // provided this copyright notice appears in all copies.
7 // Permission to modify the code and to distribute modified code is granted
8 // provided this copyright notice appears in all copies, and a notice
9 // that the code was modified is included with the copyright notice.
11 // This software is provided "as is" without express or implied warranty,
12 // and with no claim as to its suitability for any purpose.
14 // For more information, see http://www.boost.org
16 #ifndef BOOST_FUNCTION_BASE_HEADER
17 #define BOOST_FUNCTION_BASE_HEADER
24 #include <boost/config.hpp>
25 #include <boost/type_traits.hpp>
26 #include <boost/ref.hpp>
27 #include <boost/pending/ct_if.hpp>
29 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 || defined(__ICL) && __ICL <= 600 || defined(__MWERKS__) && __MWERKS__ < 0x2406
30 # define BOOST_FUNCTION_TARGET_FIX(x) x
32 # define BOOST_FUNCTION_TARGET_FIX(x)
35 #ifdef BOOST_FUNCTION_SILENT_DEPRECATED
36 # define BOOST_FUNCTION_DEPRECATED_PRE
37 # define BOOST_FUNCTION_DEPRECATED_INNER
39 # if defined (BOOST_MSVC) && (BOOST_MSVC >= 1300)
40 # define BOOST_FUNCTION_DEPRECATED_PRE __declspec(deprecated)
41 # define BOOST_FUNCTION_DEPRECATED_INNER
43 # define BOOST_FUNCTION_DEPRECATED_PRE
44 # define BOOST_FUNCTION_DEPRECATED_INNER int deprecated;
52 * A union of a function pointer and a void pointer. This is necessary
53 * because 5.2.10/6 allows reinterpret_cast<> to safely cast between
54 * function pointer types and 5.2.9/10 allows static_cast<> to safely
55 * cast between a void pointer and an object pointer. But it is not legal
56 * to cast between a function pointer and a void* (in either direction),
57 * so function requires a union of the two. */
61 const void* const_obj_ptr;
64 explicit any_pointer(void* p) : obj_ptr(p) {}
65 explicit any_pointer(const void* p) : const_obj_ptr(p) {}
66 explicit any_pointer(void (*p)()) : func_ptr(p) {}
70 * The unusable class is a placeholder for unused function arguments
71 * It is also completely unusable except that it constructable from
72 * anything. This helps compilers without partial specialization to
73 * handle Boost.Function objects returning void.
78 template<typename T> unusable(const T&) {}
81 /* Determine the return type. This supports compilers that do not support
82 * void returns or partial specialization by silently changing the return
85 template<typename T> struct function_return_type { typedef T type; };
88 struct function_return_type<void>
90 typedef unusable type;
93 // The operation type to perform on the given functor/function pointer
94 enum functor_manager_operation_type {
99 // Tags used to decide between different types of functions
100 struct function_ptr_tag {};
101 struct function_obj_tag {};
102 struct member_ptr_tag {};
103 struct function_obj_ref_tag {};
104 struct stateless_function_obj_tag {};
107 class get_function_tag
109 typedef typename ct_if<(is_pointer<F>::value),
111 function_obj_tag>::type ptr_or_obj_tag;
113 typedef typename ct_if<(is_member_pointer<F>::value),
115 ptr_or_obj_tag>::type ptr_or_obj_or_mem_tag;
117 typedef typename ct_if<(is_reference_wrapper<F>::value),
118 function_obj_ref_tag,
119 ptr_or_obj_or_mem_tag>::type or_ref_tag;
122 typedef typename ct_if<(is_stateless<F>::value),
123 stateless_function_obj_tag,
124 or_ref_tag>::type type;
127 // The trivial manager does nothing but return the same pointer (if we
128 // are cloning) or return the null pointer (if we are deleting).
129 inline any_pointer trivial_manager(any_pointer f,
130 functor_manager_operation_type op)
132 if (op == clone_functor_tag)
135 return any_pointer(reinterpret_cast<void*>(0));
139 * The functor_manager class contains a static function "manage" which
140 * can clone or destroy the given function/function object pointer.
142 template<typename Functor, typename Allocator>
143 struct functor_manager
146 typedef Functor functor_type;
148 // For function pointers, the manager is trivial
149 static inline any_pointer
150 manager(any_pointer function_ptr,
151 functor_manager_operation_type op,
154 if (op == clone_functor_tag)
157 return any_pointer(static_cast<void (*)()>(0));
160 // For function object pointers, we clone the pointer to each
161 // function has its own version.
162 static inline any_pointer
163 manager(any_pointer function_obj_ptr,
164 functor_manager_operation_type op,
167 #ifndef BOOST_NO_STD_ALLOCATOR
168 typedef typename Allocator::template rebind<functor_type>::other
170 typedef typename allocator_type::pointer pointer_type;
172 typedef functor_type* pointer_type;
173 #endif // BOOST_NO_STD_ALLOCATOR
175 # ifndef BOOST_NO_STD_ALLOCATOR
176 allocator_type allocator;
177 # endif // BOOST_NO_STD_ALLOCATOR
179 if (op == clone_functor_tag) {
181 static_cast<functor_type*>(function_obj_ptr.obj_ptr);
184 # ifndef BOOST_NO_STD_ALLOCATOR
185 pointer_type copy = allocator.allocate(1);
186 allocator.construct(copy, *f);
188 // Get back to the original pointer type
189 functor_type* new_f = static_cast<functor_type*>(copy);
191 functor_type* new_f = new functor_type(*f);
192 # endif // BOOST_NO_STD_ALLOCATOR
193 return any_pointer(static_cast<void*>(new_f));
196 /* Cast from the void pointer to the functor pointer type */
198 reinterpret_cast<functor_type*>(function_obj_ptr.obj_ptr);
200 # ifndef BOOST_NO_STD_ALLOCATOR
201 /* Cast from the functor pointer type to the allocator's pointer
203 pointer_type victim = static_cast<pointer_type>(f);
205 // Destroy and deallocate the functor
206 allocator.destroy(victim);
207 allocator.deallocate(victim, 1);
210 # endif // BOOST_NO_STD_ALLOCATOR
212 return any_pointer(static_cast<void*>(0));
216 /* Dispatch to an appropriate manager based on whether we have a
217 function pointer or a function object pointer. */
219 manage(any_pointer functor_ptr, functor_manager_operation_type op)
221 typedef typename get_function_tag<functor_type>::type tag_type;
222 return manager(functor_ptr, op, tag_type());
226 // value=1 if the given type is not "unusable"
230 BOOST_STATIC_CONSTANT(int, value = 1);
233 // value=0 for unusable types
235 struct count_if_used<unusable>
237 BOOST_STATIC_CONSTANT(int, value = 0);
240 // Count the number of arguments (from the given set) which are not
241 // "unusable" (therefore, count those arguments that are used).
242 template<typename T1, typename T2, typename T3, typename T4,
243 typename T5, typename T6, typename T7, typename T8,
244 typename T9, typename T10>
245 struct count_used_args
247 BOOST_STATIC_CONSTANT(int, value =
248 (count_if_used<T1>::value +
249 count_if_used<T2>::value +
250 count_if_used<T3>::value +
251 count_if_used<T4>::value +
252 count_if_used<T5>::value +
253 count_if_used<T6>::value +
254 count_if_used<T7>::value +
255 count_if_used<T8>::value +
256 count_if_used<T9>::value +
257 count_if_used<T10>::value));
259 } // end namespace function
260 } // end namespace detail
263 * The function_base class contains the basic elements needed for the
264 * function1, function2, function3, etc. classes. It is common to all
265 * functions (and as such can be used to tell if we have one of the
266 * functionN objects).
271 function_base() : manager(0), functor(static_cast<void*>(0)) {}
273 // Is this function empty?
274 bool empty() const { return !manager; }
276 public: // should be protected, but GCC 2.95.3 will fail to allow access
277 detail::function::any_pointer (*manager)(
278 detail::function::any_pointer,
279 detail::function::functor_manager_operation_type);
280 detail::function::any_pointer functor;
283 /* Poison comparison between Boost.Function objects (because it is
284 * meaningless). The comparisons would otherwise be allowed because of the
285 * conversion required to allow syntax such as:
286 * boost::function<int, int> f;
289 void operator==(const function_base&, const function_base&);
290 void operator!=(const function_base&, const function_base&);
294 inline bool has_empty_target(const function_base* f)
299 inline bool has_empty_target(...)
303 } // end namespace function
304 } // end namespace detail
306 // The default function policy is to do nothing before and after the call.
307 struct empty_function_policy
309 inline void precall(const function_base*) {}
310 inline void postcall(const function_base*) {}
313 // The default function mixin does nothing. The assignment and
314 // copy-construction operators are all defined because MSVC defines broken
316 struct empty_function_mixin
318 empty_function_mixin() {}
319 empty_function_mixin(const empty_function_mixin&) {}
321 empty_function_mixin& operator=(const empty_function_mixin&)
328 #endif // BOOST_FUNCTION_BASE_HEADER