AutobahnCPP --> How to pass a class non static member function over RPC

#1

Hi folks,

New to using Autobahn CPP, its a solid library.

Able to get the basic functions working end to end. So if the function like in example (add2) is defined in the same file, we were able to pass it by reference to provide method

provide_future_add = session->provide(“com.examples.calculator.add2”, &add2)

Now If I define this method inside class, which becomes a non static member function of class, how to then send this member function to provide method??

I have tried member function pointers, binding etc, it works if a member function is static, but not in non-static case.

#Sample code:

ADD2Class

{

ADD2Class();

~ADD2Class();

public:

void add2(autobahn::wamp_invocation invocation); //add2 is defined exactly as the example

};

ADD2Class addKlass;

void (ADD2Class::*fptr)(autobahn::wamp_invocation invocation) = &ADD2Class::add2;

provide_future_add = session->provide(“com.examples.calculator.add2”, fptr) – Gives an error that invalid use of non-static member function. Aparantely we need to pass both the function and the object,

provide_future_add = session->provide(“com.examples.calculator.add2”, addKlass.add2) // # error

Can someone help me in resolving this simple issue? Scope is to able to pass a non-static member function

Priyank

0 Likes

#2

Hi,

to call a member function for specific instance you need pointer to the member function and "this" instance:

Eg see http://stackoverflow.com/a/8865807/884770

Cheers,
/Tobias

···

Am 23.09.2016 um 23:10 schrieb Priyank Kumar:

Hi folks,

New to using Autobahn CPP, its a solid library.
Able to get the basic functions working end to end. So if the function like
in example (add2) is defined in the same file, we were able to pass it by
reference to provide method
provide_future_add = session->provide("com.examples.calculator.add2", &add2)

Now If I define this method inside class, which becomes a non static member
function of class, how to then send this member function to provide method??
I have tried member function pointers, binding etc, it works if a member
function is static, but not in non-static case.

#Sample code:
ADD2Class
{
ADD2Class();
~ADD2Class();
public:
void add2(autobahn::wamp_invocation invocation); //add2 is defined exactly
as the example
};

ADD2Class addKlass;

void (ADD2Class::*fptr)(autobahn::wamp_invocation invocation) = &ADD2Class
::add2;
provide_future_add = session->provide("com.examples.calculator.add2", fptr)
-- Gives an error that invalid use of non-static member function.
Aparantely we need to pass both the function and the object,
provide_future_add = session->provide("com.examples.calculator.add2",
addKlass.add2) // # error

Can someone help me in resolving this simple issue? Scope is to able to
pass a non-static member function

Priyank

0 Likes

#3

Hi, Tobias,

Though it was not straight forward, had been stuck on it for last 3 weeks. Finally got one working solution using std::function

Here is the snippet, I can send the example, callee.cpp too, that may become as reference.

#include

class Some_class
{
public:
void add2_mf(autobahn::wamp_invocation invocation)
{
auto a = invocation->argument<uint64_t>(0);
auto b = invocation->argument<uint64_t>(1);

std::cerr << "Procedure com.examples.calculator.add23 invoked: " << a << ", " << b << std::endl;

invocation->result(std::make_tuple(a + b));

}
};

//C++ 11 way
typedef std::function<void(autobahn::wamp_invocation)> MF_ptr; //Defining Member pointer
Some_class sc;
MF_ptr cp11_fptr = std::bind(&Some_class::add2_mf, &sc, std::placeholders::_1); //Strong emphasis is &sc, referece of obj

//Register a function like this
provide_future = session->provide(“com.examples.calculator.add23”, (cp11_fptr)).
then(…)

Hope this helps someone who get stuck.
Priyank

···

On Sunday, September 25, 2016 at 5:59:57 AM UTC-7, Tobias Oberstein wrote:

Hi,

to call a member function for specific instance you need pointer to the
member function and “this” instance:

Eg see http://stackoverflow.com/a/8865807/884770

Cheers,

/Tobias

Am 23.09.2016 um 23:10 schrieb Priyank Kumar:

Hi folks,

New to using Autobahn CPP, its a solid library.

Able to get the basic functions working end to end. So if the function like

