C++ 阿格!反差挫败了回叫阴谋!要更改虚拟返回类型吗

C++ 阿格!反差挫败了回叫阴谋!要更改虚拟返回类型吗,c++,boost,callback,C++,Boost,Callback,我在一个AI动态链接库上工作。它将被显式链接,所有这些都由一个简单的头include处理 我目前正在尝试为DLL创建代码,以便能够调用主EXE中的函数来操纵世界,并且能够查询函数以了解世界的状态。我现在可以调用无参数的void返回函数(无论是全局函数还是成员函数) 我现在正试图实现在EXE中调用函数并从中获取返回值的功能。(非空函数)事情进展不顺利。。我已经尝试了一段时间,现在试图找到最好的方法来实现这一点。我可以在DLL中使用boost库,但不能在EXE中使用 我将在这里转储相关代码。我知道这

我在一个AI动态链接库上工作。它将被显式链接,所有这些都由一个简单的头include处理

我目前正在尝试为DLL创建代码,以便能够调用主EXE中的函数来操纵世界,并且能够查询函数以了解世界的状态。我现在可以调用无参数的void返回函数(无论是全局函数还是成员函数)

我现在正试图实现在EXE中调用函数并从中获取返回值的功能。(非空函数)事情进展不顺利。。我已经尝试了一段时间,现在试图找到最好的方法来实现这一点。我可以在DLL中使用boost库,但不能在EXE中使用

我将在这里转储相关代码。我知道这是很多,但我希望有人能指出我可以如何提高

即使您没有阅读代码(非常容易理解),了解如何从总体上解决这个问题也会非常有帮助

下面是(我已经尝试尽可能多地删掉不相关的代码部分):

---------------------------------------EXE端头--------------------------------------

typedef void (*Command)();
typedef void (*CommandMF)(int, std::string);

typedef void (*AddEntityFunction)(int&);
typedef void (*AddActionToEntityFunction)(int, Command);
typedef void (*AddActionToEntityFunctionMF)(int, CommandMF, std::string);
typedef void (*UpdateDoubleThinkFunction)();


class MemberFunctionStorageExecute
{
    public:
        virtual void Execute() const =0;
    };


template <class T>
struct MemberFunctionStorage : MemberFunctionStorageExecute
{
    typedef void (T::*MemberFunctionWorker)();

        MemberFunctionWorker mbw;
    T *obj;

    virtual void Execute() const
{
    (obj->*mbw)();
}
};

typedef std::map<std::string, MemberFunctionStorageExecute*> MemberFunctionsList;
typedef std::map<int, MemberFunctionsList*> ListofLists;


//Template hack to allow static properties inside header
template <class T>
class DoubleThinkInterfaceImpl
{
protected: 
    static HINSTANCE hinstDLL;
    static AddEntityFunction DLLAddEntity;
    static AddActionToEntityFunction DLLAddActionToEntity;
    static AddActionToEntityFunctionMF DLLAddActionToEntityMF;
    static UpdateDoubleThinkFunction DLLUpdateDoubleThink;

    static ListofLists m_plistlist;
};
template <class T>
HINSTANCE DoubleThinkInterfaceImpl<T>::hinstDLL;
template <class T>
AddEntityFunction DoubleThinkInterfaceImpl<T>::DLLAddEntity;
template <class T>
UpdateDoubleThinkFunction DoubleThinkInterfaceImpl<T>::DLLUpdateDoubleThink;
template <class T>
AddActionToEntityFunction DoubleThinkInterfaceImpl<T>::DLLAddActionToEntity;
template <class T>
AddActionToEntityFunctionMF DoubleThinkInterfaceImpl<T>::DLLAddActionToEntityMF;
template <class T>
ListofLists DoubleThinkInterfaceImpl<T>::m_plistlist;



class DoubleThinkInterface : protected DoubleThinkInterfaceImpl<int>
{
private:
    int m_pid;
    MemberFunctionsList m_pmemfunlist;


public:
    int ID()
    {
        return m_pid;
    }

    DoubleThinkInterface()
    {
        if(!hinstDLL)
        {
            hinstDLL = LoadLibrary("DoubleThink.dll");
            DLLAddEntity = (AddEntityFunction)GetProcAddress(hinstDLL, "AddEntity");
            DLLUpdateDoubleThink = (UpdateDoubleThinkFunction)GetProcAddress(hinstDLL, "Update");
            DLLAddActionToEntity = (AddActionToEntityFunction)GetProcAddress(hinstDLL, "AddActionToEntity");
            DLLAddActionToEntityMF = (AddActionToEntityFunctionMF)GetProcAddress(hinstDLL, "AddActionToEntityMF");
        }
        DLLAddEntity(m_pid);
        DoubleThinkInterface::m_plistlist.insert(std::pair<int, MemberFunctionsList*>(m_pid, &m_pmemfunlist));
    }

