// File created: 05/22/2005 last modified: 05/22/2005 // Author: Christian Stratowa /////////////////////////// // // in new root session: // > .x macroTestCopy.C // /////////////////////////// #include "TList.h" #include "TNamed.h" #include "TString.h" #include class XProjectInfo: public TNamed { protected: Long_t fDate; //Date of creation TString fDescription; //Description public: XProjectInfo(); XProjectInfo(const char *name, Long_t date = 0, const char *description = ""); XProjectInfo(const XProjectInfo &info); XProjectInfo& operator=(const XProjectInfo& rhs); virtual ~XProjectInfo(); virtual void Copy(TObject &obj) const; Long_t GetDate() const {return fDate;} TString GetDescription() const {return fDescription;} #if !defined (__CINT__) || defined (__MAKECINT__) ClassDef(XProjectInfo,1) //ProjectInfo #endif }; class MyClass: public TNamed { protected: TList *fList; //! List to store info public: MyClass(); MyClass(const char *name, const char *title = ""); virtual ~MyClass(); virtual void ProjectInfo(const char *name, Long_t date, const char *description = ""); virtual void ProjectInfo(XProjectInfo *info); virtual void CopyProjectInfo1(const XProjectInfo info); virtual void CopyProjectInfo2(XProjectInfo &info); virtual void CopyProjectInfo3(XProjectInfo *info); virtual void PrintData(const char *name); #if !defined (__CINT__) || defined (__MAKECINT__) ClassDef(MyClass,1) //MyClass #endif }; //debug: print function names const Bool_t kCS = 1; #if !defined (__CINT__) || defined (__MAKECINT__) ClassImp(XProjectInfo); ClassImp(MyClass); #endif //______________________________________________________________________________ //______________________________________________________________________________ XProjectInfo::XProjectInfo() :TNamed() { // Default Project information constructor if(kCS) cout << "---XProjectInfo::XProjectInfo(default)------" << endl; fDate = 0; fDescription = ""; }//Constructor //______________________________________________________________________________ XProjectInfo::XProjectInfo(const char *name, Long_t date, const char *description) :TNamed(name, "Project") // :TNamed("Project", name) { // Project information constructor if(kCS) cout << "---XProjectInfo::XProjectInfo------" << endl; fDate = date; fDescription = description; }//Constructor //______________________________________________________________________________ XProjectInfo::XProjectInfo(const XProjectInfo &info) :TNamed(info),fDescription(info.fDescription) { // Project information copy constructor if(kCS) cout << "---XProjectInfo::XProjectInfo(copy)------" << endl; fDate = info.fDate; }//CopyConstructor //______________________________________________________________________________ XProjectInfo& XProjectInfo::operator=(const XProjectInfo& rhs) { // Project information assignment operator. if(kCS) cout << "---XProjectInfo::operator=------" << endl; if (this != &rhs) { TNamed::operator=(rhs); fDate = rhs.fDate; fDescription = rhs.fDescription; }//if return *this; }//operator= //______________________________________________________________________________ XProjectInfo::~XProjectInfo() { // Project information destructor if(kCS) cout << "---XProjectInfo::~XProjectInfo------" << endl; }//Destructor //______________________________________________________________________________ void XProjectInfo::Copy(TObject &obj) const { // Copy project information to obj if(kCS) cout << "------XProjectInfo::Copy------" << endl; TNamed::Copy(obj); ((XProjectInfo&)obj).fDate = fDate; ((XProjectInfo&)obj).fDescription = fDescription; }//Copy //______________________________________________________________________________ //______________________________________________________________________________ MyClass::MyClass() :TNamed() { // Default constructor if(kCS) cout << "---MyClass::MyClass(default)------" << endl; fList = 0; }//Constructor //______________________________________________________________________________ MyClass::MyClass(const char *name, const char *title) :TNamed(name, title) { // Normal constructor if(kCS) cout << "---MyClass::MyClass------" << endl; fList = new TList(); }//Constructor //______________________________________________________________________________ MyClass::~MyClass() { // DataManager destructor if(kCS) cout << "---MyClass::~MyClass------" << endl; if(fList) {fList->Delete(); delete fList; fList = 0;} }//Destructor //______________________________________________________________________________ void MyClass::ProjectInfo(const char *name, Long_t date, const char *description) { // Store project information in list if(kCS) cout << "------MyClass::ProjectInfo------" << endl; XProjectInfo *info = new XProjectInfo(name, date, description); fList->Add(info); }//ProjectInfo //______________________________________________________________________________ void MyClass::ProjectInfo(XProjectInfo *info) { // Store project information in list if(kCS) cout << "------MyClass::ProjectInfo------" << endl; fList->Add(info); }//ProjectInfo //______________________________________________________________________________ void MyClass::CopyProjectInfo1(const XProjectInfo info) { // Store project information in list if(kCS) cout << "------MyClass::CopyProjectInfo1------" << endl; XProjectInfo *infocopy = new XProjectInfo(info); fList->Add(infocopy); }//CopyProjectInfo1 //______________________________________________________________________________ void MyClass::CopyProjectInfo2(XProjectInfo &info) { // Store project information in list if(kCS) cout << "------MyClass::CopyProjectInfo2------" << endl; XProjectInfo *infocopy = new XProjectInfo(); infocopy->Copy(info); fList->Add(infocopy); }//CopyProjectInfo2 //______________________________________________________________________________ void MyClass::CopyProjectInfo3(XProjectInfo *info) { // Store project information in list if(kCS) cout << "------MyClass::CopyProjectInfo3------" << endl; // XProjectInfo *infocopy = new XProjectInfo(); XProjectInfo infocopy; info->Copy(infocopy); fList->Add(&infocopy); }//CopyProjectInfo3 //______________________________________________________________________________ void MyClass::PrintData(const char *name) { // Store find project information in list and print if(kCS) cout << "------MyClass::PrintData------" << endl; XProjectInfo *info = (XProjectInfo*)fList->FindObject(name); if (info) { cout << "project_name = " << info->GetName() << endl; cout << "project_date = " << info->GetDate() << endl; cout << "project_desc = " << info->GetDescription() << endl; }//if }//PrintData //______________________________________________________________________________ //______________________________________________________________________________ void macroTestCopy() { // Test copying of class XProjectInfo MyClass *myclass = new MyClass("MyClass","test"); myclass->ProjectInfo("ProjectA", 22052005, "descriptionA"); XProjectInfo infoB("ProjectB", 22052005, "descriptionB"); myclass->CopyProjectInfo1(infoB); XProjectInfo infoC("ProjectC", 22052005, "descriptionC"); myclass->CopyProjectInfo2(infoC); XProjectInfo *infoD = new XProjectInfo("ProjectD", 22052005, "descriptionD"); myclass->CopyProjectInfo3(infoD); myclass->PrintData("ProjectA"); myclass->PrintData("ProjectC"); myclass->PrintData("ProjectD"); myclass->PrintData("ProjectB"); delete myclass; delete infoD; } /* //______________________________________________________________________________ void TestCopy1() { // Test copying of class XProjectInfo MyClass *myclass = new MyClass("MyClass","test"); myclass->ProjectInfo("ProjectA", 22052005, "descriptionA"); XProjectInfo *infoB = new XProjectInfo("ProjectB", 22052005, "descriptionB"); myclass->CopyProjectInfo1(infoB); XProjectInfo *infoC = new XProjectInfo("ProjectC", 22052005, "descriptionC"); myclass->CopyProjectInfo2(infoC); XProjectInfo *infoD = new XProjectInfo("ProjectD", 22052005, "descriptionD"); myclass->CopyProjectInfo3(infoD); myclass->PrintData("ProjectA"); myclass->PrintData("ProjectC"); myclass->PrintData("ProjectB"); myclass->PrintData("ProjectB"); delete myclass; delete infoB; delete infoC; delete infoD; } */