blocxx
File.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005, Quest Software, Inc. All rights reserved.
3 * Copyright (C) 2006, Novell, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of
14 * Quest Software, Inc.,
15 * nor Novell, Inc.,
16 * nor the names of its contributors or employees may be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *******************************************************************************/
32 
33 
39 #ifndef BLOCXX_FILE_HPP_INCLUDE_GUARD_
40 #define BLOCXX_FILE_HPP_INCLUDE_GUARD_
41 #include "blocxx/BLOCXX_config.h"
42 #include "blocxx/Types.hpp"
43 #include "blocxx/FileSystem.hpp"
44 #include "blocxx/SafeBool.hpp"
45 #include <algorithm> // for std::swap
46 
47 namespace BLOCXX_NAMESPACE
48 {
49 
54 class BLOCXX_COMMON_API File
55 {
56 public:
57  enum ELockType
58  {
70  E_WRITE_LOCK
71  };
72 
77  {
78  }
83  File(const File& x);
87  ~File();
93  File& operator= (const File& x)
94  {
95  File(x).swap(*this);
96  return *this;
97  }
98  void swap(File& x)
99  {
100  std::swap(m_hdl, x.m_hdl);
101  }
113  size_t read(void* bfr, size_t numberOfBytes, Int64 offset=-1L) const
114  {
115  return FileSystem::read(m_hdl, bfr, numberOfBytes, offset);
116  }
127  size_t write(const void* bfr, size_t numberOfBytes, Int64 offset=-1L)
128  {
129  return FileSystem::write(m_hdl, bfr, numberOfBytes, offset);
130  }
141  Int64 seek(Int64 offset, int whence) const
142  {
143  return FileSystem::seek(m_hdl, offset, whence);
144  }
149  Int64 tell() const
150  {
151  return FileSystem::tell(m_hdl);
152  }
156  void rewind() const
157  {
158  FileSystem::rewind(m_hdl);
159  }
163  UInt64 size() const
164  {
165  return FileSystem::fileSize(m_hdl);
166  }
171  int close()
172  {
173  if (m_hdl != BLOCXX_INVALID_FILEHANDLE)
174  {
175  int rv = FileSystem::close(m_hdl);
177  return rv;
178  }
179  return 0;
180  }
185  int flush()
186  {
187  return FileSystem::flush(m_hdl);
188  }
204  int getLock(ELockType type = E_WRITE_LOCK);
220  int tryLock(ELockType type = E_WRITE_LOCK);
227  int unlock();
228 
233 
234 
239  bool operator==(const File& rhs)
240  {
241  return m_hdl == rhs.m_hdl;
242  }
243 
244  File(FileHandle hdl) : m_hdl(hdl)
245  {
246  }
247 
248 private:
249 
251 };
252 
253 } // end namespace BLOCXX_NAMESPACE
254 
255 #endif
256