signals.cpp 1.65 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/*
 * signals.cpp
 *
 *  Created on: 25/03/2015
 *      Author: raster
 */

#include "signals.hh"

Signals::Signals() {

}

Signals::~Signals() {

	// Unsubscribe from all the emiters
	std::list<class Signals *>::iterator iter;
	for (iter = this->subscribed.begin(); iter != this->subscribed.end(); ++iter) {
		(*iter)->unregister_signal("",this);
	}
}


void Signals::send_signal(std::string signal_name) {

	std::map<string,class Signals *>::iterator iter;
	for(iter = this->signal_list.find(signal_name); iter != this->signal_list.end(); ++iter) {
		if (!((*iter).second->callback_receiver(signal_name,this))) {
			break;
		}
	}
}

void Signals::register_signal(std::string signal_name, class Signals *receiver) {

	std::map<string,class Signals *>::iterator iter;
	for(iter = this->signal_list.find(signal_name); iter != this->signal_list.end(); ++iter) {
		if ((*iter).second == receiver) {
			return; // this signal is already connected to the receiver
		}
	}
	this->signal_list.insert(std::pair<string,class Signals *>(signal_name,receiver));
}

void Signals::unregister_signal(std::string signal_name, class Signals *receiver) {

	std::map<string,class Signals *>::iterator iter;
	if (signal_name != "") {
		for(iter = this->signal_list.find(signal_name); iter != this->signal_list.end(); ++iter) {
			if ((*iter).second == receiver) {
				this->signal_list.erase(iter);
				return;
			}
		}
	} else {
		for(iter = this->signal_list.begin(); iter != this->signal_list.end(); ++iter) {
			if ((*iter).second == receiver) {
				this->signal_list.erase(iter);
				return;
			}
		}
	}
}

bool Signals::callback_receiver(std::string signal_name, class Signals *sender) {
	return true;
}