Skip to content

Commit

Permalink
get rid of recursive dispatch; simplify, and get expected results
Browse files Browse the repository at this point in the history
  • Loading branch information
EricCousineau-TRI committed Jan 22, 2021
1 parent 9914530 commit 597752a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 22 deletions.
22 changes: 5 additions & 17 deletions tests/test_issue1552.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ class Dispatcher;
class Client
{
public:
Client(Dispatcher* disp): PtrD(disp)
Client()
{
std::cout << "In Client::Client\n";
}
virtual ~Client(){};
virtual void ProcessEvent()
{
std::cout << "THIS SHOULDN'T HAPPEN --In Client::ProcessEvent\n";
}
Dispatcher* PtrD;
virtual void ProcessEvent() = 0;
};

class Dispatcher
Expand All @@ -27,7 +23,6 @@ class Dispatcher
{
std::cout << "In Dispatcher::Dispatcher\n";
}
virtual ~Dispatcher(){};

void Dispatch(Client* client)
{
Expand All @@ -36,31 +31,24 @@ class Dispatcher
}
};

class DispatcherTrampoline : public Dispatcher
{
public:
using Dispatcher::Dispatcher;
};

class ClientTrampoline : public Client
{
public:
using Client::Client;

void ProcessEvent() override
{
PYBIND11_OVERLOAD(void,Client,ProcessEvent,);
PYBIND11_OVERLOAD_PURE(void,Client,ProcessEvent,);
}
};

TEST_SUBMODULE(issue1552, m)
{
py::class_<Client,ClientTrampoline> cli(m,"Client");
cli.def(py::init<Dispatcher* >());
cli.def(py::init());
cli.def("ProcessEvent",&Client::ProcessEvent);
cli.def_readwrite("PtrD",&Client::PtrD);

py::class_<Dispatcher,DispatcherTrampoline> dsp(m,"Dispatcher");
py::class_<Dispatcher> dsp(m,"Dispatcher");
dsp.def(py::init< >());
dsp.def("Dispatch",&Dispatcher::Dispatch);
}
9 changes: 4 additions & 5 deletions tests/test_issue1552.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@


class SomeClient(m.Client):
def __init__(self,d):
def __init__(self):
print("In SomeClient::__init__")
super().__init__(d);
super().__init__()

def ProcessEvent(self):
print("in SomeClient::ProcessEvent,about to call self.ProcessEvent")
self.PtrD.Dispatch(self);
print("Python ProcessEvent")


def test_main():
# https://github.com/pybind/pybind11/issues/1552
dd = m.Dispatcher()
cl = SomeClient(dd)
cl = SomeClient()
dd.Dispatch(cl)

0 comments on commit 597752a

Please sign in to comment.