INTRODUCTION Overview Download and Install Documentation Publications REPOSITORY Libraries DEVELOPER Dev Guide Dashboard PEOPLE Contributors Users Project Download Mailing lists
|
notify.h00001 /* 00002 * GearBox Project: Peer-Reviewed Open-Source Libraries for Robotics 00003 * http://gearbox.sf.net/ 00004 * Copyright (c) 2004-2008 Alex Brooks, Alexei Makarenko, Tobias Kaupp 00005 * 00006 * This distribution is licensed to you under the terms described in 00007 * the LICENSE file included in this distribution. 00008 * 00009 */ 00010 00011 #ifndef GBXICEUTILACFR_NOTIFY_H 00012 #define GBXICEUTILACFR_NOTIFY_H 00013 00014 #include <gbxsickacfr/gbxutilacfr/exceptions.h> 00015 #include <iostream> 00016 00017 // 00018 // note: this class can be libGbxUtilAcfr but we keep it with the other "data pattern" 00019 // classes: Store and Buffer. 00020 // 00021 namespace gbxsickacfr { 00022 namespace gbxiceutilacfr { 00023 00030 template<class Type> 00031 class NotifyHandler 00032 { 00033 public: 00034 virtual ~NotifyHandler() {}; 00038 virtual void handleData( const Type & obj )=0; 00039 }; 00040 00049 template<class Type> 00050 class Notify 00051 { 00052 public: 00053 Notify() 00054 : hasNotifyHandler_(false) 00055 {}; 00056 00057 virtual ~Notify() {}; 00058 00061 void setNotifyHandler( NotifyHandler<Type>* handler ); 00062 00064 bool hasNotifyHandler() { return hasNotifyHandler_; }; 00065 00069 void set( const Type & obj ); 00070 00071 protected: 00073 virtual void internalSet( const Type & obj ); 00074 00076 NotifyHandler<Type>* handler_; 00077 00078 private: 00079 00080 bool hasNotifyHandler_; 00081 }; 00082 00083 template<class Type> 00084 void Notify<Type>::setNotifyHandler( NotifyHandler<Type>* handler ) 00085 { 00086 if ( handler == 0 ) { 00087 std::cout<<"TRACE(notify.h): no handler set. Ignoring data." << std::endl; 00088 return; 00089 } 00090 00091 handler_ = handler; 00092 hasNotifyHandler_ = true; 00093 } 00094 00095 template<class Type> 00096 void Notify<Type>::set( const Type & obj ) 00097 { 00098 if ( !hasNotifyHandler_ ) { 00099 throw gbxsickacfr::gbxutilacfr::Exception( ERROR_INFO, "setting data when data handler has not been set" ); 00100 } 00101 00102 internalSet( obj ); 00103 } 00104 00105 template<class Type> 00106 void Notify<Type>::internalSet( const Type & obj ) 00107 { 00108 handler_->handleData( obj ); 00109 } 00110 00111 } 00112 } // end namespace 00113 00114 #endif |