Skip to content

Commit

Permalink
Support for globally overloaded ordering operators
Browse files Browse the repository at this point in the history
  • Loading branch information
wlav committed Mar 9, 2022
1 parent e6cac07 commit 3f3aa34
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ See :doc:`packages <packages>`, for details on the package structure.
PyPy support lags CPython support.


master: 2.4.0
-------------

* Support for globally overloaded ordering operators


2022-03-08: 2.3.0
-----------------

Expand Down
36 changes: 36 additions & 0 deletions test/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,39 @@ def test15_class_and_global_mix(self):
assert (x.end() - 1).__deref__() == 3
assert std.max_element(x.begin(), x.end())-x.begin() == 2
assert (x.end() - 3).__deref__() == 1

def test16_global_ordered_operators(self):
"""Globally defined ordered oeprators"""

import cppyy

cppyy.cppdef("""\
namespace FriendOperator {
struct ALt { ALt(int d) : data(d) {} int data; };
bool operator< (const ALt& a1, const ALt& a2) { return a1.data < a2.data; }
struct ALe { ALe(int d) : data(d) {} int data; };
bool operator<=(const ALe& a1, const ALe& a2) { return a1.data <= a2.data; }
struct AGt { AGt(int d) : data(d) {} int data; };
bool operator> (const AGt& a1, const AGt& a2) { return a1.data > a2.data; }
struct AGe { AGe(int d) : data(d) {} int data; };
bool operator>=(const AGe& a1, const AGe& a2) { return a1.data >= a2.data; }
}""")

ns = cppyy.gbl.FriendOperator

assert ns.ALt(4) < ns.ALt(5)
assert not ns.ALt(5) < ns.ALt(4)

assert ns.ALe(4) <= ns.ALe(5)
assert not ns.ALe(5) <= ns.ALe(4)

assert ns.AGt(5) > ns.AGt(4)
assert not ns.AGt(4) > ns.AGt(5)

assert ns.AGe(5) >= ns.AGe(4)
assert not ns.AGe(4) >= ns.AGe(5)

0 comments on commit 3f3aa34

Please sign in to comment.