Program Listing for File signal.hpp
↰ Return to documentation for file (include/ecl/sigslots_lite/signal.hpp
)
/*****************************************************************************
** Ifdefs
*****************************************************************************/
#ifndef ECL_SIGSLOTS_LITE_SIGNAL_HPP_
#define ECL_SIGSLOTS_LITE_SIGNAL_HPP_
/*****************************************************************************
** Includes
*****************************************************************************/
#include "slot.hpp"
#include "errors.hpp"
/*****************************************************************************
** Namespaces
*****************************************************************************/
namespace ecl {
namespace lite {
/*****************************************************************************
** Classes
*****************************************************************************/
template <typename Data=void, unsigned int Capacity = 1>
class Signal {
public:
Signal() {
for ( unsigned int i = 0; i < Capacity; ++i) {
slots[i] = NULL;
}
}
sigslots::Error connect(sigslots::SlotBase<Data> &slot) {
for (unsigned int i = 0; i < Capacity; ++i ) {
if ( slots[i] == NULL ) {
slots[i] = &slot;
return sigslots::Error(sigslots::NoError);
}
}
// if we reach here, we've run out of resources.
return sigslots::Error(sigslots::OutOfResourcesError);
}
unsigned int capacity() const { return Capacity; }
unsigned int stored() const {
unsigned int i;
for (i = 0; i < Capacity; ++i ) {
if ( slots[i] == NULL ) {
break;
}
}
return i;
}
void emit(Data data) const {
for (unsigned int i = 0; i < Capacity; ++i ) {
if ( slots[i] == NULL ) {
break;
} else {
slots[i]->execute(data);
}
}
}
private:
sigslots::SlotBase<Data> *slots[Capacity];
};
template <unsigned int Capacity>
class Signal<void,Capacity> {
public:
Signal() {
for ( unsigned int i = 0; i < Capacity; ++i) {
slots[i] = NULL;
}
}
sigslots::Error connect(sigslots::SlotBase<void> &slot) {
for (unsigned int i = 0; i < Capacity; ++i ) {
if ( slots[i] == NULL ) {
slots[i] = &slot;
return sigslots::Error(sigslots::NoError);
}
}
// if we reach here, we've run out of resources.
return sigslots::Error(sigslots::OutOfResourcesError);
}
unsigned int capacity() const { return Capacity; }
unsigned int stored() const {
unsigned int i;
for (i = 0; i < Capacity; ++i ) {
if ( slots[i] == NULL ) {
break;
}
}
return i;
}
void emit() const {
for (unsigned int i = 0; i < Capacity; ++i ) {
if ( slots[i] == NULL ) {
break;
} else {
slots[i]->execute();
}
}
}
private:
sigslots::SlotBase<void> *slots[Capacity];
};
} // namespace lite
} // namespace ecl
#endif /* ECL_SIGSLOTS_LITE_SIGNAL_HPP_ */