00001 #ifndef COMMONS_CLOSING_H
00002 #define COMMONS_CLOSING_H
00003
00004 #include <boost/utility.hpp>
00005 #include <commons/check.h>
00006 #include <commons/utility.h>
00007
00008 namespace commons
00009 {
00010
00011 using namespace boost;
00012
00013
00014
00015 struct fd_closer { static void apply(int fd) { check0x(close(fd)); } };
00016
00017 template<typename T, typename Closer>
00018 class closing
00019 {
00020 NONCOPYABLE(closing)
00021 public:
00022 closing(T x) : x(x), scoped(true) {}
00023 ~closing() { if (scoped) Closer::apply(x); }
00024 T get() { return x; }
00025 operator T() { return x; }
00026 T release() { scoped = false; return x; }
00027 private:
00028 T x;
00029 bool scoped;
00030 };
00031
00032 typedef closing<int, fd_closer> closingfd;
00033
00034 }
00035
00036 #endif