#ifndef ROOT_TTreeReaderArray
#define ROOT_TTreeReaderArray
#ifndef ROOT_TTreeReaderValue
#include "TTreeReaderValue.h"
#endif
#ifndef ROOT_TTreeReaderUtils
#include "TTreeReaderUtils.h"
#endif
namespace ROOT {
class TTreeReaderArrayBase: public TTreeReaderValueBase {
public:
TTreeReaderArrayBase(TTreeReader* reader, const char* branchname,
TDictionary* dict):
TTreeReaderValueBase(reader, branchname, dict), fImpl(0) {}
size_t GetSize() const { return fImpl->GetSize(GetProxy()); }
Bool_t IsEmpty() const { return !GetSize(); }
virtual EReadStatus GetReadStatus() const { return fImpl ? fImpl->fReadStatus : kReadError; }
protected:
void* UntypedAt(size_t idx) const { return fImpl->At(GetProxy(), idx); }
virtual void CreateProxy();
const char* GetBranchContentDataType(TBranch* branch,
TString& contentTypeName,
TDictionary* &dict) const;
TVirtualCollectionReader* fImpl;
};
}
template <typename T>
class TTreeReaderArray: public ROOT::TTreeReaderArrayBase {
public:
struct Iterator_t:
public std::iterator<std::input_iterator_tag, T, long> {
Iterator_t() :
fIndex(0), fArray(0) {}
Iterator_t(size_t idx, TTreeReaderArray* array) :
fIndex(idx), fArray(array) {}
size_t fIndex;
TTreeReaderArray* fArray;
bool IsValid() const { return fArray; }
bool operator==(const Iterator_t& lhs) const {
if (!IsValid() && !lhs.IsValid())
return true;
return fIndex == lhs.fIndex && fArray == lhs.fArray;
}
bool operator!=(const Iterator_t& lhs) const {
return !(*this == lhs);
}
Iterator_t operator++(int) {
Iterator_t ret = *this;
this->operator++();
return ret;
}
Iterator_t& operator++() {
if (IsValid()) {
++fIndex;
if (fIndex >= fArray->GetSize()) {
fArray = 0;
}
}
return *this;
}
T& operator*() const {
R__ASSERT(fArray && "invalid iterator!");
return fArray->At(fIndex);
}
};
typedef Iterator_t iterator;
TTreeReaderArray(TTreeReader& tr, const char* branchname):
TTreeReaderArrayBase(&tr, branchname, TDictionary::GetDictionary(typeid(T)))
{
}
T& At(size_t idx) { return *(T*)UntypedAt(idx); }
T& operator[](size_t idx) { return At(idx); }
Iterator_t begin() {
return IsEmpty() ? Iterator_t() : Iterator_t(0, this);
}
Iterator_t end() const { return Iterator_t(); }
protected:
#define R__TTreeReaderArray_TypeString(T) #T
virtual const char* GetDerivedTypeName() const { return R__TTreeReaderArray_TypeString(T); }
#undef R__TTreeReaderArray_TypeString
};
#endif // ROOT_TTreeReaderArray