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 NameServerRegistrator<EventType*>(nameserver, name, this),
00119 mname(name)
00120 {
00121 Logger::log() << Logger::Debug << "Event Created with name : "<< name << Logger::endl;
00122 }
00123
00127 Event()
00128 {
00129 }
00130
00136 Event(const Event& m)
00137 : Base(m),
00138 mname(m.mname)
00139 {}
00140
00148 Event& operator=(const Event& m)
00149 {
00150 if ( this == &m )
00151 return *this;
00152 mname = m.mname;
00153 this->impl = m.impl;
00154 return *this;
00155 }
00156
00163 Event(boost::shared_ptr<ActionInterface> implementation)
00164 : Base( boost::dynamic_pointer_cast< detail::EventBase<Signature> >(implementation) ),
00165 mname()
00166 {
00167 if ( !this->impl && implementation ) {
00168 log(Error) << "Tried to construct Event from incompatible type."<< endlog();
00169 }
00170 }
00171
00180 Event& operator=(boost::shared_ptr<ActionInterface> implementation)
00181 {
00182 if (this->impl == implementation)
00183 return *this;
00184 this->impl = boost::dynamic_pointer_cast< detail::EventBase<Signature> >(implementation);
00185 if ( !this->impl && implementation ) {
00186 log(Error) << "Tried to assign Event '"<< mname <<"' from incompatible type."<< endlog();
00187 }
00188 return *this;
00189 }
00190
00196 bool ready() const {
00197 return this->impl;
00198 }
00199
00203 const std::string& getName() const { return mname; }
00204
00208 int arity() const { return this->impl ? this->impl->arity() : -1; }
00209
00213 Handle connect(const SlotFunction& f)
00214 {
00215 return this->impl ? this->impl->connect( f ) : Handle();
00216 }
00217
00221 Handle connect( const SlotFunction& l, EventProcessor* ep, AsynStorageType t = EventProcessor::OnlyFirst)
00222 {
00223 return this->impl ? ep->connect( l, *this, t ) : Handle();
00224 }
00225
00229 Handle setup(const SlotFunction& f)
00230 {
00231 return this->impl ? this->impl->setup( f ) : Handle();
00232 }
00233
00237 Handle setup( const SlotFunction& l, EventProcessor* ep, AsynStorageType t = EventProcessor::OnlyFirst)
00238 {
00239 return this->impl ? ep->setup( l, *this, t ) : Handle();
00240 }
00241
00242 EventBasePtr getImplementation() const { return this->impl; }
00243
00249 static NameServer<EventType*> nameserver;
00250 };
00251
00252
00253 template<
00254 typename SignatureT
00255 >
00256 NameServer<Event<SignatureT>*>
00257 Event<SignatureT>::nameserver;
00258
00259 }
00260
00261 #endif