Improve speed of category tab title updates
[amule.git] / src / FileAutoClose.h
blob19744ddd76d6b3b5533d8a49f2ae76f49377f2c5
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2008-2009 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2008-2009 Stu Redman ( sturedman@amule.org )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #ifndef FILEAUTOCLOSE_H
27 #define FILEAUTOCLOSE_H
29 #include "CFile.h" // Needed for CFile
31 /**
32 * This class encapsulates the CFile class.
34 * It allows to close the used file handle and reopen
35 * it on usage to minimize the number of used file handles.
38 class CFileAutoClose
40 public:
42 /**
43 * Creates a closed file.
45 CFileAutoClose();
47 /**
48 * Constructor, calls Open on the specified file.
50 * To check if the file was successfully opened, a
51 * call to IsOpened() is required.
53 CFileAutoClose(const CPath& path, CFile::OpenMode mode = CFile::read);
55 /**
56 * Request auto closing of the file handle.
58 * @param now true: close immediately false: close when timeout has expired
59 * @return True if the file has been (or has already been) autoclosed.
61 bool Release(bool now = false);
63 /**
64 * Opens a file.
66 * @param path The full or relative path to the file.
67 * @param mode The opening mode (see CFile).
68 * @return True if the file was opened, false otherwise.
70 bool Open(const CPath& path, CFile::OpenMode mode = CFile::read);
72 /**
73 * Calling Create is equivilant of calling open with OpenMode 'write'.
75 * @param overwrite Specifies if the target file should be overwritten,
76 * in case that it already exists.
78 * @see CFile::Open
80 bool Create(const CPath& path, bool overwrite = false);
82 /**
83 * Closes the file.
85 * Note that calling Close on an closed file
86 * is an illegal operation.
88 bool Close();
90 /**
91 * @see CSafeFileIO::GetLength
93 * Note that calling GetLength on a closed file
94 * is an illegal operation.
96 uint64 GetLength() const;
98 /**
99 * Resizes the file to the specified length.
102 bool SetLength(uint64 newLength);
105 * Returns the path of the currently opened file.
108 const CPath& GetFilePath() const;
111 * Returns true if the file is opened, false otherwise.
113 bool IsOpened() const;
116 * Reads 'count' bytes into 'buffer'.
118 * @param buffer The target buffer.
119 * @param offset The seek address in the file.
120 * @param count The number of bytes to read.
122 * See CFileDataIO::Read
124 void ReadAt(void* buffer, uint64 offset, size_t count);
127 * Write 'count' bytes from 'buffer' into the file.
129 * @param buffer The source-data buffer.
130 * @param offset The seek address in the file.
131 * @param count The number of bytes to write.
133 * See CFileDataIO::Write
135 void WriteAt(const void* buffer, uint64 offset, size_t count);
138 * Returns true when the file-position is past or at the end of the file.
140 bool Eof();
143 * Returns the file descriptior assosiated with the file.
145 * This breaks the purpose of this class of course.
146 * Therefore the AutoClose mechanism is disabled when fd() is called.
147 * It's required for FileArea's mmap stuff.
148 * Currently FileArea objects are shortlived enough for this not being
149 * a problem anyway, but that might change in the future.
151 int fd();
154 * Reenables AutoClose disabled by fd() before.
156 void Unlock();
158 private:
159 //! A CFileAutoClose is neither copyable nor assignable.
160 //@{
161 CFileAutoClose(const CFileAutoClose&);
162 CFileAutoClose& operator=(const CFileAutoClose&);
163 //@}
166 * Check if file was autoclosed, and reopen if needed.
168 void Reopen();
170 //! The wrapped CFile.
171 CFile m_file;
173 //! The mode used to open it.
174 CFile::OpenMode m_mode;
176 //! Is it temporarily closed?
177 bool m_autoClosed;
179 //! Autoclosing is disabled if != 0
180 uint16 m_locked;
182 //! Size before it was closed.
183 uint64 m_size;
185 //! Last access time (s)
186 uint32 m_lastAccess;
190 #endif // FILEAUTOCLOSE_H
191 // File_checked_for_headers