2010-09-15 01:33:11 +04:00
|
|
|
#ifndef UTIL_SCOPED__
|
|
|
|
#define UTIL_SCOPED__
|
2010-09-10 04:36:07 +04:00
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
/* Other scoped objects in the style of scoped_ptr. */
|
2010-09-10 04:36:07 +04:00
|
|
|
|
|
|
|
#include <cstddef>
|
2010-10-27 08:04:23 +04:00
|
|
|
#include <cstdio>
|
2010-09-10 04:36:07 +04:00
|
|
|
|
|
|
|
namespace util {
|
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
template <class T, class R, R (*Free)(T*)> class scoped_thing {
|
2010-09-10 04:36:07 +04:00
|
|
|
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_; }
|
2011-01-25 22:11:48 +03:00
|
|
|
const T&operator*() const { return *c_; }
|
2010-09-10 04:36:07 +04:00
|
|
|
T &operator->() { return *c_; }
|
2011-01-25 22:11:48 +03:00
|
|
|
const T&operator->() const { return *c_; }
|
2010-09-10 04:36:07 +04:00
|
|
|
|
|
|
|
T *get() { return c_; }
|
|
|
|
const T *get() const { return c_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
T *c_;
|
2010-09-15 01:33:11 +04:00
|
|
|
|
|
|
|
scoped_thing(const scoped_thing &);
|
|
|
|
scoped_thing &operator=(const scoped_thing &);
|
2010-09-10 04:36:07 +04:00
|
|
|
};
|
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
class scoped_fd {
|
2010-09-10 04:36:07 +04:00
|
|
|
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_; }
|
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
int release() {
|
|
|
|
int ret = fd_;
|
|
|
|
fd_ = -1;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-09-10 04:36:07 +04:00
|
|
|
private:
|
|
|
|
int fd_;
|
2010-09-15 01:33:11 +04:00
|
|
|
|
|
|
|
scoped_fd(const scoped_fd &);
|
|
|
|
scoped_fd &operator=(const scoped_fd &);
|
2010-09-10 04:36:07 +04:00
|
|
|
};
|
|
|
|
|
2010-10-27 08:04:23 +04:00
|
|
|
class scoped_FILE {
|
|
|
|
public:
|
|
|
|
explicit scoped_FILE(std::FILE *file = NULL) : file_(file) {}
|
|
|
|
|
|
|
|
~scoped_FILE();
|
|
|
|
|
|
|
|
std::FILE *get() { return file_; }
|
|
|
|
const std::FILE *get() const { return file_; }
|
|
|
|
|
|
|
|
void reset(std::FILE *to = NULL) {
|
|
|
|
scoped_FILE other(file_);
|
|
|
|
file_ = to;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::FILE *file_;
|
|
|
|
};
|
|
|
|
|
2011-01-25 22:11:48 +03:00
|
|
|
// Hat tip to boost.
|
|
|
|
template <class T> class scoped_array {
|
|
|
|
public:
|
|
|
|
explicit scoped_array(T *content = NULL) : c_(content) {}
|
|
|
|
|
|
|
|
~scoped_array() { delete [] c_; }
|
|
|
|
|
|
|
|
T *get() { return c_; }
|
|
|
|
const T* get() const { return c_; }
|
|
|
|
|
|
|
|
T &operator*() { return *c_; }
|
|
|
|
const T&operator*() const { return *c_; }
|
|
|
|
|
|
|
|
T &operator->() { return *c_; }
|
|
|
|
const T&operator->() const { return *c_; }
|
|
|
|
|
|
|
|
T &operator[](std::size_t idx) { return c_[idx]; }
|
|
|
|
const T &operator[](std::size_t idx) const { return c_[idx]; }
|
|
|
|
|
|
|
|
void reset(T *to = NULL) {
|
|
|
|
scoped_array<T> other(c_);
|
|
|
|
c_ = to;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T *c_;
|
|
|
|
};
|
|
|
|
|
2010-09-10 04:36:07 +04:00
|
|
|
} // namespace util
|
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
#endif // UTIL_SCOPED__
|