Home > Software engineering >  C access callback data
C access callback data

Time:03-21

I am attempting to create a wrapper around class functions. The purpose of my wrapper is to test input, output, and enforce order of operations with various calls throughout my program. I am trying to not make any changes to the callee class. Attached is an example of what I am trying to achieve, but unable to figure out.

Main.cpp

#include "func_warpper.h"
#include "func.h"

int main()
{
    func_wrapper fw
    fun func;
    int origValue = 5;
    fw.caller([&](int origValue) { func.f(origValue); }, origValue);
    int output = func.getResult().number;
    std::cout << " value outputed by function 2 : " << output << std::endl;
    // output
    // note that above line does give me the result I am looking for
    // however, I want to be able to get this inside the function of caller
    return 0;
}

func.h .... I want this to be unmodified

#ifndef FUN_H
#define FUN_H

class fun 
{
    public:
        struct result
        {
            int number;
        };
        fun();
        ~fun();
        void f(int value);
        struct result getResult(){return this->testResult;};
        private:
            struct result testResult;
};
#endif

func.cpp .... I want this to be unmodified

#include "func.h"


fun::fun(){
    this->testResult.number = 0;
    return;
}
fun::~fun(){
    return;
}
void fun::f(int value){
    int updateValue = value * 5;
    this->testResult.number = updateValue;
}

func_wrapper.h .... I can modify this until the cows come home, please go ham with recommended changes :)

class func_wrapper
{
public:
    struct new_result
    {
        int new_number;
    };
    func_wrapper();
    ~func_wrapper();

    void caller(std::function<void(int)> clb, int val);
    struct new_result getNewResult() { return this->new_testResult; };
    private:
        struct new_result new_testResult;
};

#endif

func_wrapper.cpp .... same as above, I can modify this until the cows come home, please go ham with recommended changes :)

#include "func_wrapper.h"


func_wrapper::func_wrapper()
{
    //ctor
    this->new_testResult.new_number = 0;
    return;
}

func_wrapper::~func_wrapper()
{
    //dtor
}

void func_wrapper::caller(std::function<void(int)> clb, int val)
{
    std::cout << " value entered into function: " << val << std::endl;
    // clb(val); seems to call the function but does not store locally anything
    clb(val);
    clb;
    // clb; seems to store all the information locally however I seem unable to 
    // to reach the infromation: clb -> [functor] -> func -> testResult -> number
    // would like ...
    int output = clb ???  // the result of what gets filled from number struct
    // if I attempt to #include func.h
    // func func;
    // func.getResult().number; locally the answer is zero with or without delay
}

Through several days of searching, I have not found anything that can help with this problem, to include similar enough questions on stack overflow. Any help would be greatly appreciated, thank you.

CodePudding user response:

So, my understanding is that inside func_wrapper::caller you want to be able to access the wrapped class that is inside your callback. Unfortuately, the way you are doing it, is impossible. There is no (legitimate) way to reach inside the function and access its arguments.

However, if you break up the operation into its component parts, you can do what you want. You would want a caller function more like this:

template <typename Type, typename Function>
void caller(Type&& functor, Function function, int val)
{
   std::cout << " value entered into function: " << val << std::endl;
    
   std::invoke(function, functor, val);

   std::cout << "value inside wrapper: " << functor.getResult().number << "\rn";
}

and then call it like this.

fw.caller(func, &fun::f, origValue);

https://godbolt.org/z/151YfEeoo

CodePudding user response:

@JohnFilleau had mentioned to pass the class object instead of the function from within the class. The following is the solution based on example code that he provided, and I modified to work with the example. I realize the question is confusing but would like to thank both JohnFilleau and Taekahn for the discussion.

In main.cpp

int main()
{
    func_wrapper fw;
    fun func;
    int origValue = 5;
    fw.caller2(func, origValue);
return 0:
}

func_wrapper::caller2

    void func_wrapper::caller2(fun& fun, int val)
{
    std::cout << " value entered into function: " << val << std::endl;
    fun.f(val);
    int output = fun.getResult().number;
    std::cout << " did this work: " << output << std::endl;

}

In the header I had to add

#include "func.h"

with the change to the header as follows

void caller2(fun& fun, int val);
  • Related