From 90433660f95b84b245640808b80638e93b72d9c9 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 4 Sep 2001 17:16:30 +0000 Subject: [PATCH] Initial commit [SVN r11022] --- bind.html | 721 ++++++++++++++++++++++ bind_as_compose.cpp | 75 +++ bind_test.cpp | 269 ++++++++ bind_test_1.cpp | 104 ++++ bind_test_2.cpp | 65 ++ bind_test_3.cpp | 161 +++++ bind_test_4.cpp | 89 +++ include/boost/bind.hpp | 1255 ++++++++++++++++++++++++++++++++++++++ include/boost/mem_fn.hpp | 704 +++++++++++++++++++++ mem_fn.html | 282 +++++++++ mem_fn_test.cpp | 175 ++++++ 11 files changed, 3900 insertions(+) create mode 100644 bind.html create mode 100644 bind_as_compose.cpp create mode 100644 bind_test.cpp create mode 100644 bind_test_1.cpp create mode 100644 bind_test_2.cpp create mode 100644 bind_test_3.cpp create mode 100644 bind_test_4.cpp create mode 100644 include/boost/bind.hpp create mode 100644 include/boost/mem_fn.hpp create mode 100644 mem_fn.html create mode 100644 mem_fn_test.cpp diff --git a/bind.html b/bind.html new file mode 100644 index 0000000..0fdb19a --- /dev/null +++ b/bind.html @@ -0,0 +1,721 @@ + + + + + + + Boost: bind.hpp documentation + + + + + + + + +
+ + + + + + + + + +
+ c++boost.gif (8819 bytes) + + + + +

bind.hpp

 1.01.0001 (2001-09-02)
+
 
+ + + + +
+ +

Files

+ + +

Purpose

+ +

+boost::bind is a generalization of the standard functions +std::bind1st and std::bind2nd. It supports arbitrary function +objects, functions, function pointers, and member function pointers, and is able +to bind any argument to a specific value or route input arguments into arbitrary +positions. bind does not place any requirements on the function object; +in particular, it does not need the result_type, +first_argument_type and second_argument_type standard typedefs. +

+ +

Using bind with functions and function pointers

+ +

+Given these definitions: +

+ +
+int f(int a, int b)
+{
+    return a + b;
+}
+
+int g(int a, int b, int c)
+{
+    return a + b + c;
+}
+
+ +

+bind(f, 1, 2) will produce a "nullary" function object that +takes no arguments and returns f(1, 2). Similarly, +bind(g, 1, 2, 3)() is equivalent to g(1, 2, 3). +

+ +

+It is possible to selectively bind only some of the arguments. +bind(f, _1, 5)(x) is equivalent to f(x, 5); here +_1 is a placeholder argument that means "substitute with the first +input argument." +

+For comparison, here is the same operation expressed with the standard +library primitives: +

+ +
+std::bind1st(std::ptr_fun(f), 5)(x);
+
+ +

+bind covers the functionality of std::bind2nd as well: +

+ +
+std::bind2nd(std::ptr_fun(f), 5)(x);   // f(5, x)
+bind(f, 5, _1)(x);                     // f(5, x)
+
+ +

+bind can handle functions with more than two arguments, and its +argument substitution mechanism is more general: +

+ +
+bind(f, _2, _1)(x, y);                 // f(y, x)
+
+bind(g, _1, 9, _1)(x);                 // g(x, 9, x)
+
+bind(g, _3, _3, _3)(x, y, z);          // g(z, z, z)
+
+bind(g, _1, _1, _1)(x, y, z);          // g(x, x, x)
+
+ +

+The arguments that bind takes are copied and held internally by +the returned function object. For example, in the following code: +

+ +
+int i = 5;
+
+bind(f, i, _1);
+
+ +

+a copy of the value of i is stored into the function object. +boost::ref and boost::cref +can be used to make the function object store a reference to an object, +rather than a copy: +

+ +
+int i = 5;
+
+bind(f, ref(i), _1);
+
+ +

Using bind with function objects

+ +

+Any function object can be passed as a first argument to bind, but the +syntax is a bit different. The return type of the generated function object's +operator() has to be specified explicitly (without a typeof +operator the return type cannot be inferred in the general case): +

+ +
+struct F
+{
+    int operator()(int a, int b) { return a - b; }
+    bool operator()(long a, long b) { return a == b; }
+};
+
+F f;
+
+int x = 104;
+
+bind<int>(f, _1, _1)(x);               // f(x, x), i.e. zero
+
+ +

+[Note: when, hopefully, + +function template default arguments become part of C++, +bind will no longer require the explicit specification of the return type +when the function object defines result_type.] +

+ +

Using bind with member function pointers

+ +

+Pointers to member functions are not function objects, because they do not +support operator(). For convenience, bind accepts member function +pointers as its first argument, and the behavior is as if +boost::mem_fn has been used to convert the member +function pointer into a function object. In other words, the expression +

+ +
+bind(&X::f, args)
+
+ +

+is equivalent to +

+ +
+bind<R>(mem_fn(&X::f), args)
+
+ +

+where R is the return type of X::f. +

+

+[Note: mem_fn creates +function objects that are able to accept a pointer, a reference, or a smart +pointer to an object as its first argument; for additional information, see +the mem_fn documentation.] +

+ +

+Example: +

+ +
+struct X
+{
+    bool f(int a);
+};
+
+X x;
+
+shared_ptr<X> p(new X);
+
+int i = 5;
+
+bind(&X::f, ref(x), _1)(i);            // x.f(i)
+
+bind(&X::f, &x, _1)(i);                // (&x)->f(i)
+
+bind(&X::f, x, _1)(i);                 // (internal copy of x).f(i)
+
+bind(&X::f, p, _1)(i);                 // (internal copy of p)->f(i)
+
+ +

+The last two examples are interesting in that they produce "self-contained" +function objects. bind(&X::f, x, _1) stores a copy of x. +bind(&X::f, p, _1) stores a copy of p, and since p +is a +boost::shared_ptr, +the function object retains a reference to its instance of X and will +remain valid even when p goes out of scope or is reset(). +

+ +

Using nested binds for function composition

+ +

+Some of the arguments passed to bind may be nested bind expressions +themselves: +

+ +
+bind(f, bind(g, _1))(x);               // f(g(x))
+
+ +

+The nested subexpressions are evaluated when the function object is called. This +feature of bind can be used to perform function composition. +

+ +

+See bind_as_compose.cpp for an example that +demonstrates how to use bind to achieve similar functionality to +Boost.Compose. +

+ +

+Note that the first argument - the bound function object - is an +exception to the nesting rules. A nested bind expression passed +to bind as a first argument is not treated differently from +any other function object: +

+ +
+int x = 4;
+
+template<class F> void test(F f)
+{
+    bind(f, 5)(x);
+}
+
+int g(int, int);
+
+int main()
+{
+    test(bind(g, _1, 8));              // g(5, 8) and not g(x, 8)(5)
+}
+
+ +

Example: using bind with standard algorithms

+ +
+class image;
+
+class animation
+{
+public:
+
+    void advance(int ms);
+    bool inactive() const;
+    void render(image & target) const;
+};
+
+std::vector<animation> anims;
+
+template<class C, class P> void erase_if(C & c, P pred)
+{
+    c.erase(std::remove_if(c.begin(), c.end(), pred), c.end());
+}
+
+void update(int ms)
+{
+    std::for_each(anims.begin(), anims.end(), boost::bind(&animation::advance, _1, ms));
+    erase_if(anims, boost::mem_fn(&animation::inactive));
+}
+
+void render(image & target)
+{
+    std::for_each(anims.begin(), anims.end(), boost::bind(&animation::render, _1, boost::ref(target)));
+}
+
+ +

Example: using bind with Boost.Function

+ +
+class button
+{
+public:
+
+    boost::function<void> onClick;
+};
+
+class player
+{
+public:
+
+    void play();
+    void stop();
+};
+
+button playButton, stopButton;
+player thePlayer;
+
+void connect()
+{
+    playButton.onClick = boost::bind(&player::play, &thePlayer);
+    stopButton.onClick = boost::bind(&player::stop, &thePlayer);
+}
+
+ +

Limitations

+ +

+The function objects generated by bind take their arguments by +reference and cannot, therefore, accept non-const temporaries or literal +constants. This is an inherent limitation of the C++ language, known as the +"forwarding function problem." +

+ +

+The library uses signatures of the form +

+ +
+template<class T> void f(T & t);
+
+ +

+to accept arguments of arbitrary types and pass them on unmodified. As noted, +this does not work with non-const r-values. +

+ +

+An oft-proposed "solution" to this problem is to add an overload: +

+ +
+template<class T> void f(T & t);
+template<class T> void f(T const & t);
+
+ +

+Unfortunately, this (a) requires providing 512 overloads for nine arguments +and (b) does not actually work for const arguments, both l- and r-values, +since the two templates produce the exact same signature and cannot be +partially ordered. +

+ +

+[Note: this is a dark corner of the language, and the + +corresponding issue has not been resolved yet.] +

+ +

Interface

+ +

Synopsis

+ +
+namespace boost
+{
+
+// no arguments
+
+template<class R, class F> implementation-defined-1 bind(F f);
+
+template<class R> implementation-defined-2 bind(R (*f) ());
+
+// one argument
+
+template<class R, class F, class A1> implementation-defined-3 bind(F f, A1 a1);
+
+template<class R, class B1, class A1> implementation-defined-4 bind(R (*f) (B1), A1 a1);
+
+template<class R, class T, class A1> implementation-defined-5 bind(R (T::*f) (), A1 a1);
+
+template<class R, class T, class A1> implementation-defined-6 bind(R (T::*f) () const, A1 a1);
+
+// two arguments
+
+template<class R, class F, class A1, class A2> implementation-defined-7 bind(F f, A1 a1, A2 a2);
+
+template<class R, class B1, class B2, class A1, class A2> implementation-defined-8 bind(R (*f) (B1, B2), A1 a1, A2 a2);
+
+template<class R, class T, class B1, class A1, class A2> implementation-defined-9 bind(R (T::*f) (B1), A1 a1, A2 a2);
+
+template<class R, class T, class B1, class A1, class A2> implementation-defined-10 bind(R (T::*f) (B1) const, A1 a1, A2 a2);
+
+// implementation defined number of additional overloads for more arguments
+
+}
+
+namespace
+{
+
+implementation-defined-placeholder-type-1 _1;
+
+implementation-defined-placeholder-type-2 _2;
+
+implementation-defined-placeholder-type-3 _3;
+
+// implementation defined number of additional placeholder definitions
+
+}
+
+ +

Common requirements

+ +

+All implementation-defined-N types returned by bind are +CopyConstructible. implementation-defined-N::result_type is defined as +the return type of implementation-defined-N::operator(). +

+ +

+All implementation-defined-placeholder-N types are +CopyConstructible. Their copy constructors do not throw exceptions. +

+ +

Common definitions

+ +

+The function µ(x, v1, v2, ..., vm), where m is a nonnegative integer, is defined as: +

+ +
    +
  • x.get(), when x is of type +boost::reference_wrapper<T> for some type T;
  • +
  • vk, when x is (a copy of) the placeholder _k for some positive integer k;
  • +
  • x(v1, v2, ..., vm) +when x is (a copy of) a function object returned by bind;
  • +
  • x otherwise.
  • +
+ + +

bind

+ +

template<class R, class F> implementation-defined-1 bind(F f)

+ +

+Returns: a function object λ such that the expression +λ(v1, v2, ..., vm) is equivalent to f(), +implicitly converted to R. +

+

+Throws: Nothing unless the copy constructor of F throws an exception. +

+ +

template<class R> implementation-defined-2 bind(R (*f) ())

+ +

+Returns: a function object λ such that the expression +λ(v1, v2, ..., vm) is equivalent to f(). +

+

+Throws: Nothing. +

+ +

template<class R, class F, class A1> implementation-defined-3 bind(F f, A1 a1)

+ +

+Returns: a function object λ such that the expression +λ(v1, v2, ..., vm) is equivalent to +f(µ(a1, v1, v2, ..., vm)), +implicitly converted to R. +

+

+Throws: Nothing unless the copy constructors of F and A1 throw an exception. +

+ +

template<class R, class B1, class A1> implementation-defined-4 bind(R (*f) (B1), A1 a1)

+ +

+Returns: a function object λ such that the expression +λ(v1, v2, ..., vm) is equivalent to +f(µ(a1, v1, v2, ..., vm)). +

+

+Throws: Nothing unless the copy constructor of A1 throws an exception. +

+ +

template<class R, class T, class A1> implementation-defined-5 bind(R (T::*f) (), A1 a1)

+ +

+Effects: equivalent to bind<R>(boost::mem_fn(f), a1); +

+ +

