一切都在编程;-)
对于执行过程中程序的动态修改可能,有几种解决方案:
- 使用像LUA这样的动态语言
- 使用带动态插件的系统加载
由于您需要C++和Boost Spirit,所以我认为最好的解决方案是即时生成一个插件,然后加载它。
您的程序将生成代码,将其编译为共享库(.so),然后加载并执行它。 (有些人会发现,脏这是不安全的也不过它的简单和它的作品。)
这是Linux下的一个为例:plugin.h:
#ifndef PLUGIN_H__
#define PLUGIN_H__
#ifdef __cplusplus
extern "C" {
#endif
int process();
typedef int (*plugin_process_fn_ptr)();
#ifdef __cplusplus
}
#endif
#endif // PLUGIN_H__
请注意,我们必须使用extern C否则,C++名称修改将导致难以导入符号。
plugin.cpp:
#include "plugin.h"
#include <iostream>
using namespace std;
int process()
{
int return_value = 0;
#include "plugin_content.inc.cpp"
return return_value;
}
注意,我在这里使用一个黑客,代码将包括从另一个文件中, “plugin_content.inc.cpp”。用户的代码将被放入。
一个脚本来构建插件, “build_plugin.sh”:
#! /bin/sh
g++ -c -Wall -fPIC plugin.cpp -o plugin.o
gcc -shared -o libplugin.so plugin.o
现在调用程序,为主。CPP:
#include <iostream>
#include <fstream> // to open files
#include <dlfcn.h> // C lib to load dynamic libs
#include "plugin.h"
using namespace std;
// load the plugin and call the process() function fom it
static int process_via_plugin()
{
int return_value = -1;
void *lib_handle(NULL);
char *error(NULL);
char *plugin_lib = "./libplugin.so";
lib_handle = dlopen(plugin_lib, RTLD_LAZY);
if (!lib_handle)
{
cerr << "Error loading lib " << plugin_lib << " : " << dlerror() << endl;
exit(1);
}
char *plugin_fn = "process";
plugin_process_fn_ptr fn = (plugin_process_fn_ptr)dlsym(lib_handle, plugin_fn);
error = dlerror();
if (error)
{
cerr << "Error finding lib " << plugin_fn << " : " << error << endl;
exit(1);
}
// call the function loaded from lib
return_value = (*fn)();
dlclose(lib_handle);
lib_handle = NULL; // useless but for good habits ^^
return return_value;
}
// build or rebuild the plugin,
// we must call it when we change the plugin code code
static int build_plugin(string code)
{
{
char *plugin_code_file = "plugin_content.inc.cpp";
ofstream plugin_code(plugin_code_file, ios::out);
plugin_code << code << endl;
}
system("build_plugin.sh");
return 0;
}
// our program
int main(int argc, char *argv[])
{
cout << "Hello World !" << endl;
string code = ""
"cout << \"Hello from plugin !\" << endl;"
"";
// build a first version of the plugin and call it
build_plugin(code);
process_via_plugin();
// now we modify the code (use a GUI here)
code = ""
"cout << \"Hello from plugin, updated !\" << endl;"
"";
// rebuild the plugin and call it again
build_plugin(code);
process_via_plugin();
// do it again as much as you want.
return 0;
}
现在,建立你的程序:
g++ -Wall -rdynamic -ldl main.cpp
并执行它:
a.out
,你会得到:
Hello World !
Hello from plugin !
Hello from plugin, updated !
我给你的代码是非常基本的。例如,我们应该检查插件的编译是否成功,并向用户报告错误。现在取决于你添加更多的东西。
什么是您的操作系统? – Offirmo 2012-01-18 10:40:51