Diana Software
QStdVector.hh
Go to the documentation of this file.
1 #ifndef _Q_STDVECTOR_HH_
2 #define _Q_STDVECTOR_HH_
3 
15 #include "QObject.hh"
16 #include <vector>
17 #include <sstream>
18 
20 
21 template<class T> class QStdVector : public std::vector<T>, public QObject
22 {
23 
24  public:
27 
29  QStdVector(const std::vector<T>& rhs) { this->operator=(rhs); }
30 
32  QStdVector(const QStdVector<T>& rhs) { this->operator=(rhs); }
33 
35  void Clear()
36  {
37  std::vector<T>::clear();
38  }
39 
41  QError WriteOnStream (std::ostream &o) const
42  {
44  o<<"fSize"<<"\t"<<std::vector<T>::size()<<std::endl;
45  if(std::vector<T>::empty()) return err;
46  o<<"fData";
47  for(size_t i = 0; i < std::vector<T>::size(); i++) {
48  o<<"\t"<<std::vector<T>::operator[](i);
49  }
50  o<<std::endl;
51  return err;
52  }
54  QError ReadFromStream (std::istream &ob)
55  {
56  std::vector<T>::clear();
58  std::string tag;
59  int size = 0;
60  ob>>tag>>size;
61  if(tag != "fSize") {
63  err.SetDescription(__FILE__,__LINE__,"Expected tag fFunction.fName");
64  return err;
65  }
66  if(size < 0) {
68  err.SetDescription(__FILE__,__LINE__,"Tag fSize cannot be negative");
69  }
70  if(size == 0) return err;
71  std::vector<T>::resize(size);
72 
73  ob>>tag;
74  if(tag != "fData") {
76  err.SetDescription(__FILE__,__LINE__,"Expected tag fData after tag fSize");
77  return err;
78  }
79 
80  T element;
81  for(size_t i = 0; i < (size_t)size; i++) {
82  ob>>element;
83  if(!ob.good()) {
85  std::stringstream msg;
86  msg << "Cannot read element "<< i;
87  err.SetDescription(__FILE__,__LINE__,msg.str());
88  break;
89  } else {
90  std::vector<T>::operator[](i) = element;
91  }
92  }
93 
94  return err;
95 
96 
97  }
99  void Dump(std::ostream& o) const { WriteOnStream(o); }
100 
102  bool Fill(Diana::QObject* obj) const
103  {
104  QStdVector<T>* bt = dynamic_cast<QStdVector<T>* >(obj);
105  if(bt) {
106  *bt = *this;
107  }
108  return bt != NULL;
109  }
110 
112  QObject* Duplicate() const {
113  QStdVector<T> * obj = new QStdVector<T>;
114  return obj;
115  }
116 
118  QStdVector<T>& operator=(const std::vector<T>& rhs)
119  {
120  std::vector<T>::operator=(rhs);
121  Validate();
122  return *this;
123  }
124 
125  private:
126 
128 
129 };
130 
132 #endif
err
Definition: CheckOF.C:114
#define Q_END_NAMESPACE
Definition: QDiana.hh:22
#define Q_BEGIN_NAMESPACE
Definition: QDiana.hh:20
@ QERR_UNKNOWN_ERR
Definition: QError.hh:108
@ QERR_SIZE_NOT_MATCH
Definition: QError.hh:31
@ QERR_SUCCESS
Definition: QError.hh:27
error class with error type and description
Definition: QError.hh:115
base class for objects handled by QEvent and QGlobalDataManager.
Definition: QObject.hh:76
void Validate()
make object valid
Definition: QObject.hh:108
std::vector QObject
Definition: QStdVector.hh:22
bool Fill(Diana::QObject *obj) const
fill object of same type (like operator=)
Definition: QStdVector.hh:102
void Dump(std::ostream &o) const
Write on stream (intended for screen output)
Definition: QStdVector.hh:99
QError WriteOnStream(std::ostream &o) const
Write on stream (intended for file output)
Definition: QStdVector.hh:41
void Clear()
clear members
Definition: QStdVector.hh:35
QStdVector(const QStdVector< T > &rhs)
copy ctor
Definition: QStdVector.hh:32
QObject * Duplicate() const
create object of same type
Definition: QStdVector.hh:112
QStdVector()
ctor
Definition: QStdVector.hh:26
QStdVector(const std::vector< T > &rhs)
copy ctor
Definition: QStdVector.hh:29
QStdVector< T > & operator=(const std::vector< T > &rhs)
operator=
Definition: QStdVector.hh:118
ClassDef(QStdVector, 2)
QError ReadFromStream(std::istream &ob)
Read from stream (intended for file input)
Definition: QStdVector.hh:54