Creating costom function param header and passing that.

Hello cplusplus forum,

I have this here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
int *funclist; int func_count = 0;
void push_func( int func )
{
	if( func_count == 0 ) funclist = new int[1];
	else
	{
		int *c;
		c = new int[func_count+1];
		for( int a = 0; a < func_count; a++ ) c[a] = funclist[a];
		delete funclist;
		funclist = c;
	}
	funclist[func_count] = func;
	func_count++;
}

void func_1( int a, double u )
{
	cout << "a1:" << a << "u:" << u << endl;
}


typedef void (*func_default) (void **);
void func_call( void **u )
{
	((func_default) funclist[0])(u);
}

int WINAPI WinMain(	HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	ToggleConsole();
	
	cout << "working!" << endl;
	push_func( (int)func_1 );
	struct aw {
		int a;
		double b;
	};

	aw u;
	u.a = 10;
	u.b = 99.5;
	func_call( (void**) &u );

func1 variables contain wrong values after calling this way
I dont know how is the data from one func to the ohter passed but
what im trying to do is
1
2
3
4
5
6
7
void myfunc( int var1, double var2 ) // just random variables and types

void main()
{
    add_forward( (int)myfunc )
    call_forward( (void *)( int 8, double 50.5 ) )
}

Basically i want to create a dynamic function params header i guess.
Im not sure how to put this into words what am i saying, im sorry.
Thanks!




If I get you, why not use the auto keyword? C++14 has introduced a feature such that we can have automatic types as function and lambda parameters.

Aceix.
1. Casting function pointers to integers is unsafe.
2. Casting function pointers in such a way that the function signature changes is very unsafe.
3. This:
 
(void *)( int 8, double 50.5 )
Doesn't do anything at all like you expect.
@helios
Why is this unsafe?
When it is safe and when unsafe?
* Is casting function pointers to integers unsafe only because
not all systems are 32bits, they can be 64, 128 etc...
or is there something else? *

How should i hold the function pointers then?
Can you give ma an example?

now i have given up on that idea and now using things this way:

1
2
3
4
5
6
7
int *funclist;
funclist = new int[1];

void myfunc( int a, double b );
funclist[0] = (int)myfunc;
typedef void (*myfunc_typedef) (int, double);
((myfunc_typedef) funclist[0])(8, 50.5);


Still better than nohting and its pretty dynamic.
Last edited on
Basically i want to

We all have desires. The question is what you actually need? It is highly likely that there is an entirely different approach that fulfills your real need without massacring the linker.
#1 is unsafe because function pointers, particularly member function pointers, may be larger than a machine word.
#2 is unsafe because you're lying to the compiler about how to generate code at the call site. You can end up smashing the stack.

How should i hold the function pointers then?
You need to come up with a single interface all exposed functions can share. For example,
1
2
//Callee must parse strings
void (*generic_function)(std::vector<std::string> *);
There's also Boost.Any

http://www.boost.org/doc/libs/1_56_0/doc/html/any.html
In what cases the function pointers can be larger?
How large can it be?
Can it be largen than long long variable?
Last edited on
> Basically i want to create a dynamic function params header i guess.
>> now i have given up on that idea

Why give up on a very feasible idea?

a. Wrap the parameters that need to be specified later std::reference_wrapper<>
b. Bind the function and the wrapped parameters std::bind()
c. Wrap the result of the bind std::function<>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <functional>
#include <list>

using function_list_type =  std::list< std::function< void() > > ;
static function_list_type function_list ;

template < typename CALLABLE, typename... ARGS >
function_list_type::iterator push_func( CALLABLE&& fn, ARGS&&... args )
{
    function_list.emplace_back( std::bind( std::forward<CALLABLE>(fn),
                                           std::forward<ARGS>(args)... ) );
    auto end = function_list.end() ;
    return --end ;
}

void call_forward( function_list_type::iterator iter )
{
    const auto& fn = *iter ;
    fn() ;
}

void func_1( int a, double u )
{ std::cout << "func1 - a: " << a << ", u:" << u << '\n' ; }

struct some_class
{
    int mem_fun( int a, int b, int c )
    {
        std::cout << "some_class::mem_fun - this: " << this
                  << ", a: " << a << ", b:" << b << ", c:" << c <<'\n' ;
        return v + a -b + c ;
    }
    int v = 0 ;
};

int main()
{
    int f1_a = 0 ;
    double f1_b = 0 ;
    auto f1 = push_func( func_1, std::ref(f1_a), std::ref(f1_b) ) ;

    some_class object ;
    some_class* scmf_this = std::addressof(object) ;
    int scmf_a = 0, scmf_b = 0, scmf_c = 0 ;
    auto scmf = push_func( &some_class::mem_fun, scmf_this,
                           std::ref(scmf_a), std::ref(scmf_b), std::ref(scmf_c) ) ;

    call_forward(f1) ;
    call_forward(scmf) ;
    std::cout << "-----------\n" ;

    f1_a = 23 ;
    f1_b = 67.89 ;
    call_forward(f1) ;

    scmf_a = scmf_b = scmf_c = 999 ;
    call_forward(scmf) ;
}

http://coliru.stacked-crooked.com/a/ea5fbe0f7a3f63a2
Topic archived. No new replies allowed.