通过GUI修改代码
Modify Code through a GUI
嘿,这更像是一个问题,我想知道是否可以通过GUI询问来修改代码,因为我被要求查看是否可以创建一个用户可以更改某些属性的GUI。即出口低于
start %= -(status)
> lexeme[elementV]
> -(lexeme[elementF])
> +(inboundGroup);
上面是我的代码的一部分,它是Boost SPIRIT which parses Strings
,所以例如,是否可以将+
更改为* or -
等
+ = One
- = optional
* = multiple
你认为通过GUI可以改变这一点吗?我认为它可能只是不确定如何做到?
任何帮助我都会非常感谢
感谢Shamari
编程中一切皆有可能;-)
对于程序在执行过程中的动态修改,有几种解决方案:
- 使用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__
请注意,我们必须使用外部C,否则,C++名称篡改将使导入符号变得困难。
插件.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
现在调用程序main.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 !
我给你的代码非常基本。例如,我们应该检查插件的编译是否成功,并向用户报告错误。现在由你来添加更多内容。
相关文章:
- 我应该如何修改此代码以使用给定字符串中的字母打印菱形图案
- 修改 VS Code 中的默认C++代码段
- 如何修改代码以打印出数组 v2
- 修改链表主函数代码,用户将在其中输入节点的索引和数据以及正确的消息
- 代码错误修改
- 我在代码中收到一个运行时错误,该错误如何通过修改最多一个元素来查找数组是否可以变得不减少
- 如何跟踪通过外部代码修改类变量的所有位置
- "check if member exists using enable_if"中修改的代码不起作用
- 我在让我的代码输出整个链表时遇到问题.它只是输出我修改过的一些文本文件,而不是整个东西
- 张量流内核代码修改
- 代码审查:常量字符 * 修改
- 如果我制作一个代码,每个线程都会修改数组的完全不同的部分,则将保持缓存相干性
- 如何查看修改后的代码,其中对内联函数进行了预处理
- 是否可以有一个零成本assert(),这样代码就不必在调试和发布版本之间进行修改
- Matlab 编码器错误 "for loop index expressions.." 如何修改代码?
- 如何修改CUDA代码以获得100%的GPU负载
- 如何修改此基于MFC的代码片段以使用自己选择的窗口类名
- 是否可以用最少的代码更改来修改函数调用
- 重新配置C++代码以处理“警告:未按顺序修改和访问参数”
- 针对windows的Linux c++代码修改