manager.hpp
Go to the documentation of this file.
00001 
00010 /*****************************************************************************
00011 ** Ifdefs
00012 *****************************************************************************/
00013 
00014 #ifndef ECL_SIGSLOTS_MANAGER_HPP_
00015 #define ECL_SIGSLOTS_MANAGER_HPP_
00016 
00017 /*****************************************************************************
00018 ** Includes
00019 *****************************************************************************/
00020 
00021 #include <iostream>
00022 #include <map>
00023 #include <string>
00024 #include <ecl/exceptions/standard_exception.hpp>
00025 #include <ecl/config/macros.hpp>
00026 #include <ecl/utilities/void.hpp>
00027 #include "topic.hpp"
00028 
00029 /*****************************************************************************
00030 ** Namespaces
00031 *****************************************************************************/
00032 
00033 namespace ecl {
00034 
00035 /*****************************************************************************
00036 ** Interface
00037 *****************************************************************************/
00048 template<typename Data = Void>
00049 class SigSlotsManager {
00050 public:
00051 
00052         friend class SigSlot<Data>;
00053 
00059         static void printStatistics() {
00060                 std::cout << "Topics" << std::endl;
00061                 typename std::map< std::string, Topic<Data> >::iterator iter;
00062                 for ( iter = topics().begin(); iter != topics().end(); ++iter ) {
00063                         std::cout << iter->second;
00064                 }
00065         }
00066 
00067 private:
00071         typedef typename Topic<Data>::Subscribers Subscribers;
00072 
00082         static const Subscribers* connectSignal(const std::string& topic, SigSlot<Data>* sigslot) {
00083                 // Try and insert a new topic in case it doesn't already exist
00084                 // In any case, we always get the iterator back (to new or existing)
00085                 //
00086                 // Maybe improve the efficiency of this by specifying position
00087                 // refer to http://www.cplusplus.com/reference/stl/map/insert/
00088                 std::pair< typename std::map< std::string, Topic<Data> >::iterator,bool> ret = topics().insert( std::pair< std::string, Topic<Data> >(topic, Topic<Data>(topic)) );
00089                 Topic<Data>& current_topic = (ret.first)->second;
00090                 current_topic.addPublisher(sigslot);
00091                 return current_topic.subscribers();
00092         }
00093 
00101         static void connectSlot(const std::string& topic, SigSlot<Data>* sigslot) {
00102                 // Try and insert a new topic in case it doesn't already exist
00103                 // In any case, we always get the iterator back (to new or existing)
00104                 //
00105                 // Maybe improve the efficiency of this by specifying position
00106                 // refer to http://www.cplusplus.com/reference/stl/map/insert/
00107                 std::pair< typename std::map< std::string, Topic<Data> >::iterator,bool> ret = topics().insert(std::pair< std::string, Topic<Data> >(topic, Topic<Data>(topic)) );
00108                 Topic<Data>& current_topic = (ret.first)->second;
00109                 current_topic.addSubscriber(sigslot);
00110         }
00111 
00120         static void disconnect(const std::string& topic, SigSlot<Data>* sigslot) {
00121                 typename std::map<std::string, Topic<Data> >::iterator iter = topics().find(topic);
00122                 if ( iter != topics().end() ) {
00123                         iter->second.disconnect(sigslot);
00124                 }
00125                 if ( iter->second.empty() ) {
00126                         topics().erase(iter);
00127                 }
00128         }
00129 
00136         static bool isTopic(const std::string& topic) {
00137                 return !( topics().find(topic) == topics().end() );
00138         }
00139 
00147         static std::map< std::string, Topic<Data> >& topics() {
00148                 static std::map< std::string, Topic<Data> > topic_list;
00149                 return topic_list;
00150         }
00151 
00162         static const Subscribers& subscribers(const std::string& topic) {
00163                 typename std::map< std::string, Topic<Data> >::const_iterator iter = topics().find(topic);
00164                 /*
00165                  * Note that this is called only by SigSlotsManager::connectSignal which
00166                  * makes sure the topic name exists, so we don't need to do any error
00167                  * handling here.
00168                  */
00169                 // ecl_assert_throw( iter != topics().end(), StandardException(LOC,InvalidInputError,std::string("No sigslots topic with name:")+topic) );
00170                 return *iter->second.subscribers();
00171         }
00172 
00173 };
00174 
00175 } // namespace ecl
00176 
00177 
00178 #endif /* ECL_SIGSLOTS_MANAGER_HPP_ */


ecl_sigslots
Author(s): Daniel Stonier
autogenerated on Sun Oct 5 2014 23:35:39