1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
/****************************/
/* pyhandler.h */
/****************************/
#ifndef PYHANDLER_H
#define PYHANDLER_H
#include <wx/string.h>
#include <Python.h>
#include <wx/wxPython/wxPython.h>
#include <vector>
/* Use the boost library : */
#include <boost/python.hpp>
class PyHandler
{
typedef void (*initfunc_t )();
private:
static PyHandler* m_instance;
bool m_ModulesLoaded;
int m_current;
PyThreadState* m_mainTState;
protected:
PyHandler();
wxString m_appName;
void RunBaseScripts( const wxString& base );
// Modules
struct ModuleRecord
{
wxString name;
std::vector< initfunc_t > registry;
ModuleRecord( const wxString &modName ) :
name( modName )
{
}
};
std::vector< ModuleRecord > m_ModuleRegistry;
void DoInitModules();
// Events
struct Event
{
wxString key;
std::vector< boost::python::object > functors;
Event( const wxString &strKey ) :
key( strKey )
{
}
};
std::vector< Event > m_EventRegistry;
public:
// Singletton handling:
static PyHandler* GetInstance();
~PyHandler();
// Scope params/handling:
void SetAppName( const wxString& name );
void AddToModule( const wxString& name, initfunc_t initfunc );
int GetModuleIndex( const wxString& name ) const;
// Script and direct call
void RunScripts();
bool RunScript( const wxString& name );
bool RunSimpleString( const wxString& code );
// Common Informations
const char* GetVersion();
void InitNextModule();
// Event triggering
// - C++ interface
void DeclareEvent( const wxString& key );
void TriggerEvent( const wxString& key );
void TriggerEvent( const wxString& key,
const boost::python::object& param );
int GetEventIndex( const wxString& key );
// - Py Interface
void RegisterCallback( const wxString& key,
const boost::python::object& obj );
void UnRegisterCallback( const wxString& key,
const boost::python::object& obj );
// Object conversions
// - Py -> C++
static wxString MakeStr( const boost::python::object& objStr );
// - C++ -> Py
static boost::python::object Convert( const wxString& wxStr );
};
#define KICAD_PY_BIND_MODULE( mod ) PyHandler::GetInstance()->AddModule( init # mod )
#endif //PYHANDLER_H
|