12 #ifndef ROOT_TTreeReaderArray
13 #define ROOT_TTreeReaderArray
20 #include <type_traits>
29 class TTreeReaderArrayBase:
public TTreeReaderValueBase {
31 TTreeReaderArrayBase(TTreeReader* reader,
const char* branchname,
33 TTreeReaderValueBase(reader, branchname, dict) {}
35 std::size_t GetSize()
const {
return fImpl->GetSize(GetProxy()); }
36 Bool_t IsEmpty()
const {
return !GetSize(); }
38 virtual EReadStatus GetReadStatus()
const {
return fImpl ? fImpl->fReadStatus : kReadError; }
41 void *UntypedAt(std::size_t idx)
const {
return fImpl->At(GetProxy(), idx); }
42 virtual void CreateProxy();
43 bool GetBranchAndLeaf(TBranch* &branch, TLeaf* &myLeaf,
44 TDictionary* &branchActualType);
45 void SetImpl(TBranch* branch, TLeaf* myLeaf);
46 const char* GetBranchContentDataType(TBranch* branch,
47 TString& contentTypeName,
50 std::unique_ptr<TVirtualCollectionReader> fImpl;
75 class R__CLING_PTRCHECK(off) TTreeReaderArray final :
public ROOT::Internal::TTreeReaderArrayBase {
81 template <
typename ReaderArrayType>
85 using iterator_category = std::random_access_iterator_tag;
87 using difference_type = std::ptrdiff_t;
88 using pointer =
typename std::conditional<std::is_const<ReaderArrayType>::value,
const T *, T *>::type;
89 using reference =
typename std::conditional<std::is_const<ReaderArrayType>::value,
const T &, T &>::type;
92 TTreeReaderArray *fArray;
97 Iterator_t() : fArray(nullptr), fIndex(0u), fSize(0u) {}
100 Iterator_t(std::size_t index, TTreeReaderArray *array)
101 : fArray(array), fIndex(index), fSize(fArray ? fArray->GetSize() : 0u)
108 Iterator_t(std::size_t index,
const TTreeReaderArray *array)
109 : Iterator_t(index, const_cast<TTreeReaderArray *>(array)) {}
111 Iterator_t(Iterator_t &&) =
default;
112 Iterator_t(
const Iterator_t &) =
default;
113 Iterator_t &operator=(Iterator_t &&) =
default;
114 Iterator_t &operator=(
const Iterator_t &) =
default;
116 reference operator*()
const
118 R__ASSERT(fArray &&
"invalid iterator!");
119 return fArray->At(fIndex);
122 pointer operator->()
const {
return IsValid() ? &fArray->At(fIndex) :
nullptr; }
124 bool operator==(
const Iterator_t &other)
const
127 if (!IsValid() && !other.IsValid())
129 return fArray == other.fArray && fIndex == other.fIndex;
132 bool operator!=(
const Iterator_t &other)
const {
return !(*
this == other); }
135 Iterator_t &operator++()
145 Iterator_t operator++(
int)
153 Iterator_t &operator--()
163 Iterator_t operator--(
int)
170 Iterator_t operator+(std::ptrdiff_t n)
const {
return Iterator_t(fIndex + n, fArray); }
171 friend auto operator+(std::ptrdiff_t n,
const Iterator_t &it) -> decltype(it + n) {
return it + n; }
173 Iterator_t operator-(std::ptrdiff_t n)
const
175 const auto index = std::ptrdiff_t(fIndex);
176 const auto newIndex = index >= n ? index - n : std::numeric_limits<decltype(fIndex)>::max();
177 return Iterator_t(newIndex, fArray);
180 std::ptrdiff_t operator-(
const Iterator_t &other)
const {
return fIndex - other.fIndex; }
182 Iterator_t &operator+=(std::ptrdiff_t n) {
return (*
this = *
this + n); }
184 Iterator_t &operator-=(std::ptrdiff_t n) {
return (*
this = *
this - n); }
186 bool operator<(
const Iterator_t &other)
const {
return fIndex < other.fIndex; }
187 bool operator>(
const Iterator_t &other)
const {
return fIndex > other.fIndex; }
188 bool operator<=(
const Iterator_t &other)
const {
return !(*
this > other); }
189 bool operator>=(
const Iterator_t &other)
const {
return !(*
this < other); }
191 reference operator[](std::size_t index)
const {
return *(*
this + index); }
193 operator pointer() {
return &fArray->At(fIndex); }
195 bool IsValid()
const {
return fArray !=
nullptr; }
198 using iterator = Iterator_t<TTreeReaderArray<T>>;
199 using const_iterator = Iterator_t<const TTreeReaderArray<T>>;
202 TTreeReaderArray(TTreeReader &tr,
const char *branchname)
203 : TTreeReaderArrayBase(&tr, branchname, TDictionary::GetDictionary(typeid(T))) {}
205 T &At(std::size_t idx) {
return *
static_cast<T *
>(UntypedAt(idx)); }
206 const T &At(std::size_t idx)
const {
return *
static_cast<T *
>(UntypedAt(idx)); }
207 T &operator[](std::size_t idx) {
return At(idx); }
208 const T &operator[](std::size_t idx)
const {
return At(idx); }
210 iterator begin() {
return iterator(0u,
this); }
211 iterator end() {
return iterator(GetSize(),
this); }
212 const_iterator begin()
const {
return cbegin(); }
213 const_iterator end()
const {
return cend(); }
214 const_iterator cbegin()
const {
return const_iterator(0u,
this); }
215 const_iterator cend()
const {
return const_iterator(GetSize(),
this); }
218 #define R__TTreeReaderArray_TypeString(T) #T
219 virtual const char *GetDerivedTypeName()
const {
return R__TTreeReaderArray_TypeString(T); }
220 #undef R__TTreeReaderArray_TypeString
225 #endif // ROOT_TTreeReaderArray