Hall A ROOT/C++ Analyzer (podd)
Loading...
Searching...
No Matches
VectorObjVar.cxx
Go to the documentation of this file.
1//*-- Author : Ole Hansen 14/2/2018
2
4//
5// VectorObjVar
6//
7// A "global variable" referencing data in objects held in a std::vector
8//
10
11#include "VectorObjVar.h"
12#include "THaVar.h"
13#include "TError.h"
14#include <cassert>
15#include <vector>
16
17#define kInvalid THaVar::kInvalid
18#define kInvalidInt THaVar::kInvalidInt
19
20using namespace std;
21
22typedef vector<void*> VecType;
23
24namespace Podd {
25
26//_____________________________________________________________________________
27VectorObjVar::VectorObjVar( THaVar* pvar, const void* addr, VarType type,
28 Int_t elem_size, Int_t offset )
29 : Variable(pvar,addr,type), SeqCollectionVar(pvar,addr,type,offset),
30 fElemSize(elem_size)
31{
32 // Constructor
33 assert( fElemSize >= 0 );
34}
35
36//_____________________________________________________________________________
38{
39 // Get number of elements of the variable
40
41 assert( fValueP );
42
43 const auto *const pvec = reinterpret_cast<const VecType*>( fValueP );
44 if( !pvec )
45 return kInvalidInt;
46
47 VecType::size_type n = pvec->size();
48 if( n > static_cast<VecType::size_type>(kMaxInt) )
49 return kInvalidInt;
50 auto len = static_cast<Int_t>(n);
51#ifndef STDVECTOR_SIZE_INDEPENDENT_OF_TYPE
52 // If the vector contains the objects themselves, may need to correct for the
53 // actual object size (could be implementation-dependent)
54 if( fElemSize > 0 ) {
55 assert( len*sizeof(VecType::value_type) % fElemSize == 0 );
56 len = (len*sizeof(VecType::value_type))/fElemSize;
57 }
58#endif
59 return len;
60}
61
62//_____________________________________________________________________________
63const void* VectorObjVar::GetDataPointer( Int_t i ) const
64{
65 // Get pointer to data in objects stored in a std::vector
66
67 const char* const here = "GetDataPointer()";
68
69 static_assert( sizeof(ULong_t) == sizeof(void*), "ULong_t must be of pointer size");
70 assert( fValueP );
71
72 Int_t len = GetLen();
73 if( len == 0 || len == kInvalidInt )
74 return nullptr;
75
76 if( i<0 || i>=len ) {
77 fSelf->Error( here, "Index out of range, variable %s, index %d",
78 GetName(), i );
79 return nullptr;
80 }
81
82 const VecType& vec = *reinterpret_cast<const VecType*>(fValueP);
83 void* obj;
84 if( fElemSize == 0 ) {
85 obj = vec[i];
86 if( !obj ) {
87 fSelf->Error( here, "Null pointer in vector, variable %s, index %d. "
88 "Check detector code for bugs.", GetName(), i );
89 return nullptr;
90 }
91 } else {
92 obj = reinterpret_cast<void*>((ULong_t)&vec[0] + i*fElemSize);
93 assert(obj); // else corrupted std::vector
94 }
95
96 // Now obj points to the object. Compute data location using the offset, if needed
97 if( fOffset > 0 ) {
98 ULong_t loc = reinterpret_cast<ULong_t>(obj) + fOffset;
99 if( fType >= kDoubleP && fType <= kUCharP )
100 obj = *reinterpret_cast<void**>(loc);
101 else
102 obj = reinterpret_cast<void*>(loc);
103 }
104 return obj;
105}
106
107//_____________________________________________________________________________
109{
110 // Compare the size counter of this variable to that of 'rhs'.
111 // Trivially, VectorObjVar variables have the same size if they
112 // belong to the same std::vector
113
114 const auto *const other = dynamic_cast<const VectorObjVar*>(&rhs);
115 if( !other )
116 return false;
117
118 return fValueP == other->fValueP;
119}
120
121//_____________________________________________________________________________
122
123}// namespace Podd
int Int_t
unsigned long ULong_t
bool Bool_t
const Int_t kMaxInt
#define kInvalidInt
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
static const char *const here
Definition THaVar.cxx:64
vector< void * > VecType
const void * fValueP
Definition Variable.h:60
const char * GetName() const
Definition Variable.cxx:56
THaVar * fSelf
Definition Variable.h:59
VarType fType
Definition Variable.h:61
VectorObjVar(THaVar *pvar, const void *addr, VarType type, Int_t elem_size, Int_t offset)
virtual Bool_t HasSameSize(const Variable &rhs) const
virtual Int_t GetLen() const
virtual const void * GetDataPointer(Int_t i=0) const
virtual void Error(const char *method, const char *msgfmt,...) const
const Int_t n
STL namespace.