From deabdc22c674432344c59c2196404074dc77be6e Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Wed, 11 Jul 2001 18:55:44 +0000 Subject: [PATCH] faq.html: - Fixed HTML typo near the end index.html: - Added declarations of all members for all classes. - Added detailed description of interface according to Boost guidelines. [SVN r10591] --- faq.html | 2 +- index.html | 378 +++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 267 insertions(+), 113 deletions(-) diff --git a/faq.html b/faq.html index 73e0305..d922646 100644 --- a/faq.html +++ b/faq.html @@ -35,7 +35,7 @@ void g() { return f(); }

In November and December of 2000, the issue of cloning vs. reference counting was debated at length and it was decided that cloning gave more predictable semantics. I won't rehash the discussion here, but if it cloning is incorrect for a particular application a reference-counting allocator could be used.

Q: How do I assign from a member function?

-

Member function assignments are not included directly in boost::function because they do not conform to the syntax of function objects. Several libraries exist to wrap member functions in a function object and/or bind the first argument to the member function (the this pointer). A few libraries are Boost.Function documentation. +

Member function assignments are not included directly in boost::function because they do not conform to the syntax of function objects. Several libraries exist to wrap member functions in a function object and/or bind the first argument to the member function (the this pointer). A few libraries are described in the Boost.Function documentation.


Doug Gregor
diff --git a/index.html b/index.html index 8caca58..1550022 100644 --- a/index.html +++ b/index.html @@ -13,6 +13,11 @@

Generally, any place in which a function pointer would be used to defer a call or make a callback, Boost.Function can be used instead to allow the user greater flexibility in the implementation of the target. Targets can be any 'compatible' function object (or function pointer), meaning that the arguments to the interface designated by Boost.Function can be converted to the arguments of the target function object.

-

The function family

- +

The function family

The header <boost/function.hpp> defines the primary entry point to the function object wrappers, the class template boost::function. This class template is essentially a thin wrapper around a set of similar numbered function object wrappers, boost::function0, boost::function1, etc., where the number indicates the number of arguments passed to the function object target. The declaration of f above could also be written as:

 boost::function2<float, int, int> f;
@@ -108,65 +357,9 @@ boost::function2<float, int, int> f;
 
 

The numbered class templates contain most of the implementation and are each distinct class templates. They may be helpful if used in shared libraries, where the number of arguments supported by Boost.Function may change between revisions. Additionally, some compilers (e.g., Microsoft Visual C++ 6.0) have been known to be incapable of compiling boost::function in some instances but are able to handle the numbered variants. -

Operations on function object wrappers

- -

Each function object wrapper type (that has N actual arguments) supports the following operations: - - - - - - - - - - - - - - - - - - - - - - - -
SyntaxSemantics
-f = func_obj;
-f.set(func_obj);
Clears out f's current target and retargets f to a copy of func_obj.
-f.clear();
Removes f's target, if it has one.
-(bool)f
-!f.empty()
The conversion to bool evaluates true if a target exists, whereas empty() returns true if no target exists.
f(a1, a2, ..., aN)
Invoke f's current target with the given arguments. -
-swap(f1, f2);
-f1.swap(f2);
Swap the targets of f1 and f2, which must be of the same type. No exceptions will be thrown. -
- -

Additionally, function object wrappers may be default-constructed (as empty) or constructed from any compatible function object. They are copy constructible and copy-assignable. - -

All function object wrappers derive from boost::function_base, which implements the empty() member function and the bool conversion. Additionally, no other class may inherit boost::function_base, so user code may rely on the implicit base pointer conversion to determine if a type is a boost::function type or one of its variants. - -

Advanced usage

- - +

Advanced usage

The boost::function family supports additional customization by means of policies, mixins, and allocators. The specific usage of each of these will be explained in later sections, but they share a common problem: how to replace each default with your own version. -

For the numbered function object wrappers, one need only specify the new classes as a template parameter in the appropriate position. The following is a general definition for each of the numbered function object wrappers: -

-  template<typename Return,
-           typename Arg1,
-           typename Arg2,
-           ...
-           typename ArgN,
-           typename Policy    = empty_function_policy,
-    	   typename Mixin     = empty_function_mixin,
-    	   typename Allocator = std::allocator<function_base>
-           > class functionN { /* ... */ };
-
-

With boost::function it is not so clear, because support for an arbitrary number of parameters means that it is impossible to specify just the last parameter, but not 5 of the parameters in between. Therefore, boost::function doubles as a generative interface for the underlying numbered class templates that uses named template parameters. For instance, to specify both a policy and an allocator for a function object wrapper f taking an int and returning an int, use:

   function<int, int>::policy<MyPolicy>::allocator<MyAllocator>::type f;
@@ -174,8 +367,7 @@ f1.swap(f2);

The named template parameters policy, mixin and allocator each take one template parameter (the replacement class) and may be nested as above to generate a function object wrapper. The ::type at the end accesses the actual type that fits the given properties. -

Policies

- +

Policies

Policies define what happens directly before and directly after an invocation of a function object target is made. A policy must have two member functions, precall and postcall, each of which must be able to accept a const function object wrapper pointer. The following policy will print "before" prior to execution and "after" afterwards:

