Event.hpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef ORO_CORELIB_EVENT_HPP
00040 #define ORO_CORELIB_EVENT_HPP
00041
00042 #include "Signal.hpp"
00043 #include "Handle.hpp"
00044 #include "LocalEvent.hpp"
00045 #include <boost/call_traits.hpp>
00046 #include <boost/function.hpp>
00047 #include "NameServerRegistrator.hpp"
00048 #include "Invoker.hpp"
00049
00050 #include "Logger.hpp"
00051 #include "EventProcessor.hpp"
00052 #include <cassert>
00053
00054 namespace RTT
00055 {
00056
00081 template<
00082 typename SignatureT
00083 >
00084 class Event
00085 : public detail::InvokerSignature<boost::function_traits<SignatureT>::arity,
00086 SignatureT,
00087 boost::shared_ptr< detail::EventBase<SignatureT> > >,
00088 private NameServerRegistrator<Event<SignatureT>*>
00089 {
00090 typedef SignatureT FunctionT;
00091 std::string mname;
00092 typedef boost::shared_ptr< detail::EventBase<FunctionT> > EventBasePtr;
00093 typedef detail::InvokerSignature<boost::function_traits<FunctionT>::arity,
00094 FunctionT,
00095 EventBasePtr > Base;
00096 public:
00100 typedef EventProcessor::AsynStorageType AsynStorageType;
00101
00102 typedef boost::function<SignatureT> SlotFunction;
00103
00104 typedef Event<SignatureT> EventType;
00105
00106 typedef SignatureT Signature;
00107
00111 typedef typename boost::function_traits<SignatureT>::result_type result_type;
00112
00116 explicit Event(const std::string name )
00117 : Base( EventBasePtr(new detail::LocalEvent<Signature>()) ),
00118 mname(name)
00119 {
00120 Logger::log() << Logger::Debug << "Event Created with name : "<< name << Logger::endl;
00121 }
00122
00126 Event()
00127 {
00128 }
00129
00135 Event(const Event& m)
00136 : Base(m),
00137 mname(m.mname)
00138 {}
00139
00147 Event& operator=(const Event& m)
00148 {
00149 if ( this == &m )
00150 return *this;
00151 mname = m.mname;
00152 this->impl = m.impl;
00153 return *this;
00154 }
00155
00162 Event(boost::shared_ptr<ActionInterface> implementation)
00163 : Base( boost::dynamic_pointer_cast< detail::EventBase<Signature> >(implementation) ),
00164 mname()
00165 {
00166 if ( !this->impl && implementation ) {
00167 log(Error) << "Tried to construct Event from incompatible type."<< endlog();
00168 }
00169 }
00170
00179 Event& operator=(boost::shared_ptr<ActionInterface> implementation)
00180 {
00181 if (this->impl == implementation)
00182 return *this;
00183 this->impl = boost::dynamic_pointer_cast< detail::EventBase<Signature> >(implementation);
00184 if ( !this->impl && implementation ) {
00185 log(Error) << "Tried to assign Event '"<< mname <<"' from incompatible type."<< endlog();
00186 }
00187 return *this;
00188 }
00189
00195 bool ready() const {
00196 return this->impl;
00197 }
00198
00202 const std::string& getName() const { return mname; }
00203
00207 int arity() const { return this->impl ? this->impl->arity() : -1; }
00208
00212 Handle connect(const SlotFunction& f)
00213 {
00214 return this->impl ? this->impl->connect( f ) : Handle();
00215 }
00216
00220 Handle connect( const SlotFunction& l, EventProcessor* ep, AsynStorageType t = EventProcessor::OnlyFirst)
00221 {
00222 return this->impl ? ep->connect( l, *this, t ) : Handle();
00223 }
00224
00228 Handle setup(const SlotFunction& f)
00229 {
00230 return this->impl ? this->impl->setup( f ) : Handle();
00231 }
00232
00236 Handle setup( const SlotFunction& l, EventProcessor* ep, AsynStorageType t = EventProcessor::OnlyFirst)
00237 {
00238 return this->impl ? ep->setup( l, *this, t ) : Handle();
00239 }
00240
00241 EventBasePtr getImplementation() const { return this->impl; }
00242
00248 static NameServer<EventType*> nameserver;
00249 };
00250
00251
00252 template<
00253 typename SignatureT
00254 >
00255 NameServer<Event<SignatureT>*>
00256 Event<SignatureT>::nameserver;
00257
00258 }
00259
00260 #endif