Skip to content

Commit

Permalink
Use py::self {operator} py::self shortcut for operators
Browse files Browse the repository at this point in the history
  • Loading branch information
virtuald committed Oct 15, 2023
1 parent 9891f70 commit 6fe7c61
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 8 deletions.
17 changes: 17 additions & 0 deletions robotpy_build/autowrap/cxxparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,23 @@ def _on_class_method(
self.hctx.need_operators_h = True
if method_data.no_release_gil is None:
fctx.release_gil = False

# Use cpp_code to setup the operator
if fctx.cpp_code is None:
if len(method.parameters) == 0:
fctx.cpp_code = f"{operator} py::self"
else:
ptype, _, _, _ = _count_and_unwrap(method.parameters[0].type)
if (
isinstance(ptype, Type)
and isinstance(ptype.typename.segments[-1], NameSpecifier)
and ptype.typename.segments[-1].name == cdata.ctx.cpp_name
):
# don't try to predict the type, use py::self instead
fctx.cpp_code = f"py::self {operator} py::self"
else:
fctx.cpp_code = f"py::self {operator} {fctx.all_params[0].cpp_type_no_const}()"

if method.const:
fctx.const = True
if method.static:
Expand Down
1 change: 1 addition & 0 deletions robotpy_build/autowrap/j2_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ class FunctionContext:
namespace: str = ""

#: The operator for this method
#: - if set, cpp_type will be filled out by the parser
operator: typing.Optional[str] = None

#: True if this is a static method
Expand Down
8 changes: 0 additions & 8 deletions robotpy_build/autowrap/pybind11.cpp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,7 @@
#ifndef {{ fn.ifndef }}
{% endif %}
{%- if fn.operator -%}
{%- if fn.cpp_code -%}
.def({{ fn.cpp_code }}
{%- else -%}
{%- if ns.arg_params -%}
.def(py::self {{ fn.operator }} {{ ns.arg_params[0].cpp_type_no_const }}()
{%- else -%}
.def({{ fn.operator}} py::self
{%- endif -%}
{%- endif -%}
{%- set ns.arg_params = [] -%}
{%- elif fn.is_constructor -%}
{%- if fn.cpp_code -%}
Expand Down
2 changes: 2 additions & 0 deletions tests/cpp/rpytest/ft/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
GEnumMath,
HasFactory,
HasOperator,
HasOperatorNoDefault,
IBase,
IChild,
IFinal,
Expand Down Expand Up @@ -136,6 +137,7 @@
"GEnumMath",
"HasFactory",
"HasOperator",
"HasOperatorNoDefault",
"IBase",
"IChild",
"IFinal",
Expand Down
13 changes: 13 additions & 0 deletions tests/cpp/rpytest/ft/include/operators.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,16 @@ class HasOperator {
private:
int m_i;
};

struct HasOperatorNoDefault {
// no default constructor
explicit HasOperatorNoDefault(int set_x) {
x = set_x;
}

bool operator==(const HasOperatorNoDefault &o) const {
return x == o.x;
}

int x;
};
10 changes: 10 additions & 0 deletions tests/test_ft_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ def test_operators_eq():
assert o1 != o2


def test_operators_eq2():
o1 = ft.HasOperatorNoDefault(1)
o1a = ft.HasOperatorNoDefault(1)
o2 = ft.HasOperatorNoDefault(2)

assert o1 == o1a
assert not (o1 == o2)
assert o1 != o2


#
# static_only.h
#
Expand Down

0 comments on commit 6fe7c61

Please sign in to comment.