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 MotionHook* getMotionSink() { return NULL; }
00023 virtual void getSensorSources(std::map<std::string,DataSource*>& sources) { sources.clear(); }
00024 virtual void getImageSources(std::map<std::string,DataSource*>& sources) { sources.clear(); }
00025
00026 typedef InstanceTracker<DeviceDriver,std::string,Factory1Arg<DeviceDriver,std::string> > registry_t;
00027 static registry_t& getRegistry() { static registry_t registry; return registry; }
00028
00029
00030 class SourceListener {
00031 public:
00032 virtual ~SourceListener() {};
00033 virtual void dataSourcesUpdated()=0;
00034 };
00035
00036
00037 virtual void addSourceListener(SourceListener* l) { if(l!=NULL) sourceListeners.insert(l); }
00038
00039 virtual void removeSourceListener(SourceListener* l) { sourceListeners.erase(l); }
00040
00041 protected:
00042
00043 DeviceDriver(const std::string& , const std::string& instancename)
00044 : plist::Dictionary(), instanceName(instancename), sourceListeners()
00045 {
00046 setLoadSavePolicy(FIXED,SYNC);
00047 }
00048
00049
00050
00051
00052 virtual void registerInstance() {
00053 if(DeviceDriver * inst=getRegistry().getInstance(instanceName)) {
00054 if(inst==this)
00055 return;
00056 std::cerr << "Warning: registration of DeviceDriver " << getClassName() << " named " << instanceName << " @ " << this
00057 << " blocked by previous " << inst->getClassName() << " instance of same name @ " << inst << std::endl;
00058 }
00059 if(!getRegistry().registerInstance(getClassName(),instanceName,this))
00060 std::cerr << "Error: failed to register " << getClassName() << " named " << instanceName << " @ " << this;
00061
00062 }
00063
00064
00065 virtual void fireDataSourcesUpdated() {
00066 std::set<SourceListener*> notify=sourceListeners;
00067 for(std::set<SourceListener*>::const_iterator it=notify.begin(); it!=notify.end(); ++it) {
00068 if(sourceListeners.find(*it)!=sourceListeners.end())
00069 (*it)->dataSourcesUpdated();
00070 }
00071 }
00072
00073 const std::string instanceName;
00074 std::set<SourceListener*> sourceListeners;
00075 };
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 #endif