00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "config.h"
00020
00021 #include "EDoc/ManagedFile.h"
00022 #include "EDoc/ManagedFileCleanupIFace.h"
00023
00024 #include "EDoc/utils.h"
00025 #include "EDoc/Logger.h"
00026
00027 namespace EDoc
00028 {
00029
00030 ManagedFile::ManagedFile(std::list<std::string> identifier_in) :
00031 cleanup(NULL),
00032 identifier(identifier_in)
00033 {
00034 IncRef();
00035 }
00036
00037 ManagedFile::ManagedFile(std::string identifier_in) :
00038 cleanup(NULL)
00039 {
00040 identifier.push_back(identifier_in);
00041 IncRef();
00042 }
00043
00044 ManagedFile::ManagedFile(std::list<std::string> identifier_in,
00045 std::string work_file_name_in,
00046 ManagedFileCleanupIFace* cleanup_in) :
00047
00048 cleanup(cleanup_in),
00049 work_file_name(work_file_name_in),
00050 identifier(identifier_in)
00051 {
00052 IncRef();
00053 }
00054
00055 ManagedFile::ManagedFile(const ManagedFile& right) :
00056 cleanup(right.cleanup),
00057 work_file_name(right.work_file_name),
00058 identifier(right.identifier)
00059 {
00060 IncRef();
00061 }
00062
00063 ManagedFile& ManagedFile::operator=(const ManagedFile& right)
00064 {
00065 if (&right == this)
00066 {
00067 return *this;
00068 }
00069
00070 DecRef();
00071 cleanup = right.cleanup;
00072 work_file_name = right.work_file_name;
00073 identifier = right.identifier;
00074 IncRef();
00075
00076 return *this;
00077 }
00078
00079 ManagedFile::~ManagedFile()
00080 {
00081 DecRef();
00082 }
00083
00084 void ManagedFile::IncRef()
00085 {
00086 if (cleanup)
00087 {
00088 cleanup->ref_count++;
00089 }
00090 }
00091
00092 void ManagedFile::DecRef()
00093 {
00094 if (cleanup)
00095 {
00096 cleanup->ref_count--;
00097 if (cleanup->ref_count <= 0)
00098 {
00099 EDOC_Finest("Ref count reached 0. Deleting instance "
00100 "calling cleanup for file: " << work_file_name);
00101 delete cleanup;
00102 cleanup = NULL;
00103 work_file_name = "";
00104 Erase(identifier);
00105 }
00106 }
00107 }
00108
00109 std::string ManagedFile::GetFileNameRep() const
00110 {
00111 std::ostringstream stream;
00112 EDOC_FOREACH_CONST(std::list<std::string>, it, identifier)
00113 {
00114 stream << (*it) << ":";
00115 }
00116
00117 if (stream.str().size())
00118 {
00119 return stream.str().substr(0, stream.str().size() -1);
00120 }
00121 return "";
00122 }
00123
00124 }
00125