sigslots.cpp
Go to the documentation of this file.
00001 
00011 /*****************************************************************************
00012 ** Includes
00013 *****************************************************************************/
00014 
00015 #include <iostream>
00016 #include "../../include/ecl/sigslots_lite.hpp"
00017 
00018 /*****************************************************************************
00019 ** Foo
00020 *****************************************************************************/
00024 class Foo : public ecl::lite::MemberSlots<const char*,Foo>,
00025                         public ecl::lite::MemberSlots<void,Foo,2>
00026 {
00027 public:
00028         Foo() {}
00029 
00030         void f(const char* str) {
00031                 std::cout << "Foo::f() " << str << std::endl;
00032         }
00033         void h() {
00034                 std::cout << "Foo::h()" << std::endl;
00035         }
00036 };
00037 
00038 void f(const char* str) {
00039         std::cout << "f(char*) " << str << std::endl;
00040 }
00041 
00042 void g(const char* str) {
00043         std::cout << "g(char*) " << str << std::endl;
00044 }
00045 
00046 void h() {
00047         std::cout << "h()" << std::endl;
00048 }
00049 
00050 /*****************************************************************************
00051 ** Static Variables
00052 *****************************************************************************/
00053 
00054 template<>
00055 const unsigned int ecl::lite::GlobalSlots<const char*>::capacity = 4;
00056 
00057 template<>
00058 const unsigned int ecl::lite::GlobalSlots<void>::capacity = 2;
00059 
00064 /*****************************************************************************
00065 ** Main
00066 *****************************************************************************/
00067 
00068 int main(int argc, char **argv) {
00069 
00070         Foo foo;
00071 
00072     std::cout << std::endl;
00073     std::cout << "***********************************************************" << std::endl;
00074     std::cout << "                   Data Sigslots" << std::endl;
00075     std::cout << "***********************************************************" << std::endl;
00076     std::cout << std::endl;
00077 
00078         ecl::lite::Signal<const char*,2> dual_slot_signal;
00079         ecl::lite::Signal<const char*> lone_signal1, lone_signal2;
00080         // koenig lookup means a simple connect(dual_slot_signal,f) also works
00081         ecl::lite::connect(dual_slot_signal,f);
00082         ecl::lite::connect(dual_slot_signal,g);
00083         dual_slot_signal.emit("one signal, two global slots");
00084         std::cout << std::endl;
00085 
00086         ecl::lite::connect(lone_signal1,&Foo::f,foo);
00087         lone_signal1.emit("direct member slot connection");
00088         std::cout << std::endl;
00089 
00090         ecl::lite::connect(lone_signal2,&Foo::f,foo);
00091         lone_signal1.emit("multiple signals, one slot connection - I");
00092         lone_signal2.emit("multiple signals, one slot connection - II");
00093         std::cout << std::endl;
00094 
00095     std::cout << std::endl;
00096     std::cout << "***********************************************************" << std::endl;
00097     std::cout << "                   Void Sigslots" << std::endl;
00098     std::cout << "***********************************************************" << std::endl;
00099     std::cout << std::endl;
00100 
00101         ecl::lite::Signal<void> void_signal, member_void_signal;
00102         connect(member_void_signal,&Foo::h, foo);
00103         connect(void_signal,h);
00104         void_signal.emit();
00105         member_void_signal.emit();
00106 
00107     std::cout << std::endl;
00108     std::cout << "***********************************************************" << std::endl;
00109     std::cout << "                   Storage" << std::endl;
00110     std::cout << "***********************************************************" << std::endl;
00111     std::cout << std::endl;
00112 
00113         std::cout << "GlobalSlots<const char*> Stored  : " << ecl::lite::global_slots_stored<const char*>() << std::endl;
00114     std::cout << "GlobalSlots<const char*> Capacity: " << ecl::lite::global_slots_capacity<const char*>() << std::endl;
00115     std::cout << std::endl;
00116         std::cout << "GlobalSlots<void> Stored  : " << ecl::lite::global_slots_stored<void>() << std::endl;
00117     std::cout << "GlobalSlots<void> Capacity: " << ecl::lite::global_slots_capacity<void>() << std::endl;
00118     std::cout << std::endl;
00119 
00120         std::cout << "MemberSlots<const char*> Stored(foo)  : " << ecl::lite::member_slots_stored<const char*>(foo) << std::endl;
00121     std::cout << "MemberSlots<const char*> Capacity(Foo): " << ecl::lite::member_slots_capacity<const char*>(foo) << std::endl;
00122     std::cout << std::endl;
00123         std::cout << "MemberSlots<void> Stored(foo)  : " << ecl::lite::member_slots_stored<void>(foo) << std::endl;
00124     std::cout << "MemberSlots<void> Capacity(Foo): " << ecl::lite::member_slots_capacity<void>(foo) << std::endl;
00125     std::cout << std::endl;
00126 
00127         ecl::lite::Signal<const char*,2> signal_storage_test;
00128         std::cout << "Incrementally adding slots to signal<const char*,2>" << std::endl;
00129         std::cout << "  capacity: " << signal_storage_test.capacity() << std::endl;
00130         std::cout << "  stored  : " << signal_storage_test.stored() << std::endl;
00131         connect(signal_storage_test,f);
00132         std::cout << "  stored  : " << signal_storage_test.stored() << std::endl;
00133         connect(signal_storage_test,g);
00134         std::cout << "  stored  : " << signal_storage_test.stored() << std::endl;
00135     std::cout << std::endl;
00136 
00137     std::cout << std::endl;
00138     std::cout << "***********************************************************" << std::endl;
00139     std::cout << "                   Error Handling" << std::endl;
00140     std::cout << "***********************************************************" << std::endl;
00141     std::cout << std::endl;
00142 
00143     ecl::lite::sigslots::Error error;
00144     std::cout << "Adding two slots to a single slot capacity signal." << std::endl;
00145     ecl::lite::Signal<const char*> one_shot_signal;
00146     std::cout << "  Connecting a first slot" << std::endl;
00147     error = connect(one_shot_signal,f);
00148     if ( error.flag() != ecl::lite::sigslots::NoError ) {
00149         std::cout << "  " << error.what() << std::endl;
00150     }
00151     std::cout << "  Connecting a second slot" << std::endl;
00152     error = connect(one_shot_signal,f);
00153     if ( error.flag() != ecl::lite::sigslots::NoError ) {
00154         std::cout << "  " << error.what() << std::endl;
00155     }
00156         std::cout << std::endl;
00157 
00158     std::cout << "Adding two slots to a single slot capacity MemberSlots interface." << std::endl;
00159     ecl::lite::Signal<const char*> two_shot_signal;
00160     Foo foo_new; // 0 slots stored in MemberSlots<char*>
00161     std::cout << "  Connecting a first slot" << std::endl;
00162     error = connect(two_shot_signal, &Foo::f, foo_new); // 1 slot stored in MemberSlots<char*>
00163     if ( error.flag() != ecl::lite::sigslots::NoError ) {
00164         std::cout << "  " << error.what() << std::endl;
00165     }
00166     std::cout << "  Connecting a second slot" << std::endl;
00167     error = connect(two_shot_signal, &Foo::f, foo_new); // 2nd slot, when only 1 declared, fail!
00168     if ( error.flag() != ecl::lite::sigslots::NoError ) {
00169         std::cout << "  " << error.what() << std::endl;
00170     }
00171 
00172     std::cout << std::endl;
00173     std::cout << "***********************************************************" << std::endl;
00174     std::cout << "                   Passed" << std::endl;
00175     std::cout << "***********************************************************" << std::endl;
00176     std::cout << std::endl;
00177 
00178         return 0;
00179 }


ecl_sigslots_lite
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 18:53:25