template<class R, class T, class A1> implementation-defined-6 bind(R (T::*f) () const, A1 a1)

+ +

+Effects: equivalent to bind<R>(boost::mem_fn(f), a1); +

+ +

template<class R, class F, class A1, class A2> implementation-defined-7 bind(F f, A1 a1, A2 a2)

+ +

+Returns: a function object λ such that the expression +λ(v1, v2, ..., vm) is equivalent to +f(µ(a1, v1, v2, ..., vm), µ(a2, v1, v2, ..., vm)), +implicitly converted to R. +

+

+Throws: Nothing unless the copy constructors of F, A1 and A2 throw an exception. +

+ +

template<class R, class B1, class B2, class A1, class A2> implementation-defined-8 bind(R (*f) (B1, B2), A1 a1, A2 a2)

+ +

+Returns: a function object λ such that the expression +λ(v1, v2, ..., vm) is equivalent to +f(µ(a1, v1, v2, ..., vm), µ(a2, v1, v2, ..., vm)). +

+

+Throws: Nothing unless the copy constructors of A1 and A2 throw an exception. +

+ +

template<class R, class T, class B1, class A1, class A2> implementation-defined-9 bind(R (T::*f) (B1), A1 a1, A2 a2)

+ +

+Effects: equivalent to bind<R>(boost::mem_fn(f), a1, a2); +

+ +

template<class R, class T, class B1, class A1, class A2> implementation-defined-10 bind(R (T::*f) (B1) const, A1 a1, A2 a2)

+ +

+Effects: equivalent to bind<R>(boost::mem_fn(f), a1, a2); +

+ +

Implementation

+ +

+This implementation supports function objects with up to nine arguments. +This is an implementation detail, not an inherent limitation of the +design. +

+ +

Void returns

+ +

+The following C++ code: +

+ +
+void f();
+
+void g()
+{
+    return f();
+}
+
+ +

+is legal; in fact it was deliberately made legal in order to support +forwarding functions that return void. +

+ +

+Unfortunately, some compilers have not caught up with the C++ Standard yet +and do not allow void returns. This implementation of bind will not +work for function pointers, member function pointers or function objects that +return void if the compiler does not support the feature. A possible +workaround is to change the return type of the function object in question +from void to int and return a dummy value of 0. +

+ +

MSVC specific problems and workarounds

+ +

+Microsoft Visual C++ (up to version 7.0) does not fully support the +bind<R>(...) +syntax required by the library when arbitrary function objects are bound. +The first problem is that when boost::bind is brought into scope +with an using declaration: +

+ +
+using boost::bind;
+
+ +

+the syntax above does not work. Workaround: either use the qualified name, +boost::bind, or use an using directive instead: +

+ +
+using namespace boost;
+
+ +

+The second problem is that some libraries contain nested class templates +named bind (ironically, such code is often an MSVC specific +workaround.) Due to some quirks with the parser, such a class template +breaks the bind<R>(...) syntax, even when the name bind +is fully qualified. You may try to patch the library in question or contact +its author/maintainer. The other option is to define the macro +BOOST_BIND to something other than bind (before the inclusion of +<boost/bind.hpp>) and use this identifier throughout your code +wherever you'd normally use bind. +

+ +

