subscriber.hpp
Go to the documentation of this file.
00001 
00006 /*****************************************************************************
00007 ** Ifdefs
00008 *****************************************************************************/
00009 
00010 #ifndef mm_radio_SUBSCRIBER_HPP_
00011 #define mm_radio_SUBSCRIBER_HPP_
00012 
00013 /*****************************************************************************
00014 ** Includes
00015 *****************************************************************************/
00016 
00017 #include <ecl/utilities/function_objects.hpp>
00018 #include <mm_messages.hpp>
00019 #include <sstream>
00020 #include <string>
00021 #include "radio.hpp"
00022 
00023 /*****************************************************************************
00024 ** Namespaces
00025 *****************************************************************************/
00026 
00027 namespace mm_radio {
00028 
00029 /*****************************************************************************
00030 ** Interfaces
00031 *****************************************************************************/
00032 
00033 template<unsigned int ID, typename DataType>
00034 class Subscriber {
00035 public:
00036   typedef mm_messages::MessageRegistry MessageRegistry;
00037   typedef mm_messages::ByteArray ByteArray;
00038 
00046   Subscriber(const std::string& name,
00047              void (*f)(DataType)) :
00048     name(name),
00049     id(ID),
00050     function(new ecl::UnaryFreeFunction<DataType>(f))
00051   {
00052     if ( !MessageRegistry::isRegistered(ID) ) {
00053       std::stringstream ss;
00054       ss << "id '" << id << "' has not been registered";
00055       throw mm_messages::UnregisteredID(ss.str());
00056     }
00057     if ( !MessageRegistry::isRegisteredWithType<DataType>(ID) ) {
00058       std::stringstream ss;
00059       ss << "id '" << id << "' is registered, but not with this type";
00060       throw mm_messages::InvalidIDTypeCombination(ss.str());
00061     }
00062     Radio::registerSubscriber(name, id, &Subscriber<ID, DataType>::processPacket, (*this));
00063   }
00073   template<typename C>
00074   Subscriber(const std::string& name,
00075              void (C::*f)(DataType), C &c) :
00076     name(name),
00077     id(ID),
00078     function(new ecl::PartiallyBoundUnaryMemberFunction<C, DataType,void>(f,c))
00079   {
00080     Radio::registerSubscriber(name, id, &Subscriber<ID, DataType>::processPacket, (*this));
00081   }
00082   virtual ~Subscriber() {
00083     Radio::unregisterSubscriber(name, id);
00084     delete function;
00085   }
00086 
00087   void processPacket(const unsigned char* buffer, const unsigned int& size) {
00088     DataType data = mm_messages::Message<DataType>::decode(buffer, size);
00089     (*function)(data);
00090   }
00091 
00092 private:
00093   std::string name;
00094   unsigned int id;
00095   ecl::UnaryFunction<DataType,void> *function;
00096 };
00097 
00098 } // namespace mm_radio
00099 
00100 #endif /* mm_radio_SUBSCRIBER_HPP_ */


mm_radio
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 21:13:24