diff --git a/bazaar/BoostPyTest/BoostPyTest.h b/bazaar/BoostPyTest/BoostPyTest.h index 922ed20a1..915a29d20 100644 --- a/bazaar/BoostPyTest/BoostPyTest.h +++ b/bazaar/BoostPyTest/BoostPyTest.h @@ -4,96 +4,10 @@ #include using namespace boost::python; -#include -using namespace Upp; - -//A Simple class - -struct World -{ - void set(std::string msg) { this->msg = msg; } - std::string greet() const { return msg; } - std::string msg; -}; - -BOOST_PYTHON_MODULE(hello) -{ - scope().attr("__doc__") = "Hello module's docstring"; - class_("World", "A simple world") - .def("greet", &World::greet) - .def("set", &World::set) - ; -} - -//A wrapped instance - -struct SliderCtrlPy -{ - SliderCtrlPy(SliderCtrl& o) : o(o) {} - - void Set(const int& d) { o.SetData(d); } - int Get() const { return o.GetData(); } - - SliderCtrl& o; -}; - -BOOST_PYTHON_MODULE(UppCtrl) -{ - scope().attr("__doc__") = "ctrl module's docstring"; - class_("SliderCtrlPy", "A SliderCtrl wrapper", no_init) - .def("get", &SliderCtrlPy::Get) - .def("set", &SliderCtrlPy::Set) - ; -} - -//a convertable instance -namespace UppStringModule { - -struct String_to_python_str -{ - static PyObject* convert(const String& s) { return incref(object(s.Begin()).ptr()); } -}; - -struct String_from_python_str -{ - String_from_python_str() - { - converter::registry::push_back(&convertible, &construct, type_id()); - } - - static void* convertible(PyObject* po) - { - if(!PyString_Check(po)) return 0; - return po; - } - - static void construct(PyObject* po, converter::rvalue_from_python_stage1_data* data) - { - const char* c = PyString_AsString(po); - if(c == 0) throw_error_already_set(); - void* d = ((converter::rvalue_from_python_storage*)data)->storage.bytes; - new(d) String(c); - data->convertible = d; - } -}; - -String hello() { return String( "my new custom string" ); } //tests to-python -std::size_t size (const String& s) { return s.GetLength(); } //tests from-python - -void init() -{ - to_python_converter(); - String_from_python_str(); - def("hello", hello); - def("size", size); -} - -} //ns - -BOOST_PYTHON_MODULE(UppString) -{ - UppStringModule::init(); -} +#include "world.h" +#include "SliderCtrlPy.h" +#include "UppString.h" +#include "UppValue.h" #define LAYOUTFILE #include diff --git a/bazaar/BoostPyTest/BoostPyTest.upp b/bazaar/BoostPyTest/BoostPyTest.upp index 88196bbaa..c702ecf35 100644 --- a/bazaar/BoostPyTest/BoostPyTest.upp +++ b/bazaar/BoostPyTest/BoostPyTest.upp @@ -3,8 +3,17 @@ uses PyConsoleCtrl; file + world.h, + world.cpp, + SliderCtrlPy.h, + SliderCtrlPy.cpp, + UppString.h, + UppString.cpp, + UppValue.h, + UppValue.cpp, BoostPyTest.h, compile.txt, + modules.cppi, main.cpp, BoostPyTest.lay; diff --git a/bazaar/BoostPyTest/SliderCtrlPy.cpp b/bazaar/BoostPyTest/SliderCtrlPy.cpp new file mode 100644 index 000000000..1c609a97a --- /dev/null +++ b/bazaar/BoostPyTest/SliderCtrlPy.cpp @@ -0,0 +1,20 @@ +#include "SliderCtrlPy.h" + +void export_UppCtrl() +{ + scope().attr("__doc__") = "ctrl module's docstring"; + class_("SliderCtrlPy", "A SliderCtrl wrapper", no_init) + .def("get", &SliderCtrlPy::Get) + .def("set", &SliderCtrlPy::Set) + ; +} + +void export_SliderCtrl() +{ + scope().attr("__doc__") = "SliderCtrl module's docstring"; + class_("SliderCtrl", "An Upp SliderCtrl", no_init) + .def("get", &SliderCtrl::GetData) + .def("set", &SliderCtrl::SetData) + ; +} + diff --git a/bazaar/BoostPyTest/SliderCtrlPy.h b/bazaar/BoostPyTest/SliderCtrlPy.h new file mode 100644 index 000000000..670908029 --- /dev/null +++ b/bazaar/BoostPyTest/SliderCtrlPy.h @@ -0,0 +1,26 @@ +#ifndef _BoostPyTest_SliderCtrlPy_h_ +#define _BoostPyTest_SliderCtrlPy_h_ + +#include +using namespace boost::python; + +#include +using namespace Upp; + +//A wrapped instance + +struct SliderCtrlPy +{ + SliderCtrlPy(SliderCtrl& o) : o(o) {} + + void Set(const int& d) { o.SetData(d); } + int Get() const { return o.GetData(); } + + SliderCtrl& o; +}; + +//fw +void export_UppCtrl(); +void export_SliderCtrl(); //relys on Value export + +#endif diff --git a/bazaar/BoostPyTest/UppString.cpp b/bazaar/BoostPyTest/UppString.cpp new file mode 100644 index 000000000..9c37f647e --- /dev/null +++ b/bazaar/BoostPyTest/UppString.cpp @@ -0,0 +1,13 @@ +#include "UppString.h" + +String Stringhello() { return String( "my new custom string" ); } +std::size_t Stringsize(const String& s) { return s.GetLength(); } + +void export_UppString() +{ + to_python_converter(); + String_from_python_str(); + def("hello", Stringhello); + def("size", Stringsize); +} + diff --git a/bazaar/BoostPyTest/UppString.h b/bazaar/BoostPyTest/UppString.h new file mode 100644 index 000000000..13613f24c --- /dev/null +++ b/bazaar/BoostPyTest/UppString.h @@ -0,0 +1,46 @@ +#ifndef _BoostPyTest_UppString_h_ +#define _BoostPyTest_UppString_h_ + +#include +using namespace boost::python; + +#include +using namespace Upp; + +//a convertable instance + +struct String_to_python_str +{ + static PyObject* convert(const String& s) { return incref(object(s.Begin()).ptr()); } +}; + +struct String_from_python_str +{ + String_from_python_str() + { + converter::registry::push_back(&convertible, &construct, type_id()); + } + + static void* convertible(PyObject* po) + { + if(!PyString_Check(po)) return 0; + return po; + } + + static void construct(PyObject* po, converter::rvalue_from_python_stage1_data* data) + { + const char* c = PyString_AsString(po); + if(c == 0) throw_error_already_set(); + void* d = ((converter::rvalue_from_python_storage*)data)->storage.bytes; + new(d) String(c); + data->convertible = d; + } +}; + +String Stringhello(); //tests to-python +std::size_t Stringsize(const String& s); //tests from-python + +//fw +void export_UppString(); + +#endif diff --git a/bazaar/BoostPyTest/UppValue.cpp b/bazaar/BoostPyTest/UppValue.cpp new file mode 100644 index 000000000..def857fbf --- /dev/null +++ b/bazaar/BoostPyTest/UppValue.cpp @@ -0,0 +1,13 @@ +#include "UppValue.h" + +Value Valuehelloval() { return 2244; } +int Valuedoubleit(const Value& v) { return int(v) * 2; } + +void export_UppValue() +{ + to_python_converter(); + Value_from_python(); + def("helloval", Valuehelloval); + def("doubleit", Valuedoubleit); +} + diff --git a/bazaar/BoostPyTest/UppValue.h b/bazaar/BoostPyTest/UppValue.h new file mode 100644 index 000000000..f301f99c1 --- /dev/null +++ b/bazaar/BoostPyTest/UppValue.h @@ -0,0 +1,46 @@ +#ifndef _BoostPyTest_UppValue_h_ +#define _BoostPyTest_UppValue_h_ + +#include +using namespace boost::python; + +#include +using namespace Upp; + +//a convertable instance + +struct Value_to_python +{ + static PyObject* convert(const Value& v) { return incref(object(int(v)).ptr()); } +}; + +struct Value_from_python +{ + Value_from_python() + { + converter::registry::push_back(&convertible, &construct, type_id()); + } + + static void* convertible(PyObject* po) + { + if(!PyInt_Check(po)) return 0; + return po; + } + + static void construct(PyObject* po, converter::rvalue_from_python_stage1_data* data) + { + long c = PyInt_AS_LONG(po); + if(c == 0) throw_error_already_set(); + void* d = ((converter::rvalue_from_python_storage*)data)->storage.bytes; + new(d) Value(int(c)); + data->convertible = d; + } +}; + +Value Valuehelloval(); //tests to-python +int Valuedoubleit(const Value& v); //tests from-python + +//fw +void export_UppValue(); + +#endif diff --git a/bazaar/BoostPyTest/compile.txt b/bazaar/BoostPyTest/compile.txt index 59751acf3..fbddadf22 100644 --- a/bazaar/BoostPyTest/compile.txt +++ b/bazaar/BoostPyTest/compile.txt @@ -1 +1,6 @@ -for further infos see Py package \ No newline at end of file +for further infos see Py package + +modules.cppi +it's a special cpp file that is included in the main.cpp, where the python environment is set up. +it serves greater separation und enables great speedup of compilation since all template helpers +for export are handled in separate cpp files. this file shouldnt be included anywhere else. diff --git a/bazaar/BoostPyTest/main.cpp b/bazaar/BoostPyTest/main.cpp index 0a6a0bcef..9ce2e92f9 100644 --- a/bazaar/BoostPyTest/main.cpp +++ b/bazaar/BoostPyTest/main.cpp @@ -1,5 +1,8 @@ #include "BoostPyTest.h" +//CAUTION special module definitions to reduce compile time +#include "modules.cppi" + BoostPyTest::BoostPyTest() : slpy(sl) { @@ -9,8 +12,7 @@ BoostPyTest::BoostPyTest() int ret; ret = PyImport_AppendInittab( "hello", &inithello ); - ret = PyImport_AppendInittab( "UppCtrl", &initUppCtrl ); - ret = PyImport_AppendInittab( "UppString", &initUppString ); + ret = PyImport_AppendInittab( "upp", &initupp ); //Py_Initialize(); //should be done *after* PyImport_AppendInittab, but it still works :), leaving INITBLOCK from Py //PyCon::Init(); @@ -37,10 +39,13 @@ BoostPyTest::BoostPyTest() w.set("Welcom on earth"); scope(hello_module).attr("earth") = ptr(&w); - slpy.Set(50); - object ctrl_module = import("UppCtrl"); - main_namespace["UppCtrl"] = ctrl_module; - scope(ctrl_module).attr("sl") = ptr(&slpy); + + object upp_module = import("upp"); + main_namespace["upp"] = upp_module; + + sl.SetData(50); + scope(upp_module).attr("slpy") = ptr(&slpy); + scope(upp_module).attr("sl") = ptr(&sl); //the additional import is needless String sc = @@ -48,12 +53,18 @@ BoostPyTest::BoostPyTest() "p.set('Some Greet Text')\n" "print p.greet()\n" - "print UppCtrl.sl.get()\n" - "UppCtrl.sl.set(23)\n" + "print upp.slpy.get()\n" + "upp.slpy.set(23)\n" - "import UppString\n" - "print UppString.hello()\n" - "print UppString.size('TestString')\n" + "print upp.hello()\n" + "print upp.size('TestString')\n" + + "print upp.helloval()\n" + "print upp.doubleit(256)\n" + + "print upp.sl.get()\n" + "upp.sl.set(75)\n" + ; con.cmd.SetData(sc); diff --git a/bazaar/BoostPyTest/modules.cppi b/bazaar/BoostPyTest/modules.cppi new file mode 100644 index 000000000..e5cfb38c6 --- /dev/null +++ b/bazaar/BoostPyTest/modules.cppi @@ -0,0 +1,14 @@ +//CAUTION this one is included in the main cpp file, where the modules are registered into python environment + +BOOST_PYTHON_MODULE(hello) +{ + export_world(); +} + +BOOST_PYTHON_MODULE(upp) +{ + export_UppCtrl(); + export_UppString(); + export_UppValue(); + export_SliderCtrl(); +} diff --git a/bazaar/BoostPyTest/world.cpp b/bazaar/BoostPyTest/world.cpp new file mode 100644 index 000000000..95a089422 --- /dev/null +++ b/bazaar/BoostPyTest/world.cpp @@ -0,0 +1,10 @@ +#include "world.h" + +void export_world() +{ + scope().attr("__doc__") = "Hello module's docstring"; + class_("World", "A simple world") + .def("greet", &World::greet) + .def("set", &World::set) + ; +} diff --git a/bazaar/BoostPyTest/world.h b/bazaar/BoostPyTest/world.h new file mode 100644 index 000000000..35405cfba --- /dev/null +++ b/bazaar/BoostPyTest/world.h @@ -0,0 +1,18 @@ +#ifndef _BoostPyTest_world_h_ +#define _BoostPyTest_world_h_ + +#include +using namespace boost::python; + +//A Simple class + +struct World +{ + void set(std::string msg) { this->msg = msg; } + std::string greet() const { return msg; } + std::string msg; +}; + +void export_world(); + +#endif diff --git a/bazaar/PyConsoleCtrl/PyConsoleCtrl.cpp b/bazaar/PyConsoleCtrl/PyConsoleCtrl.cpp index b12b3e520..94878bc51 100644 --- a/bazaar/PyConsoleCtrl/PyConsoleCtrl.cpp +++ b/bazaar/PyConsoleCtrl/PyConsoleCtrl.cpp @@ -33,17 +33,76 @@ void PyConsoleCtrl::Exec() int n = cmd.GetLineCount(); ret = PyCon::Exec(c, n<=1); - if(ret == 0 && clonex.Get()) - cmd.Clear(); - + if(ret == 0) + { + if( clonex.Get()) + cmd.Clear(); + + //history + if(idx < ac.GetCount()) + { + //executed sth from the history, move it to the end of history + ac.Move(idx, ac.GetCount()); + } + else + { + //executed new, add to the end, if not yet at the end. + if(ac.IsEmpty() || ac[ac.GetCount()-1] != c) + ac.Add(c); + } + idx = ac.GetCount(); //'commit' the history + } ex.Clear(); ex <<= ret; } +void PyConsoleCtrl::ShowHistory(int i) +{ + i = minmax(i, 0, ac.GetCount()-1); + cmd.SetData(ac[i]); +} + +void PyConsoleCtrl::Inc() +{ + //got to next in hostory and display it + ++idx; + if(idx >= ac.GetCount()) + { + idx = ac.GetCount(); + Clear(); + } + else + ShowHistory(idx); +} + +void PyConsoleCtrl::Dec() +{ + //go to previous in history + --idx; + if(idx < 0) + { + idx = 0; +// Clear(); //no clear at beginning of history + } +// else + ShowHistory(idx); +} + +void PyConsoleCtrl::Dirtify() +{ + idx = ac.GetCount(); +} + PyConsoleCtrl::PyConsoleCtrl() + : idx(0) { CtrlLayout(*this); + exec.AddFrame(sb); + sb.inc.WhenRepeat = sb.inc.WhenAction = THISBACK(Dec); //reverse because of history + sb.dec.WhenRepeat = sb.dec.WhenAction = THISBACK(Inc); + cmd <<= THISBACK(Dirtify); + spl.Vert(log, cmd); spl.SetPos(6600); Add(spl.HSizePos().VSizePos(0,20)); diff --git a/bazaar/PyConsoleCtrl/PyConsoleCtrl.h b/bazaar/PyConsoleCtrl/PyConsoleCtrl.h index ced9fceb3..82ec5cabc 100644 --- a/bazaar/PyConsoleCtrl/PyConsoleCtrl.h +++ b/bazaar/PyConsoleCtrl/PyConsoleCtrl.h @@ -40,17 +40,25 @@ public: void LoadDlg(); void SaveDlg(); - void Exec(); void Clear(); + void Exec(); + void Inc(); + void Dec(); void ClearLog() { log.Clear(); } void SaveLog() { log.Save(); } LoggerCtrl log; CmdDocEdit cmd; + SpinButtons sb; protected: + void ShowHistory(int i); + void Dirtify(); + FileSel fs; + Array ac; + int idx; }; #endif diff --git a/bazaar/PyConsoleCtrl/PyConsoleCtrl.lay b/bazaar/PyConsoleCtrl/PyConsoleCtrl.lay index 37f4edadd..9009cc1df 100644 --- a/bazaar/PyConsoleCtrl/PyConsoleCtrl.lay +++ b/bazaar/PyConsoleCtrl/PyConsoleCtrl.lay @@ -1,12 +1,12 @@ LAYOUT(PyConsoleCtrlLay, 600, 400) - ITEM(Button, exec, SetLabel(t_("Exec")).RightPosZ(0, 56).BottomPosZ(0, 20)) + ITEM(Button, exec, SetLabel(t_("Exec")).RightPosZ(0, 76).BottomPosZ(0, 20)) ITEM(Button, load, SetLabel(t_("Load")).LeftPosZ(0, 28).BottomPosZ(0, 20)) ITEM(Button, save, SetLabel(t_("Save")).LeftPosZ(28, 28).BottomPosZ(0, 20)) ITEM(Button, clear, SetLabel(t_("Clear")).LeftPosZ(56, 28).BottomPosZ(0, 20)) ITEM(Button, savelog, SetLabel(t_("Save Log")).LeftPosZ(92, 48).BottomPosZ(0, 20)) ITEM(Button, clearlog, SetLabel(t_("Clear Log")).LeftPosZ(140, 48).BottomPosZ(0, 20)) - ITEM(EditString, ex, SetEditable(false).WantFocus(false).RightPosZ(56, 56).BottomPosZ(1, 19)) - ITEM(StaticText, dv___7, SetText(t_("exit#:")).SetAlign(ALIGN_RIGHT).RightPosZ(112, 48).BottomPosZ(0, 20)) + ITEM(EditString, ex, SetEditable(false).WantFocus(false).RightPosZ(76, 56).BottomPosZ(1, 19)) + ITEM(StaticText, dv___7, SetText(t_("exit#:")).SetAlign(ALIGN_RIGHT).RightPosZ(132, 48).BottomPosZ(0, 20)) ITEM(Splitter, spl, LeftPosZ(0, 600).TopPosZ(0, 380)) ITEM(ButtonOption, clonex, LeftPosZ(188, 48).BottomPosZ(0, 20)) END_LAYOUT