ROOT  6.06/09
Reference Guide
RooStringVar.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * @(#)root/roofitcore:$Id$
5  * Authors: *
6  * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7  * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8  * *
9  * Copyright (c) 2000-2005, Regents of the University of California *
10  * and Stanford University. All rights reserved. *
11  * *
12  * Redistribution and use in source and binary forms, *
13  * with or without modification, are permitted according to the terms *
14  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15  *****************************************************************************/
16 
17 //////////////////////////////////////////////////////////////////////////////
18 //
19 // BEGIN_HTML
20 // RooStringVar implements a string values RooAbsArg
21 // END_HTML
22 //
23 
24 #include "RooFit.h"
25 #include "Riostream.h"
26 
27 #include <math.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include "TObjString.h"
32 #include "TTree.h"
33 #include "RooStringVar.h"
34 #include "RooStreamParser.h"
35 #include "RooMsgService.h"
36 
37 
38 
39 using namespace std;
40 
42 
43 
44 
45 
46 ////////////////////////////////////////////////////////////////////////////////
47 /// Constructor with initial value and internal buffer size
48 
49 RooStringVar::RooStringVar(const char *name, const char *title, const char* value, Int_t size) :
50  RooAbsString(name, title, size)
51 {
52  if(!isValidString(value)) {
53  coutW(InputArguments) << "RooStringVar::RooStringVar(" << GetName()
54  << "): initial contents too long and ignored" << endl ;
55  } else {
56  strlcpy(_value,value,_len) ;
57  }
58 
59  setValueDirty() ;
60  setShapeDirty() ;
61 }
62 
63 
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 /// Copy constructor
67 
68 RooStringVar::RooStringVar(const RooStringVar& other, const char* name) :
69  RooAbsString(other, name)
70 {
71 }
72 
73 
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// Destructor
77 
79 {
80 }
81 
82 
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 
86 RooStringVar::operator TString()
87 {
88  // Cast operator to TString
89  return TString(_value) ;
90 }
91 
92 
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// Set value to given TString
96 
97 void RooStringVar::setVal(const char* value)
98 {
99  if (!isValidString(value)) {
100  coutW(InputArguments) << "RooStringVar::setVal(" << GetName() << "): new string too long and ignored" << endl ;
101  } else {
102  if (value) {
103  strlcpy(_value,value,_len) ;
104  } else {
105  _value[0] = 0 ;
106  }
107  }
108 }
109 
110 
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 /// Set value to given TString
114 
115 RooAbsArg& RooStringVar::operator=(const char* newValue)
116 {
117  if (!isValidString(newValue)) {
118  coutW(InputArguments) << "RooStringVar::operator=(" << GetName() << "): new string too long and ignored" << endl ;
119  } else {
120  if (newValue) {
121  strlcpy(_value,newValue,_len) ;
122  } else {
123  _value[0] = 0 ;
124  }
125  }
126 
127  return *this ;
128 }
129 
130 
131 
132 ////////////////////////////////////////////////////////////////////////////////
133 /// Read object contents from given stream
134 
136 {
137  TString token,errorPrefix("RooStringVar::readFromStream(") ;
138  errorPrefix.Append(GetName()) ;
139  errorPrefix.Append(")") ;
140  RooStreamParser parser(is,errorPrefix) ;
141 
142  TString newValue ;
143  Bool_t ret(kFALSE) ;
144 
145  if (compact) {
146  parser.readString(newValue,kTRUE) ;
147  } else {
148  newValue = parser.readLine() ;
149  }
150 
151  if (!isValidString(newValue)) {
152  if (verbose)
153  coutW(InputArguments) << "RooStringVar::readFromStream(" << GetName()
154  << "): new string too long and ignored" << endl ;
155  } else {
156  strlcpy(_value,newValue,_len) ;
157  }
158 
159  return ret ;
160 }
161 
162 
163 ////////////////////////////////////////////////////////////////////////////////
164 /// Write object contents to given stream
165 
166 void RooStringVar::writeToStream(ostream& os, Bool_t /*compact*/) const
167 {
168  os << getVal() ;
169 }
170 
171 
virtual Bool_t isValidString(const char *, Bool_t printError=kFALSE) const
Check if given string value is valid.
virtual void setVal(const char *newVal)
Set value to given TString.
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
STL namespace.
#define coutW(a)
Definition: RooMsgService.h:34
TString & Append(const char *cs)
Definition: TString.h:492
ClassImp(RooStringVar) RooStringVar
Constructor with initial value and internal buffer size.
virtual RooAbsArg & operator=(const char *newValue)
Set value to given TString.
char * _value
Definition: RooAbsString.h:68
bool verbose
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
TString readLine()
Read an entire line from the stream and return as TString This method recognizes the use of '\' in th...
Bool_t readString(TString &value, Bool_t zapOnError=kFALSE)
Read a string token.
virtual ~RooStringVar()
Destructor.
virtual const char * getVal() const
Return value of object. Calculated if dirty, otherwise cached value is returned.
Definition: RooStringVar.h:35
#define name(a, b)
Definition: linkTestLib0.cpp:5
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE)
Read object contents from given stream.
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
const Bool_t kTRUE
Definition: Rtypes.h:91
float value
Definition: math.cpp:443
virtual void writeToStream(std::ostream &os, Bool_t compact) const
Write object contents to given stream.