@@ -189,16 +381,13 @@ struct print_policy {
 
 

Policies are further described in the Boost discussion on generic programming techniques. -

Mixins

- +

Mixins

The function object wrappers allow any class to be "mixed in" as a base class. This allows extra members and/or functionality to be included by the user. This can be used, for instance, to overcome the limitations of policies by storing data between invocations in a base class instead of in a static member of a policy class. -

Allocators

- +

Allocators

The function object wrappers allow the user to specify a new allocator to handle the cloning of function object targets (when the wrappers are copied). The allocators used are the same as the C++ standard library allocators. The wrappers assume the allocators are stateless, and will create a new instance each time they are used (because they are rebound very often). This shares the semantics of most standard library implementations, and is explicitly allowed by the C++ standard. -

Example: Synchronized callbacks

- +

Example: Synchronized callbacks

Synchronization of callbacks in a multithreaded environment is extremely important. Using mixins and policies, a Boost.Function object may implement its own synchronization policy that ensures that only one thread can be in the callback function at any given point in time.

We will use the prototype Boost.Threads library for its recursive_mutex. Since the mutex is on a per-callback basis, we will add a mutex to the boost::function by mixin it in with this mixin class: @@ -232,17 +421,13 @@ class SynchronizedPolicy { boost::function2<float, int, int, SynchronizedPolicy, SynchronizedMixin> f;

-

Boost.Function vs. Function Pointers

- +

Boost.Function vs. Function Pointers

Boost.Function has several advantages over function pointers, namely:

@@ -258,8 +443,7 @@ And, of course, function pointers have several advantages over Boost.Function:

The above two lists were adapted from comments made by Darin Adler. -

Performance

- +

Performance

Function object wrapper size

Function object wrappers will be the size of two function pointers plus one function pointer or data pointer (whichever is larger). On common 32-bit platforms, this amounts to 12 bytes per wrapper. Additionally, the function object target will be allocated on the heap. @@ -269,15 +453,14 @@ And, of course, function pointers have several advantages over Boost.Function:

Invocation efficiency

With a properly inlining compiler, an invocation of a function object requires one call through a function pointer. If the call is to a free function pointer, an additional call must be made to that function pointer (unless the compiler has very powerful interprocedural analysis). -

Portability

- +

Portability

The function object wrappers have been designed to be as portable as possible, and to support many compilers even when they do not support the C++ standard well. The following compilers have passed all of the testcases included with boost::function.

@@ -287,37 +470,9 @@ And, of course, function pointers have several advantages over Boost.Function:
  • Intel C++ 5.0: allocators not supported
  • -

    If your compiler is not listed, there is a small set of tests to stress the capabilities of the boost::function library. A standards-compliant compiler should compile the code without any modifications, but if you find you run into problems the following macros can be defined to adapt the function object wrappers to a broken compiler: - - - - - - - - - - - - - - - - - - - - - - - - -
    Macro nameEffect and symptoms
    BOOST_FUNCTION_USE_VIRTUAL_FUNCTIONSWhen enabled, this macro uses virtual functions instead of the default function pointers. In most cases, this will generate larger executables. However, if a compiler optimizes virtual function calls well it may result in smaller, faster executables. Enabling this macro also fixes some code generation problems in some compilers... -
    BOOST_WEAK_FUNCTION_TEMPLATE_ORDERINGboost::function stresses function template ordering more than most compilers can handle. If your compiler is having trouble with free function pointer assignments, try defining this macro
    BOOST_NO_DEPENDENT_BASE_LOOKUPIf your compiler cannot seem to find operators defined in a dependent base class (i.e., if you are trying to use boost::function operators and your compiler isn't finding them), try defining this macro
    BOOST_NO_DEPENDENT_NESTED_DERIVATIONSIf your compiler can't handle the code in the function_traits_builder class, try defining this.
    BOOST_WEAK_CONVERSION_OPERATORSIf expressions such as !f (for a boost::function object f) fail, try to define this. Note that this may allow some meaningless expressions to compile, such as f+4.
    - -

    Design rationale

    - +

    If your compiler is not listed, there is a small set of tests to stress the capabilities of the boost::function library. A standards-compliant compiler should compile the code without any modifications, but if you find you run into problems please submit a bug report. +

    Design rationale

    Combatting virtual function bloat

    The use of virtual functions tends to cause 'code bloat' on many compilers. When a class contains a virtual function, it is necessary to emit an additional function that classifies the type of the object. It has been our experience that these auxiliary functions increase the size of the executable significantly when many boost::function objects are used. @@ -325,8 +480,7 @@ And, of course, function pointers have several advantages over Boost.Function:

    A compiler with strong interprocedural analysis could significantly reduce the overhead associated with virtual function calls such that the alternative used by Boost.Function is less efficient. No compiler has yet been found where this is true, but when it does occur the BOOST_FUNCTION_USE_VIRTUAL_FUNCTIONS macro can be defined to revert to the simpler implementation based on virtual functions. -

    Acknowledgements

    - +

    Acknowledgements

    Many people were involved in the construction of this library. William Kempf, Jesse Jones and Karl Nelson were all extremely helpful in isolating an interface and scope for the library. John Maddock managed the formal review, and many reviewers gave excellent comments on interface, implementation, and documentation.