    ~DoubleThinkInterface()
    {
        //if(hinstDLL != 0)
        //  FreeLibrary(hinstDLL);
    }

    void AddAction(Command action)
    {
        DLLAddActionToEntity(m_pid, action);
    }

    void Update()
    {
        DLLUpdateDoubleThink();
    }

    template <class T>
    void AddActionMF(T *object, void (T::*memberfunc)(), std::string actionName)
    {
        MemberFunctionStorage<T> *store = new MemberFunctionStorage<T>;
        store->mbw = memberfunc;
        store->obj = object;
        m_pmemfunlist.insert(std::pair<std::string, MemberFunctionStorageExecute*>(actionName, store));
        DLLAddActionToEntityMF(m_pid, &DoubleThinkInterface::ResolveMF, actionName);
    }

    static void ResolveMF(int idnum,std::string mfName)
    {
        ListofLists::iterator lit;
        lit = m_plistlist.find(idnum);
        MemberFunctionsList::iterator it;
        it = lit->second->find(mfName);
        it->second->Execute();
    }
};
class BaseEntity
{
public:
    DoubleThinkInterface dtInterface;
    BaseEntity(){}
    virtual ~BaseEntity(){}
};

class Humanoid : public BaseEntity
{
public:
    Humanoid(){}
    ~Humanoid(){}

    std::string name;

    void Move();

    int GetAge(){return 10;}
};

void Humanoid::Move()
{
    std::cout << name << ": I'm moving around and such \n";
}

void EndLifeAsWeKnowIt()
{
    cout << "Suddenly everything changed... \n";
}

int _tmain(int argc, _TCHAR* argv[])
{
    Humanoid *entity = new Humanoid();
    entity->name = "Bobby";
    entity->dtInterface.AddAction(&EndLifeAsWeKnowIt);
    entity->dtInterface.AddActionMF<Humanoid>(entity, &Humanoid::Move, "Move");

    entity->dtInterface.Update();

    int x; cin >> x;

    return 0;
}
DTEntityManager* DTEntityManager::Instance()
{
    static DTEntityManager instance;

    return &instance;
}

template<class T>
void AddAction(int id, void (*comm)(int, std::string), std::string mfid)
{
    DTEntity *ent = DTEntityManager::Instance()->GetEntityFromID(id);

    CommandMemberFunction<T> *newcomm = new CommandMemberFunction<T>();
    newcomm->comm = comm;
    newcomm->entityid = id;
    newcomm->mfid = mfid;

    ent->SetCommandMF(newcomm);
}

extern "C"
{
    DLL_EXPORT void AddEntity(int &idnumber)
    {
        DTEntity *entity = new DTEntity();
        idnumber = entity->ID();
        DTEntityManager::Instance()->RegisterEntity(entity);
    }

    DLL_EXPORT void AddActionToEntity(int id, void (*comm)())
    {
        DTEntity *ent = DTEntityManager::Instance()->GetEntityFromID(id);

        CommandGlobal<void> *newcomm = new CommandGlobal<void>();
        newcomm->comm = comm;

        ent->SetCommand(newcomm);
    }

    DLL_EXPORT void AddActionToEntityMF(int id, void (*comm)(int, std::string), std::string mfid)
    {
        AddAction<void>(id, comm, mfid);
    }

    DLL_EXPORT void AddActionToEntityMF_int(int id, void (*comm)(int, std::string), std::string mfid)
    {
        AddAction<int>(id, comm, mfid);
    }
}
class CommandBase
{
public:
    virtual void Execute() const =0;
};

template<class T>
struct CommandGlobal : CommandBase
{
    typedef boost::function<T ()> Command;
    Command comm;

    virtual T Execute() const
    {
        return comm();
    }
};

template<class T>
struct CommandMemberFunction : CommandBase
{
    typedef boost::function<T (int, std::string)> Command;
    Command comm;
    int entityid;
    std::string mfid;

    virtual T Execute() const
    {
        return comm(entityid, mfid);
    }
}; 
使用返回int的函数。给出非协方差误差。。我知道我找错了方向,但目前我也看不到任何其他解决办法


有人对如何做得更好有什么建议吗?即使它只是一个模糊的方向,我应该把我的注意力集中在我会感激它。如果你费心读这些,非常感谢

快速脏答:传递以执行对结果类型的引用,作为
void*
,并使
Execute
私有。然后将
Execute
包装在一个非虚拟包装器中,该包装器按值返回T并执行强制转换。

