about summary refs log tree commit diff
path: root/src/libnix/util.hh
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2003-10-22T10·48+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2003-10-22T10·48+0000
commit4a8948b7a60e751dd809f279f1baa434ea09a4d3 (patch)
tree3c8475fe2280f2e48654f287c46edc580bf4382c /src/libnix/util.hh
parentc62433751d5fce8c25a802989c50fee993f39c2d (diff)
* Some wrapper classes to ensure that file descriptors / directory
  handles are closed when they go out of scope.

Diffstat (limited to 'src/libnix/util.hh')
-rw-r--r--src/libnix/util.hh37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/libnix/util.hh b/src/libnix/util.hh
index 016289176b..02a9b7fcb8 100644
--- a/src/libnix/util.hh
+++ b/src/libnix/util.hh
@@ -6,6 +6,8 @@
 #include <set>
 #include <sstream>
 
+#include <sys/types.h>
+#include <dirent.h>
 #include <unistd.h>
 
 #include <boost/format.hpp>
@@ -113,4 +115,39 @@ void readFull(int fd, unsigned char * buf, size_t count);
 void writeFull(int fd, const unsigned char * buf, size_t count);
 
 
+/* Automatic cleanup of resources. */
+
+class AutoDelete
+{
+    string path;
+    bool del;
+public:
+    AutoDelete(const string & p);
+    ~AutoDelete();
+    void cancel();
+};
+
+class AutoCloseFD
+{
+    int fd;
+public:
+    AutoCloseFD();
+    AutoCloseFD(int fd);
+    ~AutoCloseFD();
+    void operator =(int fd);
+    operator int();
+};
+
+class AutoCloseDir
+{
+    DIR * dir;
+public:
+    AutoCloseDir();
+    AutoCloseDir(DIR * dir);
+    ~AutoCloseDir();
+    void operator =(DIR * dir);
+    operator DIR *();
+};
+
+
 #endif /* !__UTIL_H */