diff options
author | redpony <redpony@ec762483-ff6d-05da-a07a-a48fb63a330f> | 2010-10-18 23:24:01 +0000 |
---|---|---|
committer | redpony <redpony@ec762483-ff6d-05da-a07a-a48fb63a330f> | 2010-10-18 23:24:01 +0000 |
commit | de379496ee411993dff94e52f393f6e19437a204 (patch) | |
tree | a3fdb3b299100384e0a82dd2bc424fd52177d411 /klm/util/scoped.hh | |
parent | 08ff0e0332b562dd9c1f36fce24439db81287c68 (diff) |
kenneth's LM preliminary integration
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@681 ec762483-ff6d-05da-a07a-a48fb63a330f
Diffstat (limited to 'klm/util/scoped.hh')
-rw-r--r-- | klm/util/scoped.hh | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/klm/util/scoped.hh b/klm/util/scoped.hh new file mode 100644 index 00000000..ef62a74f --- /dev/null +++ b/klm/util/scoped.hh @@ -0,0 +1,66 @@ +#ifndef UTIL_SCOPED__ +#define UTIL_SCOPED__ + +/* Other scoped objects in the style of scoped_ptr. */ + +#include <cstddef> + +namespace util { + +template <class T, class R, R (*Free)(T*)> class scoped_thing { + public: + explicit scoped_thing(T *c = static_cast<T*>(0)) : c_(c) {} + + ~scoped_thing() { if (c_) Free(c_); } + + void reset(T *c) { + if (c_) Free(c_); + c_ = c; + } + + T &operator*() { return *c_; } + T &operator->() { return *c_; } + + T *get() { return c_; } + const T *get() const { return c_; } + + private: + T *c_; + + scoped_thing(const scoped_thing &); + scoped_thing &operator=(const scoped_thing &); +}; + +class scoped_fd { + public: + scoped_fd() : fd_(-1) {} + + explicit scoped_fd(int fd) : fd_(fd) {} + + ~scoped_fd(); + + void reset(int to) { + scoped_fd other(fd_); + fd_ = to; + } + + int get() const { return fd_; } + + int operator*() const { return fd_; } + + int release() { + int ret = fd_; + fd_ = -1; + return ret; + } + + private: + int fd_; + + scoped_fd(const scoped_fd &); + scoped_fd &operator=(const scoped_fd &); +}; + +} // namespace util + +#endif // UTIL_SCOPED__ |