I've written a simple example of the technique for you instead. I wrote
(and ran) it on a Windows machine, so apologies for the MS-specific
stuff (tmain).

Here I take a step back from XPCOM to illustrate the principal. If you
can get the principal, you can apply it to any problem you need a
similar solution (instead of just XPCOM or COM, etc). I'm about to post
quite a bit of code here, so all please forgive me for the spray of
code that follows:

//
-------------------------------------------------------------------------------

#include "stdafx.h"
#include <iostream>
#include <vector>

class EventSource
{
public:
// we use these for events in this example
        virtual void OnActionStarted(void) = 0;
        virtual void OnActionCompleted(void) = 0;
};

class Client : public EventSource  // <-- The CLIENT implements the
Event interface.
{
public:
        void OnActionCompleted(void);
        void OnActionStarted(void);
};

class Server
{
public:
        void PerformServerAction(void);
        void Register(EventSource* e);            // <-- X number of clients.
Add ours to the list
private:
        std::vector<EventSource*> listeners;
};

void Server::PerformServerAction()
{
        // i recommend std::for_each, but too I'm lazy tonight
        for(std::vector<EventSource*>::size_type i = 0; i < listeners.size();
++i)
        {
                EventSource* es = listeners.at(i);
                es->OnActionStarted();
                // perform something meaningful here.
                es->OnActionCompleted();
        }
}

void Server::Register(EventSource* e)
{
        listeners.push_back(e);
}

void Client::OnActionCompleted()
{
        std::cout << "Important action has ended." << std::endl;
}

void Client::OnActionStarted()
{
        std::cout << "Important action has started." << std::endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
        Server srv;
        Client cli;

        srv.Register(&cli);
        srv.PerformServerAction();
        return 0;
}

_______________________________________________
dev-tech-xpcom mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-xpcom

Reply via email to