Jpp  master_rocky
the software that should make you happy
AAObject.hh
Go to the documentation of this file.
1 #ifndef AAOBJECTINCLUDED
2 #define AAOBJECTINCLUDED
3 
4 #include "TObject.h"
5 
6 #include <iostream>
7 #include <iomanip>
8 #include <string>
9 #include <algorithm>
10 
12 
13 
14 /*! \brief AAObject is a base class for I/O-classes that adds the possibility
15  to add 'user' information which will also be stored in the ROOT file.
16 */
17 
18 struct AAObject : public TObject
19 {
20  std::vector<double> usr; ///< user data
22 
23  /**
24  * Get index in user data of the item with given key.
25  *
26  * \param key key
27  * \return index (-1 if key does not exists)
28  */
29  int idx( const std::string& key ) const
30  {
31  auto i = std::find (usr_names.begin(), usr_names.end(), key );
32  if (i == usr_names.end()) return -1;
33  return i - usr_names.begin();
34  }
35 
36  /**
37  * Check availability of user data of the item with given key.
38  *
39  * \param key key
40  * \return true if available; else false
41  */
42  bool haveusr( const std::string& key ) const
43  {
44  return idx( key ) >= 0;
45  }
46 
47  /**
48  * Get index in user data of the item with given key.\n
49  * This method throws a run-time exception if no user data are available.
50  *
51  * \param key key
52  * \return index (-1 if key does not exists)
53  */
54  int idxusr_checked( const std::string& key ) const
55  {
56  int r = idx( key );
57  if (r < 0)
58  {
59  THROW(Exception, "No user data for key " << key << " in aanet object of type " << this -> ClassName());
60  }
61  return r;
62  }
63 
64 
65  /**
66  * Get user data item with given key.\n
67  * This method throws a run-time exception if no user data are available.
68  *
69  * \param key key
70  * \return value
71  */
72  double getusr(const std::string& key) const
73  {
74  const int i = idx( key );
75 
76  if ( i < 0 )
77  {
78  THROW(Exception, "No user data for key " << key << " in aanet object of type " << this -> ClassName());
79  }
80 
81  if ( unsigned(i) >= usr.size() )
82  {
83  THROW(Exception, "Warning: inconsistent user data " << i << " >= " << usr.size());
84  }
85 
86  return usr[i];
87  }
88 
89  /**
90  * Set user data item with given key.\n
91  *
92  * \param key key
93  * \param value value
94  */
95  void setusr(const std::string & key, double value )
96  {
97  int i = idx( key );
98  if (i < 0)
99  {
100  if ( usr.size() < usr_names.size() )
101  {
102  // this should not happen, but let's just add empty data
103  usr.resize( usr_names.size() );
104  }
105  else
106  {
107  // this is possible, add empty ("") names
108  usr_names.resize( usr.size() );
109  }
110 
111  usr_names.push_back( key );
112  usr.push_back( value );
113  }
114  else
115  {
116  usr[i] = value;
117  }
118  }
119 
120  /**
121  * Remove (first) user data item with given key.\n
122  *
123  * \param key key
124  * \return true if data have been removed; else false
125  */
126  bool delusr( const std::string& key )
127  {
128  int i = idx( key );
129  if ( i < 0 ) return false;
130 
131  usr.erase ( usr.begin() + i );
132  usr_names.erase( usr_names.begin() + i );
133  return true;
134  }
135 
136  /**
137  * Clear user data.
138  */
139  void clearusr()
140  {
141  usr.resize(0);
142  usr_names.resize(0);
143  }
144 
145  /**
146  * Print user data (i.e. list of all pairs of keys and values).
147  *
148  * \param out output stream
149  */
150  void printusr(std::ostream& out = std::cout )
151  {
152  unsigned n = std::max( usr.size(), usr_names.size() );
153 
154  for (unsigned i = 0; i < n ; i++)
155  {
156  std::string name = "(unnamed)";
157  if ( i < usr_names.size() && usr_names[i] != "" ) name = usr_names[i];
158  out << i << " \t " << name << " : \t ";
159  if ( i < usr.size() ) out << usr[i] << std::endl;
160  else out << "(none)" << std::endl;
161  }
162  }
163 
164  /**
165  * Default constructor.
166  */
167  AAObject() : any(NULL) {}
169 
170 
171  TObject* any; ///< Pointer to "any" user data.
172 
173  ClassDef(AAObject, 6)
174 };
175 
176 #endif
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
#define ClassDef(name, version)
Definition: JRoot.hh:32
General exception.
Definition: Exception.hh:13
const int n
Definition: JPolint.hh:786
data_type r[M+1]
Definition: JPolint.hh:868
AAObject is a base class for I/O-classes that adds the possibility to add 'user' information which wi...
Definition: AAObject.hh:19
bool haveusr(const std::string &key) const
Check availability of user data of the item with given key.
Definition: AAObject.hh:42
~AAObject()
Definition: AAObject.hh:168
void clearusr()
Clear user data.
Definition: AAObject.hh:139
TObject * any
Pointer to "any" user data.
Definition: AAObject.hh:171
bool delusr(const std::string &key)
Remove (first) user data item with given key.
Definition: AAObject.hh:126
int idx(const std::string &key) const
Get index in user data of the item with given key.
Definition: AAObject.hh:29
AAObject()
Default constructor.
Definition: AAObject.hh:167
void printusr(std::ostream &out=std::cout)
Print user data (i.e.
Definition: AAObject.hh:150
void setusr(const std::string &key, double value)
Set user data item with given key.
Definition: AAObject.hh:95
double getusr(const std::string &key) const
Get user data item with given key.
Definition: AAObject.hh:72
std::vector< std::string > usr_names
user keys
Definition: AAObject.hh:21
int idxusr_checked(const std::string &key) const
Get index in user data of the item with given key.
Definition: AAObject.hh:54
std::vector< double > usr
user data
Definition: AAObject.hh:20
Definition: JRoot.hh:19