pyEmb.h 3.93 KB
Newer Older
Hurstel's avatar
Hurstel committed
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
#ifndef _PYEMB_H_
#define _PYEMB_H_

#ifndef NOPYTHON

#include <iostream>

#include <boost/python.hpp>
#include <boost/python/import.hpp>

#include "pythonWrap.h"

using namespace boost::python;

class PyEmb {
private:
	static bool b_isMainModuleSet;
	static bool b_isMainNamespaceSet;
	static bool b_isSysModuleSet;
	static bool b_isSysNameSpaceSet;
	static bool b_isInAppModuleSet;
	static bool b_isInAppNameSpaceSet;

protected:
	static object main_module;
	static object main_namespace;
	static object sys_module;
	static object sys_namespace;
	static object inApp_module;
	static object inApp_namespace;

public:
	static object getMainModule(){
		if (!b_isMainModuleSet){
			main_module= object((
				handle<>(borrowed(PyImport_AddModule("__main__")))
			));

			b_isMainModuleSet= true;
		}

		return main_module;
	}

	static object getMainNamespace(){
		if (b_isMainNamespaceSet) return main_namespace;

		getMainModule();

		main_namespace= main_module.attr("__dict__");

		b_isMainNamespaceSet= true;

		return main_namespace;
	}

	static object getSysModule(){
		if (b_isSysModuleSet) return sys_module;

		getMainNamespace();

		sys_module= object((
			handle<>(borrowed(PyImport_AddModule("sys")))
		));

		b_isSysModuleSet= true;

		return sys_module;
	}

	static object getSysNamespace(){
		if(b_isSysNameSpaceSet) return sys_namespace;

		getSysModule();

		sys_namespace= sys_module.attr("__dict__");

		main_namespace["sys"]= sys_module;

		b_isSysNameSpaceSet= true;

		return sys_namespace;
	}

	static object getInAppModule(){
		if(b_isInAppModuleSet) return inApp_module;

		getMainNamespace();

		inApp_module= object((
					handle<>(borrowed(PyImport_AddModule("InApp")))
		));

		b_isInAppModuleSet= true;

		return inApp_module;
	}

	static object getInAppNameSpace(){
		if(b_isInAppNameSpaceSet) return inApp_namespace;

		getInAppModule();
		inApp_namespace= inApp_module.attr("__dict__");

		boost::python::register_ptr_to_python<boost::shared_ptr<PyWrap_InAppMessage> >();


		inApp_namespace["IA_Agent"]=
				class_<InAppMessager::Agent, boost::noncopyable, boost::shared_ptr<PyWrap_Agent> >("IA_Agent", init<>())
					.def("sendMessage",&PyWrap_Agent::sendMessage)
					.def("recieveMessage", &PyWrap_Agent::cb_recieveMessage);

		inApp_namespace["IA_Messager"]=
				class_<PyWrap_InAppMessager>("IA_Messager")
					.def("addAgent",&PyWrap_InAppMessager::addAgent);


		/*inApp_namespace["IA_Message"]=
				class_<PyWrap_InAppMessage>("IA_Message")
					.def("create",&PyWrap_InAppMessage::create).staticmethod("create")//;
					.def("createFromStr",&PyWrap_InAppMessage::createFromStr).staticmethod("createFromStr")
					.def("getCode", &PyWrap_InAppMessage::getCode)
					.def("getArgs", &PyWrap_InAppMessage::getPyArgs);*/

		/*main_namespace["IA_MessageCode"]=
				enum_<InAppMessage::MESSAGE>("IA_MessageCode")
					.value("EMPTY_MESSAGE",InAppMessage::MESSAGE::EMPTY_MESSAGE)
					.export_values();*/

		inApp_namespace["IA_Manager"]=
			class_<PyWrap_InAppManager>("IA_Manager")
				.def("addAgent",&PyWrap_InAppManager::addAgent).staticmethod("addAgent");


		main_namespace["InApp"]= inApp_module;

		b_isInAppNameSpaceSet= true;


		return inApp_namespace;
	}

	static void PY_loadScript(std::string scriptName, std::string directory=""){
		if(!b_isMainNamespaceSet) getMainNamespace();

		if(!directory.empty()){
			PyObject* syspath= PySys_GetObject("path");
			PyList_Insert(syspath, 0, incref(object(directory).ptr()));
		}

		object mod= import(scriptName.c_str());

		main_namespace[scriptName]= mod;
	}
};

//bool PyEmb::b_isMainModuleSet= false;
//bool PyEmb::b_isMainNamespaceSet= false;
//bool PyEmb::b_isSysModuleSet= false;
//bool PyEmb::b_isSysNameSpaceSet= false;
//bool PyEmb::b_isInAppModuleSet= false;
//bool PyEmb::b_isInAppNameSpaceSet= false;

//object PyEmb::main_module;
//object PyEmb::main_namespace;
//object PyEmb::sys_module;
//object PyEmb::sys_namespace;
//object PyEmb::inApp_module;
//object PyEmb::inApp_namespace;

#endif //NOPYTHON

#endif //_PYEMB_H_