8

Some time ago, Borland have introduced in their BCB evironment an extension to C++ language. This extension is a __closure keyword. The question is, if it is possible to implement such functionality in plain C++ or C++11? If you are not familiar with __closure keyword, below code provides explanation in comments.

Thanks in advance! Toreno

#include <stdio.h>

// __closure keyword is used here !
typedef void (__closure * MemberCallback)(int x, int y, int z);

class A
{
    private:

        MemberCallback callback;


    public:

        A() : callback(NULL)
        {
        }

        void setCallback(MemberCallback newCallback)
        {
            callback = newCallback;
        }

        void call(int x, int y, int z)
        {
            if(callback)
                callback(x, y, z);
            else
                printf("NOT SET(%i, %i, %i)\n", x, y, z);
        }
};

class B
{
    public:

        void func1(int x, int y, int z)
        {
            printf("FUNC 1(%i, %i, %i)\n", x, y, z);
        }

        void func2(int x, int y, int z)
        {
            printf("FUNC 2(%i, %i, %i)\n", x, y, z);
        }

};

int main()
{
    // A and B classes do not know about each other. There is no possibility
    // to for inheritance because B class can implement multiple instances
    // of callback function
    A a;
    B b;

    a.call(1, 2, 3);  // Prints: NOT SET(1, 2, 3)

    a.setCallback(b.func1);
    a.call(4, 5, 6);  // Prints: FUNC 1(4, 5, 6)

    a.setCallback(b.func2);
    a.call(7, 8, 9);  // Prints: FUNC 2(7, 8, 9)

    return 0;
}

2 Answers2

6

std::function is exactly what you're looking for. If you want to learn how such mechanism is actually implemented in the library, here's a good series of blog posts on it. Combine that with lambda functions for capturing of local variables.

UnknownGosu
  • 854
  • 6
  • 9
  • Great! This is what I was looking for :) Thank you! –  Jul 26 '15 at 08:37
  • actually I have converted the `typedef std::function MemberCallback;` and function call to: `a.setCallback( [&b](int x, int y, int z) { b.func1(x, y, z); } );` but got error: "term does not evaluate to a function taking 3 arguments". Have you any suggestion? –  Jul 26 '15 at 11:05
  • Works perfectly! Thanks! :) –  Jul 26 '15 at 15:06
3

Re-hash of the previous answer with the full code, for others like me that want a quick reference to a common pattern:

#include <functional>
#include <stdio.h>

// __closure replacement
typedef std::function<void(int x, int y, int z)>    MemberCallback;

class A
{
public:
    void setCallback( MemberCallback newCallback ) {
        callback_ = newCallback;
    }
    void call( int x, int y, int z ) {
        if ( callback_ )
            callback_( x, y, z );
        else
            printf( "NOT SET(%i, %i, %i)\n", x, y, z );
    }

private:
    MemberCallback  callback_;
};

class B
{
public:
    void func1( int x, int y, int z ) {
        printf( "FUNC 1(%i, %i, %i)\n", x, y, z );
    }
    void func2( int x, int y, int z ) {
        printf( "FUNC 2(%i, %i, %i)\n", x, y, z );
    }
};

int main( )
{
    // A and B classes do not know about each other. There is no possibility
    // to for inheritance because B class can implement multiple instances
    // of callback function
    A a;
    B b;

    a.call( 1, 2, 3 );  // Prints: NOT SET(1, 2, 3)

    a.setCallback( [&b](int x, int y, int z){ b.func1(x, y, z); } );
    a.call( 4, 5, 6 );      // Prints: FUNC 1(4, 5, 6)

    a.setCallback( [&b](int x, int y, int z){ b.func2(x, y, z); } );
    a.call( 7, 8, 9 );      // Prints: FUNC 2(7, 8, 9)

    return 0;
}

Output:

NOT SET(1, 2, 3)
FUNC 1(4, 5, 6)
FUNC 2(7, 8, 9)
nlucas
  • 31
  • 2
  • The only but huge disadvantage of the approach is that you need to specify entire lambda or `std::bind` rather just `b.func2` which is much easier and elegant form. The __closure ensures that the pointer to the member function is provided with pointer to its owner - so equivalent to std::bind, but much simpler to provide. – no one special Dec 03 '22 at 10:22