DeviceDriver.h
Go to the documentation of this file.00001
00002 #ifndef INCLUDED_DeviceDriver_h_
00003 #define INCLUDED_DeviceDriver_h_
00004
00005 #include "Shared/InstanceTracker.h"
00006 #include "Shared/plistCollections.h"
00007 #include <set>
00008 class MotionHook;
00009 class DataSource;
00010
00011
00012 class DeviceDriver : public virtual plist::Dictionary {
00013 public:
00014
00015 virtual ~DeviceDriver() { getRegistry().destroy(instanceName); }
00016
00017
00018
00019
00020 virtual std::string getClassName() const=0;
00021
00022 virtual std::string getName() const { return instanceName; }
00023
00024 virtual MotionHook* getMotionSink() { return NULL; }
00025 virtual void getSensorSources(std::map<std::string,DataSource*>& sources) { sources.clear(); }
00026 virtual void getImageSources(std::map<std::string,DataSource*>& sources) { sources.clear(); }
00027
00028 typedef InstanceTracker<DeviceDriver,std::string,Factory1Arg<DeviceDriver,std::string> > registry_t;
00029 static registry_t& getRegistry() { static registry_t registry; return registry; }
00030
00031
00032 class SourceListener {
00033 public:
00034 virtual ~SourceListener() {};
00035 virtual void dataSourcesUpdated()=0;
00036 };
00037
00038
00039 virtual void addSourceListener(SourceListener* l) { if(l!=NULL) sourceListeners.insert(l); }
00040
00041 virtual void removeSourceListener(SourceListener* l) { sourceListeners.erase(l); }
00042
00043 protected:
00044
00045 DeviceDriver(const std::string& , const std::string& instancename)
00046 : plist::Dictionary(), instanceName(instancename), sourceListeners()
00047 {
00048 setLoadSavePolicy(FIXED,SYNC);
00049 }
00050
00051
00052
00053
00054 virtual void registerInstance() {
00055 if(DeviceDriver * inst=getRegistry().getInstance(instanceName)) {
00056 if(inst==this)
00057 return;
00058 std::cerr << "Warning: registration of DeviceDriver " << getClassName() << " named " << instanceName << " @ " << this
00059 << " blocked by previous " << inst->getClassName() << " instance of same name @ " << inst << std::endl;
00060 }
00061 if(!getRegistry().registerInstance(getClassName(),instanceName,this))
00062 std::cerr << "Error: failed to register " << getClassName() << " named " << instanceName << " @ " << this;
00063
00064 }
00065
00066
00067 virtual void fireDataSourcesUpdated() {
00068 std::set<SourceListener*> notify=sourceListeners;
00069 for(std::set<SourceListener*>::const_iterator it=notify.begin(); it!=notify.end(); ++it) {
00070 if(sourceListeners.find(*it)!=sourceListeners.end())
00071 (*it)->dataSourcesUpdated();
00072 }
00073 }
00074
00075 const std::string instanceName;
00076 std::set<SourceListener*> sourceListeners;
00077 };
00078
00079
00080
00081
00082
00083
00084 #endif