PandaRoot
PndSTDMutableContainer.h
Go to the documentation of this file.
1 //****************************************************************************
2 //* This file is part of PandaRoot. *
3 //* *
4 //* PandaRoot is distributed under the terms of the *
5 //* GNU General Public License (GPL) version 3, *
6 //* copied verbatim in the file "LICENSE". *
7 //* *
8 //* Copyright (C) 2006 - 2024 FAIR GmbH and copyright holders of PandaRoot *
9 //* The copyright holders are listed in the file "COPYRIGHTHOLDERS". *
10 //* The authors are listed in the file "AUTHORS". *
11 //****************************************************************************
12 
27 #ifndef PNDSTDMUTABLECONTAINER_HH
28 #define PNDSTDMUTABLECONTAINER_HH
29 
30 #include <stdexcept>
31 #include <vector>
32 
33 #include "TString.h"
34 
35 #include "FairLogger.h"
36 #include "FairRootManager.h"
37 
38 #include "PndMutableContainerI.h"
39 
48 template <class T>
50  public:
57  PndSTDMutableContainer(const TString &t_foldername = "", Bool_t t_persistency = kTRUE) : PndMutableContainerI<T>(t_foldername, t_persistency) {}
58 
64 
71  virtual T *GetElement(Int_t t_idx) const final
72  {
73  if (fSTD != nullptr) {
74  return &(fSTD->at(t_idx));
75  }
76  return nullptr;
77  }
78 
84  virtual ssize_t GetSize() const final
85  {
86  if (fSTD != nullptr) {
87  return fSTD->size();
88  }
89  return 0;
90  }
91 
98  virtual T *CreateCopy(const T &t_element)
99  {
100  if (fSTD != nullptr) {
101  fSTD->push_back(T{t_element});
102  } else {
103  LOG(error) << "PndSTDMutableContainer::CreateCopy() no underlying std::vector";
104  return nullptr;
105  }
106  return GetElement(GetSize() - 1);
107  }
108 
114  virtual void RemoveAt(Int_t t_index) final
115  {
116  if (this->fSTD != nullptr) {
117  this->fSTD->erase(this->fSTD->begin() + t_index);
118  }
119  }
120 
125  virtual void Compress() {}
126 
131  virtual void Reset() final
132  {
133  if (this->fSTD != nullptr) {
134  this->fSTD->clear();
135  }
136  }
137 
143  void SetData(std::vector<T> *t_container) { fSTD = t_container; }
144 
150  std::vector<T> *GetData() { return fSTD; }
151 
152  private:
153  std::vector<T> *fSTD{nullptr};
154 };
155 
156 #endif /*PNDSTDMUTABLECONTAINER_HH*/
std::vector< T > * GetData()
Get the Data.
virtual T * CreateCopy(const T &t_element)
Create a Copy object of t_element and return pointer to copy.
void SetData(std::vector< T > *t_container)
Set the Data.
virtual void Reset() final
"Delete" all elements
virtual void RemoveAt(Int_t t_index) final
remove t_index element
virtual ~PndSTDMutableContainer()
Destroy the PndSTDMutableContainer object.
PndSTDMutableContainer(const TString &t_foldername="", Bool_t t_persistency=kTRUE)
Construct a new PndSTDMutableContainer object.
virtual ssize_t GetSize() const final
Get the number of elements.
virtual T * GetElement(Int_t t_idx) const final
Get the t_idx Element.
PndSTDMutableContainer implementation of PndMutableContainerI<T> for FairRootManager and std::vector<...
virtual void Compress()
Compress.