| Revision 2127,
2.0 KB
checked in by chris, 4 years ago
(diff) |
|
Undo mangling by tailor
|
-
Property svn:eol-style set to
native
|
| Line | |
|---|
| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: Guards.h |
|---|
| 5 | // Purpose: Classes which ensure things are closed/deleted properly when |
|---|
| 6 | // going out of scope. Easy exception proof code, etc |
|---|
| 7 | // Created: 2003/07/12 |
|---|
| 8 | // |
|---|
| 9 | // -------------------------------------------------------------------------- |
|---|
| 10 | |
|---|
| 11 | #ifndef GUARDS__H |
|---|
| 12 | #define GUARDS__H |
|---|
| 13 | |
|---|
| 14 | #ifdef HAVE_UNISTD_H |
|---|
| 15 | #include <unistd.h> |
|---|
| 16 | #endif |
|---|
| 17 | |
|---|
| 18 | #include <errno.h> |
|---|
| 19 | #include <fcntl.h> |
|---|
| 20 | #include <stdlib.h> |
|---|
| 21 | #include <string.h> |
|---|
| 22 | #include <sys/stat.h> |
|---|
| 23 | |
|---|
| 24 | #include <new> |
|---|
| 25 | |
|---|
| 26 | #include "CommonException.h" |
|---|
| 27 | #include "Logging.h" |
|---|
| 28 | |
|---|
| 29 | #include "MemLeakFindOn.h" |
|---|
| 30 | |
|---|
| 31 | template <int flags = O_RDONLY | O_BINARY, int mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)> |
|---|
| 32 | class FileHandleGuard |
|---|
| 33 | { |
|---|
| 34 | public: |
|---|
| 35 | FileHandleGuard(const std::string& rFilename) |
|---|
| 36 | : mOSFileHandle(::open(rFilename.c_str(), flags, mode)) |
|---|
| 37 | { |
|---|
| 38 | if(mOSFileHandle < 0) |
|---|
| 39 | { |
|---|
| 40 | BOX_LOG_SYS_ERROR("FileHandleGuard: failed to open " |
|---|
| 41 | "file '" << rFilename << "'"); |
|---|
| 42 | THROW_EXCEPTION(CommonException, OSFileOpenError) |
|---|
| 43 | } |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | ~FileHandleGuard() |
|---|
| 47 | { |
|---|
| 48 | if(mOSFileHandle >= 0) |
|---|
| 49 | { |
|---|
| 50 | Close(); |
|---|
| 51 | } |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | void Close() |
|---|
| 55 | { |
|---|
| 56 | if(mOSFileHandle < 0) |
|---|
| 57 | { |
|---|
| 58 | THROW_EXCEPTION(CommonException, FileAlreadyClosed) |
|---|
| 59 | } |
|---|
| 60 | if(::close(mOSFileHandle) != 0) |
|---|
| 61 | { |
|---|
| 62 | THROW_EXCEPTION(CommonException, OSFileCloseError) |
|---|
| 63 | } |
|---|
| 64 | mOSFileHandle = -1; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | operator int() const |
|---|
| 68 | { |
|---|
| 69 | return mOSFileHandle; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | private: |
|---|
| 73 | int mOSFileHandle; |
|---|
| 74 | }; |
|---|
| 75 | |
|---|
| 76 | template<typename type> |
|---|
| 77 | class MemoryBlockGuard |
|---|
| 78 | { |
|---|
| 79 | public: |
|---|
| 80 | MemoryBlockGuard(int BlockSize) |
|---|
| 81 | : mpBlock(::malloc(BlockSize)) |
|---|
| 82 | { |
|---|
| 83 | if(mpBlock == 0) |
|---|
| 84 | { |
|---|
| 85 | throw std::bad_alloc(); |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | ~MemoryBlockGuard() |
|---|
| 90 | { |
|---|
| 91 | free(mpBlock); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | operator type() const |
|---|
| 95 | { |
|---|
| 96 | return (type)mpBlock; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | type GetPtr() const |
|---|
| 100 | { |
|---|
| 101 | return (type)mpBlock; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | void Resize(int NewSize) |
|---|
| 105 | { |
|---|
| 106 | void *ptrn = ::realloc(mpBlock, NewSize); |
|---|
| 107 | if(ptrn == 0) |
|---|
| 108 | { |
|---|
| 109 | throw std::bad_alloc(); |
|---|
| 110 | } |
|---|
| 111 | mpBlock = ptrn; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | private: |
|---|
| 115 | void *mpBlock; |
|---|
| 116 | }; |
|---|
| 117 | |
|---|
| 118 | #include "MemLeakFindOff.h" |
|---|
| 119 | |
|---|
| 120 | #endif // GUARDS__H |
|---|
| 121 | |
|---|
Note: See
TracBrowser
for help on using the repository browser.