00001 00010 /***************************************************************************** 00011 ** Ifdefs 00012 *****************************************************************************/ 00013 00014 #ifndef ECL_SIGSLOTS_LITE_SIGNAL_HPP_ 00015 #define ECL_SIGSLOTS_LITE_SIGNAL_HPP_ 00016 00017 /***************************************************************************** 00018 ** Includes 00019 *****************************************************************************/ 00020 00021 #include "slot.hpp" 00022 #include "errors.hpp" 00023 00024 /***************************************************************************** 00025 ** Namespaces 00026 *****************************************************************************/ 00027 00028 namespace ecl { 00029 namespace lite { 00030 00031 /***************************************************************************** 00032 ** Classes 00033 *****************************************************************************/ 00062 template <typename Data=void, unsigned int Capacity = 1> 00063 class Signal { 00064 public: 00068 Signal() { 00069 for ( unsigned int i = 0; i < Capacity; ++i) { 00070 slots[i] = NULL; 00071 } 00072 } 00088 sigslots::Error connect(sigslots::SlotBase<Data> &slot) { 00089 for (unsigned int i = 0; i < Capacity; ++i ) { 00090 if ( slots[i] == NULL ) { 00091 slots[i] = &slot; 00092 return sigslots::Error(sigslots::NoError); 00093 } 00094 } 00095 // if we reach here, we've run out of resources. 00096 return sigslots::Error(sigslots::OutOfResourcesError); 00097 } 00103 unsigned int capacity() const { return Capacity; } 00109 unsigned int stored() const { 00110 unsigned int i; 00111 for (i = 0; i < Capacity; ++i ) { 00112 if ( slots[i] == NULL ) { 00113 break; 00114 } 00115 } 00116 return i; 00117 00118 } 00119 00125 void emit(Data data) const { 00126 for (unsigned int i = 0; i < Capacity; ++i ) { 00127 if ( slots[i] == NULL ) { 00128 break; 00129 } else { 00130 slots[i]->execute(data); 00131 } 00132 } 00133 } 00134 00135 private: 00136 sigslots::SlotBase<Data> *slots[Capacity]; 00137 }; 00138 00147 template <unsigned int Capacity> 00148 class Signal<void,Capacity> { 00149 public: 00153 Signal() { 00154 for ( unsigned int i = 0; i < Capacity; ++i) { 00155 slots[i] = NULL; 00156 } 00157 } 00173 sigslots::Error connect(sigslots::SlotBase<void> &slot) { 00174 for (unsigned int i = 0; i < Capacity; ++i ) { 00175 if ( slots[i] == NULL ) { 00176 slots[i] = &slot; 00177 return sigslots::Error(sigslots::NoError); 00178 } 00179 } 00180 // if we reach here, we've run out of resources. 00181 return sigslots::Error(sigslots::OutOfResourcesError); 00182 } 00188 unsigned int capacity() const { return Capacity; } 00189 00195 unsigned int stored() const { 00196 unsigned int i; 00197 for (i = 0; i < Capacity; ++i ) { 00198 if ( slots[i] == NULL ) { 00199 break; 00200 } 00201 } 00202 return i; 00203 00204 } 00208 void emit() const { 00209 for (unsigned int i = 0; i < Capacity; ++i ) { 00210 if ( slots[i] == NULL ) { 00211 break; 00212 } else { 00213 slots[i]->execute(); 00214 } 00215 } 00216 } 00217 00218 private: 00219 sigslots::SlotBase<void> *slots[Capacity]; 00220 }; 00221 00222 00223 } // namespace lite 00224 } // namespace ecl 00225 00226 #endif /* ECL_SIGSLOTS_LITE_SIGNAL_HPP_ */