diff options
Diffstat (limited to 'klm/util/scoped.hh')
-rw-r--r-- | klm/util/scoped.hh | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/klm/util/scoped.hh b/klm/util/scoped.hh index 52864481..d36a7df3 100644 --- a/klm/util/scoped.hh +++ b/klm/util/scoped.hh @@ -20,7 +20,9 @@ template <class T, class R, R (*Free)(T*)> class scoped_thing { } T &operator*() { return *c_; } + const T&operator*() const { return *c_; } T &operator->() { return *c_; } + const T&operator->() const { return *c_; } T *get() { return c_; } const T *get() const { return c_; } @@ -80,6 +82,34 @@ class scoped_FILE { std::FILE *file_; }; +// 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_; +}; + } // namespace util #endif // UTIL_SCOPED__ |