00001 // Package : omniIFR 00002 // Persist.h Created : 2004/09/05 00003 // Author : Alex Tingle 00004 // 00005 // Copyright (C) 2004 Alex Tingle. 00006 // 00007 // This file is part of the omniIFR application. 00008 // 00009 // omniIFR is free software; you can redistribute it and/or 00010 // modify it under the terms of the GNU Lesser General Public 00011 // License as published by the Free Software Foundation; either 00012 // version 2.1 of the License, or (at your option) any later version. 00013 // 00014 // omniIFR is distributed in the hope that it will be useful, 00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 // Lesser General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU Lesser General Public 00020 // License along with this library; if not, write to the Free Software 00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 // 00023 00024 #ifndef OMNIIFR__PERSIST_H 00025 #define OMNIIFR__PERSIST_H 1 00026 00027 #ifdef HAVE_CONFIG_H 00028 # include "config.h" 00029 #endif 00030 00031 #ifdef HAVE_IOSTREAM 00032 # include <fstream> 00033 #else 00034 # include <fstream.h> 00035 #endif 00036 00037 #ifdef HAVE_STD_IOSTREAM 00038 using namespace std; 00039 #endif 00040 00041 #include <omnithread.h> 00042 00043 namespace Omniifr { 00044 00045 class Repository_impl; 00046 00048 class Persist 00049 { 00050 public: 00051 Persist(int checkpointPeriodSec); 00052 ~Persist(); 00053 00055 void setdir(const char* dir) { _dir=dir; } 00056 00062 void startup(); 00063 00068 void checkpoint(); 00069 00071 ostream& outstream(); 00072 00075 class WriteLock 00076 { 00077 public: 00078 inline WriteLock(Persist& p); 00079 inline ~WriteLock(); 00080 ostream& os; 00081 private: 00082 Persist& persist; 00083 omni_mutex_lock lk; 00084 WriteLock(const WriteLock&); 00085 }; 00086 friend class WriteLock; 00087 00088 private: 00089 Repository_impl& _ifr; 00090 omni_mutex _lock; 00091 ofstream* _outstream; 00092 bool _checkpointNeeded; 00093 string _dir; 00094 }; 00095 00096 00097 // 00098 // Inline method implementations 00099 // 00100 00101 00102 inline Persist::WriteLock::WriteLock(Persist& p): 00103 os(p.outstream()), 00104 persist(p), 00105 lk(p._lock) 00106 {} 00107 00108 inline Persist::WriteLock::~WriteLock() 00109 { 00110 os.flush(); 00111 persist._checkpointNeeded=true; 00112 } 00113 00114 } // end namespace Omniifr 00115 00116 #endif // OMNIIFR__PERSIST_H