通过GUI修改代码

Modify Code through a GUI

本文关键字:代码 修改 GUI 通过      更新时间:2023-10-16

嘿,这更像是一个问题,我想知道是否可以通过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 !

我给你的代码非常基本。例如,我们应该检查插件的编译是否成功,并向用户报告错误。现在由你来添加更多内容。