Main Page | Class List | File List | Class Members | File Members

inotify-cxx.h

Go to the documentation of this file.
00001 
00003 
00026 #ifndef _INOTIFYCXX_H_
00027 #define _INOTIFYCXX_H_
00028 
00029 #include <string>
00030 #include <deque>
00031 #include <map>
00032 
00033 // Please ensure that the following headers take the right place.
00034 #include <sys/inotify.h>
00035 #include <sys/inotify-syscalls.h>
00036 
00038 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
00039 
00041 #define INOTIFY_BUFLEN (1024 * (INOTIFY_EVENT_SIZE + 16))
00042 
00043 
00044 // forward declaration
00045 class InotifyWatch;
00046 class Inotify;
00047 
00048 
00050 
00054 class InotifyEvent
00055 {
00056 public:
00058 
00061   InotifyEvent()
00062   {
00063     memset(&m_evt, 0, sizeof(m_evt));
00064     m_evt.wd = (int32_t) -1;
00065     m_pWatch = NULL;
00066   }
00067   
00069 
00076   InotifyEvent(const struct inotify_event* pEvt, InotifyWatch* pWatch)
00077   {
00078     if (pEvt != NULL) {
00079       memcpy(&m_evt, pEvt, sizeof(m_evt));
00080       if (pEvt->name != NULL)
00081         m_name = pEvt->name;
00082       m_pWatch = pWatch;
00083     }
00084     else {
00085       memset(&m_evt, 0, sizeof(m_evt));
00086       m_evt.wd = (int32_t) -1;
00087       m_pWatch = NULL;
00088     }
00089   }
00090   
00092   ~InotifyEvent() {}
00093   
00095 
00100   inline int32_t GetDescriptor() const
00101   {
00102     return (int32_t) m_evt.wd;
00103   }
00104   
00106 
00111   inline uint32_t GetMask() const
00112   {
00113     return (uint32_t) m_evt.mask;
00114   }
00115   
00117 
00122   inline static bool IsType(uint32_t uValue, uint32_t uType)
00123   {
00124     return ((uValue & uType) != 0) && ((~uValue & uType) == 0);
00125   }
00126   
00128 
00132   inline bool IsType(uint32_t uType) const
00133   {
00134     return IsType((uint32_t) m_evt.mask, uType);
00135   }
00136   
00138 
00141   inline uint32_t GetCookie() const
00142   {
00143     return (uint32_t) m_evt.cookie;
00144   }
00145   
00147 
00150   inline uint32_t GetLength() const
00151   {
00152     return (uint32_t) m_evt.len;
00153   }
00154   
00156 
00159   inline const std::string& GetName() const
00160   {
00161     return m_name;
00162   }
00163   
00165 
00168   inline void GetName(std::string& rName) const
00169   {
00170     rName = GetName();
00171   }
00172   
00174 
00177   inline InotifyWatch* GetWatch()
00178   {
00179     return m_pWatch;
00180   }
00181   
00183 
00188   inline void GetData(struct inotify_event* pEvt)
00189   {
00190     if (pEvt != NULL)
00191       memcpy(pEvt, &m_evt, sizeof(m_evt));
00192   }
00193   
00195 
00198   inline void GetData(struct inotify_event& rEvt)
00199   {
00200     memcpy(&rEvt, &m_evt, sizeof(m_evt));
00201   }
00202   
00204 
00208   static uint32_t GetMaskByName(const std::string& rName);
00209   
00211 
00215   static void DumpTypes(uint32_t uValue, std::string& rStr);
00216   
00218 
00221   void DumpTypes(std::string& rStr) const;
00222   
00223 private:
00224   struct inotify_event m_evt; 
00225   std::string m_name;         
00226   InotifyWatch* m_pWatch;     
00227 };
00228 
00229 
00230 
00232 class InotifyWatch
00233 {
00234 public:
00236 
00243   InotifyWatch(const std::string& rPath, int32_t uMask)
00244   {
00245     m_path = rPath;
00246     m_uMask = uMask;
00247     m_wd = (int32_t) -1;
00248   }
00249   
00251   ~InotifyWatch() {}
00252   
00254 
00257   inline int32_t GetDescriptor() const
00258   {
00259     return m_wd;
00260   }
00261   
00263 
00266   inline const std::string& GetPath() const
00267   {
00268     return m_path;
00269   }
00270   
00272 
00275   inline uint32_t GetMask() const
00276   {
00277     return (uint32_t) m_uMask;
00278   }
00279   
00281 
00284   inline Inotify* GetInotify()
00285   {
00286     return m_pInotify;
00287   }
00288   
00289 private:
00290   friend class Inotify;
00291 
00292   std::string m_path;   
00293   uint32_t m_uMask;     
00294   int32_t m_wd;         
00295   Inotify* m_pInotify;  
00296 };
00297 
00298 
00300 typedef std::map<int32_t, InotifyWatch*> IN_WATCH_MAP;
00301 
00302 
00304 class Inotify
00305 {
00306 public:
00308 
00312   Inotify();
00313   
00315 
00318   ~Inotify();
00319   
00321   void Close();
00322   
00324 
00327   inline bool IsReady() const
00328   {
00329     return m_fd != -1;
00330   }
00331   
00333 
00337   bool Add(InotifyWatch* pWatch);
00338   
00340 
00344   inline bool Add(InotifyWatch& rWatch)
00345   {
00346     return Add(&rWatch);
00347   }
00348   
00350 
00355   void Remove(InotifyWatch* pWatch);
00356   
00358 
00363   inline void Remove(InotifyWatch& rWatch)
00364   {
00365     Remove(&rWatch);
00366   }
00367   
00369   void RemoveAll();
00370   
00372 
00375   inline size_t GetWatchCount() const
00376   {
00377     return (size_t) m_watches.size();
00378   }
00379   
00381 
00387   bool WaitForEvents(bool fNoIntr = false);
00388   
00390 
00396   int GetEventCount();
00397   
00399 
00406   bool GetEvent(InotifyEvent* pEvt);
00407   
00409 
00415   bool GetEvent(InotifyEvent& rEvt)
00416   {
00417     return GetEvent(&rEvt);
00418   }
00419   
00421 
00428   bool PeekEvent(InotifyEvent* pEvt);
00429   
00431 
00437   bool PeekEvent(InotifyEvent& rEvt)
00438   {
00439     return PeekEvent(&rEvt);
00440   }
00441   
00443 
00449   InotifyWatch* FindWatch(int iDescriptor);  
00450 
00451 private: 
00452   int m_fd;                             
00453   IN_WATCH_MAP m_watches;               
00454   unsigned char m_buf[INOTIFY_BUFLEN];  
00455   std::deque<InotifyEvent> m_events;    
00456 };
00457 
00458 
00459 #endif //_INOTIFYCXX_H_

Generated on Fri Sep 15 12:07:10 2006 for inotify-cxx by  doxygen 1.4.4