StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StDbFieldI.h
1 /*
2  * StDbFieldI.h
3  *
4  * Created on Aug 20, 2007
5  *
6  * Author: Zhengqiang Liang (Wayne State University)
7  * Valeri Fine (Brookhaven National Laboratory)
8  * Jerome Lauret (Brookhaven National Laboratory)
9  *
10  *
11  * This file is part of the UCM project funded under an SBIR
12  * Copyright (c) 2007-2008 STAR Collaboration - Brookhaven National Laboratory
13  *
14  * @(#)cpp/api:$Id: StDbFieldI.h,v 1.4 2010/03/30 20:05:36 fine Exp $
15  *
16  *
17  *
18  * This is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * (at your option) any later version.
22  *
23  * STAR Scheduler is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with STAR Scheduler; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  */
32 
33 #ifndef STDBFIELD_H
34 #define STDBFIELD_H
35 
36 #include <string>
37 #include <sstream>
38 #include <map>
39 #include <typeinfo>
40 
41 #include "StDataException.h"
42 #include "FieldList.h"
43 
47 namespace TxLogging {
48 class Init_StDbFieldI;
49 
50 class StDbFieldI {
51  friend class TxLogging::Init_StDbFieldI;
52  public:
60  enum EDataType {
61  kINVALID=0,
62  kBOOL,
63  kINT,
64  kUINT,
65  kLONG,
66  kULONG,
67  kDOUBLE,
68  kCHAR,
69  kUNIXTIME,
70  kSTRING,
71  kEND
72  };
73 
74  public:
75 
83  StDbFieldI(const char* name, StDbFieldI::EDataType type, int length=0);
84 
102  // StDbFieldI(const char* name, void *value, int length=0);
103 
113  StDbFieldI(const char* name, const char* value,
114  StDbFieldI::EDataType type, int length=0);
115 
119  StDbFieldI(const StDbFieldI& );
120  const StDbFieldI& operator=(const StDbFieldI& );
121 
125  virtual ~StDbFieldI();
126 
130  const char* getName() const;
131 
136  const char* fieldAsString() const;
137 
146  void setValue(const int &value);
147  void setValue(const unsigned int &value);
148  void setValue(const long &value);
149  void setValue(const unsigned long &value);
150  void setValue(const double &value);
151  void setValue(const char &value);
152  // void setValue(const std::string &value);
153 
154  int toInt() const;
155  unsigned toUInt() const;
156  long toLong() const;
157  unsigned long toULong() const;
158  double toDouble() const;
159  char toChar() const;
160  // const std::string toString() const;
168  void setValueFromString(const char* strValue);
169 
174  EDataType getType() const;
175 
181  int getMaxLength() const;
182 
187  const char*getTypeAsString() const;
188 
195  const char*getValueAsString() const;
196 
200  bool isNull() const;
201 
207  void setNull(bool Nil);
208 
216  void setIgnore(bool ignore);
217 
222  bool isIgnore() const;
223 
224  protected:
225  template<class T>
226  StDbFieldI(const char* name, const T &value, int length);
227 
228 
229  template <class T>
230  void setValue(const T &value);
231 
238  template <class T>
239  T toValue() const;
240 
241  private:
246  void copy(const StDbFieldI &f);
254  template<class T> void typeMatches() const;
255 
256  private:
257  static void MakeTypeMap();
258  std::string fName;
259  EDataType fType;
260  std::string fEncodedValue;
261  size_t fMaxLength;
262  bool fNull;
263  bool fIgnore;
264  static std::map<EDataType,std::string> fTypeMap;
265  static std::map<EDataType,std::string> fTypeMapName;
266  static std::map<std::string,EDataType> fTypeMapInv;
267 };
268 
269 
270 //---------------
271 // BEGIN TEMPLATE FUNCTION DEFINITIONS:
272 // Note that this is required by the gcc linker to properly link templated
273 // functions.
274 //---------------
275 
276 //_______________________________________________________________________________
277 template<class T> StDbFieldI::StDbFieldI(const char* name, const T &value, int length)
278  : fType(kINVALID),
279  fNull(true),
280  fEncodedValue(""),
281  fMaxLength(length),
282  fIgnore(true),
283  fName(name)
284 {
285  if (std::string(name).empty()) {
286  throw StDataException("Attempted to define field with empty name.",
287  StDataException::FIELD,
288  StUCMException::ERROR);
289  }
290  setValue(value);
291 }
292 
293 
294 //_______________________________________________________________________________
295 template <class T> void
296 StDbFieldI::setValue(const T &value)
297 {
298  typeMatches<T>();
299 
300  // Convert value to string
301  std::ostringstream encodingStream;
302  encodingStream << value;
303 
304  // If we have a string type..
305  if ( fType == kSTRING ) {
306  if ( encodingStream.str().length() > fMaxLength ) {
307  std::ostringstream lenStr;
308  lenStr << fMaxLength;
309 
310  // Conversion to std::string is safe, but potentially redundant
311  throw StDataException("Invalid value for field '" + fName
312  + "': Length of string '" + encodingStream.str()
313  + "' is longer than variant length '"
314  + lenStr.str() + "'.",
315  StDataException::FIELD,
316  StUCMException::ERROR);
317  }
318  }
319 
320  fEncodedValue = encodingStream.str();
321  fNull = false;
322  fIgnore = false;
323 }
324 
325 
326 //_______________________________________________________________________________
327 template<class T>
329 {
330  typeMatches<T>();
331 
332  std::istringstream decodingStream(fEncodedValue);
333  T realValue;
334 
335  decodingStream >> realValue;
336  return realValue;
337 }
338 
339 //_______________________________________________________________________________
340 template<class T> void
341 StDbFieldI::typeMatches() const
342 {
343  // Massive check: If the provided type T does not match the stored
344  // data type, throw an exception.
345  std::map<EDataType,std::string>::const_iterator t = fTypeMap.find(getType());
346 
347  if (t == fTypeMap.end()) {
348  throw StDataException( "Type mismatch in field '" + fName + "': Actual type "
349  + getTypeAsString() + std::string(", requested type ")
350  + typeid(T).name(),
351  StDataException::FIELD,
352  StUCMException::ERROR);
353  }
354 }
355 }
356 #endif
EDataType getType() const
Definition: StDbFieldI.cxx:205
virtual ~StDbFieldI()
Deconstructor.
Definition: StDbFieldI.cxx:141
StDbFieldI(const char *name, StDbFieldI::EDataType type, int length=0)
Definition: StDbFieldI.cxx:52
void setNull(bool Nil)
Definition: StDbFieldI.cxx:228
void setIgnore(bool ignore)
Definition: StDbFieldI.cxx:262
bool isIgnore() const
Definition: StDbFieldI.cxx:256
const char * getTypeAsString() const
Definition: StDbFieldI.cxx:241
const char * fieldAsString() const
Definition: StDbFieldI.cxx:173
const char * getName() const
Definition: StDbFieldI.cxx:166
const char * getValueAsString() const
Definition: StDbFieldI.cxx:213
void setValue(const int &value)
void setValueFromString(const char *strValue)
Definition: StDbFieldI.cxx:185
bool isNull() const
Definition: StDbFieldI.cxx:220
int getMaxLength() const
Definition: StDbFieldI.cxx:249