Skip to content

Commit 98d9853

Browse files
committed
Replaced the post-construction wrapper with stackable policies.
1 parent 6d26bca commit 98d9853

File tree

3 files changed

+40
-35
lines changed

3 files changed

+40
-35
lines changed

src/core/modules/memory/memory_wrap.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -839,17 +839,18 @@ void export_calling_convention(scope _memory)
839839
no_init)
840840

841841
.def("__init__",
842-
make_constructor_initializer(
843-
make_constructor(
844-
&ICallingConventionWrapper::__init__,
845-
default_call_policies(),
846-
("arg_types", "return_type", arg("alignment")=4, arg("default_convention")=CONV_CUSTOM)
842+
make_constructor(
843+
&ICallingConventionWrapper::__init__,
844+
post_constructor_policies<
845+
initialize_wrapper_policies<boost::shared_ptr<ICallingConventionWrapper> >
846+
>(
847+
make_function(
848+
&ICallingConventionWrapper::Initialize,
849+
default_call_policies(),
850+
args("self", "arg_types", "return_type", "alignment", "default_convention")
851+
)
847852
),
848-
make_function(
849-
&ICallingConventionWrapper::Initialize,
850-
initializer_call_policies<>(),
851-
("self", "arg_types", "return_type", arg("alignment")=4, arg("default_convention")=CONV_CUSTOM)
852-
)
853+
("arg_types", "return_type", arg("alignment")=4, arg("default_convention")=CONV_CUSTOM)
853854
),
854855
"Initialize the calling convention.\n"
855856
"\n"

src/core/modules/memory/memory_wrap.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,8 @@ class ICallingConventionWrapper: public ICallingConvention, public wrapper<ICall
9595
);
9696
}
9797

98-
void Initialize(object self, object oArgTypes, DataType_t returnType, int iAlignment=4, Convention_t eDefaultConv=CONV_CUSTOM)
98+
void Initialize(object self, object oArgTypes, DataType_t returnType, int iAlignment, Convention_t eDefaultConv)
9999
{
100-
// Initialize our wrapper so that Python overrides are properly resolved.
101-
detail::initialize_wrapper(self.ptr(), this);
102-
103100
// If we didn't receive a default convention on construction, try to resolve one from the Python instance.
104101
if (!m_pDefaultCallingConvention)
105102
{

src/core/utilities/wrap_macros.h

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -249,46 +249,53 @@ typedef return_value_policy<copy_const_reference> copy_const_reference_policy;
249249
typedef return_value_policy<return_by_value> return_by_value_policy;
250250

251251
//---------------------------------------------------------------------------------
252-
// Provides post-construction initialization support of the Python instances.
252+
// Call policies that initializes the wrapper hierarchy.
253253
//---------------------------------------------------------------------------------
254-
template<typename BasePolicies = default_call_policies>
255-
struct initializer_call_policies : BasePolicies
254+
template<typename HeldType, typename BasePolicies = default_call_policies, int iSelf = -1>
255+
struct initialize_wrapper_policies : BasePolicies
256256
{
257257
template<typename ArgumentPackage>
258258
static PyObject *postcall(const ArgumentPackage &args, PyObject *pResult)
259259
{
260-
return incref(Py_None); // __init__ should always return None
260+
PyObject *pSelf = detail::get(boost::mpl::int_<iSelf>(), args);
261+
detail::initialize_wrapper(
262+
pSelf,
263+
get_pointer((HeldType)extract<HeldType>(pSelf))
264+
);
265+
266+
return BasePolicies::postcall(args, pResult);
261267
}
262268
};
263269

264-
template<typename Constructor, typename Initializer>
265-
struct constructor_initializer
270+
//---------------------------------------------------------------------------------
271+
// Provides post-construction initialization support of the Python instances.
272+
//---------------------------------------------------------------------------------
273+
template<typename BasePolicies = default_call_policies, int iSelf = -1>
274+
struct post_constructor_policies : BasePolicies
266275
{
267276
public:
268-
constructor_initializer(Constructor constructor, Initializer initializer):
269-
m_constructor(constructor),
277+
post_constructor_policies(object initializer):
270278
m_initializer(initializer)
271279
{
272280
}
273281

274-
object operator()(boost::python::tuple args, dict kwargs)
282+
template<typename ArgumentPackage>
283+
PyObject *postcall(const ArgumentPackage &args, PyObject *pResult)
275284
{
276-
m_constructor(*args, **kwargs);
277-
return m_initializer(*(make_tuple(args[0]) + args), **kwargs);
285+
BasePolicies::postcall(args, pResult);
286+
m_initializer(
287+
*(make_tuple(
288+
object(handle<>(incref(detail::get(boost::mpl::int_<iSelf>(), args))))) +
289+
boost::python::tuple(handle<>(args.base))
290+
)
291+
);
292+
293+
decref(pResult);
294+
return incref(Py_None); // __init__ should always return None
278295
}
279296

280297
private:
281-
object m_constructor;
282298
object m_initializer;
283299
};
284300

285-
template<typename Constructor, typename Initializer>
286-
object make_constructor_initializer(Constructor constructor, Initializer initializer)
287-
{
288-
return raw_function(
289-
constructor_initializer<Constructor, Initializer>(constructor, initializer),
290-
1 // self
291-
);
292-
};
293-
294301
#endif // _WRAP_MACROS_H

0 commit comments

Comments
 (0)