#include "msg.h"
#include "config.h"
#include TYPEINFO_HDR

namespace msg {
// ------ default comparison: messages of equal types are idempotent -------------------
bool Message::operator < (const Message& that) const {
    return typeid (*this).before (typeid (that));
}
// ------ send message to all registered listeners --------------------------------------
int Talker::broadcast (const Message* msg) {
    int audience=0;
    if (broadcastSwitch) {
        for (t_listeners::iterator item = listeners.begin(); item != listeners.end(); item++) {
            (*item)->receive (msg);
            audience++;
        }
    }
    return audience;
}
// ------ store msg if not already stored -----------------------------------------------
void IdempotentListener::receive (const Message* m) {
    store.insert (m);
}
// ------ test if any idempotent msg present --------------------------------------------
const Message* IdempotentListener::find (const Message* m) {
    store_t::iterator msg = store.find (m);
    return (msg == store.end() ? NULL : *msg);
}

}
