2012-01-17 71 views
0

嘿,這是更多的問題,我想知道是否有可能通過GUI修改代碼,因爲我被問到是否可以創建一個GUI,用戶可以更改某些屬性。即
的一個實例是低於通過GUI修改代碼

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

以上是我的代碼部分是Boost SPIRIT which parses Strings因此,例如,纔有可能改變+* or -

+ = One 
- = optional 
* = multiple 

你是否認爲這是可能的通過圖形用戶界面改變它,我認爲它可能只是不確定如何做到這一點?

任何幫助,我將非常感激

感謝Shamari

+0

什麼是您的操作系統? – Offirmo 2012-01-18 10:40:51

回答

1

一切都在編程;-)

對於執行過程中程序的動態修改可能,有幾種解決方案:

  • 使用像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 ! 

我給你的代碼是非常基本的。例如,我們應該檢查插件的編譯是否成功,並向用戶報告錯誤。現在取決於你添加更多的東西。