+[Note: BOOST_BIND is not a general renaming mechanism. It is not part of the +interface, and is not guaranteed to work on other compilers, or persist between +library versions. In short, don't use it unless you absolutely have to.] +

+ +

Visitor support

+ +

+[Note: this is an experimental feature. It may evolve over time +when other libraries start to exploit it; or it may be removed altogether if +no other library needs it. It is not part of the interface.] +

+ +

+For better integration with other libraries, the function objects returned by +bind define a member function +

+ +
+template<class Visitor> void accept(Visitor & v);
+
+ +

+that applies v, as a function object, to its internal state. Using +accept is implementation-specific and not intended for end users. +

+ +

+See bind_test_4.cpp for an example. +

+ +

Acknowledgements

+ +

+Earlier efforts that have influenced the library design: +

+ + + +

+Doug Gregor suggested that a visitor mechanism would allow bind to +interoperate with a signal/slot library. +

+ +

+John Maddock fixed a MSVC-specific conflict between bind and the +type traits library. +

+ +

+Numerous improvements were suggested during the formal review period by +Ross Smith, Richard Crossley, Jens Maurer, Ed Brey, and others. Review manager +was Darin Adler. +

+ +

+The precise semantics of bind were refined in discussions with Jaakko Järvi. +

+ +




Copyright © 2001 by Peter Dimov and Multi Media +Ltd. Permission to copy, use, modify, sell and distribute this document is +granted provided this copyright notice appears in all copies. This document +is provided "as is" without express or implied warranty, and with +no claim as to its suitability for any purpose.

+ +
+ +
+ + + diff --git a/bind_as_compose.cpp b/bind_as_compose.cpp new file mode 100644 index 0000000..0997697 --- /dev/null +++ b/bind_as_compose.cpp @@ -0,0 +1,75 @@ +#if defined(_MSC_VER) && !defined(__ICL) +#pragma warning(disable: 4786) // identifier truncated in debug info +#pragma warning(disable: 4710) // function not inlined +#pragma warning(disable: 4711) // function selected for automatic inline expansion +#pragma warning(disable: 4514) // unreferenced inline removed +#endif + +// +// bind_as_compose.cpp - function composition using bind.hpp +// +// Version 1.00.0001 (2001-08-30) +// +// Copyright (c) 2001 Peter Dimov and Multi Media Ltd. +// +// Permission to copy, use, modify, sell and distribute this software +// is granted provided this copyright notice appears in all copies. +// This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. +// + +#include +#include +#include + +std::string f(std::string const & x) +{ + return "f(" + x + ")"; +} + +std::string g(std::string const & x) +{ + return "g(" + x + ")"; +} + +std::string h(std::string const & x, std::string const & y) +{ + return "h(" + x + ", " + y + ")"; +} + +std::string k() +{ + return "k()"; +} + +template void test(F f) +{ + std::cout << f("x", "y") << '\n'; +} + +int main() +{ + using namespace boost; + + // compose_f_gx + + test( bind(f, bind(g, _1)) ); + + // compose_f_hxy + + test( bind(f, bind(h, _1, _2)) ); + + // compose_h_fx_gx + + test( bind(h, bind(f, _1), bind(g, _1)) ); + + // compose_h_fx_gy + + test( bind(h, bind(f, _1), bind(g, _2)) ); + + // compose_f_k + + test( bind(f, bind(k)) ); + + return 0; +} diff --git a/bind_test.cpp b/bind_test.cpp new file mode 100644 index 0000000..d71a838 --- /dev/null +++ b/bind_test.cpp @@ -0,0 +1,269 @@ +#if defined(_MSC_VER) && !defined(__ICL) +#pragma warning(disable: 4786) // identifier truncated in debug info +#pragma warning(disable: 4710) // function not inlined +#pragma warning(disable: 4711) // function selected for automatic inline expansion +#pragma warning(disable: 4514) // unreferenced inline removed +#endif + +// +// bind_test.cpp - monolithic test for bind.hpp +// +// Version 1.00.0002 (2001-09-02) +// +// Copyright (c) 2001 Peter Dimov and Multi Media Ltd. +// +// Permission to copy, use, modify, sell and distribute this software +// is granted provided this copyright notice appears in all copies. +// This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. +// + +#include +#include +#include + +#define BOOST_INCLUDE_MAIN +#include + +// + +long f_0() +{ + return 17041L; +} + +long f_1(long a) +{ + return a; +} + +long f_2(long a, long b) +{ + return a + 10 * b; +} + +long f_3(long a, long b, long c) +{ + return a + 10 * b + 100 * c; +} + +long f_4(long a, long b, long c, long d) +{ + return a + 10 * b + 100 * c + 1000 * d; +} + +long f_5(long a, long b, long c, long d, long e) +{ + return a + 10 * b + 100 * c + 1000 * d + 10000 * e; +} + +long f_6(long a, long b, long c, long d, long e, long f) +{ + return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f; +} + +long f_7(long a, long b, long c, long d, long e, long f, long g) +{ + return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g; +} + +long f_8(long a, long b, long c, long d, long e, long f, long g, long h) +{ + return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h; +} + +long f_9(long a, long b, long c, long d, long e, long f, long g, long h, long i) +{ + return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h + 100000000 * i; +} + +void function_test() +{ + using namespace boost; + + int const i = 1; + + BOOST_TEST( bind(f_0)(i) == 17041L ); + BOOST_TEST( bind(f_1, _1)(i) == 1L ); + BOOST_TEST( bind(f_2, _1, 2)(i) == 21L ); + BOOST_TEST( bind(f_3, _1, 2, 3)(i) == 321L ); + BOOST_TEST( bind(f_4, _1, 2, 3, 4)(i) == 4321L ); + BOOST_TEST( bind(f_5, _1, 2, 3, 4, 5)(i) == 54321L ); + BOOST_TEST( bind(f_6, _1, 2, 3, 4, 5, 6)(i) == 654321L ); + BOOST_TEST( bind(f_7, _1, 2, 3, 4, 5, 6, 7)(i) == 7654321L ); + BOOST_TEST( bind(f_8, _1, 2, 3, 4, 5, 6, 7, 8)(i) == 87654321L ); + BOOST_TEST( bind(f_9, _1, 2, 3, 4, 5, 6, 7, 8, 9)(i) == 987654321L ); +} + +// + +struct Y +{ + short operator()(short & r) const { return ++r; } + int operator()(int a, int b) const { return a + 10 * b; } + long operator() (long a, long b, long c) const { return a + 10 * b + 100 * c; } +}; + +void function_object_test() +{ + using namespace boost; + + short i(6); + + int const k = 3; + + BOOST_TEST( bind(Y(), ref(i))() == 7 ); + BOOST_TEST( bind(Y(), ref(i))() == 8 ); + BOOST_TEST( bind(Y(), i, _1)(k) == 38 ); + BOOST_TEST( bind(Y(), i, _1, 9)(k) == 938 ); +} + +// + +struct X +{ + mutable unsigned int hash; + + X(): hash(0) {} + + int f0() { f1(17); return 0; } + int g0() const { g1(17); return 0; } + + int f1(int a1) { hash = (hash * 17041 + a1) % 32768; return 0; } + int g1(int a1) const { hash = (hash * 17041 + a1 * 2) % 32768; return 0; } + + int f2(int a1, int a2) { f1(a1); f1(a2); return 0; } + int g2(int a1, int a2) const { g1(a1); g1(a2); return 0; } + + int f3(int a1, int a2, int a3) { f2(a1, a2); f1(a3); return 0; } + int g3(int a1, int a2, int a3) const { g2(a1, a2); g1(a3); return 0; } + + int f4(int a1, int a2, int a3, int a4) { f3(a1, a2, a3); f1(a4); return 0; } + int g4(int a1, int a2, int a3, int a4) const { g3(a1, a2, a3); g1(a4); return 0; } + + int f5(int a1, int a2, int a3, int a4, int a5) { f4(a1, a2, a3, a4); f1(a5); return 0; } + int g5(int a1, int a2, int a3, int a4, int a5) const { g4(a1, a2, a3, a4); g1(a5); return 0; } + + int f6(int a1, int a2, int a3, int a4, int a5, int a6) { f5(a1, a2, a3, a4, a5); f1(a6); return 0; } + int g6(int a1, int a2, int a3, int a4, int a5, int a6) const { g5(a1, a2, a3, a4, a5); g1(a6); return 0; } + + int f7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) { f6(a1, a2, a3, a4, a5, a6); f1(a7); return 0; } + int g7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) const { g6(a1, a2, a3, a4, a5, a6); g1(a7); return 0; } + + int f8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { f7(a1, a2, a3, a4, a5, a6, a7); f1(a8); return 0; } + int g8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) const { g7(a1, a2, a3, a4, a5, a6, a7); g1(a8); return 0; } +}; + +void member_function_test() +{ + using namespace boost; + + X x; + + // 0 + + bind(&X::f0, &x)(); + bind(&X::f0, ref(x))(); + + bind(&X::g0, &x)(); + bind(&X::g0, x)(); + bind(&X::g0, ref(x))(); + + // 1 + + bind(&X::f1, &x, 1)(); + bind(&X::f1, ref(x), 1)(); + + bind(&X::g1, &x, 1)(); + bind(&X::g1, x, 1)(); + bind(&X::g1, ref(x), 1)(); + + // 2 + + bind(&X::f2, &x, 1, 2)(); + bind(&X::f2, ref(x), 1, 2)(); + + bind(&X::g2, &x, 1, 2)(); + bind(&X::g2, x, 1, 2)(); + bind(&X::g2, ref(x), 1, 2)(); + + // 3 + + bind(&X::f3, &x, 1, 2, 3)(); + bind(&X::f3, ref(x), 1, 2, 3)(); + + bind(&X::g3, &x, 1, 2, 3)(); + bind(&X::g3, x, 1, 2, 3)(); + bind(&X::g3, ref(x), 1, 2, 3)(); + + // 4 + + bind(&X::f4, &x, 1, 2, 3, 4)(); + bind(&X::f4, ref(x), 1, 2, 3, 4)(); + + bind(&X::g4, &x, 1, 2, 3, 4)(); + bind(&X::g4, x, 1, 2, 3, 4)(); + bind(&X::g4, ref(x), 1, 2, 3, 4)(); + + // 5 + + bind(&X::f5, &x, 1, 2, 3, 4, 5)(); + bind(&X::f5, ref(x), 1, 2, 3, 4, 5)(); + + bind(&X::g5, &x, 1, 2, 3, 4, 5)(); + bind(&X::g5, x, 1, 2, 3, 4, 5)(); + bind(&X::g5, ref(x), 1, 2, 3, 4, 5)(); + + // 6 + + bind(&X::f6, &x, 1, 2, 3, 4, 5, 6)(); + bind(&X::f6, ref(x), 1, 2, 3, 4, 5, 6)(); + + bind(&X::g6, &x, 1, 2, 3, 4, 5, 6)(); + bind(&X::g6, x, 1, 2, 3, 4, 5, 6)(); + bind(&X::g6, ref(x), 1, 2, 3, 4, 5, 6)(); + + // 7 + + bind(&X::f7, &x, 1, 2, 3, 4, 5, 6, 7)(); + bind(&X::f7, ref(x), 1, 2, 3, 4, 5, 6, 7)(); + + bind(&X::g7, &x, 1, 2, 3, 4, 5, 6, 7)(); + bind(&X::g7, x, 1, 2, 3, 4, 5, 6, 7)(); + bind(&X::g7, ref(x), 1, 2, 3, 4, 5, 6, 7)(); + + // 8 + + bind(&X::f8, &x, 1, 2, 3, 4, 5, 6, 7, 8)(); + bind(&X::f8, ref(x), 1, 2, 3, 4, 5, 6, 7, 8)(); + + bind(&X::g8, &x, 1, 2, 3, 4, 5, 6, 7, 8)(); + bind(&X::g8, x, 1, 2, 3, 4, 5, 6, 7, 8)(); + bind(&X::g8, ref(x), 1, 2, 3, 4, 5, 6, 7, 8)(); + + BOOST_TEST( x.hash == 23558 ); +} + +void nested_bind_test() +{ + using namespace boost; + + int const x = 1; + int const y = 2; + + BOOST_TEST( bind(f_1, bind(f_1, _1))(x) == 1L ); + BOOST_TEST( bind(f_1, bind(f_2, _1, _2))(x, y) == 21L ); + BOOST_TEST( bind(f_2, bind(f_1, _1), bind(f_1, _1))(x) == 11L ); + BOOST_TEST( bind(f_2, bind(f_1, _1), bind(f_1, _2))(x, y) == 21L ); + BOOST_TEST( bind(f_1, bind(f_0))() == 17041L ); +} + +int test_main(int, char * []) +{ + function_test(); + function_object_test(); + member_function_test(); + nested_bind_test(); + + return 0; +} diff --git a/bind_test_1.cpp b/bind_test_1.cpp new file mode 100644 index 0000000..b456dbe --- /dev/null +++ b/bind_test_1.cpp @@ -0,0 +1,104 @@ +#if defined(_MSC_VER) && !defined(__ICL) +#pragma warning(disable: 4786) // identifier truncated in debug info +#pragma warning(disable: 4710) // function not inlined +#pragma warning(disable: 4711) // function selected for automatic inline expansion +#pragma warning(disable: 4514) // unreferenced inline removed +#endif + +// +// bind_test_1.cpp - tests bind.hpp with function pointers +// +// Version 1.00.0003 (2001-07-13) +// +// Copyright (c) 2001 Peter Dimov and Multi Media Ltd. +// +// Permission to copy, use, modify, sell and distribute this software +// is granted provided this copyright notice appears in all copies. +// This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. +// + +#include +#include + +long f_1(long a) +{ + return a; +} + +long f_2(long a, long b) +{ + return a + 10 * b; +} + +long f_3(long a, long b, long c) +{ + return a + 10 * b + 100 * c; +} + +long f_4(long a, long b, long c, long d) +{ + return a + 10 * b + 100 * c + 1000 * d; +} + +long f_5(long a, long b, long c, long d, long e) +{ + return a + 10 * b + 100 * c + 1000 * d + 10000 * e; +} + +long f_6(long a, long b, long c, long d, long e, long f) +{ + return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f; +} + +long f_7(long a, long b, long c, long d, long e, long f, long g) +{ + return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g; +} + +long f_8(long a, long b, long c, long d, long e, long f, long g, long h) +{ + return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h; +} + +long f_9(long a, long b, long c, long d, long e, long f, long g, long h, long i) +{ + return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h + 100000000 * i; +} + +unsigned long hash = 0; + +template void test(F f) +{ + int const i = 1; + hash = (hash * 17041 + f(i)) % 32768; +} + +int detect_errors(bool x) +{ + if(x) + { + std::cerr << "no errors detected.\n"; + return 0; + } + else + { + std::cerr << "test failed.\n"; + return 1; + } +} + +int main() +{ + test(boost::bind(f_1, _1)); + test(boost::bind(f_2, _1, 2)); + test(boost::bind(f_3, _1, 2, 3)); + test(boost::bind(f_4, _1, 2, 3, 4)); + test(boost::bind(f_5, _1, 2, 3, 4, 5)); + test(boost::bind(f_6, _1, 2, 3, 4, 5, 6)); + test(boost::bind(f_7, _1, 2, 3, 4, 5, 6, 7)); + test(boost::bind(f_8, _1, 2, 3, 4, 5, 6, 7, 8)); + test(boost::bind(f_9, _1, 2, 3, 4, 5, 6, 7, 8, 9)); + + return detect_errors(hash == 18797); +} diff --git a/bind_test_2.cpp b/bind_test_2.cpp new file mode 100644 index 0000000..ac7b6b8 --- /dev/null +++ b/bind_test_2.cpp @@ -0,0 +1,65 @@ +#if defined(_MSC_VER) && !defined(__ICL) +#pragma warning(disable: 4786) // identifier truncated in debug info +#pragma warning(disable: 4710) // function not inlined +#pragma warning(disable: 4711) // function selected for automatic inline expansion +#pragma warning(disable: 4514) // unreferenced inline removed +#endif + +// +// bind_test_2.cpp - tests bind.hpp with function objects +// +// Version 1.00.0005 (2001-07-13) +// +// Copyright (c) 2001 Peter Dimov and Multi Media Ltd. +// +// Permission to copy, use, modify, sell and distribute this software +// is granted provided this copyright notice appears in all copies. +// This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. +// + +#include +#include +#include + +struct X +{ + short operator()(short & r) const { return ++r; } + int operator()(int a, int b) const { return a + 10 * b; } + long operator() (long a, long b, long c) const { return a + 10 * b + 100 * c; } +}; + +unsigned long hash = 0; + +template void test(F f, int const i) +{ + hash = (hash * 17041 + f(i)) % 32768; +} + +int detect_errors(bool x) +{ + if(x) + { + std::cerr << "no errors detected.\n"; + return 0; + } + else + { + std::cerr << "test failed.\n"; + return 1; + } +} + +int main() +{ + using namespace boost; + + short i(6); + + test(bind(X(), ref(i)), 1); + test(bind(X(), ref(i)), 2); + test(bind(X(), i, _1), 3); + test(bind(X(), i, _1, 9), 4); + + return detect_errors(hash == 24857); +} diff --git a/bind_test_3.cpp b/bind_test_3.cpp new file mode 100644 index 0000000..067c75e --- /dev/null +++ b/bind_test_3.cpp @@ -0,0 +1,161 @@ +#if defined(_MSC_VER) && !defined(__ICL) +#pragma warning(disable: 4786) // identifier truncated in debug info +#pragma warning(disable: 4710) // function not inlined +#pragma warning(disable: 4711) // function selected for automatic inline expansion +#pragma warning(disable: 4514) // unreferenced inline removed +#endif + +// +// bind_test_3.cpp - tests bind.hpp with member function pointers +// +// Version 1.00.0003 (2001-07-13) +// +// Copyright (c) 2001 Peter Dimov and Multi Media Ltd. +// +// Permission to copy, use, modify, sell and distribute this software +// is granted provided this copyright notice appears in all copies. +// This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. +// + +#include +#include +#include + +struct X +{ + mutable unsigned int hash; + + X(): hash(0) {} + + int f0() { f1(17); return 0; } + int g0() const { g1(17); return 0; } + + int f1(int a1) { hash = (hash * 17041 + a1) % 32768; return 0; } + int g1(int a1) const { hash = (hash * 17041 + a1 * 2) % 32768; return 0; } + + int f2(int a1, int a2) { f1(a1); f1(a2); return 0; } + int g2(int a1, int a2) const { g1(a1); g1(a2); return 0; } + + int f3(int a1, int a2, int a3) { f2(a1, a2); f1(a3); return 0; } + int g3(int a1, int a2, int a3) const { g2(a1, a2); g1(a3); return 0; } + + int f4(int a1, int a2, int a3, int a4) { f3(a1, a2, a3); f1(a4); return 0; } + int g4(int a1, int a2, int a3, int a4) const { g3(a1, a2, a3); g1(a4); return 0; } + + int f5(int a1, int a2, int a3, int a4, int a5) { f4(a1, a2, a3, a4); f1(a5); return 0; } + int g5(int a1, int a2, int a3, int a4, int a5) const { g4(a1, a2, a3, a4); g1(a5); return 0; } + + int f6(int a1, int a2, int a3, int a4, int a5, int a6) { f5(a1, a2, a3, a4, a5); f1(a6); return 0; } + int g6(int a1, int a2, int a3, int a4, int a5, int a6) const { g5(a1, a2, a3, a4, a5); g1(a6); return 0; } + + int f7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) { f6(a1, a2, a3, a4, a5, a6); f1(a7); return 0; } + int g7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) const { g6(a1, a2, a3, a4, a5, a6); g1(a7); return 0; } + + int f8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { f7(a1, a2, a3, a4, a5, a6, a7); f1(a8); return 0; } + int g8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) const { g7(a1, a2, a3, a4, a5, a6, a7); g1(a8); return 0; } +}; + +int detect_errors(bool x) +{ + if(x) + { + std::cerr << "no errors detected.\n"; + return 0; + } + else + { + std::cerr << "test failed.\n"; + return 1; + } +} + +int main() +{ + using namespace boost; + + X x; + + // 0 + + bind(&X::f0, &x)(); + bind(&X::f0, ref(x))(); + + bind(&X::g0, &x)(); + bind(&X::g0, x)(); + bind(&X::g0, ref(x))(); + + // 1 + + bind(&X::f1, &x, 1)(); + bind(&X::f1, ref(x), 1)(); + + bind(&X::g1, &x, 1)(); + bind(&X::g1, x, 1)(); + bind(&X::g1, ref(x), 1)(); + + // 2 + + bind(&X::f2, &x, 1, 2)(); + bind(&X::f2, ref(x), 1, 2)(); + + bind(&X::g2, &x, 1, 2)(); + bind(&X::g2, x, 1, 2)(); + bind(&X::g2, ref(x), 1, 2)(); + + // 3 + + bind(&X::f3, &x, 1, 2, 3)(); + bind(&X::f3, ref(x), 1, 2, 3)(); + + bind(&X::g3, &x, 1, 2, 3)(); + bind(&X::g3, x, 1, 2, 3)(); + bind(&X::g3, ref(x), 1, 2, 3)(); + + // 4 + + bind(&X::f4, &x, 1, 2, 3, 4)(); + bind(&X::f4, ref(x), 1, 2, 3, 4)(); + + bind(&X::g4, &x, 1, 2, 3, 4)(); + bind(&X::g4, x, 1, 2, 3, 4)(); + bind(&X::g4, ref(x), 1, 2, 3, 4)(); + + // 5 + + bind(&X::f5, &x, 1, 2, 3, 4, 5)(); + bind(&X::f5, ref(x), 1, 2, 3, 4, 5)(); + + bind(&X::g5, &x, 1, 2, 3, 4, 5)(); + bind(&X::g5, x, 1, 2, 3, 4, 5)(); + bind(&X::g5, ref(x), 1, 2, 3, 4, 5)(); + + // 6 + + bind(&X::f6, &x, 1, 2, 3, 4, 5, 6)(); + bind(&X::f6, ref(x), 1, 2, 3, 4, 5, 6)(); + + bind(&X::g6, &x, 1, 2, 3, 4, 5, 6)(); + bind(&X::g6, x, 1, 2, 3, 4, 5, 6)(); + bind(&X::g6, ref(x), 1, 2, 3, 4, 5, 6)(); + + // 7 + + bind(&X::f7, &x, 1, 2, 3, 4, 5, 6, 7)(); + bind(&X::f7, ref(x), 1, 2, 3, 4, 5, 6, 7)(); + + bind(&X::g7, &x, 1, 2, 3, 4, 5, 6, 7)(); + bind(&X::g7, x, 1, 2, 3, 4, 5, 6, 7)(); + bind(&X::g7, ref(x), 1, 2, 3, 4, 5, 6, 7)(); + + // 8 + + bind(&X::f8, &x, 1, 2, 3, 4, 5, 6, 7, 8)(); + bind(&X::f8, ref(x), 1, 2, 3, 4, 5, 6, 7, 8)(); + + bind(&X::g8, &x, 1, 2, 3, 4, 5, 6, 7, 8)(); + bind(&X::g8, x, 1, 2, 3, 4, 5, 6, 7, 8)(); + bind(&X::g8, ref(x), 1, 2, 3, 4, 5, 6, 7, 8)(); + + return detect_errors(x.hash == 23558); +} diff --git a/bind_test_4.cpp b/bind_test_4.cpp new file mode 100644 index 0000000..d0ce912 --- /dev/null +++ b/bind_test_4.cpp @@ -0,0 +1,89 @@ +#if defined(_MSC_VER) && !defined(__ICL) +#pragma warning(disable: 4786) // identifier truncated in debug info +#pragma warning(disable: 4710) // function not inlined +#pragma warning(disable: 4711) // function selected for automatic inline expansion +#pragma warning(disable: 4514) // unreferenced inline removed +#endif + +// +// bind_test_4.cpp - tests bind.hpp with a visitor +// +// Version 1.00.0003 (2001-08-30) +// +// Copyright (c) 2001 Peter Dimov and Multi Media Ltd. +// +// Permission to copy, use, modify, sell and distribute this software +// is granted provided this copyright notice appears in all copies. +// This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. +// + +#include +#include +#include +#include + +int hash = 0; + +struct ref_visitor +{ + template void operator()(boost::_bi::bind_t const & b) const + { + b.accept(*this); + } + + template void operator()(boost::reference_wrapper const & r) const + { + std::cout << "Reference to " << typeid(T).name() << " @ " << &r.get() << " (with value " << r.get() << ")\n"; + hash += r.get(); + } + +#ifndef BOOST_MSVC + + template void operator()(T const &) const + { + std::cout << "Catch-all: " << typeid(T).name() << '\n'; + } + +#else + + void operator()(...) const + { + } + +#endif + +}; + +int f(int & i, int & j) +{ + ++i; + --j; + return i + j; +} + +int x = 2; +int y = 7; + +int detect_errors(bool x) +{ + if(x) + { + std::cerr << "no errors detected.\n"; + return 0; + } + else + { + std::cerr << "test failed.\n"; + return 1; + } +} + +int main() +{ + using namespace boost; + + ref_visitor()(bind(bind(f, ref(x), _1), ref(y))); + + return detect_errors(hash == 9); +} diff --git a/include/boost/bind.hpp b/include/boost/bind.hpp new file mode 100644 index 0000000..27e7c0b --- /dev/null +++ b/include/boost/bind.hpp @@ -0,0 +1,1255 @@ +#ifndef BOOST_BIND_HPP_INCLUDED +#define BOOST_BIND_HPP_INCLUDED + +#if _MSC_VER >= 1020 +#pragma once +#endif + +// +// bind.hpp - binds function objects to arguments +// +// Version 1.01.0001 (2001-08-29) +// +// Copyright (c) 2001 Peter Dimov and Multi Media Ltd. +// +// Permission to copy, use, modify, sell and distribute this software +// is granted provided this copyright notice appears in all copies. +// This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. +// +// See http://www.boost.org/libs/bind/bind.html for documentation. +// + +#include +#include +#include + +namespace boost +{ + +namespace _bi // implementation details +{ + +template class bind_t; + +// value + +template class value +{ +public: + + value(T const & t): t_(t) {} + + T & get() { return t_; } + T const & get() const { return t_; } + +private: + + T t_; + value & operator= (value const &); +}; + +// arg + +template class arg {}; + +// type + +template class type {}; + +// listN + +class list0 +{ +public: + + list0() {} + + template T & operator[] (value & v) const { return v.get(); } + + template T const & operator[] (value const & v) const { return v.get(); } + + template T & operator[] (reference_wrapper const & v) const { return v.get(); } + + template R operator[] (bind_t & b) const { return b.eval(*this); } + + template R operator[] (bind_t const & b) const { return b.eval(*this); } + + template R operator()(type, F f, A & /* a */) const + { + return f(); + } + + template void accept(V &) const + { + } + +private: + + list0 & operator= (list0 const &); +}; + +template class list1 +{ +public: + + explicit list1(A1 a1): a1_(a1) {} + + A1 operator[] (arg<1>) const { return a1_; } + + template T & operator[] (value & v) const { return v.get(); } + + template T const & operator[] (value const & v) const { return v.get(); } + + template T & operator[] (reference_wrapper const & v) const { return v.get(); } + + template R operator[] (bind_t & b) const { return b.eval(*this); } + + template R operator[] (bind_t const & b) const { return b.eval(*this); } + + template R operator()(type, F f, A & a) const + { + return f(a[a1_]); + } + + template void accept(V & v) const + { + v(a1_); + } + +private: + + A1 a1_; + + list1 & operator= (list1 const &); +}; + +template class list2 +{ +public: + + list2(A1 a1, A2 a2): a1_(a1), a2_(a2) {} + + A1 operator[] (arg<1>) const { return a1_; } + A2 operator[] (arg<2>) const { return a2_; } + + template T & operator[] (value & v) const { return v.get(); } + + template T const & operator[] (value const & v) const { return v.get(); } + + template T & operator[] (reference_wrapper const & v) const { return v.get(); } + + template R operator[] (bind_t & b) const { return b.eval(*this); } + + template R operator[] (bind_t const & b) const { return b.eval(*this); } + + template R operator()(type, F f, A & a) const + { + return f(a[a1_], a[a2_]); + } + + template void accept(V & v) const + { + v(a1_); + v(a2_); + } + +private: + + A1 a1_; + A2 a2_; + + list2 & operator= (list2 const &); +}; + +template class list3 +{ +public: + + list3(A1 a1, A2 a2, A3 a3): a1_(a1), a2_(a2), a3_(a3) {} + + A1 operator[] (arg<1>) const { return a1_; } + A2 operator[] (arg<2>) const { return a2_; } + A3 operator[] (arg<3>) const { return a3_; } + + template T & operator[] (value & v) const { return v.get(); } + + template T const & operator[] (value const & v) const { return v.get(); } + + template T & operator[] (reference_wrapper const & v) const { return v.get(); } + + template R operator[] (bind_t & b) const { return b.eval(*this); } + + template R operator[] (bind_t const & b) const { return b.eval(*this); } + + template R operator()(type, F f, A & a) const + { + return f(a[a1_], a[a2_], a[a3_]); + } + + template void accept(V & v) const + { + v(a1_); + v(a2_); + v(a3_); + } + +private: + + A1 a1_; + A2 a2_; + A3 a3_; + + list3 & operator= (list3 const &); +}; + +template class list4 +{ +public: + + list4(A1 a1, A2 a2, A3 a3, A4 a4): a1_(a1), a2_(a2), a3_(a3), a4_(a4) {} + + A1 operator[] (arg<1>) const { return a1_; } + A2 operator[] (arg<2>) const { return a2_; } + A3 operator[] (arg<3>) const { return a3_; } + A4 operator[] (arg<4>) const { return a4_; } + + template T & operator[] (value & v) const { return v.get(); } + + template T const & operator[] (value const & v) const { return v.get(); } + + template T & operator[] (reference_wrapper const & v) const { return v.get(); } + + template R operator[] (bind_t & b) const { return b.eval(*this); } + + template R operator[] (bind_t const & b) const { return b.eval(*this); } + + template R operator()(type, F f, A & a) const + { + return f(a[a1_], a[a2_], a[a3_], a[a4_]); + } + + template void accept(V & v) const + { + v(a1_); + v(a2_); + v(a3_); + v(a4_); + } + +private: + + A1 a1_; + A2 a2_; + A3 a3_; + A4 a4_; + + list4 & operator= (list4 const &); +}; + +template class list5 +{ +public: + + list5(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5): a1_(a1), a2_(a2), a3_(a3), a4_(a4), a5_(a5) {} + + A1 operator[] (arg<1>) const { return a1_; } + A2 operator[] (arg<2>) const { return a2_; } + A3 operator[] (arg<3>) const { return a3_; } + A4 operator[] (arg<4>) const { return a4_; } + A5 operator[] (arg<5>) const { return a5_; } + + template T & operator[] (value & v) const { return v.get(); } + + template T const & operator[] (value const & v) const { return v.get(); } + + template T & operator[] (reference_wrapper const & v) const { return v.get(); } + + template R operator[] (bind_t & b) const { return b.eval(*this); } + + template R operator[] (bind_t const & b) const { return b.eval(*this); } + + template R operator()(type, F f, A & a) const + { + return f(a[a1_], a[a2_], a[a3_], a[a4_], a[a5_]); + } + + template void accept(V & v) const + { + v(a1_); + v(a2_); + v(a3_); + v(a4_); + v(a5_); + } + +private: + + A1 a1_; + A2 a2_; + A3 a3_; + A4 a4_; + A5 a5_; + + list5 & operator= (list5 const &); +}; + +template class list6 +{ +public: + + list6(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6): a1_(a1), a2_(a2), a3_(a3), a4_(a4), a5_(a5), a6_(a6) {} + + A1 operator[] (arg<1>) const { return a1_; } + A2 operator[] (arg<2>) const { return a2_; } + A3 operator[] (arg<3>) const { return a3_; } + A4 operator[] (arg<4>) const { return a4_; } + A5 operator[] (arg<5>) const { return a5_; } + A6 operator[] (arg<6>) const { return a6_; } + + template T & operator[] (value & v) const { return v.get(); } + + template T const & operator[] (value const & v) const { return v.get(); } + + template T & operator[] (reference_wrapper const & v) const { return v.get(); } + + template R operator[] (bind_t & b) const { return b.eval(*this); } + + template R operator[] (bind_t const & b) const { return b.eval(*this); } + + template R operator()(type, F f, A & a) const + { + return f(a[a1_], a[a2_], a[a3_], a[a4_], a[a5_], a[a6_]); + } + + template void accept(V & v) const + { + v(a1_); + v(a2_); + v(a3_); + v(a4_); + v(a5_); + v(a6_); + } + +private: + + A1 a1_; + A2 a2_; + A3 a3_; + A4 a4_; + A5 a5_; + A6 a6_; + + list6 & operator= (list6 const &); +}; + +template class list7 +{ +public: + + list7(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7): a1_(a1), a2_(a2), a3_(a3), a4_(a4), a5_(a5), a6_(a6), a7_(a7) {} + + A1 operator[] (arg<1>) const { return a1_; } + A2 operator[] (arg<2>) const { return a2_; } + A3 operator[] (arg<3>) const { return a3_; } + A4 operator[] (arg<4>) const { return a4_; } + A5 operator[] (arg<5>) const { return a5_; } + A6 operator[] (arg<6>) const { return a6_; } + A7 operator[] (arg<7>) const { return a7_; } + + template T & operator[] (value & v) const { return v.get(); } + + template T const & operator[] (value const & v) const { return v.get(); } + + template T & operator[] (reference_wrapper const & v) const { return v.get(); } + + template R operator[] (bind_t & b) const { return b.eval(*this); } + + template R operator[] (bind_t const & b) const { return b.eval(*this); } + + template R operator()(type, F f, A & a) const + { + return f(a[a1_], a[a2_], a[a3_], a[a4_], a[a5_], a[a6_], a[a7_]); + } + + template void accept(V & v) const + { + v(a1_); + v(a2_); + v(a3_); + v(a4_); + v(a5_); + v(a6_); + v(a7_); + } + +private: + + A1 a1_; + A2 a2_; + A3 a3_; + A4 a4_; + A5 a5_; + A6 a6_; + A7 a7_; + + list7 & operator= (list7 const &); +}; + +template class list8 +{ +public: + + list8(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8): a1_(a1), a2_(a2), a3_(a3), a4_(a4), a5_(a5), a6_(a6), a7_(a7), a8_(a8) {} + + A1 operator[] (arg<1>) const { return a1_; } + A2 operator[] (arg<2>) const { return a2_; } + A3 operator[] (arg<3>) const { return a3_; } + A4 operator[] (arg<4>) const { return a4_; } + A5 operator[] (arg<5>) const { return a5_; } + A6 operator[] (arg<6>) const { return a6_; } + A7 operator[] (arg<7>) const { return a7_; } + A8 operator[] (arg<8>) const { return a8_; } + + template T & operator[] (value & v) const { return v.get(); } + + template T const & operator[] (value const & v) const { return v.get(); } + + template T & operator[] (reference_wrapper const & v) const { return v.get(); } + + template R operator[] (bind_t & b) const { return b.eval(*this); } + + template R operator[] (bind_t const & b) const { return b.eval(*this); } + + template R operator()(type, F f, A & a) const + { + return f(a[a1_], a[a2_], a[a3_], a[a4_], a[a5_], a[a6_], a[a7_], a[a8_]); + } + + template void accept(V & v) const + { + v(a1_); + v(a2_); + v(a3_); + v(a4_); + v(a5_); + v(a6_); + v(a7_); + v(a8_); + } + +private: + + A1 a1_; + A2 a2_; + A3 a3_; + A4 a4_; + A5 a5_; + A6 a6_; + A7 a7_; + A8 a8_; + + list8 & operator= (list8 const &); +}; + +template class list9 +{ +public: + + list9(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9): a1_(a1), a2_(a2), a3_(a3), a4_(a4), a5_(a5), a6_(a6), a7_(a7), a8_(a8), a9_(a9) {} + + A1 operator[] (arg<1>) const { return a1_; } + A2 operator[] (arg<2>) const { return a2_; } + A3 operator[] (arg<3>) const { return a3_; } + A4 operator[] (arg<4>) const { return a4_; } + A5 operator[] (arg<5>) const { return a5_; } + A6 operator[] (arg<6>) const { return a6_; } + A7 operator[] (arg<7>) const { return a7_; } + A8 operator[] (arg<8>) const { return a8_; } + A9 operator[] (arg<9>) const { return a9_; } + + template T & operator[] (value & v) const { return v.get(); } + + template T const & operator[] (value const & v) const { return v.get(); } + + template T & operator[] (reference_wrapper const & v) const { return v.get(); } + + template R operator[] (bind_t & b) const { return b.eval(*this); } + + template R operator[] (bind_t const & b) const { return b.eval(*this); } + + template R operator()(type, F f, A & a) const + { + return f(a[a1_], a[a2_], a[a3_], a[a4_], a[a5_], a[a6_], a[a7_], a[a8_], a[a9_]); + } + + template void accept(V & v) const + { + v(a1_); + v(a2_); + v(a3_); + v(a4_); + v(a5_); + v(a6_); + v(a7_); + v(a8_); + v(a9_); + } + +private: + + A1 a1_; + A2 a2_; + A3 a3_; + A4 a4_; + A5 a5_; + A6 a6_; + A7 a7_; + A8 a8_; + A9 a9_; + + list9 & operator= (list9 const &); +}; + +// bind_t + +template class bind_t +{ +public: + + typedef R result_type; + + bind_t(F f, L const & l): f_(f), l_(l) {} + + R operator()() + { + list0 a; + return l_(type(), f_, a); + } + + R operator()() const + { + list0 a; + return l_(type(), f_, a); + } + + template R operator()(A1 & a1) + { + list1 a(a1); + return l_(type(), f_, a); + } + + template R operator()(A1 & a1) const + { + list1 a(a1); + return l_(type(), f_, a); + } + + template R operator()(A1 & a1, A2 & a2) + { + list2 a(a1, a2); + return l_(type(), f_, a); + } + + template R operator()(A1 & a1, A2 & a2) const + { + list2 a(a1, a2); + return l_(type(), f_, a); + } + + template R operator()(A1 & a1, A2 & a2, A3 & a3) + { + list3 a(a1, a2, a3); + return l_(type(), f_, a); + } + + template R operator()(A1 & a1, A2 & a2, A3 & a3) const + { + list3 a(a1, a2, a3); + return l_(type(), f_, a); + } + + template R operator()(A1 & a1, A2 & a2, A3 & a3, A4 & a4) + { + list4 a(a1, a2, a3, a4); + return l_(type(), f_, a); + } + + template R operator()(A1 & a1, A2 & a2, A3 & a3, A4 & a4) const + { + list4 a(a1, a2, a3, a4); + return l_(type(), f_, a); + } + + template R operator()(A1 & a1, A2 & a2, A3 & a3, A4 & a4, A5 & a5) + { + list5 a(a1, a2, a3, a4, a5); + return l_(type(), f_, a); + } + + template R operator()(A1 & a1, A2 & a2, A3 & a3, A4 & a4, A5 & a5) const + { + list5 a(a1, a2, a3, a4, a5); + return l_(type(), f_, a); + } + + template R operator()(A1 & a1, A2 & a2, A3 & a3, A4 & a4, A5 & a5, A6 & a6) + { + list6 a(a1, a2, a3, a4, a5, a6); + return l_(type(), f_, a); + } + + template R operator()(A1 & a1, A2 & a2, A3 & a3, A4 & a4, A5 & a5, A6 & a6) const + { + list6 a(a1, a2, a3, a4, a5, a6); + return l_(type(), f_, a); + } + + template R operator()(A1 & a1, A2 & a2, A3 & a3, A4 & a4, A5 & a5, A6 & a6, A7 & a7) + { + list7 a(a1, a2, a3, a4, a5, a6, a7); + return l_(type(), f_, a); + } + + template R operator()(A1 & a1, A2 & a2, A3 & a3, A4 & a4, A5 & a5, A6 & a6, A7 & a7) const + { + list7 a(a1, a2, a3, a4, a5, a6, a7); + return l_(type(), f_, a); + } + + template R operator()(A1 & a1, A2 & a2, A3 & a3, A4 & a4, A5 & a5, A6 & a6, A7 & a7, A8 & a8) + { + list8 a(a1, a2, a3, a4, a5, a6, a7, a8); + return l_(type(), f_, a); + } + + template R operator()(A1 & a1, A2 & a2, A3 & a3, A4 & a4, A5 & a5, A6 & a6, A7 & a7, A8 & a8) const + { + list8 a(a1, a2, a3, a4, a5, a6, a7, a8); + return l_(type(), f_, a); + } + + template R operator()(A1 & a1, A2 & a2, A3 & a3, A4 & a4, A5 & a5, A6 & a6, A7 & a7, A8 & a8, A9 & a9) + { + list9 a(a1, a2, a3, a4, a5, a6, a7, a8, a9); + return l_(type(), f_, a); + } + + template R operator()(A1 & a1, A2 & a2, A3 & a3, A4 & a4, A5 & a5, A6 & a6, A7 & a7, A8 & a8, A9 & a9) const + { + list9 a(a1, a2, a3, a4, a5, a6, a7, a8, a9); + return l_(type(), f_, a); + } + + template R eval(A & a) + { + return l_(type(), f_, a); + } + + template R eval(A & a) const + { + return l_(type(), f_, a); + } + + template void accept(V & v) const + { + v(f_); + l_.accept(v); + } + +private: + + F f_; + L l_; + + bind_t & operator= (bind_t const &); +}; + +// add_value + +#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION + +template struct add_value +{ + typedef value type; +}; + +template struct add_value< value > +{ + typedef value type; +}; + +template struct add_value< reference_wrapper > +{ + typedef reference_wrapper type; +}; + +template struct add_value< arg > +{ + typedef arg type; +}; + +template struct add_value< bind_t > +{ + typedef bind_t type; +}; + +#else + +template struct _avt_0; + +template<> struct _avt_0<1> +{ + template struct inner + { + typedef T type; + }; +}; + +template<> struct _avt_0<2> +{ + template struct inner + { + typedef value type; + }; +}; + +typedef char (&_avt_r1) [1]; +typedef char (&_avt_r2) [2]; + +template _avt_r1 _avt_f(value); +template _avt_r1 _avt_f(reference_wrapper); +template _avt_r1 _avt_f(arg); +template _avt_r1 _avt_f(bind_t); + +_avt_r2 _avt_f(...); + +template struct add_value +{ + static T t(); + typedef typename _avt_0::template inner::type type; +}; + +#endif + +// list_av_N + +template struct list_av_1 +{ + typedef typename add_value::type B1; + typedef list1 type; +}; + +template struct list_av_2 +{ + typedef typename add_value::type B1; + typedef typename add_value::type B2; + typedef list2 type; +}; + +template struct list_av_3 +{ + typedef typename add_value::type B1; + typedef typename add_value::type B2; + typedef typename add_value::type B3; + typedef list3 type; +}; + +template struct list_av_4 +{ + typedef typename add_value::type B1; + typedef typename add_value::type B2; + typedef typename add_value::type B3; + typedef typename add_value::type B4; + typedef list4 type; +}; + +template struct list_av_5 +{ + typedef typename add_value::type B1; + typedef typename add_value::type B2; + typedef typename add_value::type B3; + typedef typename add_value::type B4; + typedef typename add_value::type B5; + typedef list5 type; +}; + +template struct list_av_6 +{ + typedef typename add_value::type B1; + typedef typename add_value::type B2; + typedef typename add_value::type B3; + typedef typename add_value::type B4; + typedef typename add_value::type B5; + typedef typename add_value::type B6; + typedef list6 type; +}; + +template struct list_av_7 +{ + typedef typename add_value::type B1; + typedef typename add_value::type B2; + typedef typename add_value::type B3; + typedef typename add_value::type B4; + typedef typename add_value::type B5; + typedef typename add_value::type B6; + typedef typename add_value::type B7; + typedef list7 type; +}; + +template struct list_av_8 +{ + typedef typename add_value::type B1; + typedef typename add_value::type B2; + typedef typename add_value::type B3; + typedef typename add_value::type B4; + typedef typename add_value::type B5; + typedef typename add_value::type B6; + typedef typename add_value::type B7; + typedef typename add_value::type B8; + typedef list8 type; +}; + +template struct list_av_9 +{ + typedef typename add_value::type B1; + typedef typename add_value::type B2; + typedef typename add_value::type B3; + typedef typename add_value::type B4; + typedef typename add_value::type B5; + typedef typename add_value::type B6; + typedef typename add_value::type B7; + typedef typename add_value::type B8; + typedef typename add_value::type B9; + typedef list9 type; +}; + +} // namespace _bi + +// bind + +#ifndef BOOST_BIND +#define BOOST_BIND bind +#endif + +// generic function objects + +template + _bi::bind_t + BOOST_BIND(F f) +{ + typedef _bi::list0 list_type; + return _bi::bind_t (f, list_type()); +} + +template + _bi::bind_t::type> + BOOST_BIND(F f, A1 a1) +{ + typedef typename _bi::list_av_1::type list_type; + return _bi::bind_t (f, list_type(a1)); +} + +template + _bi::bind_t::type> + BOOST_BIND(F f, A1 a1, A2 a2) +{ + typedef typename _bi::list_av_2::type list_type; + return _bi::bind_t (f, list_type(a1, a2)); +} + +template + _bi::bind_t::type> + BOOST_BIND(F f, A1 a1, A2 a2, A3 a3) +{ + typedef typename _bi::list_av_3::type list_type; + return _bi::bind_t(f, list_type(a1, a2, a3)); +} + +template + _bi::bind_t::type> + BOOST_BIND(F f, A1 a1, A2 a2, A3 a3, A4 a4) +{ + typedef typename _bi::list_av_4::type list_type; + return _bi::bind_t(f, list_type(a1, a2, a3, a4)); +} + +template + _bi::bind_t::type> + BOOST_BIND(F f, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) +{ + typedef typename _bi::list_av_5::type list_type; + return _bi::bind_t(f, list_type(a1, a2, a3, a4, a5)); +} + +template + _bi::bind_t::type> + BOOST_BIND(F f, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) +{ + typedef typename _bi::list_av_6::type list_type; + return _bi::bind_t(f, list_type(a1, a2, a3, a4, a5, a6)); +} + +template + _bi::bind_t::type> + BOOST_BIND(F f, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) +{ + typedef typename _bi::list_av_7::type list_type; + return _bi::bind_t(f, list_type(a1, a2, a3, a4, a5, a6, a7)); +} + +template + _bi::bind_t::type> + BOOST_BIND(F f, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8) +{ + typedef typename _bi::list_av_8::type list_type; + return _bi::bind_t(f, list_type(a1, a2, a3, a4, a5, a6, a7, a8)); +} + +template + _bi::bind_t::type> + BOOST_BIND(F f, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9) +{ + typedef typename _bi::list_av_9::type list_type; + return _bi::bind_t(f, list_type(a1, a2, a3, a4, a5, a6, a7, a8, a9)); +} + +// function pointers + +template + _bi::bind_t + BOOST_BIND(R (*f) ()) +{ + typedef R (*F) (); + typedef _bi::list0 list_type; + return _bi::bind_t (f, list_type()); +} + +template + _bi::bind_t::type> + BOOST_BIND(R (*f) (B1), A1 a1) +{ + typedef R (*F) (B1); + typedef typename _bi::list_av_1::type list_type; + return _bi::bind_t (f, list_type(a1)); +} + +template + _bi::bind_t::type> + BOOST_BIND(R (*f) (B1, B2), A1 a1, A2 a2) +{ + typedef R (*F) (B1, B2); + typedef typename _bi::list_av_2::type list_type; + return _bi::bind_t (f, list_type(a1, a2)); +} + +template + _bi::bind_t::type> + BOOST_BIND(R (*f) (B1, B2, B3), A1 a1, A2 a2, A3 a3) +{ + typedef R (*F) (B1, B2, B3); + typedef typename _bi::list_av_3::type list_type; + return _bi::bind_t(f, list_type(a1, a2, a3)); +} + +template + _bi::bind_t::type> + BOOST_BIND(R (*f) (B1, B2, B3, B4), A1 a1, A2 a2, A3 a3, A4 a4) +{ + typedef R (*F) (B1, B2, B3, B4); + typedef typename _bi::list_av_4::type list_type; + return _bi::bind_t(f, list_type(a1, a2, a3, a4)); +} + +template + _bi::bind_t::type> + BOOST_BIND(R (*f) (B1, B2, B3, B4, B5), A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) +{ + typedef R (*F) (B1, B2, B3, B4, B5); + typedef typename _bi::list_av_5::type list_type; + return _bi::bind_t(f, list_type(a1, a2, a3, a4, a5)); +} + +template + _bi::bind_t::type> + BOOST_BIND(R (*f) (B1, B2, B3, B4, B5, B6), A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) +{ + typedef R (*F) (B1, B2, B3, B4, B5, B6); + typedef typename _bi::list_av_6::type list_type; + return _bi::bind_t(f, list_type(a1, a2, a3, a4, a5, a6)); +} + +template + _bi::bind_t::type> + BOOST_BIND(R (*f) (B1, B2, B3, B4, B5, B6, B7), A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) +{ + typedef R (*F) (B1, B2, B3, B4, B5, B6, B7); + typedef typename _bi::list_av_7::type list_type; + return _bi::bind_t(f, list_type(a1, a2, a3, a4, a5, a6, a7)); +} + +template + _bi::bind_t::type> + BOOST_BIND(R (*f) (B1, B2, B3, B4, B5, B6, B7, B8), A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8) +{ + typedef R (*F) (B1, B2, B3, B4, B5, B6, B7, B8); + typedef typename _bi::list_av_8::type list_type; + return _bi::bind_t(f, list_type(a1, a2, a3, a4, a5, a6, a7, a8)); +} + +template + _bi::bind_t::type> + BOOST_BIND(R (*f) (B1, B2, B3, B4, B5, B6, B7, B8, B9), A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9) +{ + typedef R (*F) (B1, B2, B3, B4, B5, B6, B7, B8, B9); + typedef typename _bi::list_av_9::type list_type; + return _bi::bind_t(f, list_type(a1, a2, a3, a4, a5, a6, a7, a8, a9)); +} + +// member function pointers + +// 0 + +template + _bi::bind_t, typename _bi::list_av_1::type> + BOOST_BIND(R (T::*f) (), A1 a1) +{ + typedef _mfi::mf0 F; + typedef typename _bi::list_av_1::type list_type; + return _bi::bind_t(F(f), list_type(a1)); +} + +template + _bi::bind_t, typename _bi::list_av_1::type> + BOOST_BIND(R (T::*f) () const, A1 a1) +{ + typedef _mfi::cmf0 F; + typedef typename _bi::list_av_1::type list_type; + return _bi::bind_t(F(f), list_type(a1)); +} + +// 1 + +template + _bi::bind_t, typename _bi::list_av_2::type> + BOOST_BIND(R (T::*f) (B1), A1 a1, A2 a2) +{ + typedef _mfi::mf1 F; + typedef typename _bi::list_av_2::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2)); +} + +template + _bi::bind_t, typename _bi::list_av_2::type> + BOOST_BIND(R (T::*f) (B1) const, A1 a1, A2 a2) +{ + typedef _mfi::cmf1 F; + typedef typename _bi::list_av_2::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2)); +} + +// 2 + +template + _bi::bind_t, typename _bi::list_av_3::type> + BOOST_BIND(R (T::*f) (B1, B2), A1 a1, A2 a2, A3 a3) +{ + typedef _mfi::mf2 F; + typedef typename _bi::list_av_3::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3)); +} + +template + _bi::bind_t, typename _bi::list_av_3::type> + BOOST_BIND(R (T::*f) (B1, B2) const, A1 a1, A2 a2, A3 a3) +{ + typedef _mfi::cmf2 F; + typedef typename _bi::list_av_3::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3)); +} + +// 3 + +template + _bi::bind_t, typename _bi::list_av_4::type> + BOOST_BIND(R (T::*f) (B1, B2, B3), A1 a1, A2 a2, A3 a3, A4 a4) +{ + typedef _mfi::mf3 F; + typedef typename _bi::list_av_4::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4)); +} + +template + _bi::bind_t, typename _bi::list_av_4::type> + BOOST_BIND(R (T::*f) (B1, B2, B3) const, A1 a1, A2 a2, A3 a3, A4 a4) +{ + typedef _mfi::cmf3 F; + typedef typename _bi::list_av_4::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4)); +} + +// 4 + +template + _bi::bind_t, typename _bi::list_av_5::type> + BOOST_BIND(R (T::*f) (B1, B2, B3, B4), A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) +{ + typedef _mfi::mf4 F; + typedef typename _bi::list_av_5::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4, a5)); +} + +template + _bi::bind_t, typename _bi::list_av_5::type> + BOOST_BIND(R (T::*f) (B1, B2, B3, B4) const, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) +{ + typedef _mfi::cmf4 F; + typedef typename _bi::list_av_5::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4, a5)); +} + +// 5 + +template + _bi::bind_t, typename _bi::list_av_6::type> + BOOST_BIND(R (T::*f) (B1, B2, B3, B4, B5), A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) +{ + typedef _mfi::mf5 F; + typedef typename _bi::list_av_6::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4, a5, a6)); +} + +template + _bi::bind_t, typename _bi::list_av_6::type> + BOOST_BIND(R (T::*f) (B1, B2, B3, B4, B5) const, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) +{ + typedef _mfi::cmf5 F; + typedef typename _bi::list_av_6::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4, a5, a6)); +} + +// 6 + +template + _bi::bind_t, typename _bi::list_av_7::type> + BOOST_BIND(R (T::*f) (B1, B2, B3, B4, B5, B6), A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) +{ + typedef _mfi::mf6 F; + typedef typename _bi::list_av_7::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4, a5, a6, a7)); +} + +template + _bi::bind_t, typename _bi::list_av_7::type> + BOOST_BIND(R (T::*f) (B1, B2, B3, B4, B5, B6) const, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) +{ + typedef _mfi::cmf6 F; + typedef typename _bi::list_av_7::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4, a5, a6, a7)); +} + +// 7 + +template + _bi::bind_t, typename _bi::list_av_8::type> + BOOST_BIND(R (T::*f) (B1, B2, B3, B4, B5, B6, B7), A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8) +{ + typedef _mfi::mf7 F; + typedef typename _bi::list_av_8::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4, a5, a6, a7, a8)); +} + +template + _bi::bind_t, typename _bi::list_av_8::type> + BOOST_BIND(R (T::*f) (B1, B2, B3, B4, B5, B6, B7) const, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8) +{ + typedef _mfi::cmf7 F; + typedef typename _bi::list_av_8::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4, a5, a6, a7, a8)); +} + +// 8 + +template + _bi::bind_t, typename _bi::list_av_9::type> + BOOST_BIND(R (T::*f) (B1, B2, B3, B4, B5, B6, B7, B8), A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9) +{ + typedef _mfi::mf8 F; + typedef typename _bi::list_av_9::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4, a5, a6, a7, a8, a9)); +} + +template + _bi::bind_t, typename _bi::list_av_9::type> + BOOST_BIND(R (T::*f) (B1, B2, B3, B4, B5, B6, B7, B8) const, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9) +{ + typedef _mfi::cmf8 F; + typedef typename _bi::list_av_9::type list_type; + return _bi::bind_t(F(f), list_type(a1, a2, a3, a4, a5, a6, a7, a8, a9)); +} + +} // namespace boost + +namespace +{ + boost::_bi::arg<1> _1; + boost::_bi::arg<2> _2; + boost::_bi::arg<3> _3; + boost::_bi::arg<4> _4; + boost::_bi::arg<5> _5; + boost::_bi::arg<6> _6; + boost::_bi::arg<7> _7; + boost::_bi::arg<8> _8; + boost::_bi::arg<9> _9; +} + +#endif // #ifndef BOOST_BIND_HPP_INCLUDED diff --git a/include/boost/mem_fn.hpp b/include/boost/mem_fn.hpp new file mode 100644 index 0000000..f6709ed --- /dev/null +++ b/include/boost/mem_fn.hpp @@ -0,0 +1,704 @@ +#ifndef BOOST_MEM_FN_HPP_INCLUDED +#define BOOST_MEM_FN_HPP_INCLUDED + +#if _MSC_VER >= 1020 +#pragma once +#endif + +// +// mem_fn.hpp - a generalization of std::mem_fun[_ref] +// +// Version 1.02.0001 (2001-08-30) +// +// Copyright (c) 2001 Peter Dimov and Multi Media Ltd. +// +// Permission to copy, use, modify, sell and distribute this software +// is granted provided this copyright notice appears in all copies. +// This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. +// +// See http://www.boost.org/libs/bind/mem_fn.html for documentation. +// + +namespace boost +{ + +// get_pointer(p) extracts a ->* capable pointer from p + +template T * get_pointer(T * p) +{ + return p; +} + +// implementation of get_pointer for boost::shared_ptr +// this will probably migrate to boost/shared_ptr.hpp + +template class shared_ptr; + +template T * get_pointer(shared_ptr const & p) +{ + return p.get(); +} + +// + +namespace _mfi // mem_fun_impl +{ + +// mf0 + +template class mf0 +{ +public: + + typedef R result_type; + typedef T * first_argument_type; + +private: + + typedef R (T::*F) (); + F f_; + +public: + + explicit mf0(F f): f_(f) {} + + R operator()(T * p) const + { + return (p->*f_)(); + } + + template R operator()(U & u) const + { + return (get_pointer(u)->*f_)(); + } + + R operator()(T & t) const + { + return (t.*f_)(); + } +}; + +// cmf0 + +template class cmf0 +{ +public: + + typedef R result_type; + typedef T const * first_argument_type; + +private: + + typedef R (T::*F) () const; + F f_; + +public: + + explicit cmf0(F f): f_(f) {} + + template R operator()(U const & u) const + { + return (get_pointer(u)->*f_)(); + } + + R operator()(T const & t) const + { + return (t.*f_)(); + } +}; + +// mf1 + +template class mf1 +{ +public: + + typedef R result_type; + typedef T * first_argument_type; + typedef A1 second_argument_type; + +private: + + typedef R (T::*F) (A1); + F f_; + +public: + + explicit mf1(F f): f_(f) {} + + R operator()(T * p, A1 a1) const + { + return (p->*f_)(a1); + } + + template R operator()(U & u, A1 a1) const + { + return (get_pointer(u)->*f_)(a1); + } + + R operator()(T & t, A1 a1) const + { + return (t.*f_)(a1); + } +}; + +// cmf1 + +template class cmf1 +{ +public: + + typedef R result_type; + typedef T const * first_argument_type; + typedef A1 second_argument_type; + +private: + + typedef R (T::*F) (A1) const; + F f_; + +public: + + explicit cmf1(F f): f_(f) {} + + template R operator()(U const & u, A1 a1) const + { + return (get_pointer(u)->*f_)(a1); + } + + R operator()(T const & t, A1 a1) const + { + return (t.*f_)(a1); + } +}; + +// mf2 + +template class mf2 +{ +public: + + typedef R result_type; + +private: + + typedef R (T::*F) (A1, A2); + F f_; + +public: + + explicit mf2(F f): f_(f) {} + + R operator()(T * p, A1 a1, A2 a2) const + { + return (p->*f_)(a1, a2); + } + + template R operator()(U & u, A1 a1, A2 a2) const + { + return (get_pointer(u)->*f_)(a1, a2); + } + + R operator()(T & t, A1 a1, A2 a2) const + { + return (t.*f_)(a1, a2); + } +}; + +// cmf2 + +template class cmf2 +{ +public: + + typedef R result_type; + +private: + + typedef R (T::*F) (A1, A2) const; + F f_; + +public: + + explicit cmf2(F f): f_(f) {} + + template R operator()(U const & u, A1 a1, A2 a2) const + { + return (get_pointer(u)->*f_)(a1, a2); + } + + R operator()(T const & t, A1 a1, A2 a2) const + { + return (t.*f_)(a1, a2); + } +}; + +// mf3 + +template class mf3 +{ +public: + + typedef R result_type; + +private: + + typedef R (T::*F) (A1, A2, A3); + F f_; + +public: + + explicit mf3(F f): f_(f) {} + + R operator()(T * p, A1 a1, A2 a2, A3 a3) const + { + return (p->*f_)(a1, a2, a3); + } + + template R operator()(U & u, A1 a1, A2 a2, A3 a3) const + { + return (get_pointer(u)->*f_)(a1, a2, a3); + } + + R operator()(T & t, A1 a1, A2 a2, A3 a3) const + { + return (t.*f_)(a1, a2, a3); + } +}; + +// cmf3 + +template class cmf3 +{ +public: + + typedef R result_type; + +private: + + typedef R (T::*F) (A1, A2, A3) const; + F f_; + +public: + + explicit cmf3(F f): f_(f) {} + + template R operator()(U const & u, A1 a1, A2 a2, A3 a3) const + { + return (get_pointer(u)->*f_)(a1, a2, a3); + } + + R operator()(T const & t, A1 a1, A2 a2, A3 a3) const + { + return (t.*f_)(a1, a2, a3); + } +}; + +// mf4 + +template class mf4 +{ +public: + + typedef R result_type; + +private: + + typedef R (T::*F) (A1, A2, A3, A4); + F f_; + +public: + + explicit mf4(F f): f_(f) {} + + R operator()(T * p, A1 a1, A2 a2, A3 a3, A4 a4) const + { + return (p->*f_)(a1, a2, a3, a4); + } + + template R operator()(U & u, A1 a1, A2 a2, A3 a3, A4 a4) const + { + return (get_pointer(u)->*f_)(a1, a2, a3, a4); + } + + R operator()(T & t, A1 a1, A2 a2, A3 a3, A4 a4) const + { + return (t.*f_)(a1, a2, a3, a4); + } +}; + +// cmf4 + +template class cmf4 +{ +public: + + typedef R result_type; + +private: + + typedef R (T::*F) (A1, A2, A3, A4) const; + F f_; + +public: + + explicit cmf4(F f): f_(f) {} + + template R operator()(U const & u, A1 a1, A2 a2, A3 a3, A4 a4) const + { + return (get_pointer(u)->*f_)(a1, a2, a3, a4); + } + + R operator()(T const & t, A1 a1, A2 a2, A3 a3, A4 a4) const + { + return (t.*f_)(a1, a2, a3, a4); + } +}; + +// mf5 + +template class mf5 +{ +public: + + typedef R result_type; + +private: + + typedef R (T::*F) (A1, A2, A3, A4, A5); + F f_; + +public: + + explicit mf5(F f): f_(f) {} + + R operator()(T * p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) const + { + return (p->*f_)(a1, a2, a3, a4, a5); + } + + template R operator()(U & u, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) const + { + return (get_pointer(u)->*f_)(a1, a2, a3, a4, a5); + } + + R operator()(T & t, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) const + { + return (t.*f_)(a1, a2, a3, a4, a5); + } +}; + +// cmf5 + +template class cmf5 +{ +public: + + typedef R result_type; + +private: + + typedef R (T::*F) (A1, A2, A3, A4, A5) const; + F f_; + +public: + + explicit cmf5(F f): f_(f) {} + + template R operator()(U const & u, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) const + { + return (get_pointer(u)->*f_)(a1, a2, a3, a4, a5); + } + + R operator()(T const & t, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) const + { + return (t.*f_)(a1, a2, a3, a4, a5); + } +}; + +// mf6 + +template class mf6 +{ +public: + + typedef R result_type; + +private: + + typedef R (T::*F) (A1, A2, A3, A4, A5, A6); + F f_; + +public: + + explicit mf6(F f): f_(f) {} + + R operator()(T * p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) const + { + return (p->*f_)(a1, a2, a3, a4, a5, a6); + } + + template R operator()(U & u, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) const + { + return (get_pointer(u)->*f_)(a1, a2, a3, a4, a5, a6); + } + + R operator()(T & t, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) const + { + return (t.*f_)(a1, a2, a3, a4, a5, a6); + } +}; + +// cmf6 + +template class cmf6 +{ +public: + + typedef R result_type; + +private: + + typedef R (T::*F) (A1, A2, A3, A4, A5, A6) const; + F f_; + +public: + + explicit cmf6(F f): f_(f) {} + + template R operator()(U const & u, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) const + { + return (get_pointer(u)->*f_)(a1, a2, a3, a4, a5, a6); + } + + R operator()(T const & t, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6) const + { + return (t.*f_)(a1, a2, a3, a4, a5, a6); + } +}; + +// mf7 + +template class mf7 +{ +public: + + typedef R result_type; + +private: + + typedef R (T::*F) (A1, A2, A3, A4, A5, A6, A7); + F f_; + +public: + + explicit mf7(F f): f_(f) {} + + R operator()(T * p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) const + { + return (p->*f_)(a1, a2, a3, a4, a5, a6, a7); + } + + template R operator()(U & u, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) const + { + return (get_pointer(u)->*f_)(a1, a2, a3, a4, a5, a6, a7); + } + + R operator()(T & t, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) const + { + return (t.*f_)(a1, a2, a3, a4, a5, a6, a7); + } +}; + +// cmf7 + +template class cmf7 +{ +public: + + typedef R result_type; + +private: + + typedef R (T::*F) (A1, A2, A3, A4, A5, A6, A7) const; + F f_; + +public: + + explicit cmf7(F f): f_(f) {} + + template R operator()(U const & u, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) const + { + return (get_pointer(u)->*f_)(a1, a2, a3, a4, a5, a6, a7); + } + + R operator()(T const & t, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7) const + { + return (t.*f_)(a1, a2, a3, a4, a5, a6, a7); + } +}; + +// mf8 + +template class mf8 +{ +public: + + typedef R result_type; + +private: + + typedef R (T::*F) (A1, A2, A3, A4, A5, A6, A7, A8); + F f_; + +public: + + explicit mf8(F f): f_(f) {} + + R operator()(T * p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8) const + { + return (p->*f_)(a1, a2, a3, a4, a5, a6, a7, a8); + } + + template R operator()(U & u, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8) const + { + return (get_pointer(u)->*f_)(a1, a2, a3, a4, a5, a6, a7, a8); + } + + R operator()(T & t, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8) const + { + return (t.*f_)(a1, a2, a3, a4, a5, a6, a7, a8); + } +}; + +// cmf8 + +template class cmf8 +{ +public: + + typedef R result_type; + +private: + + typedef R (T::*F) (A1, A2, A3, A4, A5, A6, A7, A8) const; + F f_; + +public: + + explicit cmf8(F f): f_(f) {} + + R operator()(T const * p, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8) const + { + return (p->*f_)(a1, a2, a3, a4, a5, a6, a7, a8); + } + + template R operator()(U const & u, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8) const + { + return (get_pointer(u)->*f_)(a1, a2, a3, a4, a5, a6, a7, a8); + } + + R operator()(T const & t, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8) const + { + return (t.*f_)(a1, a2, a3, a4, a5, a6, a7, a8); + } +}; + +} // namespace _mfi + +// mem_fn + +template _mfi::mf0 mem_fn(R (T::*f) ()) +{ + return _mfi::mf0(f); +} + +template _mfi::cmf0 mem_fn(R (T::*f) () const) +{ + return _mfi::cmf0(f); +} + +template _mfi::mf1 mem_fn(R (T::*f) (A1)) +{ + return _mfi::mf1(f); +} + +template _mfi::cmf1 mem_fn(R (T::*f) (A1) const) +{ + return _mfi::cmf1(f); +} + +template _mfi::mf2 mem_fn(R (T::*f) (A1, A2)) +{ + return _mfi::mf2(f); +} + +template _mfi::cmf2 mem_fn(R (T::*f) (A1, A2) const) +{ + return _mfi::cmf2(f); +} + +template _mfi::mf3 mem_fn(R (T::*f) (A1, A2, A3)) +{ + return _mfi::mf3(f); +} + +template _mfi::cmf3 mem_fn(R (T::*f) (A1, A2, A3) const) +{ + return _mfi::cmf3(f); +} + +template _mfi::mf4 mem_fn(R (T::*f) (A1, A2, A3, A4)) +{ + return _mfi::mf4(f); +} + +template _mfi::cmf4 mem_fn(R (T::*f) (A1, A2, A3, A4) const) +{ + return _mfi::cmf4(f); +} + +template _mfi::mf5 mem_fn(R (T::*f) (A1, A2, A3, A4, A5)) +{ + return _mfi::mf5(f); +} + +template _mfi::cmf5 mem_fn(R (T::*f) (A1, A2, A3, A4, A5) const) +{ + return _mfi::cmf5(f); +} + +template _mfi::mf6 mem_fn(R (T::*f) (A1, A2, A3, A4, A5, A6)) +{ + return _mfi::mf6(f); +} + +template _mfi::cmf6 mem_fn(R (T::*f) (A1, A2, A3, A4, A5, A6) const) +{ + return _mfi::cmf6(f); +} + +template _mfi::mf7 mem_fn(R (T::*f) (A1, A2, A3, A4, A5, A6, A7)) +{ + return _mfi::mf7(f); +} + +template _mfi::cmf7 mem_fn(R (T::*f) (A1, A2, A3, A4, A5, A6, A7) const) +{ + return _mfi::cmf7(f); +} + +template _mfi::mf8 mem_fn(R (T::*f) (A1, A2, A3, A4, A5, A6, A7, A8)) +{ + return _mfi::mf8(f); +} + +template _mfi::cmf8 mem_fn(R (T::*f) (A1, A2, A3, A4, A5, A6, A7, A8) const) +{ + return _mfi::cmf8(f); +} + +} // namespace boost + +#endif // #ifndef BOOST_MEM_FN_HPP_INCLUDED diff --git a/mem_fn.html b/mem_fn.html new file mode 100644 index 0000000..470b01f --- /dev/null +++ b/mem_fn.html @@ -0,0 +1,282 @@ + + + + + + + Boost: mem_fn.hpp documentation + + + + + + + + +
+ + + + + + + + + +
+ c++boost.gif (8819 bytes) + + + + +

mem_fn.hpp

 1.02.0001 (2001-08-30)
+
 
+ + + + +
+ +

Files

+ + +

Purpose

+ +

+boost::mem_fn is a generalization of the standard functions +std::mem_fun and std::mem_fun_ref. It supports member +function pointers with more than one argument, and the returned function +object can take a pointer, a reference, or a smart pointer to an object +instance as its first argument. +

+ +

+mem_fn takes one argument, a pointer to a member function, and +returns a function object suitable for use with standard or user-defined +algorithms: +

+ +
+struct X
+{
+    void f();
+};
+
+void g(std::vector<X> & v)
+{
+    std::for_each(v.begin(), v.end(), boost::mem_fn(&X::f));
+};
+
+void h(std::vector<X *> const & v)
+{
+    std::for_each(v.begin(), v.end(), boost::mem_fn(&X::f));
+};
+
+void k(std::vector<boost::shared_ptr<X> > const & v)
+{
+    std::for_each(v.begin(), v.end(), boost::mem_fn(&X::f));
+};
+
+ +

+The returned function object takes the same arguments as the input member +function plus a "flexible" first argument that represents the object instance. +

+ +

+When the function object is invoked with a first argument x that is +neither a pointer nor a reference to the appropriate class (X in the +example above), it uses get_pointer(x) to obtain a pointer from +x. Library authors can "register" their smart pointer classes by +supplying an appropriate get_pointer overload, allowing mem_fn +to recognize and support them. +

+ +

+A get_pointer overload for boost::shared_ptr is supplied. +

+ +

+[Note: get_pointer is not restricted to return a pointer. Any object +that can be used in a member function call expression (x->*pmf)(...) +will work.] +

+ +

+[Note: the library uses an unqualified call to get_pointer. Therefore, +it will find, through argument-dependent lookup, get_pointer overloads +that are defined in the same namespace as the corresponding smart pointer +class, in addition to any boost::get_pointer overloads.] +

+ +

+All function objects returned by mem_fn expose a result_type +typedef that represents the return type of the member function. +

+ +

Interface

+ +

Synopsis

+ +
+namespace boost
+{
+
+template<class T> T * get_pointer(T * p);
+
+template<class T> T * get_pointer(shared_ptr<T> const & p);
+
+template<class R, class T> implementation-defined-1 mem_fn(R (T::*pmf) ());
+
+template<class R, class T> implementation-defined-2 mem_fn(R (T::*pmf) () const);
+
+template<class R, class T, class A1> implementation-defined-3 mem_fn(R (T::*pmf) (A1));
+
+template<class R, class T, class A1> implementation-defined-4 mem_fn(R (T::*pmf) (A1) const);
+
+template<class R, class T, class A1, class A2> implementation-defined-5 mem_fn(R (T::*pmf) (A1, A2));
+
+template<class R, class T, class A1, class A2> implementation-defined-6 mem_fn(R (T::*pmf) (A1, A2) const);
+
+// implementation defined number of additional overloads for more arguments
+
+}
+
+ +

Common requirements

+ +

+All implementation-defined-N types mentioned in the Synopsis are +CopyConstructible and Assignable. +Their copy constructors and assignment operators do not throw exceptions. +implementation-defined-N::result_type is defined as +the return type of the member function pointer passed as an argument to mem_fn +(R in the Synopsis.) +

+ +

get_pointer

+ +

template<class T> T * get_pointer(T * p)

+ +

+Returns: p. +

+

+Throws: Nothing. +

+ +

template<class T> T * get_pointer(shared_ptr<T> const & p)

+ +

+Returns: p.get(). +

+

+Throws: Nothing. +

+ +

mem_fn

+ +

template<class R, class T> implementation-defined-1 mem_fn(R (T::*pmf) ())

+ +

+Returns: a function object f such that the expression +f(t) is equivalent to (t.*pmf)() when t +is an l-value of type T, (get_pointer(t)->*pmf)() otherwise. +

+

+Throws: Nothing. +

+ +

template<class R, class T> implementation-defined-2 mem_fn(R (T::*pmf) () const)

+ +

+Returns: a function object f such that the expression +f(t) is equivalent to (t.*pmf)() when t +is of type T [const], (get_pointer(t)->*pmf)() otherwise. +

+

+Throws: Nothing. +

+ +

template<class R, class T, class A1> implementation-defined-3 mem_fn(R (T::*pmf) (A1))

+ +

+Returns: a function object f such that the expression +f(t, a1) is equivalent to (t.*pmf)(a1) when t +is an l-value of type T, (get_pointer(t)->*pmf)(a1) otherwise. +

+

+Throws: Nothing. +

+ +

template<class R, class T, class A1> implementation-defined-4 mem_fn(R (T::*pmf) (A1) const)

+ +

+Returns: a function object f such that the expression +f(t, a1) is equivalent to (t.*pmf)(a1) when t +is of type T [const], (get_pointer(t)->*pmf)(a1) otherwise. +

+

+Throws: Nothing. +

+ +

template<class R, class T, class A1, class A2> implementation-defined-5 mem_fn(R (T::*pmf) (A1, A2))

+ +

+Returns: a function object f such that the expression +f(t, a1, a2) is equivalent to (t.*pmf)(a1, a2) when t +is an l-value of type T, (get_pointer(t)->*pmf)(a1, a2) otherwise. +

+

+Throws: Nothing. +

+ +

template<class R, class T, class A1, class A2> implementation-defined-6 mem_fn(R (T::*pmf) (A1, A2) const)

+ +

+Returns: a function object f such that the expression +f(t, a1, a2) is equivalent to (t.*pmf)(a1, a2) when t +is of type T [const], (get_pointer(t)->*pmf)(a1, a2) otherwise. +

+

+Throws: Nothing. +

+ +

Implementation

+ +

+This implementation supports member functions with up to eight arguments. +This is not an inherent limitation of the design, but an implementation +detail. +

+ +

Acknowledgements

+ +

+Rene Jager's initial suggestion of using traits classes to make +mem_fn adapt to user-defined smart pointers inspired the +get_pointer-based design. +

+ +

+Numerous improvements were suggested during the formal review period by +Richard Crossley, Jens Maurer, Ed Brey, and others. Review manager +was Darin Adler. +

+ + +




Copyright © 2001 by Peter Dimov and Multi Media +Ltd. Permission to copy, use, modify, sell and distribute this document is +granted provided this copyright notice appears in all copies. This document +is provided "as is" without express or implied warranty, and with +no claim as to its suitability for any purpose.

+ +
+ +
+ + + diff --git a/mem_fn_test.cpp b/mem_fn_test.cpp new file mode 100644 index 0000000..6dcf13f --- /dev/null +++ b/mem_fn_test.cpp @@ -0,0 +1,175 @@ +#if defined(_MSC_VER) && !defined(__ICL) +#pragma warning(disable: 4786) // identifier truncated in debug info +#pragma warning(disable: 4710) // function not inlined +#pragma warning(disable: 4711) // function selected for automatic inline expansion +#pragma warning(disable: 4514) // unreferenced inline removed +#endif + +// +// mem_fn_test.cpp - a test for mem_fn.hpp +// +// Version 1.02.0001 (2001-08-30) +// +// Copyright (c) 2001 Peter Dimov and Multi Media Ltd. +// +// Permission to copy, use, modify, sell and distribute this software +// is granted provided this copyright notice appears in all copies. +// This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. +// + +#include +#include +#include + +struct X +{ + mutable unsigned int hash; + + X(): hash(0) {} + + int f0() { f1(17); return 0; } + int g0() const { g1(17); return 0; } + + int f1(int a1) { hash = (hash * 17041 + a1) % 32768; return 0; } + int g1(int a1) const { hash = (hash * 17041 + a1 * 2) % 32768; return 0; } + + int f2(int a1, int a2) { f1(a1); f1(a2); return 0; } + int g2(int a1, int a2) const { g1(a1); g1(a2); return 0; } + + int f3(int a1, int a2, int a3) { f2(a1, a2); f1(a3); return 0; } + int g3(int a1, int a2, int a3) const { g2(a1, a2); g1(a3); return 0; } + + int f4(int a1, int a2, int a3, int a4) { f3(a1, a2, a3); f1(a4); return 0; } + int g4(int a1, int a2, int a3, int a4) const { g3(a1, a2, a3); g1(a4); return 0; } + + int f5(int a1, int a2, int a3, int a4, int a5) { f4(a1, a2, a3, a4); f1(a5); return 0; } + int g5(int a1, int a2, int a3, int a4, int a5) const { g4(a1, a2, a3, a4); g1(a5); return 0; } + + int f6(int a1, int a2, int a3, int a4, int a5, int a6) { f5(a1, a2, a3, a4, a5); f1(a6); return 0; } + int g6(int a1, int a2, int a3, int a4, int a5, int a6) const { g5(a1, a2, a3, a4, a5); g1(a6); return 0; } + + int f7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) { f6(a1, a2, a3, a4, a5, a6); f1(a7); return 0; } + int g7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) const { g6(a1, a2, a3, a4, a5, a6); g1(a7); return 0; } + + int f8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { f7(a1, a2, a3, a4, a5, a6, a7); f1(a8); return 0; } + int g8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) const { g7(a1, a2, a3, a4, a5, a6, a7); g1(a8); return 0; } +}; + +int detect_errors(bool x) +{ + if(x) + { + std::cerr << "no errors detected.\n"; + return 0; + } + else + { + std::cerr << "test failed.\n"; + return 1; + } +} + +int main() +{ + using boost::mem_fn; + + X x; + + X const & rcx = x; + X const * pcx = &x; + + boost::shared_ptr sp(new X); + + mem_fn(&X::f0)(x); + mem_fn(&X::f0)(&x); + mem_fn(&X::f0)(sp); + + mem_fn(&X::g0)(x); + mem_fn(&X::g0)(rcx); + mem_fn(&X::g0)(&x); + mem_fn(&X::g0)(pcx); + mem_fn(&X::g0)(sp); + + mem_fn(&X::f1)(x, 1); + mem_fn(&X::f1)(&x, 1); + mem_fn(&X::f1)(sp, 1); + + mem_fn(&X::g1)(x, 1); + mem_fn(&X::g1)(rcx, 1); + mem_fn(&X::g1)(&x, 1); + mem_fn(&X::g1)(pcx, 1); + mem_fn(&X::g1)(sp, 1); + + mem_fn(&X::f2)(x, 1, 2); + mem_fn(&X::f2)(&x, 1, 2); + mem_fn(&X::f2)(sp, 1, 2); + + mem_fn(&X::g2)(x, 1, 2); + mem_fn(&X::g2)(rcx, 1, 2); + mem_fn(&X::g2)(&x, 1, 2); + mem_fn(&X::g2)(pcx, 1, 2); + mem_fn(&X::g2)(sp, 1, 2); + + mem_fn(&X::f3)(x, 1, 2, 3); + mem_fn(&X::f3)(&x, 1, 2, 3); + mem_fn(&X::f3)(sp, 1, 2, 3); + + mem_fn(&X::g3)(x, 1, 2, 3); + mem_fn(&X::g3)(rcx, 1, 2, 3); + mem_fn(&X::g3)(&x, 1, 2, 3); + mem_fn(&X::g3)(pcx, 1, 2, 3); + mem_fn(&X::g3)(sp, 1, 2, 3); + + mem_fn(&X::f4)(x, 1, 2, 3, 4); + mem_fn(&X::f4)(&x, 1, 2, 3, 4); + mem_fn(&X::f4)(sp, 1, 2, 3, 4); + + mem_fn(&X::g4)(x, 1, 2, 3, 4); + mem_fn(&X::g4)(rcx, 1, 2, 3, 4); + mem_fn(&X::g4)(&x, 1, 2, 3, 4); + mem_fn(&X::g4)(pcx, 1, 2, 3, 4); + mem_fn(&X::g4)(sp, 1, 2, 3, 4); + + mem_fn(&X::f5)(x, 1, 2, 3, 4, 5); + mem_fn(&X::f5)(&x, 1, 2, 3, 4, 5); + mem_fn(&X::f5)(sp, 1, 2, 3, 4, 5); + + mem_fn(&X::g5)(x, 1, 2, 3, 4, 5); + mem_fn(&X::g5)(rcx, 1, 2, 3, 4, 5); + mem_fn(&X::g5)(&x, 1, 2, 3, 4, 5); + mem_fn(&X::g5)(pcx, 1, 2, 3, 4, 5); + mem_fn(&X::g5)(sp, 1, 2, 3, 4, 5); + + mem_fn(&X::f6)(x, 1, 2, 3, 4, 5, 6); + mem_fn(&X::f6)(&x, 1, 2, 3, 4, 5, 6); + mem_fn(&X::f6)(sp, 1, 2, 3, 4, 5, 6); + + mem_fn(&X::g6)(x, 1, 2, 3, 4, 5, 6); + mem_fn(&X::g6)(rcx, 1, 2, 3, 4, 5, 6); + mem_fn(&X::g6)(&x, 1, 2, 3, 4, 5, 6); + mem_fn(&X::g6)(pcx, 1, 2, 3, 4, 5, 6); + mem_fn(&X::g6)(sp, 1, 2, 3, 4, 5, 6); + + mem_fn(&X::f7)(x, 1, 2, 3, 4, 5, 6, 7); + mem_fn(&X::f7)(&x, 1, 2, 3, 4, 5, 6, 7); + mem_fn(&X::f7)(sp, 1, 2, 3, 4, 5, 6, 7); + + mem_fn(&X::g7)(x, 1, 2, 3, 4, 5, 6, 7); + mem_fn(&X::g7)(rcx, 1, 2, 3, 4, 5, 6, 7); + mem_fn(&X::g7)(&x, 1, 2, 3, 4, 5, 6, 7); + mem_fn(&X::g7)(pcx, 1, 2, 3, 4, 5, 6, 7); + mem_fn(&X::g7)(sp, 1, 2, 3, 4, 5, 6, 7); + + mem_fn(&X::f8)(x, 1, 2, 3, 4, 5, 6, 7, 8); + mem_fn(&X::f8)(&x, 1, 2, 3, 4, 5, 6, 7, 8); + mem_fn(&X::f8)(sp, 1, 2, 3, 4, 5, 6, 7, 8); + + mem_fn(&X::g8)(x, 1, 2, 3, 4, 5, 6, 7, 8); + mem_fn(&X::g8)(rcx, 1, 2, 3, 4, 5, 6, 7, 8); + mem_fn(&X::g8)(&x, 1, 2, 3, 4, 5, 6, 7, 8); + mem_fn(&X::g8)(pcx, 1, 2, 3, 4, 5, 6, 7, 8); + mem_fn(&X::g8)(sp, 1, 2, 3, 4, 5, 6, 7, 8); + + return detect_errors(x.hash == 17610 && sp->hash == 2155); +}