From bbc2ef5b80e66fb5d2ca18bfeb52769dc563e57b Mon Sep 17 00:00:00 2001 From: kohait Date: Wed, 13 Apr 2011 21:12:59 +0000 Subject: [PATCH] bazaar: BoostPyTest: Value export for the common types, including String and ValueArray git-svn-id: svn://ultimatepp.org/upp/trunk@3340 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- bazaar/BoostPyTest/BoostPyTest.lay | 1 + bazaar/BoostPyTest/BoostPyTest.upp | 3 +- bazaar/BoostPyTest/SliderCtrlPy.cpp | 9 +++ bazaar/BoostPyTest/SliderCtrlPy.h | 3 + bazaar/BoostPyTest/UppString.h | 8 +- bazaar/BoostPyTest/UppValue.cpp | 5 ++ bazaar/BoostPyTest/UppValue.h | 121 ++++++++++++++++++++++++++-- bazaar/BoostPyTest/init | 1 + bazaar/BoostPyTest/main.cpp | 6 ++ bazaar/BoostPyTest/modules.cppi | 2 + bazaar/ValueCtrl/ValueCtrl.cpp | 5 +- bazaar/ValueCtrlTest/main.cpp | 2 +- 12 files changed, 153 insertions(+), 13 deletions(-) diff --git a/bazaar/BoostPyTest/BoostPyTest.lay b/bazaar/BoostPyTest/BoostPyTest.lay index 5c6ff7c02..7bc5c94fb 100644 --- a/bazaar/BoostPyTest/BoostPyTest.lay +++ b/bazaar/BoostPyTest/BoostPyTest.lay @@ -1,5 +1,6 @@ LAYOUT(BoostPyTestLayout, 672, 380) ITEM(PyConsoleCtrl, con, HSizePosZ(0, 248).VSizePosZ(0, 0)) ITEM(SliderCtrl, sl, RightPosZ(8, 232).TopPosZ(12, 48)) + ITEM(ValueCtrl, vc, RightPosZ(8, 232).TopPosZ(76, 56)) END_LAYOUT diff --git a/bazaar/BoostPyTest/BoostPyTest.upp b/bazaar/BoostPyTest/BoostPyTest.upp index c702ecf35..cb6adb14a 100644 --- a/bazaar/BoostPyTest/BoostPyTest.upp +++ b/bazaar/BoostPyTest/BoostPyTest.upp @@ -1,6 +1,7 @@ uses CtrlLib, - PyConsoleCtrl; + PyConsoleCtrl, + ValueCtrl; file world.h, diff --git a/bazaar/BoostPyTest/SliderCtrlPy.cpp b/bazaar/BoostPyTest/SliderCtrlPy.cpp index 1c609a97a..40c4bb05b 100644 --- a/bazaar/BoostPyTest/SliderCtrlPy.cpp +++ b/bazaar/BoostPyTest/SliderCtrlPy.cpp @@ -18,3 +18,12 @@ void export_SliderCtrl() ; } +void export_ValueCtrl() +{ + scope().attr("__doc__") = "ValueCtrl module's docstring"; + class_("ValueCtrl", "An Upp ValueCtrl", no_init) + .def("get", &ValueCtrl::GetData) + .def("set", &ValueCtrl::SetData) + ; +} + diff --git a/bazaar/BoostPyTest/SliderCtrlPy.h b/bazaar/BoostPyTest/SliderCtrlPy.h index 670908029..4ee4fba9f 100644 --- a/bazaar/BoostPyTest/SliderCtrlPy.h +++ b/bazaar/BoostPyTest/SliderCtrlPy.h @@ -7,6 +7,8 @@ using namespace boost::python; #include using namespace Upp; +#include + //A wrapped instance struct SliderCtrlPy @@ -22,5 +24,6 @@ struct SliderCtrlPy //fw void export_UppCtrl(); void export_SliderCtrl(); //relys on Value export +void export_ValueCtrl(); //relys on Value export #endif diff --git a/bazaar/BoostPyTest/UppString.h b/bazaar/BoostPyTest/UppString.h index 13613f24c..85b7c39c0 100644 --- a/bazaar/BoostPyTest/UppString.h +++ b/bazaar/BoostPyTest/UppString.h @@ -23,16 +23,18 @@ struct String_from_python_str static void* convertible(PyObject* po) { - if(!PyString_Check(po)) return 0; - return po; + if(PyString_Check(po)) return po; + return 0; } static void construct(PyObject* po, converter::rvalue_from_python_stage1_data* data) { + void* d = ((converter::rvalue_from_python_storage*)data)->storage.bytes; + 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; } }; diff --git a/bazaar/BoostPyTest/UppValue.cpp b/bazaar/BoostPyTest/UppValue.cpp index def857fbf..c5fad9ff1 100644 --- a/bazaar/BoostPyTest/UppValue.cpp +++ b/bazaar/BoostPyTest/UppValue.cpp @@ -11,3 +11,8 @@ void export_UppValue() def("doubleit", Valuedoubleit); } +void export_UppValueArray() +{ + to_python_converter(); + ValueArray_from_python(); +} diff --git a/bazaar/BoostPyTest/UppValue.h b/bazaar/BoostPyTest/UppValue.h index f301f99c1..21e31fe45 100644 --- a/bazaar/BoostPyTest/UppValue.h +++ b/bazaar/BoostPyTest/UppValue.h @@ -11,7 +11,20 @@ using namespace Upp; struct Value_to_python { - static PyObject* convert(const Value& v) { return incref(object(int(v)).ptr()); } + static PyObject* convert(const Value& v) + { + switch(v.GetType()) + { + case INT_V: return incref(object(int(v)).ptr()); + case BOOL_V: return incref(object(bool(v)).ptr()); + case INT64_V: return incref(object(int64(v)).ptr()); + case DOUBLE_V: return incref(object(double(v)).ptr()); + case STRING_V: return incref(object(String(v)).ptr()); + case VALUEARRAY_V: return incref(object(ValueArray(v)).ptr()); + case VOID_V: + default: return incref(object().ptr()); //none + } + } }; struct Value_from_python @@ -23,16 +36,70 @@ struct Value_from_python static void* convertible(PyObject* po) { - if(!PyInt_Check(po)) return 0; - return po; + if(po == Py_None) return po; + if(PyInt_Check(po)) return po; + if(PyBool_Check(po)) return po; + if(PyLong_Check(po)) return po; + if(PyFloat_Check(po)) return po; + if(PyString_Check(po)) return po; + if(PyList_Check(po)) return po; + return 0; } 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)); + + if(po == Py_None) + { + new(d) Value(); + } + else + if(PyBool_Check(po)) //before Int, cause bool is derived from PyTypeInt + { + bool c = false; + if(po != Py_False) { + if(po != Py_True) throw_error_already_set(); + c = true; + } + new(d) Value(c); + } + else + if(PyInt_Check(po)) + { + long c = PyInt_AsLong(po); + new(d) Value(c); + } + else + //notmally here bool + if(PyLong_Check(po)) + { + long long c = PyLong_AsLongLong(po); + new(d) Value(c); + } + else + if(PyFloat_Check(po)) + { + double c = PyFloat_AsDouble(po); + new(d) Value(c); + } + else + if(PyString_Check(po)) + { + String c = extract(object(handle<>(borrowed(po)))); //use upper converter, really borrowed? + new(d) Value(c); + } + else + if(PyList_Check(po)) + { + ValueArray c = extract(object(handle<>(borrowed(po)))); //use upper converter, really borrowed? + new(d) Value(c); + } + else + { + throw_error_already_set(); //FIXME dont know type, which to throw? + } + data->convertible = d; } }; @@ -40,7 +107,49 @@ struct Value_from_python Value Valuehelloval(); //tests to-python int Valuedoubleit(const Value& v); //tests from-python +//ValueArray + +struct ValueArray_to_python +{ + static PyObject* convert(const ValueArray& v) + { + list l; + const Vector& vv = v.Get(); + for(int i = 0; i < vv.GetCount(); i++) + l.append(object(vv[i])); + return incref(l.ptr()); //need to create a list + } +}; + +struct ValueArray_from_python +{ + ValueArray_from_python() + { + converter::registry::push_back(&convertible, &construct, type_id()); + } + + static void* convertible(PyObject* po) + { + if(PyList_Check(po)) return po; + return 0; + } + + static void construct(PyObject* po, converter::rvalue_from_python_stage1_data* data) + { + void* d = ((converter::rvalue_from_python_storage*)data)->storage.bytes; + + Vector vv; + list l(handle<>(borrowed(po))); + for(int i = 0; i < len(l); i++) + vv.Add(extract(l[i])); + new(d) ValueArray(vv); + + data->convertible = d; + } +}; + //fw void export_UppValue(); +void export_UppValueArray(); #endif diff --git a/bazaar/BoostPyTest/init b/bazaar/BoostPyTest/init index 08e6e5c87..c57aa9a73 100644 --- a/bazaar/BoostPyTest/init +++ b/bazaar/BoostPyTest/init @@ -2,4 +2,5 @@ #define _BoostPyTest_icpp_init_stub #include "CtrlLib/init" #include "PyConsoleCtrl/init" +#include "ValueCtrl/init" #endif diff --git a/bazaar/BoostPyTest/main.cpp b/bazaar/BoostPyTest/main.cpp index 9ce2e92f9..64690d63a 100644 --- a/bazaar/BoostPyTest/main.cpp +++ b/bazaar/BoostPyTest/main.cpp @@ -47,6 +47,9 @@ BoostPyTest::BoostPyTest() scope(upp_module).attr("slpy") = ptr(&slpy); scope(upp_module).attr("sl") = ptr(&sl); + vc.SetData(123); + scope(upp_module).attr("vc") = ptr(&vc); + //the additional import is needless String sc = "p = hello.World()\n" @@ -65,6 +68,9 @@ BoostPyTest::BoostPyTest() "print upp.sl.get()\n" "upp.sl.set(75)\n" + "upp.vc.set(range(10))\n" + "upp.vc.get()\n" + ; con.cmd.SetData(sc); diff --git a/bazaar/BoostPyTest/modules.cppi b/bazaar/BoostPyTest/modules.cppi index e5cfb38c6..86a65232d 100644 --- a/bazaar/BoostPyTest/modules.cppi +++ b/bazaar/BoostPyTest/modules.cppi @@ -10,5 +10,7 @@ BOOST_PYTHON_MODULE(upp) export_UppCtrl(); export_UppString(); export_UppValue(); + export_UppValueArray(); export_SliderCtrl(); + export_ValueCtrl(); } diff --git a/bazaar/ValueCtrl/ValueCtrl.cpp b/bazaar/ValueCtrl/ValueCtrl.cpp index a1e7b62d5..bc22fc33a 100644 --- a/bazaar/ValueCtrl/ValueCtrl.cpp +++ b/bazaar/ValueCtrl/ValueCtrl.cpp @@ -93,7 +93,7 @@ void ValuePopUp::SetType(int _vt) v = Value(); //reset if(_vt==VOID_V) //prevent loop { - if(pc) return; + if(pc) return; //keep last Editor _vt = STRING_V; //default, if no editor yet } pc = DefaultValueEditor(_vt); @@ -107,7 +107,7 @@ void ValuePopUp::SetType(int _vt) void ValuePopUp::Updated() { int _vt = v.GetType(); - if(_vt != vtype && !v.IsNull()) + if(_vt != vtype && !v.IsNull()) //change only if needed, keep editor for !null SetType(_vt); if(type.GetData() != vtype) type.SetData(vtype); @@ -140,6 +140,7 @@ ValuePopUp::ValuePopUp() type.Add(int(LOGPOS_V), "LOGPOS_V"); //type.Add(int(VALUE_V), "VALUE_V"); type.Add(int(VALUEARRAY_V), "VALUEARRAY_V"); + //type.Add(int(VALUEMAP_V), "VALUEMAP_V"); type <<= THISBACK(TypeAction); ok <<= THISBACK(Acceptor); diff --git a/bazaar/ValueCtrlTest/main.cpp b/bazaar/ValueCtrlTest/main.cpp index 2c86b7462..046eca267 100644 --- a/bazaar/ValueCtrlTest/main.cpp +++ b/bazaar/ValueCtrlTest/main.cpp @@ -5,7 +5,7 @@ ValueCtrlTest::ValueCtrlTest() CtrlLayout(*this, "Window title"); clear <<= THISBACK(Clear); - Value v = ValueArray(Vector() << "123.8" << "Test"); + Value v = ValueArray(Vector() << 123.8 << "Test"); vc.SetData(v); vc <<= THISBACK(ActionCB); Add(vc.HSizePos().BottomPos(0, 20));