2010-09-15 01:33:11 +04:00
|
|
|
#ifndef UTIL_SCOPED__
|
|
|
|
#define UTIL_SCOPED__
|
2012-10-30 23:33:22 +04:00
|
|
|
/* Other scoped objects in the style of scoped_ptr. */
|
2010-09-10 04:36:07 +04:00
|
|
|
|
2011-09-21 20:06:48 +04:00
|
|
|
#include "util/exception.hh"
|
2010-09-10 04:36:07 +04:00
|
|
|
#include <cstddef>
|
2011-09-21 20:06:48 +04:00
|
|
|
#include <cstdlib>
|
2010-09-10 04:36:07 +04:00
|
|
|
|
|
|
|
namespace util {
|
|
|
|
|
2011-09-21 20:06:48 +04:00
|
|
|
class scoped_malloc {
|
2010-09-10 04:36:07 +04:00
|
|
|
public:
|
2011-09-21 20:06:48 +04:00
|
|
|
scoped_malloc() : p_(NULL) {}
|
2010-09-10 04:36:07 +04:00
|
|
|
|
2011-09-21 20:06:48 +04:00
|
|
|
scoped_malloc(void *p) : p_(p) {}
|
2010-09-10 04:36:07 +04:00
|
|
|
|
2011-09-21 20:06:48 +04:00
|
|
|
~scoped_malloc() { std::free(p_); }
|
2010-09-10 04:36:07 +04:00
|
|
|
|
2011-09-21 20:06:48 +04:00
|
|
|
void reset(void *p = NULL) {
|
|
|
|
scoped_malloc other(p_);
|
|
|
|
p_ = p;
|
2010-09-10 04:36:07 +04:00
|
|
|
}
|
|
|
|
|
2011-09-21 20:06:48 +04:00
|
|
|
void call_realloc(std::size_t to) {
|
|
|
|
void *ret;
|
2013-01-17 15:58:58 +04:00
|
|
|
UTIL_THROW_IF(!(ret = std::realloc(p_, to)) && to, ErrnoException, "realloc to " << to << " bytes failed.");
|
2011-09-21 20:06:48 +04:00
|
|
|
p_ = ret;
|
2010-09-15 01:33:11 +04:00
|
|
|
}
|
|
|
|
|
2011-09-21 20:06:48 +04:00
|
|
|
void *get() { return p_; }
|
|
|
|
const void *get() const { return p_; }
|
2010-10-27 08:04:23 +04:00
|
|
|
|
|
|
|
private:
|
2011-09-21 20:06:48 +04:00
|
|
|
void *p_;
|
|
|
|
|
|
|
|
scoped_malloc(const scoped_malloc &);
|
|
|
|
scoped_malloc &operator=(const scoped_malloc &);
|
2010-10-27 08:04:23 +04:00
|
|
|
};
|
|
|
|
|
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[](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_;
|
2012-10-30 23:33:22 +04:00
|
|
|
|
|
|
|
scoped_array(const scoped_array &);
|
|
|
|
void operator=(const scoped_array &);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T> class scoped_ptr {
|
|
|
|
public:
|
|
|
|
explicit scoped_ptr(T *content = NULL) : c_(content) {}
|
|
|
|
|
|
|
|
~scoped_ptr() { 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_ptr<T> other(c_);
|
|
|
|
c_ = to;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T *c_;
|
|
|
|
|
|
|
|
scoped_ptr(const scoped_ptr &);
|
|
|
|
void operator=(const scoped_ptr &);
|
2011-01-25 22:11:48 +03:00
|
|
|
};
|
|
|
|
|
2010-09-10 04:36:07 +04:00
|
|
|
} // namespace util
|
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
#endif // UTIL_SCOPED__
|