diff options
author | Hieu Hoang <hieuhoang@gmail.com> | 2012-11-12 23:56:18 +0400 |
---|---|---|
committer | Hieu Hoang <hieuhoang@gmail.com> | 2012-11-12 23:56:18 +0400 |
commit | 5e3ef23cef6101d2c098eb3445f562e8f595655b (patch) | |
tree | b8c332b6fa82bae84ea4910967a10ba1b08a7107 /moses/FilePtr.h | |
parent | 8c785cff2b1be3cccd76ea9026f71b649762dfc3 (diff) |
move moses/src/* to moses/
Diffstat (limited to 'moses/FilePtr.h')
-rw-r--r-- | moses/FilePtr.h | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/moses/FilePtr.h b/moses/FilePtr.h new file mode 100644 index 000000000..b9c835e59 --- /dev/null +++ b/moses/FilePtr.h @@ -0,0 +1,89 @@ +// $Id$ + +/* ---------------------------------------------------------------- */ +/* Copyright 2005 (c) by RWTH Aachen - Lehrstuhl fuer Informatik VI */ +/* Richard Zens */ +/* ---------------------------------------------------------------- */ + +#ifndef moses_FilePtr_h +#define moses_FilePtr_h + +#include "File.h" + +namespace Moses +{ + +/** smart pointer for on-demand loading from file + * requirement: T has a constructor T(FILE*) + */ +template<typename T> class FilePtr +{ +public: + typedef T* Ptr; +private: + FILE* f; + OFF_T pos; + mutable Ptr t; +public: + FilePtr(FILE* f_=0,OFF_T p=0) : f(f_),pos(p),t(0) {} + ~FilePtr() {} + + void set(FILE* f_,OFF_T p) { + f=f_; + pos=p; + } + void free() { + delete t; + t=0; + } + + T& operator* () { + load(); + return *t; + } + Ptr operator->() { + load(); + return t; + } + operator Ptr () { + load(); + return t; + } + + const T& operator* () const { + load(); + return *t; + } + Ptr operator->() const { + load(); + return t; + } + operator Ptr () const { + load(); + return t; + } + + // direct access to pointer, use with care! + Ptr getPtr() { + return t; + } + Ptr getPtr() const { + return t; + } + + operator bool() const { + return (f && pos!=InvalidOffT); + } + + void load() const { + if(t) return; + if(f && pos!=InvalidOffT) { + fSeek(f,pos); + t=new T(f); + } + } +}; + +} + +#endif |