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