00001 // Version as of 10/20/02. 00002 00003 #define LOCOBJECT_CPP 00004 00005 #include "locobject.h" 00006 #include "message.h" 00007 00008 #include <iomanip> 00009 00010 long CObject::objcount = 0L; 00011 00012 CObjectRegistry GlobalObjectRegistry; 00013 00014 //------------------------------------------------------------------------- 00015 // Definitions for class CObject. 00016 //------------------------------------------------------------------------- 00017 00018 CObject& CObject::CopyState(const CObject& m) 00019 { 00020 MESSAGE<<"CopyState function appropriate to object is not available."<<ENDM_WARN; 00021 return *this = m; // Use of m avoids possible warning 00022 } 00023 00024 void CObject::ReportObjectPars(ostream& strm) const 00025 { 00026 strm << "this " << this << endl 00027 << "instcount " << instcount << endl 00028 << "objcount " << objcount << endl; 00029 } 00030 00031 CObject* CObject::Clone(void) const 00032 { 00033 MESSAGE<<"Clone function appropriate to object is not available."<<ENDM_WARN; 00034 return NULL; 00035 } 00036 00037 //bool CObject::Read(const CCommand& c){ 00038 // if (c.arguments[0] == objtype) {return Read(c.parameters);} 00039 // else {return false;} 00040 //} 00041 00042 //bool CObject::Write(CCommand& c){ 00043 // c.arguments.push_back(objtype); 00044 // Write(c.parameters); 00045 // return true; 00046 //} 00047 00048 //------------------------------------------------------------------------- 00049 // Definitions for class CLocatorBase. 00050 //------------------------------------------------------------------------- 00051 00052 CLocatorBase::CLocatorBase(const CLocatorBase& lo) 00053 { 00054 pObj = lo.pObj; 00055 if (pObj) pObj->instcount++; 00056 } 00057 00058 CLocatorBase& CLocatorBase::operator=(const CLocatorBase& lo) 00059 { 00060 if (this != &lo) { 00061 Dispose(); 00062 pObj = lo.pObj; 00063 if (pObj) pObj->instcount++; 00064 } 00065 return *this; 00066 } 00067 00068 CLocatorBase& CLocatorBase::AttachObject(CObject* _pObj) 00069 { 00070 Dispose(); 00071 if (_pObj != NULL) { 00072 pObj = _pObj; 00073 pObj->instcount = 1L; 00074 CObject::objcount++; 00075 }else { 00076 pObj = NULL; 00077 } 00078 return *this; 00079 } 00080 00081 CLocatorBase& CLocatorBase::AttachClonedObject(const CLocatorBase& lo, bool copystate) 00082 { 00083 CObject* pNewObj = lo->Clone(); 00084 AttachObject(pNewObj); 00085 if (copystate) pObj->CopyState(*lo); 00086 return *this; 00087 } 00088 00089 void CLocatorBase::Report(ostream& strm) const 00090 { 00091 strm << setw(12) << "pObj" 00092 << setw(12) << "instcount" 00093 << setw(12) << "objcount" 00094 << endl 00095 << setw(12) << pObj 00096 << setw(12) << (pObj ? pObj->instcount : 0L) 00097 << setw(12) << CObject::objcount 00098 << endl; 00099 } 00100 00101 void CLocatorBase::Dispose(void) 00102 { 00103 if (pObj != NULL) { 00104 pObj->instcount--; 00105 if (pObj->instcount == 0L) { 00106 CObject::objcount--; 00107 delete pObj; 00108 pObj = NULL; 00109 } 00110 } 00111 } 00112 00113 void CLocatorBase::ReportError(void) const 00114 { 00115 MESSAGE<<"Attempt to access an object that is not present."<<ENDM_WARN; 00116 } 00117 00118 //------------------------------------------------------------------------- 00119 // Definitions for class CObjectRegistry. 00120 //------------------------------------------------------------------------- 00121 00122 void CObjectRegistry::Register(const CLocatorBase& lo, const string& ID) 00123 { 00124 if (!insert(value_type(ID, lo)).second){MESSAGE<<"Duplicate identifier "<<ENDM_FATAL;exit(-1);} 00125 } 00126 00127 bool CObjectRegistry::Unregister(const string& ID) 00128 { 00129 iterator it = find(ID); 00130 if (it == end()) { 00131 return false; 00132 } else { 00133 erase(it); 00134 return true; 00135 } 00136 } 00137 00138 CObject* CObjectRegistry::Create(const string& ID) const 00139 { 00140 const_iterator it = find(ID); 00141 if (it == end()){MESSAGE<< "Identifier " + ID + " does not exist."<<ENDM_FATAL;exit(-1);} 00142 CObject* result = (*it).second->Clone(); 00143 result->setObjType(ID); 00144 return result; 00145 } 00146 00147 void CObjectRegistry::Report(ostream& strm) const 00148 {for (const_iterator it = begin(); it != end(); ++it)strm << " " << (*it).first << endl;}