2012-01-17 2 views
0

안녕하세요 이것은 질문이 더 많습니다. 사용자가 특정 속성을 변경할 수있는 GUI를 만들 수 있는지 묻는 메시지가 표시되어 GUI를 통해 코드를 수정할 수 있는지 알고 싶습니다. 즉 exmaple이GUI를 통해 코드 수정

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

아래 위 Boost SPIRIT which parses Strings 그래서 예를 들어는 +로 변경 할 수있을 것입니다 내 코드의 일부입니다 * or -

+ = One 
- = optional 
* = multiple 

당신은 그것이 가능하다고 생각하십니까 GUI를 통해 그것을 바꾸려면 어떻게해야할지 모르겠습니다.

어떤 도움을 나는 매우 감사하게 될 것입니다

감사 Shamari

+0

OS가 무엇입니까? – Offirmo

답변

1

모든 프로그래밍 ;-) 실행 중에 프로그램의 동적 변경에 대한

에서 가능하며, 거기에 몇 가지 솔루션 :

  • LUA와 같은 동적 언어 사용
  • 동적 인 플러그인 시스템 사용 로딩

C++과 부스트 스피릿이 필요하므로 최선의 해결책은 즉시 플러그인을 생성하여로드하는 것입니다.

프로그램에서 코드를 생성하고 공유 라이브러리 (.so)으로 컴파일 한 다음로드하여 실행하십시오. (. 어떤 사람들은 또한 불안의 더티를 찾을 수 그러나 그것은 간단하고 그것을 작동합니다..) 여기

는 리눅스에 대한 exemple입니다 : 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 ! 

을 내가 너희에게 코드를 매우 기본입니다. 예를 들어, 플러그인 컴파일이 성공적인지 확인하고 오류를 사용자에게보고해야합니다. 이제 더 많은 것을 추가하는 것은 당신에게 달려 있습니다.