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
This commit is contained in:
kohait 2011-04-13 21:12:59 +00:00
parent d4d3d7f8ad
commit bbc2ef5b80
12 changed files with 153 additions and 13 deletions

View file

@ -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

View file

@ -1,6 +1,7 @@
uses
CtrlLib,
PyConsoleCtrl;
PyConsoleCtrl,
ValueCtrl;
file
world.h,

View file

@ -18,3 +18,12 @@ void export_SliderCtrl()
;
}
void export_ValueCtrl()
{
scope().attr("__doc__") = "ValueCtrl module's docstring";
class_<ValueCtrl, boost::noncopyable>("ValueCtrl", "An Upp ValueCtrl", no_init)
.def("get", &ValueCtrl::GetData)
.def("set", &ValueCtrl::SetData)
;
}

View file

@ -7,6 +7,8 @@ using namespace boost::python;
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
#include <ValueCtrl/ValueCtrl.h>
//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

View file

@ -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<String>*)data)->storage.bytes;
const char* c = PyString_AsString(po);
if(c == 0) throw_error_already_set();
void* d = ((converter::rvalue_from_python_storage<String>*)data)->storage.bytes;
new(d) String(c);
data->convertible = d;
}
};

View file

@ -11,3 +11,8 @@ void export_UppValue()
def("doubleit", Valuedoubleit);
}
void export_UppValueArray()
{
to_python_converter<ValueArray, ValueArray_to_python>();
ValueArray_from_python();
}

View file

@ -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<Value>*)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<String>(object(handle<>(borrowed(po)))); //use upper converter, really borrowed?
new(d) Value(c);
}
else
if(PyList_Check(po))
{
ValueArray c = extract<ValueArray>(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<Value>& 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<ValueArray>());
}
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<ValueArray>*)data)->storage.bytes;
Vector<Value> vv;
list l(handle<>(borrowed(po)));
for(int i = 0; i < len(l); i++)
vv.Add(extract<Value>(l[i]));
new(d) ValueArray(vv);
data->convertible = d;
}
};
//fw
void export_UppValue();
void export_UppValueArray();
#endif

View file

@ -2,4 +2,5 @@
#define _BoostPyTest_icpp_init_stub
#include "CtrlLib/init"
#include "PyConsoleCtrl/init"
#include "ValueCtrl/init"
#endif

View file

@ -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);

View file

@ -10,5 +10,7 @@ BOOST_PYTHON_MODULE(upp)
export_UppCtrl();
export_UppString();
export_UppValue();
export_UppValueArray();
export_SliderCtrl();
export_ValueCtrl();
}

View file

@ -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);

View file

@ -5,7 +5,7 @@ ValueCtrlTest::ValueCtrlTest()
CtrlLayout(*this, "Window title");
clear <<= THISBACK(Clear);
Value v = ValueArray(Vector<Value>() << "123.8" << "Test");
Value v = ValueArray(Vector<Value>() << 123.8 << "Test");
vc.SetData(v);
vc <<= THISBACK(ActionCB);
Add(vc.HSizePos().BottomPos(0, 20));