/*
 * NMSTL, the Networking, Messaging, Servers, and Threading Library for C++
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * This example is very similar to one in libasync.
 *
 */

#include <nmstl/ptr>
#include <nmstl/callback>

using namespace nmstl;
using namespace std;

void
printstrings(string a, string b, string c)
{
    cout << a << " " << b << " " << c << endl;
}

class Foo {
    string me;

public:
    Foo(string me) : me(me) {}

    void say(string a, string b, string c) {
        cout << me << " says: " << a << " " << b << " " << c << endl;
    }
};

int main()
{
    {
        // METHOD 1
        callback<void> cb0 = wrap(&printstrings, "cb0a", "cb0b", "cb0c");
        callback<void, string> cb1 = wrap(&printstrings, "cb1a", "cb1b");
        callback<void, string, string> cb2 = wrap(&printstrings, "cb2a");
        callback<void, string, string, string> cb3 = wrap(&printstrings);

        cb0();                        // prints: cb0a cb0b cb0c
        cb1("cb1c");                  // prints: cb1a cb1b cb1c
        cb2("cb2b", "cb2c");          // prints: cb2a cb2b cb2c
        cb3("cb3a", "cb3b", "cb3c");  // prints: cb3a cb3b cb3c

        Foo yomama("Yo Mama");
        callback<void> ocb0 = wrap(yomama, &Foo::say, "yo", "yo", "ma");
        callback<void, string> ocb1 = wrap(yomama, &Foo::say, "yo", "yo");
        callback<void, string, string> ocb2 = wrap(yomama, &Foo::say, "yo");
        callback<void, string, string, string> ocb3 = wrap(yomama, &Foo::say);

        ocb0();
        ocb1("MA");
        ocb2("YO", "MA");
        ocb3("YO", "YO", "MA");
    }

    {
        // METHOD 2: use constructors directly
        callback<void> cb0(&printstrings, "cb0a", "cb0b", "cb0c");
        callback<void, string> cb1(&printstrings, "cb1a", "cb1b");
        callback<void, string, string> cb2(&printstrings, "cb2a");
        callback<void, string, string, string> cb3(&printstrings);

        cb0();                        // prints: cb0a cb0b cb0c
        cb1("cb1c");                  // prints: cb1a cb1b cb1c
        cb2("cb2b", "cb2c");          // prints: cb2a cb2b cb2c
        cb3("cb3a", "cb3b", "cb3c");  // prints: cb3a cb3b cb3c

        Foo yomama("Yo Mama");
        callback<void> ocb0(yomama, &Foo::say, "yo", "yo", "ma");
        callback<void, string> ocb1(yomama, &Foo::say, "yo", "yo");
        callback<void, string, string> ocb2(yomama, &Foo::say, "yo");
        callback<void, string, string, string> ocb3(yomama, &Foo::say);

        ocb0();
        ocb1("MA");
        ocb2("YO", "MA");
        ocb3("YO", "YO", "MA");
    }

    return 0;
}
