aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorMax Horn2004-06-28 22:34:22 +0000
committerMax Horn2004-06-28 22:34:22 +0000
commita5df4fba77214a93442a0a9e9607cd0a523f24a8 (patch)
tree292a43b385dfef15874f6a79988a46a7961fd6ac /common
parent875d0a580e2294c4f44fa1ecff06a5894b011f85 (diff)
downloadscummvm-rg350-a5df4fba77214a93442a0a9e9607cd0a523f24a8.tar.gz
scummvm-rg350-a5df4fba77214a93442a0a9e9607cd0a523f24a8.tar.bz2
scummvm-rg350-a5df4fba77214a93442a0a9e9607cd0a523f24a8.zip
Added simple ref-counting to the File class
svn-id: r14106
Diffstat (limited to 'common')
-rw-r--r--common/file.cpp31
-rw-r--r--common/file.h18
2 files changed, 42 insertions, 7 deletions
diff --git a/common/file.cpp b/common/file.cpp
index 4043143fd4..411e1d64a8 100644
--- a/common/file.cpp
+++ b/common/file.cpp
@@ -104,16 +104,35 @@ void File::resetDefaultDirectories() {
_defaultDirectories.clear();
}
-File::File() {
- _handle = NULL;
- _ioFailed = false;
- _name = 0;
+File::File()
+ : _handle(0), _ioFailed(false), _refcount(1), _name(0) {
}
+//#define DEBUG_FILE_REFCOUNT
+
File::~File() {
+#ifdef DEBUG_FILE_REFCOUNT
+ warning("File::~File on file '%s'", _name);
+#endif
close();
delete [] _name;
}
+void File::incRef() {
+#ifdef DEBUG_FILE_REFCOUNT
+ warning("File::incRef on file '%s'", _name);
+#endif
+ _refcount++;
+}
+
+void File::decRef() {
+#ifdef DEBUG_FILE_REFCOUNT
+ warning("File::decRef on file '%s'", _name);
+#endif
+ if (--_refcount == 0) {
+ delete this;
+ }
+}
+
bool File::open(const char *filename, AccessMode mode, const char *directory) {
if (_handle) {
@@ -156,6 +175,10 @@ bool File::open(const char *filename, AccessMode mode, const char *directory) {
_name = new char[len+1];
memcpy(_name, filename, len+1);
+#ifdef DEBUG_FILE_REFCOUNT
+ warning("File::open on file '%s'", _name);
+#endif
+
return true;
}
diff --git a/common/file.h b/common/file.h
index d2bc217dbb..ad2c5d5980 100644
--- a/common/file.h
+++ b/common/file.h
@@ -29,10 +29,18 @@
class File : public Common::ReadStream, public Common::WriteStream {
private:
-
- FILE * _handle;
+ /** POSIX file handle to the actual file; 0 if no file is open. */
+ FILE *_handle;
+
+ /** Status flag which tells about recent I/O failures. */
bool _ioFailed;
- char *_name; // For debugging
+
+ /** Simple ref-counter for File objects. */
+ int32 _refcount;
+
+ /** The name of this file, for debugging. */
+ char *_name;
+
static FILE *fopenNoCase(const char *filename, const char *directory, const char *mode);
@@ -49,6 +57,10 @@ public:
File();
virtual ~File();
+
+ void incRef();
+ void decRef();
+
bool open(const char *filename, AccessMode mode = kFileReadMode, const char *directory = NULL);
void close();
bool isOpen() const;