ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/Ref.h
Revision: 1.1
Committed: Tue Feb 17 14:57:29 2009 UTC (16 years, 2 months ago) by bendavid
Content type: text/plain
Branch: MAIN
Log Message:
Added templated Ref class

File Contents

# User Rev Content
1 bendavid 1.1 //--------------------------------------------------------------------------------------------------
2     // $Id: BaseCollection.h,v 1.3 2008/12/10 11:25:00 loizides Exp $
3     //
4     // Ref
5     //
6     // Templated reimplimentation of TRef
7     //
8     // Authors: J.Bendavid
9     //--------------------------------------------------------------------------------------------------
10    
11     #ifndef MITANA_DATACONT_REF_H
12     #define MITANA_DATACONT_REF_H
13    
14     #include <TObject.h>
15     #include <TRefTable.h>
16     #include <TProcessID.h>
17     #include <TError.h>
18     #include "MitAna/DataCont/interface/ProcIDRef.h"
19    
20     namespace mithep
21     {
22     template<class ArrayElement>
23     class Ref
24     {
25     public:
26     Ref() : fPID(0), fUID(0) {}
27     Ref(const ArrayElement *ae) { SetObject(ae); }
28     virtual ~Ref() {}
29    
30     Bool_t IsNull() const { return fUID==0 ? kTRUE : kFALSE; }
31     Bool_t IsValid() const { return !IsNull(); }
32     const ArrayElement *Obj() const;
33     ArrayElement *Obj();
34     Bool_t RefsObject(const ArrayElement *ae) const;
35     void SetObject(const ArrayElement *ae);
36    
37     void operator= (const ArrayElement *ae) { SetObject(ae); }
38     Bool_t operator==(const ArrayElement *ae) { return RefsObject(ae); }
39    
40     protected:
41     TObject *GetObject() const;
42    
43     ProcIDRef fPID;//||
44     UInt_t fUID;
45    
46     ClassDef(Ref, 1) // Base class of all our collections
47     };
48     }
49    
50     //--------------------------------------------------------------------------------------------------
51     template<class ArrayElement>
52     TObject *mithep::Ref<ArrayElement>::GetObject() const
53     {
54     // Return entry at given index. Code adapted from TRef::GetObject().
55    
56     if (IsNull())
57     return 0;
58    
59     TProcessID *pid= fPID.Pid();
60    
61     if (!pid) {
62     Fatal("GetObject","Process id pointer is null!");
63     return 0;
64     }
65    
66     if (!TProcessID::IsValid(pid)) {
67     Fatal("GetObject","Process id is invalid!");
68     return 0;
69     }
70    
71     //try to find the object from the table of the corresponding PID
72     TObject *obj = pid->GetObjectWithID(fUID);
73    
74     //We are now assuming that the object table is cleaned so that all unloaded objects have null
75     //pointers in the object table
76     //This guarantees that the check below is valid for determining whether we need to go to the
77     //TRefTable
78     if (obj)
79     return obj;
80    
81     //the reference may be in the TRefTable
82     TRefTable *table = TRefTable::GetRefTable();
83     if (table) {
84     table->SetUID(fUID, pid);
85     table->Notify();
86     obj = pid->GetObjectWithID(fUID);
87     }
88    
89     return obj;
90    
91     }
92    
93     //--------------------------------------------------------------------------------------------------
94     template<class ArrayElement>
95     const ArrayElement *mithep::Ref<ArrayElement>::Obj() const
96     {
97     // Return entry at given index. Code adapted from TRef::GetObject().
98    
99     return static_cast<const ArrayElement*>(GetObject());
100     }
101    
102     //--------------------------------------------------------------------------------------------------
103     template<class ArrayElement>
104     ArrayElement *mithep::Ref<ArrayElement>::Obj()
105     {
106     // Return entry at given index. Code adapted from TRef::GetObject().
107    
108     return static_cast<ArrayElement*>(GetObject());
109     }
110    
111     //--------------------------------------------------------------------------------------------------
112     template<class ArrayElement>
113     Bool_t mithep::Ref<ArrayElement>::RefsObject(const ArrayElement *ae) const
114     {
115     // check whether RefArray contains a given object
116    
117     if (IsNull())
118     return kFALSE;
119    
120     if (!ae->TestBit(kIsReferenced))
121     return kFALSE;
122    
123     UInt_t oUid = ae->GetUniqueID();
124     TProcessID *oPid = TProcessID::GetProcessWithUID(oUid, const_cast<ArrayElement*>(ae));
125    
126     if ( (fUID&0xffffff)==(oUid&0xffffff) && fPID.Pid()->GetUniqueID()==oPid->GetUniqueID())
127     return kTRUE;
128     else
129     return kFALSE;
130    
131     }
132    
133     //--------------------------------------------------------------------------------------------------
134     template<class ArrayElement>
135     void mithep::Ref<ArrayElement>::SetObject(const ArrayElement *ae)
136     {
137     // Add new reference to object.
138    
139     // check if the object can belong here and assign or get its uid
140     if (ae->TestBit(kHasUUID)) {
141     Fatal("Add", "Object can not be added as it has not UUID!");
142     return;
143     }
144    
145     if (ae->TestBit(kIsReferenced)) {
146     fUID = ae->GetUniqueID();
147     fPID.SetPid(TProcessID::GetProcessWithUID(fUID, const_cast<ArrayElement*>(ae)));
148     } else {
149     fPID.SetPid(TProcessID::GetSessionProcessID());
150     fUID = TProcessID::AssignID(const_cast<ArrayElement*>(ae));
151     }
152    
153     }
154    
155     #endif