in example (add2) is defined in the same file, we were able to pass it by

reference to provide method

provide_future_add = session->provide(“com.examples.calculator.add2”, &add2)

Now If I define this method inside class, which becomes a non static member

function of class, how to then send this member function to provide method??

I have tried member function pointers, binding etc, it works if a member

function is static, but not in non-static case.

#Sample code:

ADD2Class

{

ADD2Class();

~ADD2Class();

public:

void add2(autobahn::wamp_invocation invocation); //add2 is defined exactly

as the example

};

ADD2Class addKlass;

void (ADD2Class::*fptr)(autobahn::wamp_invocation invocation) = &ADD2Class

::add2;

provide_future_add = session->provide(“com.examples.calculator.add2”, fptr)

– Gives an error that invalid use of non-static member function.

Aparantely we need to pass both the function and the object,

provide_future_add = session->provide(“com.examples.calculator.add2”,

addKlass.add2) // # error

Can someone help me in resolving this simple issue? Scope is to able to

pass a non-static member function

Priyank

0 Likes

#4

Hi Priyank,

Hi, Tobias,

Though it was not straight forward, had been stuck on it for last 3 weeks.

Great! I mean, great that you didn't give up and solved it!

Finally got one working solution using std::function

Here is the snippet, I can send the example, callee.cpp too, that may
become as reference.

That would be very nice. Because it seems to be useful in situations, and not obvious how to do.

If you can shelf out yet a litte more time, a PR that adds an example (with some comments/hints) to

https://github.com/crossbario/autobahn-cpp/tree/master/examples

would be highly appreciated!

Cheers,
/Tobias

···

Am 27.09.2016 um 19:05 schrieb Priyank Kumar:

#include <functional>

class Some_class
{
public:
    void add2_mf(autobahn::wamp_invocation invocation)
{
    auto a = invocation->argument<uint64_t>(0);
    auto b = invocation->argument<uint64_t>(1);

    std::cerr << "Procedure com.examples.calculator.add23 invoked: " << a
<< ", " << b << std::endl;

    invocation->result(std::make_tuple(a + b));
}
};

  //C++ 11 way
     typedef std::function<void(autobahn::wamp_invocation)> MF_ptr;
//Defining Member pointer
     Some_class sc;
     MF_ptr cp11_fptr = std::bind(&Some_class::add2_mf, &sc,
std::placeholders::_1); //Strong emphasis is &sc, referece of obj

//Register a function like this
provide_future = session->provide("com.examples.calculator.add23",
(cp11_fptr)).
then(....)

Hope this helps someone who get stuck.
Priyank

On Sunday, September 25, 2016 at 5:59:57 AM UTC-7, Tobias Oberstein wrote:

Hi,

to call a member function for specific instance you need pointer to the
member function and "this" instance:

Eg see http://stackoverflow.com/a/8865807/884770

Cheers,
/Tobias

Am 23.09.2016 um 23:10 schrieb Priyank Kumar:

Hi folks,

New to using Autobahn CPP, its a solid library.
Able to get the basic functions working end to end. So if the function

like

in example (add2) is defined in the same file, we were able to pass it

by

reference to provide method
provide_future_add = session->provide("com.examples.calculator.add2",

&add2)

Now If I define this method inside class, which becomes a non static

member

function of class, how to then send this member function to provide

method??

I have tried member function pointers, binding etc, it works if a member
function is static, but not in non-static case.

#Sample code:
ADD2Class
{
ADD2Class();
~ADD2Class();
public:
void add2(autobahn::wamp_invocation invocation); //add2 is defined

exactly

as the example
};

ADD2Class addKlass;

void (ADD2Class::*fptr)(autobahn::wamp_invocation invocation) =

&ADD2Class

::add2;
provide_future_add = session->provide("com.examples.calculator.add2",

fptr)

-- Gives an error that invalid use of non-static member function.
Aparantely we need to pass both the function and the object,
provide_future_add = session->provide("com.examples.calculator.add2",
addKlass.add2) // # error

Can someone help me in resolving this simple issue? Scope is to able to
pass a non-static member function

Priyank

0 Likes