Skip to content

Commit 597752a

Browse files
get rid of recursive dispatch; simplify, and get expected results
1 parent 9914530 commit 597752a

File tree

2 files changed

+9
-22
lines changed

2 files changed

+9
-22
lines changed

tests/test_issue1552.cpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@ class Dispatcher;
88
class Client
99
{
1010
public:
11-
Client(Dispatcher* disp): PtrD(disp)
11+
Client()
1212
{
1313
std::cout << "In Client::Client\n";
1414
}
1515
virtual ~Client(){};
16-
virtual void ProcessEvent()
17-
{
18-
std::cout << "THIS SHOULDN'T HAPPEN --In Client::ProcessEvent\n";
19-
}
20-
Dispatcher* PtrD;
16+
virtual void ProcessEvent() = 0;
2117
};
2218

2319
class Dispatcher
@@ -27,7 +23,6 @@ class Dispatcher
2723
{
2824
std::cout << "In Dispatcher::Dispatcher\n";
2925
}
30-
virtual ~Dispatcher(){};
3126

3227
void Dispatch(Client* client)
3328
{
@@ -36,31 +31,24 @@ class Dispatcher
3631
}
3732
};
3833

39-
class DispatcherTrampoline : public Dispatcher
40-
{
41-
public:
42-
using Dispatcher::Dispatcher;
43-
};
44-
4534
class ClientTrampoline : public Client
4635
{
4736
public:
4837
using Client::Client;
4938

5039
void ProcessEvent() override
5140
{
52-
PYBIND11_OVERLOAD(void,Client,ProcessEvent,);
41+
PYBIND11_OVERLOAD_PURE(void,Client,ProcessEvent,);
5342
}
5443
};
5544

5645
TEST_SUBMODULE(issue1552, m)
5746
{
5847
py::class_<Client,ClientTrampoline> cli(m,"Client");
59-
cli.def(py::init<Dispatcher* >());
48+
cli.def(py::init());
6049
cli.def("ProcessEvent",&Client::ProcessEvent);
61-
cli.def_readwrite("PtrD",&Client::PtrD);
6250

63-
py::class_<Dispatcher,DispatcherTrampoline> dsp(m,"Dispatcher");
51+
py::class_<Dispatcher> dsp(m,"Dispatcher");
6452
dsp.def(py::init< >());
6553
dsp.def("Dispatch",&Dispatcher::Dispatch);
6654
}

tests/test_issue1552.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55

66

77
class SomeClient(m.Client):
8-
def __init__(self,d):
8+
def __init__(self):
99
print("In SomeClient::__init__")
10-
super().__init__(d);
10+
super().__init__()
1111

1212
def ProcessEvent(self):
13-
print("in SomeClient::ProcessEvent,about to call self.ProcessEvent")
14-
self.PtrD.Dispatch(self);
13+
print("Python ProcessEvent")
1514

1615

1716
def test_main():
1817
# https://github.com/pybind/pybind11/issues/1552
1918
dd = m.Dispatcher()
20-
cl = SomeClient(dd)
19+
cl = SomeClient()
2120
dd.Dispatch(cl)

0 commit comments

Comments
 (0)