- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在研究一个 AI 动态链接库。它将被显式链接,所有这些都由一个简单的头文件包含处理。
我目前正在尝试为 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();
}
};
----------------------------EXE端示例------------ --------------------------
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;
}
------------------------DLL端代码---------------- ------------------
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);
}
}
------------------------DLL 端用于保持回调的结构------------ --------------
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);
}
};
由于这一行,目前 DLL 无法编译:
AddAction<int>(id, comm, mfid);
因为它试图覆盖
virtual void Execute() const =0;
带有返回 int 的函数。给出非协方差错误。我知道我找错了树,但目前我也看不到任何其他解决方案。
有没有人对如何做得更好有任何建议?即使它只是一个模糊的方向,我也应该将注意力集中在我会很感激。如果您愿意阅读所有这些内容,非常感谢!
最佳答案
我觉得你太复杂了。看这个:
/*************** 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);
}
关于c++ - 阿格!反差挫败回调阴谋!想要更改虚拟返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3307302/
我正在研究 learnyounode 的 HTTP 客户端作业。 我想知道为什么控制台记录来自response.on(“end”,callback)的数据仅输出预期输出的最后一部分,而控制台记录来自r
我正在尝试创建一个对象列表(在我的示例中为 List),我在其中使用 json 将对象添加到此列表,但该列表仍为空。这是我的代码: public List readCardsFromJson() {
我有一个 JavaScript 函数“print_something”,它在大约 300 个 jsp 帮助页面中实现。我发现这个“print_something”函数必须被纠正。所以我正在寻找一个不更
有 2 个 HTML 下拉列表,一个用于 12 小时时间,一个用于每小时 5 分钟的时间间隔。 .. 1 .. 12 .. 0 .. 55 .. 一直在尝试使用 if/
我有一个 A 类,我打算在它与设备驱动程序交互时将其放入共享库中。 我有一个 B 类,将来可能是 C、D、E...,它将使用共享库中的 A 类。 我想要在类 A 中设置回调函数的功能,以便当特定事件发
我需要能够在处理完 Observable.next() 之后执行回调。 我有一个组件“A”,它有一个主题使用 Subject.next() 发送通知。我有一个组件“B”,它订阅了 Subject.as
我有一张在顶部和底部单元格下方带有阴影的表格(此处使用 Matt Gallagher 的解决方案:http://cocoawithlove.com/2009/08/adding-shadow-effe
有人可以向我解释一下为什么这段代码有效 renderSquare(i) { return ( this.handleClick(i)} /> ); } 但
我可以让两个不同的客户端监听相同的 WCF 回调并让它们都接收相同的数据而不必进行两次处理吗? 最佳答案 不是真的 - 至少不是直接的。你所描述的听起来很像发布/订阅模式。 WCF 服务基本上在任何给
我是 SignalR 的新手,如果这个问题太明显,我深表歉意,但我在文档中找不到任何答案。 这是我的代码。 /*1*/ actions.client.doActionA = function (r
我有这个应用程序,您可以在其中输入一些文本并按下一个按钮,将此文本添加到自定义小部件中。这是代码: import 'dart:core'; import 'package:flutter/materi
我读到当您还想使用模型回调时不能使用 Keras 进行交叉验证,但是 this post表明这毕竟是可能的。但是,我很难将其纳入我的上下文。 为了更详细地探讨这个问题,我正在关注 machinelea
我尝试在重力表单中提交表单失败后运行一些 jQuery 代码,也就是验证发现错误时。 我尝试使用 Ajax:complete 回调,但它根本不触发。 我尝试运行的代码基本上将监听器添加到选择下拉列表中
我有一个 $image,我 .fadeIn 和 .fadeOut,然后 .remove .fadeOut 完成。这是我的代码: $image .fadeIn() .fadeOut(func
我正在处理一个自定义文件路径类,它应该始终执行一个函数 写入相应的系统文件及其文件对象后 关闭。该函数将文件路径的内容上传到远程位置。 我希望上传功能完全在用户的幕后发生 透视,即用户可以像使用其他任
这里是 javascript 新手,所以回调在我的大脑中仍然有点不确定。 我想做的是:给定一个“菜单”,它是一个 objectId 数组,查询与该 objectId 相对应的每个 foodItem,获
我正在学习回调,我编写了以下代码: var http = require('http'); var str = ""; var count = 2; function jugglingAsync(ca
这是我的困境,我有一系列被调用的函数,我正在使用回调函数在它们完成时执行函数。回调返回一个值并且效果也很好,我的问题是当我向回调添加参数时我无法再访问返回值。这是一个有效的例子: function m
This question already has answers here: Explanation of function pointers (4个答案) 上个月关闭。 如何将函数指针作为参数传递
我无法让以下代码工作。假设 ajax 调用有效,并且 msg['username'] 预设为 'john'。我想我对如何将变量传递给回调感到困惑。编辑:我认为我的主要困惑是如何从 Ajax 中获取“m
我是一名优秀的程序员,十分优秀!