快速脏答:传递以执行对结果类型的引用作为
void*
,并将
Execute
设为私有。然后在非虚拟包装器中包装
Execute
,该包装器按值返回T并执行转换。

我认为您太复杂了。见此:

/*************** both ***************/
typedef void (*Prototype1)();
typedef void (*Prototype2)(int);
typedef int (*Prototype3)();

struct FuncList {
    Prototype3 func1;
    Prototype1 func2;
    Prototype1 func3;
    Prototype2 func4;
    Prototype1 func5;
    // ...
};

/*************** dll ***************/

FuncList func_list;

Prototype3 &func1 = func_list.func1;
Prototype1 &func2 = func_list.func2;
Prototype1 &func3 = func_list.func3;
Prototype2 &func4 = func_list.func4;
Prototype1 &func5 = func_list.func5;

/* DLLEXPORT */ void SetFuncList(const FuncList &list) { func_list = list; }

void UsageExample()
{
    /* Just call the function */
    func2();
}

/*************** exe ***************/

/* declarations (functions must be defined somewhere) */
int func1();
void func2();
void func3();
void func4(int);
void func5();

const FuncList func_list = {
    func1,
    func2,
    func3,
    func4,
    func5
};

typedef void (*SetFuncListProc)(const FuncList &list);
SetFuncListProc SetFuncList;

void Init()
{
    /* ... load the DLL, load "SetFuncList" ... */

    SetFuncList(func_list);
}

我觉得你太复杂了。见此:

/*************** both ***************/
typedef void (*Prototype1)();
typedef void (*Prototype2)(int);
typedef int (*Prototype3)();

struct FuncList {
    Prototype3 func1;
    Prototype1 func2;
    Prototype1 func3;
    Prototype2 func4;
    Prototype1 func5;
    // ...
};

/*************** dll ***************/

FuncList func_list;

Prototype3 &func1 = func_list.func1;
Prototype1 &func2 = func_list.func2;
Prototype1 &func3 = func_list.func3;
Prototype2 &func4 = func_list.func4;
Prototype1 &func5 = func_list.func5;

/* DLLEXPORT */ void SetFuncList(const FuncList &list) { func_list = list; }

void UsageExample()
{
    /* Just call the function */
    func2();
}

/*************** exe ***************/

/* declarations (functions must be defined somewhere) */
int func1();
void func2();
void func3();
void func4(int);
void func5();

const FuncList func_list = {
    func1,
    func2,
    func3,
    func4,
    func5
};

typedef void (*SetFuncListProc)(const FuncList &list);
SetFuncListProc SetFuncList;

void Init()
{
    /* ... load the DLL, load "SetFuncList" ... */

    SetFuncList(func_list);
}

这不需要将CommandBase作为模板吗?我希望DTEntities将这些结构(具有不同的Execute()返回类型)保存为CommandBase*的映射。这个解决方案会考虑到这一点吗?谢谢如果有人能用一点代码来澄清亚历山德拉的想法,我将不胜感激。谢谢大家。这难道不需要将CommandBase作为模板吗?我希望DTEntities将这些结构(具有不同的Execute()返回类型)保存为CommandBase*的映射。这个解决方案会考虑到这一点吗?谢谢如果有人能用一点代码来澄清亚历山德拉的想法,我将不胜感激。谢谢大家。
/*************** both ***************/
typedef void (*Prototype1)();
typedef void (*Prototype2)(int);
typedef int (*Prototype3)();

struct FuncList {
    Prototype3 func1;
    Prototype1 func2;
    Prototype1 func3;
    Prototype2 func4;
    Prototype1 func5;
    // ...
};

/*************** dll ***************/

FuncList func_list;

Prototype3 &func1 = func_list.func1;
Prototype1 &func2 = func_list.func2;
Prototype1 &func3 = func_list.func3;
Prototype2 &func4 = func_list.func4;
Prototype1 &func5 = func_list.func5;

/* DLLEXPORT */ void SetFuncList(const FuncList &list) { func_list = list; }

void UsageExample()
{
    /* Just call the function */
    func2();
}

/*************** exe ***************/

/* declarations (functions must be defined somewhere) */
int func1();
void func2();
void func3();
void func4(int);
void func5();

const FuncList func_list = {
    func1,
    func2,
    func3,
    func4,
    func5
};

typedef void (*SetFuncListProc)(const FuncList &list);
SetFuncListProc SetFuncList;

void Init()
{
    /* ... load the DLL, load "SetFuncList" ... */

    SetFuncList(func_list);
}