ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/DataCont/interface/Ref.h
Revision: 1.7
Committed: Sun Sep 19 23:46:08 2010 UTC (14 years, 7 months ago) by bendavid
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_025c_branch2, Mit_025c_branch1, Mit_028a, Mit_025c_branch0, Mit_028, Mit_027a, Mit_027, Mit_026, Mit_025e, Mit_025d, Mit_025c, Mit_025b, Mit_025a, Mit_025, Mit_025pre2, Mit_024b, Mit_025pre1, Mit_024a, Mit_024, Mit_023, Mit_022a, Mit_022, Mit_020d, TMit_020d, Mit_020c, Mit_021, Mit_021pre2, Mit_021pre1, Mit_020b, Mit_020a, Mit_020, Mit_020pre1, Mit_018, Mit_017, Mit_017pre3, Mit_017pre2, Mit_017pre1, Mit_016, Mit_015b, Mit_015a, Mit_015, Mit_014e
Branch point for: Mit_025c_branch
Changes since 1.6: +7 -2 lines
Log Message:
add hard protections for null pointers from refs

File Contents

# User Rev Content
1 bendavid 1.1 //--------------------------------------------------------------------------------------------------
2 bendavid 1.7 // $Id: Ref.h,v 1.6 2009/03/24 05:44:14 loizides Exp $
3 bendavid 1.1 //
4     // Ref
5     //
6 loizides 1.6 // Templated implementation of our own TRef.
7 bendavid 1.1 //
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 loizides 1.3 #include "MitAna/DataCont/interface/RefResolver.h"
19 bendavid 1.1 #include "MitAna/DataCont/interface/ProcIDRef.h"
20    
21     namespace mithep
22     {
23     template<class ArrayElement>
24     class Ref
25     {
26     public:
27 loizides 1.3 Ref() : fPID(0), fUID(0) {}
28 bendavid 1.1 Ref(const ArrayElement *ae) { SetObject(ae); }
29 loizides 1.3 virtual ~Ref() {}
30 bendavid 1.1
31     Bool_t IsNull() const { return fUID==0 ? kTRUE : kFALSE; }
32 loizides 1.4 Bool_t IsValid() const { return !IsNull(); }
33 bendavid 1.1 const ArrayElement *Obj() const;
34     ArrayElement *Obj();
35     Bool_t RefsObject(const ArrayElement *ae) const;
36     void SetObject(const ArrayElement *ae);
37    
38 loizides 1.4 void operator= (const ArrayElement *ae) { SetObject(ae); }
39 bendavid 1.1 Bool_t operator==(const ArrayElement *ae) { return RefsObject(ae); }
40    
41     protected:
42     TObject *GetObject() const;
43    
44 loizides 1.3 ProcIDRef fPID; //||process id corresponding to referenced object
45     UInt_t fUID; //unique id of the referenced object
46    
47     ClassDef(Ref, 1) // Templated implementation of our own TRef
48 bendavid 1.1 };
49     }
50    
51     //--------------------------------------------------------------------------------------------------
52     template<class ArrayElement>
53     TObject *mithep::Ref<ArrayElement>::GetObject() const
54     {
55 loizides 1.3 // Return pointer to object. Code adapted from TRef::GetObject().
56 bendavid 1.1
57     if (IsNull())
58     return 0;
59    
60 loizides 1.3 TProcessID *pid = fPID.Pid();
61 bendavid 1.1 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 bendavid 1.7 TObject *obj = RefResolver::GetObjectWithID(fUID,pid);
72     if (!obj) {
73     Fatal("Ref::GetObject","Null pointer for valid ref!");
74     }
75    
76     return obj;
77 bendavid 1.1 }
78    
79     //--------------------------------------------------------------------------------------------------
80     template<class ArrayElement>
81     const ArrayElement *mithep::Ref<ArrayElement>::Obj() const
82     {
83     // Return entry at given index. Code adapted from TRef::GetObject().
84    
85     return static_cast<const ArrayElement*>(GetObject());
86     }
87    
88     //--------------------------------------------------------------------------------------------------
89     template<class ArrayElement>
90     ArrayElement *mithep::Ref<ArrayElement>::Obj()
91     {
92     // Return entry at given index. Code adapted from TRef::GetObject().
93    
94     return static_cast<ArrayElement*>(GetObject());
95     }
96    
97     //--------------------------------------------------------------------------------------------------
98     template<class ArrayElement>
99     Bool_t mithep::Ref<ArrayElement>::RefsObject(const ArrayElement *ae) const
100     {
101 loizides 1.4 // Check whether Ref points to given object.
102 bendavid 1.1
103     if (IsNull())
104     return kFALSE;
105    
106     if (!ae->TestBit(kIsReferenced))
107     return kFALSE;
108    
109     UInt_t oUid = ae->GetUniqueID();
110     TProcessID *oPid = TProcessID::GetProcessWithUID(oUid, const_cast<ArrayElement*>(ae));
111 loizides 1.4 if (!oPid)
112     return kFALSE;
113 bendavid 1.1
114 loizides 1.4 if ( (fUID&0xffffff)!=(oUid&0xffffff) || fPID.Pid()->GetUniqueID()!=oPid->GetUniqueID())
115 bendavid 1.1 return kFALSE;
116 loizides 1.4 return kTRUE;
117 bendavid 1.1 }
118    
119     //--------------------------------------------------------------------------------------------------
120     template<class ArrayElement>
121     void mithep::Ref<ArrayElement>::SetObject(const ArrayElement *ae)
122     {
123 loizides 1.4 // Set reference to object.
124    
125     if (!ae)
126     return;
127 bendavid 1.1
128     // check if the object can belong here and assign or get its uid
129     if (ae->TestBit(kHasUUID)) {
130 loizides 1.5 Fatal("Add", "Object cannot be added as it has not UUID!");
131 bendavid 1.1 return;
132     }
133    
134     if (ae->TestBit(kIsReferenced)) {
135     fUID = ae->GetUniqueID();
136     fPID.SetPid(TProcessID::GetProcessWithUID(fUID, const_cast<ArrayElement*>(ae)));
137     } else {
138     fPID.SetPid(TProcessID::GetSessionProcessID());
139     fUID = TProcessID::AssignID(const_cast<ArrayElement*>(ae));
140     }
141     }
142     #endif