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-2010 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 <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 gbxiceutilacfr { 00022 00029 template<class Type> 00030 class NotifyHandler 00031 { 00032 public: 00033 virtual ~NotifyHandler() {}; 00037 virtual void handleData( const Type & obj )=0; 00038 }; 00039 00051 template<class Type> 00052 class Notify 00053 { 00054 public: 00055 Notify() 00056 : hasNotifyHandler_(false) 00057 {}; 00058 00059 virtual ~Notify() {}; 00060 00063 void setNotifyHandler( NotifyHandler<Type>* handler ); 00064 00066 bool hasNotifyHandler() { return hasNotifyHandler_; }; 00067 00071 void set( const Type & obj ); 00072 00073 protected: 00075 virtual void internalSet( const Type & obj ); 00076 00078 NotifyHandler<Type>* handler_; 00079 00080 private: 00081 00082 bool hasNotifyHandler_; 00083 }; 00084 00085 template<class Type> 00086 void Notify<Type>::setNotifyHandler( NotifyHandler<Type>* handler ) 00087 { 00088 if ( handler == 0 ) { 00089 std::cout<<"TRACE(notify.h): no handler set. Ignoring data." << std::endl; 00090 return; 00091 } 00092 00093 handler_ = handler; 00094 hasNotifyHandler_ = true; 00095 } 00096 00097 template<class Type> 00098 void Notify<Type>::set( const Type & obj ) 00099 { 00100 if ( !hasNotifyHandler_ ) { 00101 throw gbxutilacfr::Exception( ERROR_INFO, "setting data when data handler has not been set" ); 00102 } 00103 00104 internalSet( obj ); 00105 } 00106 00107 template<class Type> 00108 void Notify<Type>::internalSet( const Type & obj ) 00109 { 00110 handler_->handleData( obj ); 00111 } 00112 00113 } // end namespace 00114 00115 #endif |