From b1ed81ef3216b212295afa76c5d20a56fb647204 Mon Sep 17 00:00:00 2001 From: Chris Dyer Date: Mon, 13 Oct 2014 00:42:37 -0400 Subject: new kenlm --- klm/util/scoped.hh | 129 ++++++++++++++++++++++++++++------------------------- 1 file changed, 67 insertions(+), 62 deletions(-) (limited to 'klm/util/scoped.hh') diff --git a/klm/util/scoped.hh b/klm/util/scoped.hh index b642d064..60c36c36 100644 --- a/klm/util/scoped.hh +++ b/klm/util/scoped.hh @@ -1,9 +1,10 @@ -#ifndef UTIL_SCOPED__ -#define UTIL_SCOPED__ +#ifndef UTIL_SCOPED_H +#define UTIL_SCOPED_H /* Other scoped objects in the style of scoped_ptr. */ #include "util/exception.hh" #include +#include namespace util { @@ -16,89 +17,93 @@ class MallocException : public ErrnoException { void *MallocOrThrow(std::size_t requested); void *CallocOrThrow(std::size_t requested); -class scoped_malloc { +/* Unfortunately, defining the operator* for void * makes the compiler complain. + * So scoped is specialized to void. This includes the functionality common to + * both, namely everything except reference. + */ +template class scoped_base { public: - scoped_malloc() : p_(NULL) {} + explicit scoped_base(T *p = NULL) : p_(p) {} - scoped_malloc(void *p) : p_(p) {} + ~scoped_base() { Closer::Close(p_); } - ~scoped_malloc(); - - void reset(void *p = NULL) { - scoped_malloc other(p_); + void reset(T *p = NULL) { + scoped_base other(p_); p_ = p; } - void call_realloc(std::size_t to); - - void *get() { return p_; } - const void *get() const { return p_; } - - private: - void *p_; - - scoped_malloc(const scoped_malloc &); - scoped_malloc &operator=(const scoped_malloc &); -}; - -// Hat tip to boost. -template 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 *get() { return p_; } + const T *get() const { return p_; } - T &operator*() { return *c_; } - const T&operator*() const { return *c_; } + T *operator->() { return p_; } + const T *operator->() const { return p_; } - 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 other(c_); - c_ = to; + T *release() { + T *ret = p_; + p_ = NULL; + return ret; } - private: - T *c_; + protected: + T *p_; - scoped_array(const scoped_array &); - void operator=(const scoped_array &); + private: + scoped_base(const scoped_base &); + scoped_base &operator=(const scoped_base &); }; -template class scoped_ptr { +template class scoped : public scoped_base { public: - explicit scoped_ptr(T *content = NULL) : c_(content) {} + explicit scoped(T *p = NULL) : scoped_base(p) {} - ~scoped_ptr() { delete c_; } + T &operator*() { return *scoped_base::p_; } + const T&operator*() const { return *scoped_base::p_; } +}; - T *get() { return c_; } - const T* get() const { return c_; } +template class scoped : public scoped_base { + public: + explicit scoped(void *p = NULL) : scoped_base(p) {} +}; - T &operator*() { return *c_; } - const T&operator*() const { return *c_; } +/* Closer for c functions like std::free and cmph cleanup functions */ +template struct scoped_c_forward { + static void Close(T *p) { clean(p); } +}; +// Call a C function to delete stuff +template class scoped_c : public scoped > { + public: + explicit scoped_c(T *p = NULL) : scoped >(p) {} +}; - T *operator->() { return c_; } - const T*operator->() const { return c_; } +class scoped_malloc : public scoped_c { + public: + explicit scoped_malloc(void *p = NULL) : scoped_c(p) {} - T &operator[](std::size_t idx) { return c_[idx]; } - const T &operator[](std::size_t idx) const { return c_[idx]; } + void call_realloc(std::size_t to); +}; - void reset(T *to = NULL) { - scoped_ptr other(c_); - c_ = to; - } +/* scoped_array using delete[] */ +struct scoped_delete_array_forward { + template static void Close(T *p) { delete [] p; } +}; +// Hat tip to boost. +template class scoped_array : public scoped { + public: + explicit scoped_array(T *p = NULL) : scoped(p) {} - private: - T *c_; + T &operator[](std::size_t idx) { return scoped::p_[idx]; } + const T &operator[](std::size_t idx) const { return scoped::p_[idx]; } +}; - scoped_ptr(const scoped_ptr &); - void operator=(const scoped_ptr &); +/* scoped_ptr using delete. If only there were a template typedef. */ +struct scoped_delete_forward { + template static void Close(T *p) { delete p; } +}; +template class scoped_ptr : public scoped { + public: + explicit scoped_ptr(T *p = NULL) : scoped(p) {} }; } // namespace util -#endif // UTIL_SCOPED__ +#endif // UTIL_SCOPED_H -- cgit v1.2.3