[英]Send a variable into a void pointer, or a void pointer reference in C++
I'm attempting map boost::functions
. 我正在尝试map
boost::functions
。 So I can call them by a string name, with input and output parameters. 因此,我可以使用带有输入和输出参数的字符串名称来调用它们。
Example: 例:
MyFuncs::CallFunc("MyClass::TestFunc", void* input, void* output);
I would like to create the input and output variables, and send a their pointers to MyFuncs::CallFunc()
to be handled. 我想创建输入和输出变量,并将它们的指针发送到
MyFuncs::CallFunc()
进行处理。
I can send a (void* input)
pointer into the function, and extract it's value to int, string etc. 我可以将
(void* input)
指针发送到函数中,并将其值提取为int,string等。
I cannot send a (void* output)
that allocates it's own space to receive a value. 我无法发送分配自己的空间来接收值的
(void* output)
。 I must create a new type(var)
inside the called function, or the variable will got out of scope once the function returns. 我必须在调用的函数内部创建一个
new type(var)
,否则一旦函数返回,变量将超出范围。
MyFuncs.h MyFuncs.h
//MyFuncs.h
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <map>
class MyFuncs
{
public:
static std::map<std::string, boost::function<void (void*, void*&)> > myfuncs;
template<class T>
static void RegisterFunc(const std::string& name, void (T::*func) (void*, void*&), T* instance)
{
myfuncs[name] = boost::bind(func, instance, _1, _2);
}
static void CallFunc(const std::string& name, void* input, void*& output)
{
myfuncs[name](input, output);
}
};
std::map<std::string, boost::function<void (void*, void*&)> > MyFuncs::myfuncs;
MyClass.h MyClass.h
//MyClass.h
#include "MyFuncs.h"
class MyClass
{
public:
MyClass()
{
MyFuncs::RegisterFunc("MyClass::GetNumber", &MyClass::GetNumber, this);
MyFuncs::RegisterFunc("MyClass::GetString", &MyClass::GetString, this);
MyFuncs::RegisterFunc("MyClass::EditNumber", &MyClass::EditNumber, this);
MyFuncs::RegisterFunc("MyClass::EditString", &MyClass::EditString, this);
}
void GetNumber(void* input, void*& output)
{
int var = 1234;
output = static_cast<void*>(new int(var)); //WORKS, var eats memory
//output = static_cast<void*>(&var); //ERROR, var goes out of scope
}
void GetString(void* input, void*& output)
{
std::string var = "Get test";
output = static_cast<void*>(new std::string(var)); //WORKS, var eats memory
//output = static_cast<void*>(&var); //ERROR, var goes out of scope
}
void EditNumber(void* input, void*& output)
{
int var = *static_cast<int*>(input); //WORKS, var gets 4321 OK
output = static_cast<void*>(new int(var)); //WORKS, var eats memory
//output = static_cast<void*>(&var); //ERROR, var goes out of scope
}
void EditString(void* input, void*& output)
{
std::string var = *static_cast<std::string*>(input); //WORKS, var gets "Edit test" OK
output = static_cast<void*>(new std::string(var)); //WORKS, var eats memory
//output = static_cast<void*>(&var); //ERROR, var goes out of scope
}
};
MyApp.cpp MyApp.cpp
//MyApp.cpp
#include "MyClass.h"
void main()
{
MyClass myclass;
void* in;
void* out;
MyFuncs::CallFunc("MyClass::GetNumber", NULL, out); //atempting to fill the variable
int getNumOut = *static_cast<int*>(out);
printf("MyClass::GetNumber = %d \n", getNumOut);
MyFuncs::CallFunc("MyClass::GetString", NULL, out); //atempting to fill the variable
std::string getStrOut = *static_cast<std::string*>(out);
printf("MyClass::GetString = %s \n", getStrOut.c_str());
int editNum = 4321;
in = static_cast<void*>(&editNum);
MyFuncs::CallFunc("MyClass::EditNumber", in, out); //atempting to fill the variable
int editNumOut = *static_cast<int*>(out);
printf("MyClass::EditNumber = %d \n", editNumOut);
std::string editStr = "Edit test";
in = static_cast<void*>(&editStr);
MyFuncs::CallFunc("MyClass::EditString", in, out); //atempting to fill the variable
std::string editStrOut = *static_cast<std::string*>(out);
printf("MyClass::EditString = %s \n", editStrOut.c_str());
getchar(); //wait for close
}
void* out
is declared inside main
. void* out
在main
内部声明。 For out
to be valid, it should point to something whose lifetime is atleast that of main
. 为了使
out
有效,它应该指向寿命至少与main
事物。 Since you have not declared data members of type int
and string
inside your class MyClass
which can be returned via out
, so you need to allocate the memory. 由于您尚未在类
MyClass
中声明可以通过out
返回的int
和string
类型的数据成员,因此您需要分配内存。
Suppose you declared data member int var;
假设您声明了数据成员
int var;
in class MyClass
then GetNumber would look like: 在
MyClass
类中,则GetNumber如下所示:
class MyClass
{
private:
int var;
public:
MyClass()
{
//Register function
var = 10;
}
void GetNumber(void* input, void*& output)
{
//int var = 1234; //<-- instead of using local "var", now use MyClass::var i.e this->var
output = static_cast<void*>(&(this->var));
}
};
SOLUTION 解
Without using static_cast or extra boost functions, I was able to use pure C++. 在不使用static_cast或额外的boost函数的情况下,我能够使用纯C ++。
*(int*)output = var;
and *(std::string*)output = var;
和
*(std::string*)output = var;
seem to do the trick. 似乎可以解决问题。
MyFuncs.h MyFuncs.h
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <map>
class MyFuncs
{
public:
//A list of function names linked to boost::functions.
static std::map<std::string, boost::function<void (void*, void*)> > myfuncs;
//Register a class function by name. MyFuncs::RegisterFunc("MyClass::Test", &MyClass::Test, instance);
template<class T>
static void RegisterFunc(const std::string& name, void (T::*func) (void*, void*), T* instance)
{
myfuncs[name] = boost::bind(func, instance, _1, _2);
}
//Call functions by name, with input and output. MyFunct::CallFunc("MyClass::Test", input, output);
static void CallFunc(const std::string& name, void* input, void* output)
{
myfuncs[name](input, output);
}
};
std::map<std::string, boost::function<void (void*, void*)> > MyFuncs::myfuncs;
MyClass.h MyClass.h
#include "MyFuncs.h"
class MyClass
{
public:
MyClass()
{
MyFuncs::RegisterFunc("MyClass::GetNumber", &MyClass::GetNumber, this);
MyFuncs::RegisterFunc("MyClass::GetString", &MyClass::GetString, this);
MyFuncs::RegisterFunc("MyClass::EditNumber", &MyClass::EditNumber, this);
MyFuncs::RegisterFunc("MyClass::EditString", &MyClass::EditString, this);
}
void GetNumber(void* input, void* output)
{
int var = 1234;
*(int*)output = var;
}
void GetString(void* input, void* output)
{
std::string var = "Get test";
*(std::string*)output = var;
}
void EditNumber(void* input, void* output)
{
int in = *(int*)input; //4321
int out = in - 4320; //result is 1;
*(int*)output = out;
}
void EditString(void* input, void* output)
{
std::string in = *(std::string*)input; //"Edit Test"
std::string out = in += " (edited)"; //result is "Edit test (edited)"
*(std::string*)output = out;
}
};
MyApp.cpp MyApp.cpp
#include "MyClass.h"
void main()
{
MyClass myclass;
int getNum;
MyFuncs::CallFunc("MyClass::GetNumber", NULL, &getNum);
printf("MyClass::GetNumber = %d \n", getNum);
std::string getStr;
MyFuncs::CallFunc("MyClass::GetString", NULL, &getStr);
printf("MyClass::GetString = %s \n", getStr.c_str());
int editNumIn = 4321;
int editNumOut;
MyFuncs::CallFunc("MyClass::EditNumber", &editNumIn, &editNumOut);
printf("MyClass::EditNumber = %d \n", editNumOut);
std::string editStrIn = "Edit test";
std::string editStrOut;
MyFuncs::CallFunc("MyClass::EditString", &editStrIn, &editStrOut);
printf("MyClass::EditString = %s \n", editStrOut.c_str());
getchar(); //wait for close
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.