]> git.lyx.org Git - lyx.git/blob - boost/boost/function/function_base.hpp
update boost
[lyx.git] / boost / boost / function / function_base.hpp
1 // Boost.Function library
2
3 // Copyright (C) 2001, 2002 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 #ifdef BOOST_FUNCTION_SILENT_DEPRECATED
36 #  define BOOST_FUNCTION_DEPRECATED_PRE
37 #  define BOOST_FUNCTION_DEPRECATED_INNER
38 #else
39 #  if defined (BOOST_MSVC) && (BOOST_MSVC >= 1300)
40 #    define BOOST_FUNCTION_DEPRECATED_PRE __declspec(deprecated)
41 #    define BOOST_FUNCTION_DEPRECATED_INNER
42 #  else
43 #    define BOOST_FUNCTION_DEPRECATED_PRE
44 #    define BOOST_FUNCTION_DEPRECATED_INNER int deprecated;
45 #  endif
46 #endif
47
48 namespace boost {
49   namespace detail {
50     namespace function {
51       /**
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. */
58       union any_pointer 
59       {
60         void* obj_ptr;
61         const void* const_obj_ptr;
62         void (*func_ptr)();
63
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) {}
67       };
68
69       /**
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.
74        */
75       struct unusable 
76       {
77         unusable() {}
78         template<typename T> unusable(const T&) {}
79       };
80
81       /* Determine the return type. This supports compilers that do not support
82        * void returns or partial specialization by silently changing the return
83        * type to "unusable". 
84        */
85       template<typename T> struct function_return_type { typedef T type; };
86
87       template<> 
88       struct function_return_type<void> 
89       {
90         typedef unusable type;
91       };
92
93       // The operation type to perform on the given functor/function pointer
94       enum functor_manager_operation_type { 
95         clone_functor_tag, 
96         destroy_functor_tag
97       };
98
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 {};
105
106       template<typename F>
107       class get_function_tag
108       {
109         typedef typename ct_if<(is_pointer<F>::value),
110                             function_ptr_tag,
111                             function_obj_tag>::type ptr_or_obj_tag;
112
113         typedef typename ct_if<(is_member_pointer<F>::value),
114                             member_ptr_tag,
115                             ptr_or_obj_tag>::type ptr_or_obj_or_mem_tag;
116
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;
120
121       public:
122         typedef typename ct_if<(is_stateless<F>::value),
123                             stateless_function_obj_tag,
124                             or_ref_tag>::type type;
125       };
126
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)
131       {
132         if (op == clone_functor_tag)
133           return f;
134         else
135           return any_pointer(reinterpret_cast<void*>(0));
136       }
137
138       /**
139        * The functor_manager class contains a static function "manage" which
140        * can clone or destroy the given function/function object pointer. 
141        */
142       template<typename Functor, typename Allocator>
143       struct functor_manager
144       {
145       private:
146         typedef Functor functor_type;
147
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,
152                 function_ptr_tag)
153         {
154           if (op == clone_functor_tag)
155             return function_ptr;
156           else
157             return any_pointer(static_cast<void (*)()>(0));
158         }
159
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,
165                 function_obj_tag)
166         {
167 #ifndef BOOST_NO_STD_ALLOCATOR
168         typedef typename Allocator::template rebind<functor_type>::other 
169           allocator_type;
170         typedef typename allocator_type::pointer pointer_type;
171 #else
172         typedef functor_type* pointer_type;
173 #endif // BOOST_NO_STD_ALLOCATOR
174
175 #  ifndef BOOST_NO_STD_ALLOCATOR
176           allocator_type allocator;
177 #  endif // BOOST_NO_STD_ALLOCATOR
178
179           if (op == clone_functor_tag) {
180             functor_type* f = 
181               static_cast<functor_type*>(function_obj_ptr.obj_ptr);
182
183             // Clone the functor
184 #  ifndef BOOST_NO_STD_ALLOCATOR
185             pointer_type copy = allocator.allocate(1);
186             allocator.construct(copy, *f);
187
188             // Get back to the original pointer type
189             functor_type* new_f = static_cast<functor_type*>(copy);
190 #  else
191             functor_type* new_f = new functor_type(*f);
192 #  endif // BOOST_NO_STD_ALLOCATOR
193             return any_pointer(static_cast<void*>(new_f));
194           }
195           else {
196             /* Cast from the void pointer to the functor pointer type */
197             functor_type* f = 
198               reinterpret_cast<functor_type*>(function_obj_ptr.obj_ptr);
199
200 #  ifndef BOOST_NO_STD_ALLOCATOR
201             /* Cast from the functor pointer type to the allocator's pointer
202                type */
203             pointer_type victim = static_cast<pointer_type>(f);
204
205             // Destroy and deallocate the functor
206             allocator.destroy(victim);
207             allocator.deallocate(victim, 1);
208 #  else
209             delete f;
210 #  endif // BOOST_NO_STD_ALLOCATOR
211
212             return any_pointer(static_cast<void*>(0));
213           }
214         }
215       public:
216         /* Dispatch to an appropriate manager based on whether we have a
217            function pointer or a function object pointer. */
218         static any_pointer
219         manage(any_pointer functor_ptr, functor_manager_operation_type op)
220         {
221           typedef typename get_function_tag<functor_type>::type tag_type;
222           return manager(functor_ptr, op, tag_type());
223         }
224       };
225
226       // value=1 if the given type is not "unusable"
227       template<typename T>
228       struct count_if_used
229       {
230         BOOST_STATIC_CONSTANT(int, value = 1);
231       };
232     
233       // value=0 for unusable types
234       template<>
235       struct count_if_used<unusable>
236       {
237         BOOST_STATIC_CONSTANT(int, value = 0);
238       };
239     
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
246       {
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));
258       };
259     } // end namespace function
260   } // end namespace detail
261
262   /**
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). 
267    */
268   class function_base 
269   {
270   public:
271     function_base() : manager(0), functor(static_cast<void*>(0)) {}
272     
273     // Is this function empty?
274     bool empty() const { return !manager; }
275     
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;
281
282 #if (defined __SUNPRO_CC) && (__SUNPRO_CC <= 0x530) && !(defined BOOST_NO_COMPILER_CONFIG)
283     // Sun C++ 5.3 can't handle the safe_bool idiom, so don't use it
284     operator bool () const { return !this->empty(); }
285 #else
286   private:
287     struct dummy {
288       void nonnull() {};
289     };
290
291     typedef void (dummy::*safe_bool)();
292
293   public:
294     operator safe_bool () const 
295       { return (this->empty())? 0 : &dummy::nonnull; }
296
297     safe_bool operator!() const
298       { return (this->empty())? &dummy::nonnull : 0; }
299 #endif
300   };
301
302   /* Poison comparison between Boost.Function objects (because it is 
303    * meaningless). The comparisons would otherwise be allowed because of the
304    * conversion required to allow syntax such as:
305    *   boost::function<int, int> f;
306    *   if (f) { f(5); }
307    */
308   void operator==(const function_base&, const function_base&);
309   void operator!=(const function_base&, const function_base&);
310
311   namespace detail {
312     namespace function {
313       inline bool has_empty_target(const function_base* f)
314       {
315         return f->empty();
316       }
317
318       inline bool has_empty_target(...)
319       {
320         return false;
321       }
322     } // end namespace function
323   } // end namespace detail
324
325   // The default function policy is to do nothing before and after the call.
326   struct empty_function_policy
327   {
328     inline void precall(const function_base*) {}
329     inline void postcall(const function_base*) {}
330   };
331
332   // The default function mixin does nothing. The assignment and
333   // copy-construction operators are all defined because MSVC defines broken
334   // versions.
335   struct empty_function_mixin 
336   {
337     empty_function_mixin() {}
338     empty_function_mixin(const empty_function_mixin&) {}
339
340     empty_function_mixin& operator=(const empty_function_mixin&) 
341     {
342       return *this; 
343     }
344   };
345 }
346
347 #endif // BOOST_FUNCTION_BASE_HEADER