#pragma once /////////////////////////////////////////////////////////////////////// // Notifier.h - Enable child thread to notify parent thread // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2016 // /////////////////////////////////////////////////////////////////////// #include #include #include /////////////////////////////////////////////////////////////////////// // Interface INotify used for application specific event handling template struct INotify { virtual void doNotify(EventArgs ea) = 0; virtual ~INotify(){} }; /////////////////////////////////////////////////////////////////////// // Interface INotifier used by application processing function // run on child thread in asyncRun template struct INotifier { virtual void invokeEventProcessing(EventArgs arg) = 0; virtual void reg(INotify* pNotify) = 0; virtual ~INotifier() {} }; /////////////////////////////////////////////////////////////////////// // Notifier class provides facilities to: // - run application specific callable object ca in asyncRun(ca) // - register event Handler objects that derive from INotify // - invoke notifications when called from callable object ca template class Notifier : public INotifier { public: void invokeEventProcessing(EventArgs ea); std::thread asyncRun(CallObj ca); void reg(INotify* pNotify); private: std::vector*> _notified; }; template void Notifier::reg(INotify* pINotify) { _notified.push_back(pINotify); } template void Notifier::invokeEventProcessing(EventArgs ea) { for (auto pNotify : _notified) pNotify->doNotify(ea); } template std::thread Notifier::asyncRun(CallObj co) { std::function runner = [this, co]() { co(this); }; std::thread t(runner); return t; }