1 // Boost.Function library
3 // Copyright (C) 2001 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>
28 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 || defined(__ICL) && __ICL <= 600 || defined(__MWERKS__) && __MWERKS__ < 0x2406
29 # define BOOST_FUNCTION_TARGET_FIX(x) x
31 # define BOOST_FUNCTION_TARGET_FIX(x)
38 * The ct_if implementation is temporary code. When a Boost metaprogramming
39 * library is introduced, Boost.Function will use it instead.
44 template<typename Then, typename Else>
53 template<typename Then, typename Else>
60 template<bool Condition>
63 typedef SelectThen type;
67 struct Selector<false>
69 typedef SelectElse type;
71 } // end namespace intimate
73 template<bool Condition, typename Then, typename Else>
76 typedef typename intimate::Selector<Condition>::type select;
77 typedef typename select::template Result<Then,Else>::type type;
81 * A union of a function pointer and a void pointer. This is necessary
82 * because 5.2.10/6 allows reinterpret_cast<> to safely cast between
83 * function pointer types and 5.2.9/10 allows static_cast<> to safely
84 * cast between a void pointer and an object pointer. But it is not legal
85 * to cast between a function pointer and a void* (in either direction),
86 * so function requires a union of the two. */
90 const void* const_obj_ptr;
93 explicit any_pointer(void* p) : obj_ptr(p) {}
94 explicit any_pointer(const void* p) : const_obj_ptr(p) {}
95 explicit any_pointer(void (*p)()) : func_ptr(p) {}
99 * The unusable class is a placeholder for unused function arguments
100 * It is also completely unusable except that it constructable from
101 * anything. This helps compilers without partial specialization to
102 * handle Boost.Function objects returning void.
107 template<typename T> unusable(const T&) {}
110 /* Determine the return type. This supports compilers that do not support
111 * void returns or partial specialization by silently changing the return
112 * type to "unusable".
114 template<typename T> struct function_return_type { typedef T type; };
117 struct function_return_type<void>
119 typedef unusable type;
122 // The operation type to perform on the given functor/function pointer
123 enum functor_manager_operation_type {
128 // Tags used to decide between different types of functions
129 struct function_ptr_tag {};
130 struct function_obj_tag {};
131 struct member_ptr_tag {};
132 struct function_obj_ref_tag {};
133 struct stateless_function_obj_tag {};
136 class get_function_tag
138 typedef typename ct_if<(is_pointer<F>::value),
140 function_obj_tag>::type ptr_or_obj_tag;
142 typedef typename ct_if<(is_member_pointer<F>::value),
144 ptr_or_obj_tag>::type ptr_or_obj_or_mem_tag;
146 typedef typename ct_if<(is_reference_wrapper<F>::value),
147 function_obj_ref_tag,
148 ptr_or_obj_or_mem_tag>::type or_ref_tag;
151 typedef typename ct_if<(is_stateless<F>::value),
152 stateless_function_obj_tag,
153 or_ref_tag>::type type;
156 // The trivial manager does nothing but return the same pointer (if we
157 // are cloning) or return the null pointer (if we are deleting).
158 inline any_pointer trivial_manager(any_pointer f,
159 functor_manager_operation_type op)
161 if (op == clone_functor_tag)
164 return any_pointer(reinterpret_cast<void*>(0));
168 * The functor_manager class contains a static function "manage" which
169 * can clone or destroy the given function/function object pointer.
171 template<typename Functor, typename Allocator>
172 struct functor_manager
175 typedef Functor functor_type;
177 // For function pointers, the manager is trivial
178 static inline any_pointer
179 manager(any_pointer function_ptr,
180 functor_manager_operation_type op,
183 if (op == clone_functor_tag)
186 return any_pointer(static_cast<void (*)()>(0));
189 // For function object pointers, we clone the pointer to each
190 // function has its own version.
191 static inline any_pointer
192 manager(any_pointer function_obj_ptr,
193 functor_manager_operation_type op,
196 #ifndef BOOST_NO_STD_ALLOCATOR
197 typedef typename Allocator::template rebind<functor_type>::other
199 typedef typename allocator_type::pointer pointer_type;
201 typedef functor_type* pointer_type;
202 #endif // BOOST_NO_STD_ALLOCATOR
204 # ifndef BOOST_NO_STD_ALLOCATOR
205 allocator_type allocator;
206 # endif // BOOST_NO_STD_ALLOCATOR
208 if (op == clone_functor_tag) {
210 static_cast<functor_type*>(function_obj_ptr.obj_ptr);
213 # ifndef BOOST_NO_STD_ALLOCATOR
214 pointer_type copy = allocator.allocate(1);
215 allocator.construct(copy, *f);
217 // Get back to the original pointer type
218 functor_type* new_f = static_cast<functor_type*>(copy);
220 functor_type* new_f = new functor_type(*f);
221 # endif // BOOST_NO_STD_ALLOCATOR
222 return any_pointer(static_cast<void*>(new_f));
225 /* Cast from the void pointer to the functor pointer type */
227 reinterpret_cast<functor_type*>(function_obj_ptr.obj_ptr);
229 # ifndef BOOST_NO_STD_ALLOCATOR
230 /* Cast from the functor pointer type to the allocator's pointer
232 pointer_type victim = static_cast<pointer_type>(f);
234 // Destroy and deallocate the functor
235 allocator.destroy(victim);
236 allocator.deallocate(victim, 1);
239 # endif // BOOST_NO_STD_ALLOCATOR
241 return any_pointer(static_cast<void*>(0));
245 /* Dispatch to an appropriate manager based on whether we have a
246 function pointer or a function object pointer. */
248 manage(any_pointer functor_ptr, functor_manager_operation_type op)
250 typedef typename get_function_tag<functor_type>::type tag_type;
251 return manager(functor_ptr, op, tag_type());
255 // value=1 if the given type is not "unusable"
259 BOOST_STATIC_CONSTANT(int, value = 1);
262 // value=0 for unusable types
264 struct count_if_used<unusable>
266 BOOST_STATIC_CONSTANT(int, value = 0);
269 // Count the number of arguments (from the given set) which are not
270 // "unusable" (therefore, count those arguments that are used).
271 template<typename T1, typename T2, typename T3, typename T4,
272 typename T5, typename T6, typename T7, typename T8,
273 typename T9, typename T10>
274 struct count_used_args
276 BOOST_STATIC_CONSTANT(int, value =
277 (count_if_used<T1>::value +
278 count_if_used<T2>::value +
279 count_if_used<T3>::value +
280 count_if_used<T4>::value +
281 count_if_used<T5>::value +
282 count_if_used<T6>::value +
283 count_if_used<T7>::value +
284 count_if_used<T8>::value +
285 count_if_used<T9>::value +
286 count_if_used<T10>::value));
288 } // end namespace function
289 } // end namespace detail
292 * The function_base class contains the basic elements needed for the
293 * function1, function2, function3, etc. classes. It is common to all
294 * functions (and as such can be used to tell if we have one of the
295 * functionN objects).
300 function_base() : manager(0), functor(static_cast<void*>(0)) {}
302 // Is this function empty?
303 bool empty() const { return !manager; }
305 public: // should be protected, but GCC 2.95.3 will fail to allow access
306 detail::function::any_pointer (*manager)(
307 detail::function::any_pointer,
308 detail::function::functor_manager_operation_type);
309 detail::function::any_pointer functor;
311 #if (defined __SUNPRO_CC) && (__SUNPRO_CC <= 0x530) && !(defined BOOST_NO_COMPILER_CONFIG)
312 // Sun C++ 5.3 can't handle the safe_bool idiom, so don't use it
313 operator bool () const { return !this->empty(); }
320 typedef void (dummy::*safe_bool)();
323 operator safe_bool () const
324 { return (this->empty())? 0 : &dummy::nonnull; }
326 safe_bool operator!() const
327 { return (this->empty())? &dummy::nonnull : 0; }
331 /* Poison comparison between Boost.Function objects (because it is
332 * meaningless). The comparisons would otherwise be allowed because of the
333 * conversion required to allow syntax such as:
334 * boost::function<int, int> f;
337 void operator==(const function_base&, const function_base&);
338 void operator!=(const function_base&, const function_base&);
342 inline bool has_empty_target(const function_base* f)
347 inline bool has_empty_target(...)
351 } // end namespace function
352 } // end namespace detail
354 // The default function policy is to do nothing before and after the call.
355 struct empty_function_policy
357 inline void precall(const function_base*) {}
358 inline void postcall(const function_base*) {}
361 // The default function mixin does nothing. The assignment and
362 // copy-construction operators are all defined because MSVC defines broken
364 struct empty_function_mixin
366 empty_function_mixin() {}
367 empty_function_mixin(const empty_function_mixin&) {}
369 empty_function_mixin& operator=(const empty_function_mixin&)
376 #endif // BOOST_FUNCTION_BASE_HEADER