Skip to content

Commit 801fcda

Browse files
committed
WIP : Explicit float to half binding
1 parent 6dfdc6f commit 801fcda

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

src/IECorePython/HalfBinding.cpp

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,47 @@ namespace IECorePython
4949

5050
struct half_to_float
5151
{
52-
static PyObject* convert( const half &x )
53-
{
54-
return PyFloat_FromDouble( x );
55-
}
52+
static PyObject* convert( const half &x )
53+
{
54+
return PyFloat_FromDouble( x );
55+
}
56+
};
57+
58+
struct HalfFromPython
59+
{
60+
HalfFromPython()
61+
{
62+
converter::registry::push_back(
63+
&convertible,
64+
&construct,
65+
type_id<half> ()
66+
);
67+
}
68+
69+
static void *convertible( PyObject *obj_ptr )
70+
{
71+
if ( !PyFloat_Check( obj_ptr ) )
72+
{
73+
return nullptr;
74+
}
75+
return obj_ptr;
76+
}
77+
78+
static void construct( PyObject *obj_ptr, converter::rvalue_from_python_stage1_data *data )
79+
{
80+
assert( obj_ptr );
81+
82+
void* storage = (( converter::rvalue_from_python_storage<half>* ) data )->storage.bytes;
83+
new( storage ) half( extract<float>( obj_ptr ) );
84+
data->convertible = storage;
85+
}
5686
};
5787

5888
void bindHalf()
5989
{
60-
to_python_converter<half, half_to_float>();
61-
implicitly_convertible<float, half>();
90+
to_python_converter<half, half_to_float>();
91+
HalfFromPython();
92+
// implicitly_convertible<float, half>();
6293
}
6394

6495
}

0 commit comments

Comments
 (0)