通过GUI修改代码
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

Hey this is more of a question, i want to know if it is possible to modify code through GUI asking because i was asked to see if i could create a GUI where the user can change certain attributes. i.e an exmaple is below

start %= -(status)
       >  lexeme[elementV]
       > -(lexeme[elementF])
       > +(inboundGroup);

Above is part of my code which is Boost SPIRIT which parses Strings so for example would it be possible to change the + to a * or - etc

+ = One 
- = optional
* = multiple

Do you think it would be possible to change that through a GUI i think it could be just not sure on how to do it?

Any help i will be very grateful

Thanks Shamari

#0

Everything is possible in programming ;-)

For dynamic modification of a program during execution, there are several solutions :

  • Use a dynamic language like LUA
  • Use a plugin system with dynamic loading

Since you require C++ and Boost Spirit, I think the best solution is to generate a plugin on the fly and load it afterwards.

Your program will generate code, compile it into a shared library (.so) and then load and execute it. (Some people will find that dirty. It's insecure also. But it's simple and it works.)

Here is an exemple for 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__

Note that we must use extern C or else, C++ name mangling will make it difficult to import symbols.

plugin.cpp :

#include "plugin.h"
#include <iostream>
using namespace std;

int process()
{
    int return_value = 0;

#include "plugin_content.inc.cpp"

    return return_value;
}

Note that I use a hack here, the code will be included from another file, "plugin_content.inc.cpp". The code from user will be put inside.

a script to build the plugin, "build_plugin.sh" :

#! /bin/sh

g++ -c -Wall -fPIC plugin.cpp -o plugin.o
gcc -shared  -o libplugin.so   plugin.o

Now the calling program, 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;
}

Now, build your program :

g++ -Wall -rdynamic -ldl main.cpp

and execute it :

a.out

and you get :

Hello World !
Hello from plugin !
Hello from plugin, updated !

The code I give you is very basic. For example, we should check if the compilation of the plugin is successful and report errors to the user. Now it's up to you to add more stuff.

推荐文章

Grails JSON最大长度

Grails JSON最大长度

推荐文章

如何在TRadioGroup周围隐藏边框

如何在TRadioGroup周围隐藏边框

推荐文章

同一模型不同条件下的cakephp多重分页

同一模型不同条件下的cakephp多重分页

推荐文章

如果codeigniter模型返回0行,则显示自定义错误消息。如何延伸到一般案件?

如果codeigniter模型返回0行,则显示自定义错误消息。如何延伸到一般案件?

推荐文章

FireBird SQL:跳过一个返回参数

FireBird SQL:跳过一个返回参数

推荐文章

如何在j2me中动态创建文本字段?

如何在j2me中动态创建文本字段?

推荐文章

一个应用程序有可能改变Android主屏幕吗?

一个应用程序有可能改变Android主屏幕吗?

推荐文章

如何在C++源代码、DLL或VC++编译器创建的任何文件中查找所有全局变量?

如何在C++源代码、DLL或VC++编译器创建的任何文件中查找所有全局变量?

推荐文章

如何将main方法移到Scala中的另一个类?

如何将main方法移到Scala中的另一个类?

推荐文章

.htaccess:将初始输出重写回子域

.htaccess:将初始输出重写回子域

推荐文章

为什么通过getJSON进行简单的google web搜索总是不起作用?

为什么通过getJSON进行简单的google web搜索总是不起作用?

推荐文章

Magento字符串问题

Magento字符串问题

推荐文章

mysql和sqlserver检索前1条记录的通用查询

mysql和sqlserver检索前1条记录的通用查询

推荐文章

Jetty作为一个库:如何处理多个线程?

Jetty作为一个库:如何处理多个线程?

推荐文章

在C中创建新目录

在C中创建新目录

推荐文章

打开pdf时收到以下消息“Internet无法下载……xyz fromabc.com网站无法在IE中的“缓存”中写入文件?

打开pdf时收到以下消息“Internet无法下载……xyz fromabc.com网站无法在IE中的“缓存”中写入文件?