Dobiasd / FunctionalPlus

Functional Programming Library for C++. Write concise and readable C++ code.
http://www.editgym.com/fplus-api-search/
Boost Software License 1.0
2.1k stars 167 forks source link

make c++ class function to c pointer #254

Closed nblog closed 2 years ago

nblog commented 2 years ago

hi, please allow me non-standard english.

i encountered a situation on a project:

third party provides C interface:

typedef void (*callback)(int a);
void test1( callback cb );

i used c++ class in my own project

class mycls
{
    public:

        ~mycls() { };

        mycls() { 
            test1(
                // this->my_callback, must be static function 
            );
        };

        void my_callback(int a) {

        }
}

i found a way to solve it, but it was not the best result. callback using lambda with closures

class mycls
{
    public:

        ~mycls() { };

        mycls() { 
            auto my_callback = [this](int a) {
                ...
            };

            test1(
                make_function_ptr(my_callback)
            );
        };
}

is this feature suitable for join?

Dobiasd commented 2 years ago

Hi,

so you have this library with a C function in its interface, but what do you want to do with it using FunctionalPlus?

In case you want to pass it around to C++ functions, wrapping it in a lambda seems like a good idea.

What do you mean by "it was not the best result"?

is this feature suitable for join?

With "join", you mean, you'd like to create a pull request to have something merged into the master branch of FunctionalPlus?

nblog commented 2 years ago

What do you mean by "it was not the best result"?

I hope is a class:: method. but, I don’t know what to do

class mycls
{
    public:

        ~mycls() { };

        mycls() { 
            test1(
                make_function_ptr(this->my_callback);
            );
        };

        void my_callback(int a) {

        }
}

With "join", you mean, you'd like to create a pull request to have something merged into the master branch of FunctionalPlus?

Yes, I think it is a good function, but I don’t know if it meets the "FunctionalPlus".

Dobiasd commented 2 years ago

I hope is a class:: method. but, I don’t know what to do

Can't you just use a "normal" free function instead of a member function?

Yes, I think it is a good function, but I don’t know if it meets the "FunctionalPlus".

Ah, so you propose to add your make_function_ptr (and convert_lambda) to the library so that an arbitrary C++ function can be converted to a C compatible function pointer. Is this correct?

nblog commented 2 years ago

Can't you just use a "normal" free function instead of a member function?

Yes. can only use lambda function.

Ah, so you propose to add your make_function_ptr (and convert_lambda) to the library so that an arbitrary C++ function can be converted to a C compatible function pointer. Is this correct?

Yes.

Dobiasd commented 2 years ago

Ah, so you propose to add your make_function_ptr (and convert_lambda) to the library so that an arbitrary C++ function can be converted to a C compatible function pointer. Is this correct?

Yes.

Ok, got it. :slightly_smiling_face:


However, I still would like to understand, what you meant by the following:

i found a way to solve it, but it was not the best result.

If your solution is not the best result, I hesitate to put it into the library. What do you think is wrong with the result?


I also don't get the following:

I hope is a class:: method. but, I don’t know what to do

Does your proposed solution solve it, or are you still looking for a solution?

nblog commented 2 years ago

If your solution is not the best result, I hesitate to put it into the library. What do you think is wrong with the result?

make_function_ptrreally useful, but can be seen from the code that it only supports lambda function.

Does your proposed solution solve it, or are you still looking for a solution?

using lambda, I did get the desired result.


but I will continue to look for ways to achieve the following effect, let it support the member functions in the class. like make_function_ptr_plus ? 😋:

class mycls
{
    public:

        ~mycls() { };

        mycls() { 
            test1(
                make_function_ptr_plus(this->my_callback);
            );
        };

        void my_callback(int a) {

        }
}
Dobiasd commented 2 years ago

Ah, thanks. Now I understand. :+1:

A pointer to a static member function would be no problem, but the problem with using a "normal" member function this way is, that a member function not only takes the int a parameter but also the explicit this parameter, which you have to bind.

Maybe the following three links can be of help:


I'm not yet sure if it would make sense to add make_function_ptr (and convert_lambda) to FunctionalPlus though since FunctionalPlus up to now is not really concerned with C interoperability.