mirror of
https://github.com/boostorg/function.git
synced 2025-07-04 00:06:31 +02:00
Initial Boost.Function commit
[SVN r10372]
This commit is contained in:
47
faq.html
Normal file
47
faq.html
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Boost.Function Frequently Asked Questions</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080">
|
||||||
|
<h1><IMG SRC="../../c++boost.gif" WIDTH="276" HEIGHT="86">boost::function Frequently Asked Questions</h1>
|
||||||
|
|
||||||
|
<h2>Q: I see void pointers; is this [mess] type safe?</h2>
|
||||||
|
<p>Yes, <code>boost::function</code> is type safe even though it uses void pointers and pointers to functions returning void and taking no arguments. Essentially, all type information is encoded in the functions that manage and invoke function pointers and function objects. Only these functions are instantiated with the exact type that is pointed to by the void pointer or pointer to void function. The reason that both are required is that one may cast between void pointers and object pointers safely or between different types of function pointers (provided you don't invoke a function pointer with the wrong type).
|
||||||
|
|
||||||
|
<h2>Q: Why are there workarounds for void returns? C++ allows them!</h2>
|
||||||
|
<p>Void returns are permitted by the C++ standard, as in this code snippet:
|
||||||
|
<pre>
|
||||||
|
void f();
|
||||||
|
void g() { return f(); }
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> One reason for not using void returns is that not all compilers support them. In fact, very few compilers seem to support this trivial feature. Additionally, <code>boost::function</code> is more flexible because it does not use void returns. Consider the following code:
|
||||||
|
<pre>
|
||||||
|
int do_something(int);
|
||||||
|
|
||||||
|
boost::function<void, int> f;
|
||||||
|
f = do_something;
|
||||||
|
</pre>
|
||||||
|
<p> This is a valid usage of <code>boost::function</code> because void returns are not used. With void returns, we would attempting to compile ill-formed code similar to:
|
||||||
|
<pre>
|
||||||
|
int f();
|
||||||
|
void g() { return f(); }
|
||||||
|
</pre>
|
||||||
|
<p> In essence, not using void returns allows <code>boost::function</code> to swallow a return value. This is consistent with allowing the user to assign and invoke functions and function objects with parameters that don't exactly match.
|
||||||
|
|
||||||
|
<h2>Q: Why (function) cloning? </h2>
|
||||||
|
<p> 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.
|
||||||
|
|
||||||
|
<h2>Q: How do I assign from a member function?</h2>
|
||||||
|
<p> Member function assignments are not included directly in <code>boost::function</code> 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 <code>this</code> pointer). A few libraries are <a href="index.html#member_func>described</a> in the <a href="index.html">Boost.Function</a> documentation.
|
||||||
|
|
||||||
|
<hr>
|
||||||
|
<address><a href="mailto:gregod@cs.rpi.edu">Doug Gregor</a></address>
|
||||||
|
<!-- Created: Fri Feb 16 09:30:41 EST 2001 -->
|
||||||
|
<!-- hhmts start -->
|
||||||
|
Last modified: Sun Jun 17 10:06:31 EDT 2001
|
||||||
|
<!-- hhmts end -->
|
||||||
|
</body>
|
||||||
|
</html>
|
331
index.html
Normal file
331
index.html
Normal file
@ -0,0 +1,331 @@
|
|||||||
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Boost.Function</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080">
|
||||||
|
|
||||||
|
<h1><IMG SRC="../../c++boost.gif" WIDTH="276" HEIGHT="86">Header <<a HREF="../../boost/function.hpp">boost/function.hpp</a>></h1>
|
||||||
|
|
||||||
|
<p> The header <<a HREF="../../boost/function.hpp">boost/function.hpp</a>> includes a family of class templates that are function object wrappers. The notion is similar to a generalized callback. It shares features with function pointers in that both define a call interface (e.g., a function taking two integer arguments and returning a floating-point value) through which some implementation can be called, and the implementation that is invoked may change throughout the course of the program.
|
||||||
|
|
||||||
|
<p> 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.
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><a href="#usage">Basic usage</a>
|
||||||
|
<ul>
|
||||||
|
<li>Free functions</li>
|
||||||
|
<li>Member functions</li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
<li><a href="#family">The <code>function</code> family</a></li>
|
||||||
|
<li><a href="#operations">Operations on function object wrappers</a></li>
|
||||||
|
<li><a href="#advanced">Advanced usage</a>
|
||||||
|
<ul>
|
||||||
|
<li><a href="#policies">Policies</a></li>
|
||||||
|
<li><a href="#mixins">Mixins</a></li>
|
||||||
|
<li><a href="#allocators">Allocators</a></li>
|
||||||
|
<li><a href="#synchronizing">Example: Synchronized functions</a></li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
<li><a href="#vspointers">Boost.Function vs. Function Pointers</a></li>
|
||||||
|
<li><a href="#performance">Performance</a></li>
|
||||||
|
<li><a href="#portability">Portability</a></li>
|
||||||
|
<li><a href="#design">Design rationale</a></li>
|
||||||
|
<li><a href="faq.html">Frequently Asked Questions</a></li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<h2>Basic usage</h2>
|
||||||
|
<a name="usage"></a>
|
||||||
|
<p> A wrapper is defined simply by specializing a <code>function</code> object with the desired return type and argument types. Any number of arguments may be supplied, up to some implementation-defined limit (10 is the default maximum). The following declares a function object wrapper <code>f</code> that takes two <code>int</code> parameters and returns a <code>float</code>:
|
||||||
|
<pre>
|
||||||
|
boost::function<float, int, int> f;
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> By default, function object wrappers are empty, so we can create a
|
||||||
|
function object to assign to <code>f</code>:
|
||||||
|
<pre>
|
||||||
|
struct int_div { float operator()(int x, int y) const { return ((float)x)/y; }; };
|
||||||
|
f = int_div();
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> Now we can use <code>f</code> to execute the underlying function object
|
||||||
|
<code>int_div</code>:
|
||||||
|
<pre>
|
||||||
|
std::cout << f(5, 3) << std::endl;
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> We are free to assign any compatible function object to <code>f</code>. If <code>int_div</code> had been declared to take two <code>long</code> operands,
|
||||||
|
the implicit conversions would have been applied without any user interference.
|
||||||
|
|
||||||
|
<p> Invoking a function object wrapper that does not actually contain a function object is a precondition violation. We can check for an empty function object wrapper by querying its <code>empty()</code> method or, more succinctly, by using it in a boolean context: if it evaluates true, it contains a function object target, i.e.,
|
||||||
|
<pre>
|
||||||
|
if (f)
|
||||||
|
std::cout << f(5, 3) << std::endl;
|
||||||
|
else
|
||||||
|
std::cout << "f has no target" << std::endl;
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> We can clear out a function target using the <code>clear()</code> member functor.
|
||||||
|
|
||||||
|
<h3>Free functions</h3>
|
||||||
|
<p> Free function pointers can be considered singleton function objects with const function call operators, and can therefore be directly used with the function object wrappers:
|
||||||
|
<pre>
|
||||||
|
float mul_ints(int x, int y) { return ((float)x) * y; }
|
||||||
|
f = &mul_ints;
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<h3>Member functions</h3>
|
||||||
|
<a name="member_func">
|
||||||
|
<p> In many systems, callbacks often call to member functions of a particular
|
||||||
|
object. Handling argument binding is beyond the scope of Boost.Function. However, there are several libraries that perform 'argument binding', including
|
||||||
|
<ul>
|
||||||
|
<li> The C++ Standard library. Using <code>std::bind1st</code> and <code>std::mem_fun</code> together one can bind the object of a pointer-to-member function for use with Boost.Function:
|
||||||
|
<pre>
|
||||||
|
struct X {
|
||||||
|
int foo(int);
|
||||||
|
};
|
||||||
|
|
||||||
|
boost::function<int, int> f;
|
||||||
|
X x;
|
||||||
|
f = std::bind1st(std::mem_fun(&X::foo), &x);
|
||||||
|
|
||||||
|
f(5); // Call x.foo(5)</pre></li>
|
||||||
|
|
||||||
|
<li><a href="http://lambda.cs.utu.fi/">The Lambda library</a>. This library provides a powerful composition mechanism to construct function objects that uses very natural C++ syntax. Lambda requires a compiler that is reasonably conformant to the C++ standard. Note that it is not a Boost library.</li>
|
||||||
|
|
||||||
|
<li>Peter Dimov's <a href="http://groups.yahoo.com/group/boost/files/bind/bind.hpp">bind</a> library. It has a smaller scope than the Lambda Library but is more tolerant of broken compilers. It is an unreviewed library in development.</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<h2>The <code>function</code> family</h2>
|
||||||
|
<a name="family"></a>
|
||||||
|
<p> The header <<a HREF="../../boost/function.hpp">boost/function.hpp</a>> defines the primary entry point to the function object wrappers, the class template <code>boost::function</code>. This class template is essentially a thin wrapper around a set of similar numbered function object wrappers, <code>boost::function0</code>, <code>boost::function1</code>, etc., where the number indicates the number of arguments passed to the function object target. The declaration of <code>f</code> above could also be written as:
|
||||||
|
<pre>
|
||||||
|
boost::function2<float, int, int> f;
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> 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 <code>boost::function</code> in some instances but are able to handle the numbered variants.
|
||||||
|
|
||||||
|
<h2>Operations on function object wrappers</h2>
|
||||||
|
<a name="operations"></a>
|
||||||
|
<p>Each function object wrapper type (that has N actual arguments) supports the following operations:
|
||||||
|
<table border=1>
|
||||||
|
<tr>
|
||||||
|
<th>Syntax</th>
|
||||||
|
<th>Semantics</th>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><pre>
|
||||||
|
f = func_obj;
|
||||||
|
f.set(func_obj);</pre></td>
|
||||||
|
<td>Clears out <code>f</code>'s current target and retargets <code>f</code> to a copy of <code>func_obj</code>.</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><pre>
|
||||||
|
f.clear();</pre></td>
|
||||||
|
<td>Removes <code>f</code>'s target, if it has one.</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><pre>
|
||||||
|
(bool)f
|
||||||
|
!f.empty()</pre></td>
|
||||||
|
<td>True if <code>f</code> has no target. The conversion to <code>bool</code> evaluates true if a target exists, whereas <code>empty()</code> returns true if no target exists.</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><pre>f(a1, a2, ..., aN)</pre></td>
|
||||||
|
<td>Invoke <code>f</code>'s current target with the given arguments.
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><pre>
|
||||||
|
swap(f1, f2);
|
||||||
|
f1.swap(f2);</pre></td>
|
||||||
|
<td>Swap the targets of <code>f1</code> and <code>f2</code>, which must be of the same type. No exceptions will be thrown.
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<p> Additionally, function object wrappers may be default-constructed (as empty) or constructed from any compatible function object. They are copy constructible and copy-assignable.
|
||||||
|
|
||||||
|
<p> All function object wrappers derive from <code>boost::function_base</code>, which implements the <code>empty()</code> member function and the <code>bool</code> conversion. Additionally, no other class may inherit <code>boost::function_base</code>, so user code may rely on the implicit base pointer conversion to determine if a type is a <code>boost::function</code> type or one of its variants.
|
||||||
|
|
||||||
|
<h2>Advanced usage</h2>
|
||||||
|
<a name="advanced"></a>
|
||||||
|
|
||||||
|
<p> The <code>boost::function</code> 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.
|
||||||
|
|
||||||
|
<p>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:
|
||||||
|
<pre>
|
||||||
|
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 { /* ... */ };
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> With <code>boost::function</code> 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, <code>boost::function</code> 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 <code>f</code> taking an <code>int</code> and returning an <code>int</code>, use:
|
||||||
|
<pre>
|
||||||
|
function<int, int>::policy<MyPolicy>::allocator<MyAllocator>::type f;
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> The named template parameters <code>policy</code>, <code>mixin</code> and <code>allocator</code> each take one template parameter (the replacement class) and may be nested as above to generate a function object wrapper. The <code>::type</code> at the end accesses the actual type that fits the given properties.
|
||||||
|
|
||||||
|
<h3>Policies</h3>
|
||||||
|
<a name="policies"></a>
|
||||||
|
<p> 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, <code>precall</code> and <code>postcall</code>, each of which must be able to accept a <code>const</code> function object wrapper pointer. The following policy will print "before" prior to execution and "after" afterwards:
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
struct print_policy {
|
||||||
|
void precall(const boost::function_base*) { std::cout << "before"; }
|
||||||
|
void postcall(const boost::function_base*) { std::cout << "after"; }
|
||||||
|
};
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> A new instance of the policy class will be created prior to calling the function object target and will be preserved until after the call has returned. Therefore, for any invocation the <code>precall</code> and <code>postcall</code> will be executed on the same policy class instance; however, policy class instances will not be kept between target invocations.
|
||||||
|
|
||||||
|
<p> Policies are further <a href="http://www.boost.org/more/generic_programming.html#policy">described</a> in the Boost discussion on <a href="http://www.boost.org/more/generic_programming.html">generic programming techniques</a>.
|
||||||
|
|
||||||
|
<h3>Mixins</h3>
|
||||||
|
<a name="mixins"></a>
|
||||||
|
<p> 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 <code>static</code> member of a policy class.
|
||||||
|
|
||||||
|
<h3>Allocators</h3>
|
||||||
|
<a name="allocators"></a>
|
||||||
|
<p> 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.
|
||||||
|
|
||||||
|
<h3>Example: Synchronized callbacks</h3>
|
||||||
|
<a name="synchronizing"></a>
|
||||||
|
<p> 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.
|
||||||
|
|
||||||
|
<p> We will use the prototype Boost.Threads library for its <code>recursive_mutex</code>. Since the mutex is on a per-callback basis, we will add a mutex to the <code>boost::function</code> by mixin it in with this mixin class:
|
||||||
|
<pre>
|
||||||
|
class SynchronizedMixin {
|
||||||
|
mutable boost::recursive_mutex mutex;
|
||||||
|
};
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> Next, we create a policy that obtains a lock before the target is called (via the <code>precall</code> function) and releases the lock after the target has been called (via the <code>postcall</code> function):
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
class SynchronizedPolicy {
|
||||||
|
std::auto_ptr<boost::recursive_mutex::lock> lock;
|
||||||
|
|
||||||
|
void precall(const SynchronizedMixin* f)
|
||||||
|
{
|
||||||
|
lock.reset(new boost::recursive_mutex::lock(f->mutex));
|
||||||
|
}
|
||||||
|
|
||||||
|
void postcall(const SynchronizedMixin* f)
|
||||||
|
{
|
||||||
|
lock.reset();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p>The use of <code>std::auto_ptr</code> ensures that the lock will be destroyed (and therefore released) if an exception is thrown by the target function. Now we can use the policy and mixin together to create a synchronized callback:
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
boost::function2<float, int, int, SynchronizedPolicy, SynchronizedMixin> f;
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<h2>Boost.Function vs. Function Pointers</h2>
|
||||||
|
<a name="vspointers"></a>
|
||||||
|
<p>Boost.Function has several advantages over function pointers, namely:
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>Boost.Function allows arbitrary compatible function objects to be targets (instead of requiring an exact function signature).</li>
|
||||||
|
|
||||||
|
<li>Boost.Function may be used with argument-binding and other function object construction libraries.</li>
|
||||||
|
|
||||||
|
<li>Boost.Function has predictible debug behavior when an empty function object is called. </li>
|
||||||
|
|
||||||
|
<li>Boost.Function can be adapted to perform operations before and after each call, allowing, for instance, synchronization primitives to be made part of the function type.</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
And, of course, function pointers have several advantages over Boost.Function:
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li> Function pointers are smaller (the size of one pointer instead of three) </li>
|
||||||
|
<li> Function pointers are faster (Boost.Function may require two calls through function pointers) </li>
|
||||||
|
<li> Function pointers are backward-compatible with C libraries.</li>
|
||||||
|
<li> More readable error messages. </li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
|
||||||
|
<p> The above two lists were adapted from comments made by Darin Adler.
|
||||||
|
|
||||||
|
<h2>Performance</h2>
|
||||||
|
<a name="performance"></a>
|
||||||
|
<h3>Function object wrapper size</h3>
|
||||||
|
<p> 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.
|
||||||
|
|
||||||
|
<h3>Copying efficiency</h3>
|
||||||
|
<p> Copying function object wrappers requires allocating member for a copy of the function object target. The default allocator may be replaced with a faster custom allocator if the cost of this cloning becomes prohibitive.
|
||||||
|
|
||||||
|
<h3>Invocation efficiency</h3>
|
||||||
|
<p> 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).
|
||||||
|
|
||||||
|
<h2>Portability</h2>
|
||||||
|
<a name="portability"></a>
|
||||||
|
<p> 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 <code>boost::function</code>.
|
||||||
|
<ul>
|
||||||
|
<li>GCC 2.95.3</li>
|
||||||
|
<li>GCC 3.0</li>
|
||||||
|
<li>SGI MIPSpro 7.3.0</li>
|
||||||
|
<li>Borland C++ 5.5.1</li>
|
||||||
|
<li>Comeau C++ 4.2.44 (beta 3)</li>
|
||||||
|
<li>Metrowerks Codewarrior 6.1</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p> The following compilers work with <code>boost::function</code>, but have some problems:
|
||||||
|
<ul>
|
||||||
|
<li>Microsoft Visual C++ 6.0 (service pack 5): allocators not supported, some problems with <code>boost::function</code> class template (numbered variants seem to work)</li>
|
||||||
|
<li>Intel C++ 5.0: allocators not supported</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p> If your compiler is not listed, there is a small set of tests to stress the capabilities of the <code>boost::function</code> 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:
|
||||||
|
<table border=1>
|
||||||
|
<tr>
|
||||||
|
<th>Macro name</th>
|
||||||
|
<th>Effect and symptoms</th>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>BOOST_FUNCTION_USE_VIRTUAL_FUNCTIONS</td>
|
||||||
|
<td>When 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...
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>BOOST_WEAK_FUNCTION_TEMPLATE_ORDERING</td>
|
||||||
|
<td><code>boost::function</code> 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</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>BOOST_NO_DEPENDENT_BASE_LOOKUP</td>
|
||||||
|
<td>If your compiler cannot seem to find operators defined in a dependent base class (i.e., if you are trying to use <code>boost::function</code> operators and your compiler isn't finding them), try defining this macro</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>BOOST_NO_DEPENDENT_NESTED_DERIVATIONS</td>
|
||||||
|
<td>If your compiler can't handle the code in the <code>function_traits_builder</code> class, try defining this.</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>BOOST_WEAK_CONVERSION_OPERATORS</td>
|
||||||
|
<td>If expressions such as <code>!f</code> (for a <code>boost::function</code> object <code>f</code>) fail, try to define this. Note that this may allow some meaningless expressions to compile, such as <code>f+4</code>.</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<h2>Design rationale</h2>
|
||||||
|
<a name="design"></a>
|
||||||
|
|
||||||
|
<h3>Combatting virtual function bloat</h3>
|
||||||
|
<p> 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 <code>boost::function</code> objects are used.
|
||||||
|
|
||||||
|
<p> In Boost.Function, an alternative but equivalent approach was taken using free functions instead of virtual functions. The Boost.Function object essentially holds two pointers to make a valid target call: a void pointer to the function object it contains and a void pointer to an "invoker" that can call the function object, given the function pointer. This invoker function performs the argument and return value conversions Boost.Function provides. A third pointer points to a free function called the "manager", which handles the cloning and destruction of function objects. The scheme is typesafe because the only functions that actually handle the function object, the invoker and the manager, are instantiated given the type of the function object, so they can safely cast the incoming void pointer (the function object pointer) to the appropriate type.
|
||||||
|
|
||||||
|
<p> 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.
|
||||||
|
|
||||||
|
|
||||||
|
<hr>
|
||||||
|
<address><a href="mailto:gregod@cs.rpi.edu">Doug Gregor</a></address>
|
||||||
|
</body>
|
||||||
|
</html>
|
73
test/allocator_test.cpp
Normal file
73
test/allocator_test.cpp
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
#define BOOST_INCLUDE_MAIN
|
||||||
|
#include <boost/test/test_tools.hpp>
|
||||||
|
#include <cassert>
|
||||||
|
#include <functional>
|
||||||
|
#include <boost/function.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace boost;
|
||||||
|
|
||||||
|
static int alloc_count = 0;
|
||||||
|
static int dealloc_count = 0;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct counting_allocator : public std::allocator<T>
|
||||||
|
{
|
||||||
|
template<typename U>
|
||||||
|
struct rebind
|
||||||
|
{
|
||||||
|
typedef counting_allocator<U> other;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
T* allocate(std::size_t n)
|
||||||
|
{
|
||||||
|
alloc_count++;
|
||||||
|
return std::allocator<T>::allocate(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
void deallocate(T* p, std::size_t n)
|
||||||
|
{
|
||||||
|
dealloc_count++;
|
||||||
|
std::allocator<T>::deallocate(p, n);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static int do_minus(int x, int y) { return x-y; }
|
||||||
|
|
||||||
|
struct DoNothing
|
||||||
|
{
|
||||||
|
void operator()() const {}
|
||||||
|
};
|
||||||
|
|
||||||
|
static void do_nothing() {}
|
||||||
|
|
||||||
|
int
|
||||||
|
test_main(int, char*[])
|
||||||
|
{
|
||||||
|
function<int, int, int>::allocator< counting_allocator<int> >::type f;
|
||||||
|
f = plus<int>();
|
||||||
|
f.clear();
|
||||||
|
BOOST_TEST(alloc_count == 1);
|
||||||
|
BOOST_TEST(dealloc_count == 1);
|
||||||
|
|
||||||
|
alloc_count = 0;
|
||||||
|
dealloc_count = 0;
|
||||||
|
f = &do_minus;
|
||||||
|
f.clear();
|
||||||
|
|
||||||
|
function<void>::allocator< counting_allocator<int> >::type fv;
|
||||||
|
alloc_count = 0;
|
||||||
|
dealloc_count = 0;
|
||||||
|
fv = DoNothing();
|
||||||
|
fv.clear();
|
||||||
|
BOOST_TEST(alloc_count == 1);
|
||||||
|
BOOST_TEST(dealloc_count == 1);
|
||||||
|
|
||||||
|
alloc_count = 0;
|
||||||
|
dealloc_count = 0;
|
||||||
|
fv = &do_nothing;
|
||||||
|
fv.clear();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
25
test/defarg_test.cpp
Normal file
25
test/defarg_test.cpp
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#define BOOST_INCLUDE_MAIN
|
||||||
|
#include <boost/test/test_tools.hpp>
|
||||||
|
#include <boost/function.hpp>
|
||||||
|
#include <functional>
|
||||||
|
#include <cassert>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace boost;
|
||||||
|
|
||||||
|
static int sub_ints(int x = 0, int y = 0, int z = 0) { return x-y-z; }
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_two_args()
|
||||||
|
{
|
||||||
|
function<int, int, int> sub(&sub_ints);
|
||||||
|
BOOST_TEST(sub(10, 2) == 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
test_main(int, char* [])
|
||||||
|
{
|
||||||
|
test_two_args();
|
||||||
|
return 0;
|
||||||
|
}
|
550
test/function_n_test.cpp
Normal file
550
test/function_n_test.cpp
Normal file
@ -0,0 +1,550 @@
|
|||||||
|
#define BOOST_INCLUDE_MAIN
|
||||||
|
#include <boost/test/test_tools.hpp>
|
||||||
|
#include <boost/function.hpp>
|
||||||
|
#include <functional>
|
||||||
|
#include <cassert>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using namespace boost;
|
||||||
|
using std::string;
|
||||||
|
using std::negate;
|
||||||
|
|
||||||
|
static int global_int;
|
||||||
|
|
||||||
|
struct write_five_obj { void operator()() const { global_int = 5; } };
|
||||||
|
struct write_three_obj { int operator()() const { global_int = 3; return 7; }};
|
||||||
|
static void write_five() { global_int = 5; }
|
||||||
|
static void write_three() { global_int = 3; }
|
||||||
|
struct generate_five_obj { int operator()() const { return 5; } };
|
||||||
|
struct generate_three_obj { int operator()() const { return 3; } };
|
||||||
|
static int generate_five() { return 5; }
|
||||||
|
static int generate_three() { return 3; }
|
||||||
|
static string identity_str(const string& s) { return s; }
|
||||||
|
static string string_cat(const string& s1, const string& s2) { return s1+s2; }
|
||||||
|
static int sum_ints(int x, int y) { return x+y; }
|
||||||
|
|
||||||
|
struct write_const_1_nonconst_2
|
||||||
|
{
|
||||||
|
void operator()() { global_int = 2; }
|
||||||
|
void operator()() const { global_int = 1; }
|
||||||
|
};
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_zero_args()
|
||||||
|
{
|
||||||
|
typedef function0<void> func_void_type;
|
||||||
|
|
||||||
|
write_five_obj five;
|
||||||
|
write_three_obj three;
|
||||||
|
|
||||||
|
// Default construction
|
||||||
|
func_void_type v1;
|
||||||
|
BOOST_TEST(v1.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function
|
||||||
|
v1 = five;
|
||||||
|
BOOST_TEST(!v1.empty());
|
||||||
|
|
||||||
|
// Invocation of a function
|
||||||
|
global_int = 0;
|
||||||
|
v1();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// clear() method
|
||||||
|
v1.clear();
|
||||||
|
BOOST_TEST(v1.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function
|
||||||
|
v1 = three;
|
||||||
|
BOOST_TEST(!v1.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v1();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function
|
||||||
|
v1 = five;
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v1();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// clear()
|
||||||
|
v1.clear();
|
||||||
|
BOOST_TEST(v1.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function from a free function
|
||||||
|
v1 = &write_five;
|
||||||
|
BOOST_TEST(!v1.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v1();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function from a free function
|
||||||
|
v1 = &write_three;
|
||||||
|
BOOST_TEST(!v1.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v1();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment
|
||||||
|
v1 = five;
|
||||||
|
BOOST_TEST(!v1.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v1();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function from a free function
|
||||||
|
v1 = &write_three;
|
||||||
|
BOOST_TEST(!v1.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v1();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Construction from another function (that is empty)
|
||||||
|
v1.clear();
|
||||||
|
func_void_type v2(v1);;
|
||||||
|
BOOST_TEST(!v2);
|
||||||
|
|
||||||
|
// Assignment to an empty function
|
||||||
|
v2 = three;
|
||||||
|
BOOST_TEST(!v2.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v2();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function
|
||||||
|
v2.set(five);
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v2();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
v2.clear();
|
||||||
|
BOOST_TEST(v2.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function from a free function
|
||||||
|
v2.set(&write_five);
|
||||||
|
BOOST_TEST(v2);
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v2();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function from a free function
|
||||||
|
v2 = &write_three;
|
||||||
|
BOOST_TEST(!v2.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v2();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Swapping
|
||||||
|
v1 = five;
|
||||||
|
swap(v1, v2);
|
||||||
|
v2();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
v1();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
swap(v1, v2);
|
||||||
|
v1.clear();
|
||||||
|
|
||||||
|
// Assignment
|
||||||
|
v2 = five;
|
||||||
|
BOOST_TEST(!v2.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v2();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function from a free function
|
||||||
|
v2 = &write_three;
|
||||||
|
BOOST_TEST(!v2.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v2();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment to a function from an empty function
|
||||||
|
v2 = v1;
|
||||||
|
BOOST_TEST(v2.empty());
|
||||||
|
|
||||||
|
// Assignment to a function from a function with a functor
|
||||||
|
v1 = three;
|
||||||
|
v2 = v1;
|
||||||
|
BOOST_TEST(!v1.empty());
|
||||||
|
BOOST_TEST(!v2.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v1();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
global_int = 0;
|
||||||
|
v2();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assign to a function from a function with a function
|
||||||
|
v2 = &write_five;
|
||||||
|
v1 = v2;
|
||||||
|
BOOST_TEST(!v1.empty());
|
||||||
|
BOOST_TEST(!v2.empty());
|
||||||
|
global_int = 0;
|
||||||
|
v1();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
global_int = 0;
|
||||||
|
v2();
|
||||||
|
BOOST_TEST(global_int == 5);;
|
||||||
|
|
||||||
|
// Construct a function given another function containing a function
|
||||||
|
func_void_type v3(v1);;
|
||||||
|
|
||||||
|
// Invocation of a function
|
||||||
|
global_int = 0;
|
||||||
|
v3();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// clear() method
|
||||||
|
v3.clear();
|
||||||
|
BOOST_TEST(!v3);
|
||||||
|
|
||||||
|
// Assignment to an empty function
|
||||||
|
v3 = three;
|
||||||
|
BOOST_TEST(!v3.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v3();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function
|
||||||
|
v3 = five;
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v3();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// clear()
|
||||||
|
v3.clear();
|
||||||
|
BOOST_TEST(v3.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function from a free function
|
||||||
|
v3 = &write_five;
|
||||||
|
BOOST_TEST(!v3.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v3();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function from a free function
|
||||||
|
v3 = &write_three;
|
||||||
|
BOOST_TEST(!v3.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v3();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment
|
||||||
|
v3 = five;
|
||||||
|
BOOST_TEST(!v3.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v3();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Construction of a function from a function containing a functor
|
||||||
|
func_void_type v4(v3);
|
||||||
|
|
||||||
|
// Invocation of a function
|
||||||
|
global_int = 0;
|
||||||
|
v4();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// clear() method
|
||||||
|
v4.clear();
|
||||||
|
BOOST_TEST(v4.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function
|
||||||
|
v4 = three;
|
||||||
|
BOOST_TEST(!v4.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v4();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function
|
||||||
|
v4 = five;
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v4();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// clear()
|
||||||
|
v4.clear();
|
||||||
|
BOOST_TEST(v4.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function from a free function
|
||||||
|
v4 = &write_five;
|
||||||
|
BOOST_TEST(!v4.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v4();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function from a free function
|
||||||
|
v4 = &write_three;
|
||||||
|
BOOST_TEST(!v4.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v4();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment
|
||||||
|
v4 = five;
|
||||||
|
BOOST_TEST(!v4.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v4();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Construction of a function from a functor
|
||||||
|
func_void_type v5(five);
|
||||||
|
|
||||||
|
// Invocation of a function
|
||||||
|
global_int = 0;
|
||||||
|
v5();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// clear() method
|
||||||
|
v5.clear();
|
||||||
|
BOOST_TEST(v5.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function
|
||||||
|
v5 = three;
|
||||||
|
BOOST_TEST(!v5.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v5();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function
|
||||||
|
v5 = five;
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v5();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// clear()
|
||||||
|
v5.clear();
|
||||||
|
BOOST_TEST(v5.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function from a free function
|
||||||
|
v5 = &write_five;
|
||||||
|
BOOST_TEST(!v5.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v5();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function from a free function
|
||||||
|
v5 = &write_three;
|
||||||
|
BOOST_TEST(!v5.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v5();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment
|
||||||
|
v5 = five;
|
||||||
|
BOOST_TEST(!v5.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v5();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Construction of a function from a function
|
||||||
|
func_void_type v6(&write_five);
|
||||||
|
|
||||||
|
// Invocation of a function
|
||||||
|
global_int = 0;
|
||||||
|
v6();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// clear() method
|
||||||
|
v6.clear();
|
||||||
|
BOOST_TEST(v6.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function
|
||||||
|
v6 = three;
|
||||||
|
BOOST_TEST(!v6.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v6();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function
|
||||||
|
v6 = five;
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v6();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// clear()
|
||||||
|
v6.clear();
|
||||||
|
BOOST_TEST(v6.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function from a free function
|
||||||
|
v6 = &write_five;
|
||||||
|
BOOST_TEST(!v6.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v6();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function from a free function
|
||||||
|
v6 = &write_three;
|
||||||
|
BOOST_TEST(!v6.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v6();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment
|
||||||
|
v6 = five;
|
||||||
|
BOOST_TEST(!v6.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v6();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Const vs. non-const
|
||||||
|
write_const_1_nonconst_2 one_or_two;
|
||||||
|
const function0<void> v7(one_or_two);
|
||||||
|
function <void> v8(one_or_two);
|
||||||
|
|
||||||
|
global_int = 0;
|
||||||
|
v7();
|
||||||
|
BOOST_TEST(global_int == 1);
|
||||||
|
|
||||||
|
global_int = 0;
|
||||||
|
v8();
|
||||||
|
BOOST_TEST(global_int == 2);
|
||||||
|
|
||||||
|
// Test return values
|
||||||
|
typedef function0<int> func_int_type;
|
||||||
|
generate_five_obj gen_five;
|
||||||
|
generate_three_obj gen_three;
|
||||||
|
|
||||||
|
func_int_type i0(gen_five);
|
||||||
|
|
||||||
|
BOOST_TEST(i0() == 5);
|
||||||
|
i0 = gen_three;
|
||||||
|
BOOST_TEST(i0() == 3);
|
||||||
|
i0 = &generate_five;
|
||||||
|
BOOST_TEST(i0() == 5);
|
||||||
|
i0 = &generate_three;
|
||||||
|
BOOST_TEST(i0() == 3);
|
||||||
|
BOOST_TEST(i0);
|
||||||
|
i0.clear();
|
||||||
|
BOOST_TEST(!i0);
|
||||||
|
|
||||||
|
// Test return values with compatible types
|
||||||
|
typedef function0<long> func_long_type;
|
||||||
|
func_long_type i1(gen_five);
|
||||||
|
|
||||||
|
BOOST_TEST(i1() == 5);
|
||||||
|
i1 = gen_three;
|
||||||
|
BOOST_TEST(i1() == 3);
|
||||||
|
i1 = &generate_five;
|
||||||
|
BOOST_TEST(i1() == 5);
|
||||||
|
i1 = &generate_three;
|
||||||
|
BOOST_TEST(i1() == 3);
|
||||||
|
BOOST_TEST(i1);
|
||||||
|
i1.clear();
|
||||||
|
BOOST_TEST(!i1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_one_arg()
|
||||||
|
{
|
||||||
|
negate<int> neg;
|
||||||
|
|
||||||
|
function1<int, int> f1(neg);
|
||||||
|
BOOST_TEST(f1(5) == -5);
|
||||||
|
|
||||||
|
function1<string, string> id(&identity_str);
|
||||||
|
BOOST_TEST(id("str") == "str");
|
||||||
|
|
||||||
|
function1<std::string, char*> id2(&identity_str);
|
||||||
|
BOOST_TEST(id2("foo") == "foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_two_args()
|
||||||
|
{
|
||||||
|
function2<string, const string&, const string&> cat(&string_cat);
|
||||||
|
BOOST_TEST(cat("str", "ing") == "string");
|
||||||
|
|
||||||
|
function2<int, short, short> sum(&sum_ints);
|
||||||
|
BOOST_TEST(sum(2, 3) == 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_emptiness()
|
||||||
|
{
|
||||||
|
function0<float> f1;
|
||||||
|
BOOST_TEST(f1.empty());
|
||||||
|
|
||||||
|
function0<float> f2;
|
||||||
|
f2 = f1;
|
||||||
|
BOOST_TEST(f2.empty());
|
||||||
|
|
||||||
|
function0<double> f3;
|
||||||
|
f3 = f2;;
|
||||||
|
BOOST_TEST(f3.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
test_main(int, char* [])
|
||||||
|
{
|
||||||
|
test_zero_args();
|
||||||
|
test_one_arg();
|
||||||
|
test_two_args();
|
||||||
|
test_emptiness();
|
||||||
|
return 0;
|
||||||
|
}
|
550
test/function_test.cpp
Normal file
550
test/function_test.cpp
Normal file
@ -0,0 +1,550 @@
|
|||||||
|
#define BOOST_INCLUDE_MAIN
|
||||||
|
#include <boost/test/test_tools.hpp>
|
||||||
|
#include <boost/function.hpp>
|
||||||
|
#include <functional>
|
||||||
|
#include <cassert>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using namespace boost;
|
||||||
|
using std::string;
|
||||||
|
using std::negate;
|
||||||
|
|
||||||
|
static int global_int;
|
||||||
|
|
||||||
|
struct write_five_obj { void operator()() const { global_int = 5; } };
|
||||||
|
struct write_three_obj { int operator()() const { global_int = 3; return 7; }};
|
||||||
|
static void write_five() { global_int = 5; }
|
||||||
|
static void write_three() { global_int = 3; }
|
||||||
|
struct generate_five_obj { int operator()() const { return 5; } };
|
||||||
|
struct generate_three_obj { int operator()() const { return 3; } };
|
||||||
|
static int generate_five() { return 5; }
|
||||||
|
static int generate_three() { return 3; }
|
||||||
|
static string identity_str(const string& s) { return s; }
|
||||||
|
static string string_cat(const string& s1, const string& s2) { return s1+s2; }
|
||||||
|
static int sum_ints(int x, int y) { return x+y; }
|
||||||
|
|
||||||
|
struct write_const_1_nonconst_2
|
||||||
|
{
|
||||||
|
void operator()() { global_int = 2; }
|
||||||
|
void operator()() const { global_int = 1; }
|
||||||
|
};
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_zero_args()
|
||||||
|
{
|
||||||
|
typedef function<void> func_void_type;
|
||||||
|
|
||||||
|
write_five_obj five;
|
||||||
|
write_three_obj three;
|
||||||
|
|
||||||
|
// Default construction
|
||||||
|
func_void_type v1;
|
||||||
|
BOOST_TEST(v1.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function
|
||||||
|
v1 = five;
|
||||||
|
BOOST_TEST(!v1.empty());
|
||||||
|
|
||||||
|
// Invocation of a function
|
||||||
|
global_int = 0;
|
||||||
|
v1();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// clear() method
|
||||||
|
v1.clear();
|
||||||
|
BOOST_TEST(v1.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function
|
||||||
|
v1 = three;
|
||||||
|
BOOST_TEST(!v1.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v1();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function
|
||||||
|
v1 = five;
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v1();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// clear()
|
||||||
|
v1.clear();
|
||||||
|
BOOST_TEST(v1.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function from a free function
|
||||||
|
v1 = &write_five;
|
||||||
|
BOOST_TEST(!v1.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v1();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function from a free function
|
||||||
|
v1 = &write_three;
|
||||||
|
BOOST_TEST(!v1.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v1();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment
|
||||||
|
v1 = five;
|
||||||
|
BOOST_TEST(!v1.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v1();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function from a free function
|
||||||
|
v1 = &write_three;
|
||||||
|
BOOST_TEST(!v1.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v1();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Construction from another function (that is empty)
|
||||||
|
v1.clear();
|
||||||
|
func_void_type v2(v1);;
|
||||||
|
BOOST_TEST(!v2);
|
||||||
|
|
||||||
|
// Assignment to an empty function
|
||||||
|
v2 = three;
|
||||||
|
BOOST_TEST(!v2.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v2();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function
|
||||||
|
v2.set(five);
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v2();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
v2.clear();
|
||||||
|
BOOST_TEST(v2.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function from a free function
|
||||||
|
v2.set(&write_five);
|
||||||
|
BOOST_TEST(v2);
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v2();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function from a free function
|
||||||
|
v2 = &write_three;
|
||||||
|
BOOST_TEST(!v2.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v2();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Swapping
|
||||||
|
v1 = five;
|
||||||
|
swap(v1, v2);
|
||||||
|
v2();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
v1();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
swap(v1, v2);
|
||||||
|
v1.clear();
|
||||||
|
|
||||||
|
// Assignment
|
||||||
|
v2 = five;
|
||||||
|
BOOST_TEST(!v2.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v2();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function from a free function
|
||||||
|
v2 = &write_three;
|
||||||
|
BOOST_TEST(!v2.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v2();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment to a function from an empty function
|
||||||
|
v2 = v1;
|
||||||
|
BOOST_TEST(v2.empty());
|
||||||
|
|
||||||
|
// Assignment to a function from a function with a functor
|
||||||
|
v1 = three;
|
||||||
|
v2 = v1;
|
||||||
|
BOOST_TEST(!v1.empty());
|
||||||
|
BOOST_TEST(!v2.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v1();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
global_int = 0;
|
||||||
|
v2();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assign to a function from a function with a function
|
||||||
|
v2 = &write_five;
|
||||||
|
v1 = v2;
|
||||||
|
BOOST_TEST(!v1.empty());
|
||||||
|
BOOST_TEST(!v2.empty());
|
||||||
|
global_int = 0;
|
||||||
|
v1();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
global_int = 0;
|
||||||
|
v2();
|
||||||
|
BOOST_TEST(global_int == 5);;
|
||||||
|
|
||||||
|
// Construct a function given another function containing a function
|
||||||
|
func_void_type v3(v1);;
|
||||||
|
|
||||||
|
// Invocation of a function
|
||||||
|
global_int = 0;
|
||||||
|
v3();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// clear() method
|
||||||
|
v3.clear();
|
||||||
|
BOOST_TEST(!v3);
|
||||||
|
|
||||||
|
// Assignment to an empty function
|
||||||
|
v3 = three;
|
||||||
|
BOOST_TEST(!v3.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v3();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function
|
||||||
|
v3 = five;
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v3();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// clear()
|
||||||
|
v3.clear();
|
||||||
|
BOOST_TEST(v3.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function from a free function
|
||||||
|
v3 = &write_five;
|
||||||
|
BOOST_TEST(!v3.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v3();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function from a free function
|
||||||
|
v3 = &write_three;
|
||||||
|
BOOST_TEST(!v3.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v3();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment
|
||||||
|
v3 = five;
|
||||||
|
BOOST_TEST(!v3.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v3();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Construction of a function from a function containing a functor
|
||||||
|
func_void_type v4(v3);
|
||||||
|
|
||||||
|
// Invocation of a function
|
||||||
|
global_int = 0;
|
||||||
|
v4();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// clear() method
|
||||||
|
v4.clear();
|
||||||
|
BOOST_TEST(v4.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function
|
||||||
|
v4 = three;
|
||||||
|
BOOST_TEST(!v4.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v4();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function
|
||||||
|
v4 = five;
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v4();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// clear()
|
||||||
|
v4.clear();
|
||||||
|
BOOST_TEST(v4.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function from a free function
|
||||||
|
v4 = &write_five;
|
||||||
|
BOOST_TEST(!v4.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v4();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function from a free function
|
||||||
|
v4 = &write_three;
|
||||||
|
BOOST_TEST(!v4.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v4();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment
|
||||||
|
v4 = five;
|
||||||
|
BOOST_TEST(!v4.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v4();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Construction of a function from a functor
|
||||||
|
func_void_type v5(five);
|
||||||
|
|
||||||
|
// Invocation of a function
|
||||||
|
global_int = 0;
|
||||||
|
v5();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// clear() method
|
||||||
|
v5.clear();
|
||||||
|
BOOST_TEST(v5.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function
|
||||||
|
v5 = three;
|
||||||
|
BOOST_TEST(!v5.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v5();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function
|
||||||
|
v5 = five;
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v5();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// clear()
|
||||||
|
v5.clear();
|
||||||
|
BOOST_TEST(v5.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function from a free function
|
||||||
|
v5 = &write_five;
|
||||||
|
BOOST_TEST(!v5.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v5();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function from a free function
|
||||||
|
v5 = &write_three;
|
||||||
|
BOOST_TEST(!v5.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v5();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment
|
||||||
|
v5 = five;
|
||||||
|
BOOST_TEST(!v5.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v5();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Construction of a function from a function
|
||||||
|
func_void_type v6(&write_five);
|
||||||
|
|
||||||
|
// Invocation of a function
|
||||||
|
global_int = 0;
|
||||||
|
v6();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// clear() method
|
||||||
|
v6.clear();
|
||||||
|
BOOST_TEST(v6.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function
|
||||||
|
v6 = three;
|
||||||
|
BOOST_TEST(!v6.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v6();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function
|
||||||
|
v6 = five;
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v6();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// clear()
|
||||||
|
v6.clear();
|
||||||
|
BOOST_TEST(v6.empty());
|
||||||
|
|
||||||
|
// Assignment to an empty function from a free function
|
||||||
|
v6 = &write_five;
|
||||||
|
BOOST_TEST(!v6.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v6();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Assignment to a non-empty function from a free function
|
||||||
|
v6 = &write_three;
|
||||||
|
BOOST_TEST(!v6.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v6();
|
||||||
|
BOOST_TEST(global_int == 3);
|
||||||
|
|
||||||
|
// Assignment
|
||||||
|
v6 = five;
|
||||||
|
BOOST_TEST(!v6.empty());
|
||||||
|
|
||||||
|
// Invocation
|
||||||
|
global_int = 0;
|
||||||
|
v6();
|
||||||
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
|
// Const vs. non-const
|
||||||
|
write_const_1_nonconst_2 one_or_two;
|
||||||
|
const function<void> v7(one_or_two);
|
||||||
|
function <void> v8(one_or_two);
|
||||||
|
|
||||||
|
global_int = 0;
|
||||||
|
v7();
|
||||||
|
BOOST_TEST(global_int == 1);
|
||||||
|
|
||||||
|
global_int = 0;
|
||||||
|
v8();
|
||||||
|
BOOST_TEST(global_int == 2);
|
||||||
|
|
||||||
|
// Test return values
|
||||||
|
typedef function<int> func_int_type;
|
||||||
|
generate_five_obj gen_five;
|
||||||
|
generate_three_obj gen_three;
|
||||||
|
|
||||||
|
func_int_type i0(gen_five);
|
||||||
|
|
||||||
|
BOOST_TEST(i0() == 5);
|
||||||
|
i0 = gen_three;
|
||||||
|
BOOST_TEST(i0() == 3);
|
||||||
|
i0 = &generate_five;
|
||||||
|
BOOST_TEST(i0() == 5);
|
||||||
|
i0 = &generate_three;
|
||||||
|
BOOST_TEST(i0() == 3);
|
||||||
|
BOOST_TEST(i0);
|
||||||
|
i0.clear();
|
||||||
|
BOOST_TEST(!i0);
|
||||||
|
|
||||||
|
// Test return values with compatible types
|
||||||
|
typedef function<long> func_long_type;
|
||||||
|
func_long_type i1(gen_five);
|
||||||
|
|
||||||
|
BOOST_TEST(i1() == 5);
|
||||||
|
i1 = gen_three;
|
||||||
|
BOOST_TEST(i1() == 3);
|
||||||
|
i1 = &generate_five;
|
||||||
|
BOOST_TEST(i1() == 5);
|
||||||
|
i1 = &generate_three;
|
||||||
|
BOOST_TEST(i1() == 3);
|
||||||
|
BOOST_TEST(i1);
|
||||||
|
i1.clear();
|
||||||
|
BOOST_TEST(!i1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_one_arg()
|
||||||
|
{
|
||||||
|
negate<int> neg;
|
||||||
|
|
||||||
|
function<int, int> f1(neg);
|
||||||
|
BOOST_TEST(f1(5) == -5);
|
||||||
|
|
||||||
|
function<string, string> id(&identity_str);
|
||||||
|
BOOST_TEST(id("str") == "str");
|
||||||
|
|
||||||
|
function<std::string, char*> id2(&identity_str);
|
||||||
|
BOOST_TEST(id2("foo") == "foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_two_args()
|
||||||
|
{
|
||||||
|
function<string, const string&, const string&> cat(&string_cat);
|
||||||
|
BOOST_TEST(cat("str", "ing") == "string");
|
||||||
|
|
||||||
|
function<int, short, short> sum(&sum_ints);
|
||||||
|
BOOST_TEST(sum(2, 3) == 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_emptiness()
|
||||||
|
{
|
||||||
|
function<float> f1;
|
||||||
|
BOOST_TEST(f1.empty());
|
||||||
|
|
||||||
|
function<float> f2;
|
||||||
|
f2 = f1;
|
||||||
|
BOOST_TEST(f2.empty());
|
||||||
|
|
||||||
|
function<double> f3;
|
||||||
|
f3 = f2;;
|
||||||
|
BOOST_TEST(f3.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
test_main(int, char* [])
|
||||||
|
{
|
||||||
|
test_zero_args();
|
||||||
|
test_one_arg();
|
||||||
|
test_two_args();
|
||||||
|
test_emptiness();
|
||||||
|
return 0;
|
||||||
|
}
|
20
test/function_test_fail1.cpp
Normal file
20
test/function_test_fail1.cpp
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#define BOOST_INCLUDE_MAIN
|
||||||
|
#include <boost/test/test_tools.hpp>
|
||||||
|
#include <boost/function.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace boost;
|
||||||
|
|
||||||
|
int
|
||||||
|
test_main(int, char*[])
|
||||||
|
{
|
||||||
|
function<int> f1;
|
||||||
|
function<int> f2;
|
||||||
|
|
||||||
|
if (f1 == f2) {
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_CRITICAL_ERROR("This should not have compiled.");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
19
test/function_test_fail2.cpp
Normal file
19
test/function_test_fail2.cpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#define BOOST_INCLUDE_MAIN
|
||||||
|
#include <boost/test/test_tools.hpp>
|
||||||
|
#include <boost/function.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace boost;
|
||||||
|
|
||||||
|
static int bad_fn(float f) { return static_cast<int>(f); }
|
||||||
|
|
||||||
|
int
|
||||||
|
test_main(int, char*[])
|
||||||
|
{
|
||||||
|
function<int> f1;
|
||||||
|
f1 = bad_fn;
|
||||||
|
|
||||||
|
BOOST_CRITICAL_ERROR("This should not have compiled.");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
25
test/mixin_test.cpp
Normal file
25
test/mixin_test.cpp
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#define BOOST_INCLUDE_MAIN
|
||||||
|
#include <boost/test/test_tools.hpp>
|
||||||
|
#include <cassert>
|
||||||
|
#include <functional>
|
||||||
|
#include <boost/function.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace boost;
|
||||||
|
|
||||||
|
struct id_mixin
|
||||||
|
{
|
||||||
|
int id;
|
||||||
|
};
|
||||||
|
|
||||||
|
int
|
||||||
|
test_main(int, char*[])
|
||||||
|
{
|
||||||
|
function<int, int, int>::mixin<id_mixin>::type f;
|
||||||
|
f = plus<int>();
|
||||||
|
f.id = 7;
|
||||||
|
f.clear();
|
||||||
|
BOOST_TEST(f.id == 7);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
32
test/policy_test.cpp
Normal file
32
test/policy_test.cpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#define BOOST_INCLUDE_MAIN
|
||||||
|
#include <boost/test/test_tools.hpp>
|
||||||
|
#include <cassert>
|
||||||
|
#include <iostream>
|
||||||
|
#include <functional>
|
||||||
|
#include <boost/function.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace boost;
|
||||||
|
|
||||||
|
struct counting_policy
|
||||||
|
{
|
||||||
|
static int count;
|
||||||
|
|
||||||
|
void precall(function_base*) { count++; }
|
||||||
|
void postcall(function_base*) { count+=2; }
|
||||||
|
};
|
||||||
|
|
||||||
|
int counting_policy::count = 0;
|
||||||
|
|
||||||
|
int
|
||||||
|
test_main(int, char*[])
|
||||||
|
{
|
||||||
|
function<int, int, int>::policy<counting_policy>::type f;
|
||||||
|
|
||||||
|
f = plus<int>();
|
||||||
|
|
||||||
|
BOOST_TEST(5 == f(2,3));
|
||||||
|
BOOST_TEST(counting_policy::count==3);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Reference in New Issue
Block a user