]> git.lyx.org Git - lyx.git/blob - boost/boost/function/function_base.hpp
update from Boost CVS
[lyx.git] / boost / boost / function / function_base.hpp
1 // Boost.Function library
2
3 // Copyright (C) 2001 Doug Gregor (gregod@cs.rpi.edu)
4 //
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.
10 //
11 // This software is provided "as is" without express or implied warranty,
12 // and with no claim as to its suitability for any purpose.
13
14 // For more information, see http://www.boost.org
15
16 #ifndef BOOST_FUNCTION_BASE_HEADER
17 #define BOOST_FUNCTION_BASE_HEADER
18
19 #include <string>
20 #include <stdexcept>
21 #include <memory>
22 #include <new>
23 #include <typeinfo>
24 #include <boost/config.hpp>
25 #include <boost/type_traits.hpp>
26 #include <boost/ref.hpp>
27 #include <boost/pending/ct_if.hpp>
28
29 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 || defined(__ICL) && __ICL <= 600 || defined(__MWERKS__) && __MWERKS__ < 0x2406
30 #  define BOOST_FUNCTION_TARGET_FIX(x) x
31 #else
32 #  define BOOST_FUNCTION_TARGET_FIX(x)
33 #endif // not MSVC
34
35 #if defined (BOOST_MSVC) && (BOOST_MSVC >= 1300)
36 #  define BOOST_FUNCTION_DEPRECATED_PRE __declspec(deprecated)
37 #  define BOOST_FUNCTION_DEPRECATED_INNER
38 #else
39 #  define BOOST_FUNCTION_DEPRECATED_PRE
40 #  define BOOST_FUNCTION_DEPRECATED_INNER int deprecated;
41 #endif
42
43 namespace boost {
44   namespace detail {
45     namespace function {
46       /**
47        * A union of a function pointer and a void pointer. This is necessary
48        * because 5.2.10/6 allows reinterpret_cast<> to safely cast between
49        * function pointer types and 5.2.9/10 allows static_cast<> to safely
50        * cast between a void pointer and an object pointer. But it is not legal
51        * to cast between a function pointer and a void* (in either direction),
52        * so function requires a union of the two. */
53       union any_pointer
54       {
55         void* obj_ptr;
56         const void* const_obj_ptr;
57         void (*func_ptr)();
58
59         explicit any_pointer(void* p) : obj_ptr(p) {}
60         explicit any_pointer(const void* p) : const_obj_ptr(p) {}
61         explicit any_pointer(void (*p)()) : func_ptr(p) {}
62       };
63
64       /**
65        * The unusable class is a placeholder for unused function arguments
66        * It is also completely unusable except that it constructable from
67        * anything. This helps compilers without partial specialization to
68        * handle Boost.Function objects returning void.
69        */
70       struct unusable
71       {
72         unusable() {}
73         template<typename T> unusable(const T&) {}
74       };
75
76       /* Determine the return type. This supports compilers that do not support
77        * void returns or partial specialization by silently changing the return
78        * type to "unusable".
79        */
80       template<typename T> struct function_return_type { typedef T type; };
81
82       template<>
83       struct function_return_type<void>
84       {
85         typedef unusable type;
86       };
87
88       // The operation type to perform on the given functor/function pointer
89       enum functor_manager_operation_type {
90         clone_functor_tag,
91         destroy_functor_tag
92       };
93
94       // Tags used to decide between different types of functions
95       struct function_ptr_tag {};
96       struct function_obj_tag {};
97       struct member_ptr_tag {};
98       struct function_obj_ref_tag {};
99       struct stateless_function_obj_tag {};
100
101       template<typename F>
102       class get_function_tag
103       {
104         typedef typename ct_if<(is_pointer<F>::value),
105                             function_ptr_tag,
106                             function_obj_tag>::type ptr_or_obj_tag;
107
108         typedef typename ct_if<(is_member_pointer<F>::value),
109                             member_ptr_tag,
110                             ptr_or_obj_tag>::type ptr_or_obj_or_mem_tag;
111
112         typedef typename ct_if<(is_reference_wrapper<F>::value),
113                              function_obj_ref_tag,
114                              ptr_or_obj_or_mem_tag>::type or_ref_tag;
115
116       public:
117         typedef typename ct_if<(is_stateless<F>::value),
118                             stateless_function_obj_tag,
119                             or_ref_tag>::type type;
120       };
121
122       // The trivial manager does nothing but return the same pointer (if we
123       // are cloning) or return the null pointer (if we are deleting).
124       inline any_pointer trivial_manager(any_pointer f,
125                                          functor_manager_operation_type op)
126       {
127         if (op == clone_functor_tag)
128           return f;
129         else
130           return any_pointer(reinterpret_cast<void*>(0));
131       }
132
133       /**
134        * The functor_manager class contains a static function "manage" which
135        * can clone or destroy the given function/function object pointer.
136        */
137       template<typename Functor, typename Allocator>
138       struct functor_manager
139       {
140       private:
141         typedef Functor functor_type;
142
143         // For function pointers, the manager is trivial
144         static inline any_pointer
145         manager(any_pointer function_ptr,
146                 functor_manager_operation_type op,
147                 function_ptr_tag)
148         {
149           if (op == clone_functor_tag)
150             return function_ptr;
151           else
152             return any_pointer(static_cast<void (*)()>(0));
153         }
154
155         // For function object pointers, we clone the pointer to each
156         // function has its own version.
157         static inline any_pointer
158         manager(any_pointer function_obj_ptr,
159                 functor_manager_operation_type op,
160                 function_obj_tag)
161         {
162 #ifndef BOOST_NO_STD_ALLOCATOR
163         typedef typename Allocator::template rebind<functor_type>::other
164           allocator_type;
165         typedef typename allocator_type::pointer pointer_type;
166 #else
167         typedef functor_type* pointer_type;
168 #endif // BOOST_NO_STD_ALLOCATOR
169
170 #  ifndef BOOST_NO_STD_ALLOCATOR
171           allocator_type allocator;
172 #  endif // BOOST_NO_STD_ALLOCATOR
173
174           if (op == clone_functor_tag) {
175             functor_type* f =
176               static_cast<functor_type*>(function_obj_ptr.obj_ptr);
177
178             // Clone the functor
179 #  ifndef BOOST_NO_STD_ALLOCATOR
180             pointer_type copy = allocator.allocate(1);
181             allocator.construct(copy, *f);
182
183             // Get back to the original pointer type
184             functor_type* new_f = static_cast<functor_type*>(copy);
185 #  else
186             functor_type* new_f = new functor_type(*f);
187 #  endif // BOOST_NO_STD_ALLOCATOR
188             return any_pointer(static_cast<void*>(new_f));
189           }
190           else {
191             /* Cast from the void pointer to the functor pointer type */
192             functor_type* f =
193               reinterpret_cast<functor_type*>(function_obj_ptr.obj_ptr);
194
195 #  ifndef BOOST_NO_STD_ALLOCATOR
196             /* Cast from the functor pointer type to the allocator's pointer
197                type */
198             pointer_type victim = static_cast<pointer_type>(f);
199
200             // Destroy and deallocate the functor
201             allocator.destroy(victim);
202             allocator.deallocate(victim, 1);
203 #  else
204             delete f;
205 #  endif // BOOST_NO_STD_ALLOCATOR
206
207             return any_pointer(static_cast<void*>(0));
208           }
209         }
210       public:
211         /* Dispatch to an appropriate manager based on whether we have a
212            function pointer or a function object pointer. */
213         static any_pointer
214         manage(any_pointer functor_ptr, functor_manager_operation_type op)
215         {
216           typedef typename get_function_tag<functor_type>::type tag_type;
217           return manager(functor_ptr, op, tag_type());
218         }
219       };
220
221       // value=1 if the given type is not "unusable"
222       template<typename T>
223       struct count_if_used
224       {
225         BOOST_STATIC_CONSTANT(int, value = 1);
226       };
227
228       // value=0 for unusable types
229       template<>
230       struct count_if_used<unusable>
231       {
232         BOOST_STATIC_CONSTANT(int, value = 0);
233       };
234
235       // Count the number of arguments (from the given set) which are not
236       // "unusable" (therefore, count those arguments that are used).
237       template<typename T1, typename T2, typename T3, typename T4,
238                typename T5, typename T6, typename T7, typename T8,
239                typename T9, typename T10>
240       struct count_used_args
241       {
242         BOOST_STATIC_CONSTANT(int, value =
243                               (count_if_used<T1>::value +
244                                count_if_used<T2>::value +
245                                count_if_used<T3>::value +
246                                count_if_used<T4>::value +
247                                count_if_used<T5>::value +
248                                count_if_used<T6>::value +
249                                count_if_used<T7>::value +
250                                count_if_used<T8>::value +
251                                count_if_used<T9>::value +
252                                count_if_used<T10>::value));
253       };
254     } // end namespace function
255   } // end namespace detail
256
257   /**
258    * The function_base class contains the basic elements needed for the
259    * function1, function2, function3, etc. classes. It is common to all
260    * functions (and as such can be used to tell if we have one of the
261    * functionN objects).
262    */
263   class function_base
264   {
265   public:
266     function_base() : manager(0), functor(static_cast<void*>(0)) {}
267
268     // Is this function empty?
269     bool empty() const { return !manager; }
270
271   public: // should be protected, but GCC 2.95.3 will fail to allow access
272     detail::function::any_pointer (*manager)(
273                            detail::function::any_pointer,
274                            detail::function::functor_manager_operation_type);
275     detail::function::any_pointer functor;
276
277 #if (defined __SUNPRO_CC) && (__SUNPRO_CC <= 0x530) && !(defined BOOST_NO_COMPILER_CONFIG)
278     // Sun C++ 5.3 can't handle the safe_bool idiom, so don't use it
279     operator bool () const { return !this->empty(); }
280 #else
281   private:
282     struct dummy {
283       void nonnull() {};
284     };
285
286     typedef void (dummy::*safe_bool)();
287
288   public:
289     operator safe_bool () const
290       { return (this->empty())? 0 : &dummy::nonnull; }
291
292     safe_bool operator!() const
293       { return (this->empty())? &dummy::nonnull : 0; }
294 #endif
295   };
296
297   /* Poison comparison between Boost.Function objects (because it is
298    * meaningless). The comparisons would otherwise be allowed because of the
299    * conversion required to allow syntax such as:
300    *   boost::function<int, int> f;
301    *   if (f) { f(5); }
302    */
303   void operator==(const function_base&, const function_base&);
304   void operator!=(const function_base&, const function_base&);
305
306   namespace detail {
307     namespace function {
308       inline bool has_empty_target(const function_base* f)
309       {
310         return f->empty();
311       }
312
313       inline bool has_empty_target(...)
314       {
315         return false;
316       }
317     } // end namespace function
318   } // end namespace detail
319
320   // The default function policy is to do nothing before and after the call.
321   struct empty_function_policy
322   {
323     inline void precall(const function_base*) {}
324     inline void postcall(const function_base*) {}
325   };
326
327   // The default function mixin does nothing. The assignment and
328   // copy-construction operators are all defined because MSVC defines broken
329   // versions.
330   struct empty_function_mixin
331   {
332     empty_function_mixin() {}
333     empty_function_mixin(const empty_function_mixin&) {}
334
335     empty_function_mixin& operator=(const empty_function_mixin&)
336     {
337       return *this;
338     }
339   };
340 }
341
342 #endif // BOOST_FUNCTION_BASE_HEADER