MINT2
NamedParameterBase.cpp
Go to the documentation of this file.
1 // author: Jonas Rademacker (Jonas.Rademacker@bristol.ac.uk)
2 // status: Mon 9 Feb 2009 19:17:56 GMT
3 
7 
8 using namespace std;
9 using namespace MINT;
10 
11 //ParsedParameterFile* NamedParameterBase::_ppF=0;
12 
13 bool NamedParameterBase::IveSaidItOnce=0;
14 
15 std::string NamedParameterBase::_defaultFile = ""; // if "", read default-stream
16 std::istream* NamedParameterBase::_defaultStreamPtr = &std::cin;
17 
18 
19 bool NamedParameterBase::setDefaultInputFile(const std::string& fname){
20  _defaultFile = fname;
21  return true;
22 }
23 
24 bool NamedParameterBase::setDefaultInputStream(std::istream& str){
25  _defaultFile="";
26  _defaultStreamPtr = &str;
27  return true;
28 }
29 
30 
31 
32 bool NamedParameterBase::reloadFile(const std::string& id){
33  bool success = true;
34  success |= (bool) getFlist()->reloadFile(id);
35  success |= read(id);
36  success |= setFromParsedFile();
37  return success;
38 }
39 
40 NamedParameterBase::NamedParameterBase(const std::string& name_in
41  , const char* fname
43  )
44  : INamedParameter()
45  , _name(name_in)
46  , _gotInitialised(false)
47  , _quiet(vb==NamedParameterBase::QUIET)
48 {
51  _name));
52  if(0 == fname){
53  if((! _quiet) && 0 == IveSaidItOnce){
54  IveSaidItOnce=true;
55  cout << "INFO in constructor of NamedParameterBase:"
56  << "\n > For parameter named: \"" << _name << "\""
57  << "\n > You did not provide a filename."
58  << "\n > I'll read from stdin. This is completely OK,"
59  << "\n > but if there is no input from stdin ending in EOF,"
60  << "\n > the program will stall after this message."
61  << "\n > If you find yourself in this situation, press Ctr-d."
62  << "\n > In future, run the program as ./program < inputFile.txt"
63  << "\n > even if inputFile.txt is just an empty dummy file."
64  << "\n > This message is only printed once."
65  << endl;
66  }
68  }else{
70  }
71 }
73  : INamedParameter()
74  , _name(other._name)
75  , _ppF(other._ppF)
76  , _gotInitialised(other._gotInitialised)
77  , _quiet(other._quiet)
78 {}
79 
81  if( (! _gotInitialised) && (! _quiet)){
82  cout << "WARNING in destructor of NamedParameterBase:"
83  << "\n > Hello. My name is \"" << _name << "\"."
84  << "\n > I'm being destroyed, and"
85  << "\n > I never even got initialised."
86  << "\n > Did you do that deliberatly?"
87  << endl;
88  }
89 }
90 
93 }
94 
96  // if(0 != _ppF) delete _ppF;
97  // above now managed by ParsedParameterFileList
98  _ppF = ppF_in;
99  return true;
100 }
101 
103  if(! ("" == _defaultFile)) return read(_defaultFile);
104  else return read(*_defaultStreamPtr);
105 }
106 bool NamedParameterBase::read(std::istream& is){
107  _ppF = getFlist()->getParsedFile(is);
108  if(0 == _ppF){
109  cout << "SERIOUS ERROR IN NamedParameterBase::read(std::istream& is)"
110  << "\n > call to ParsedParameterFileList::getParsedFile"
111  << "\n > return 0 pointer. This is bad. Let's see how long"
112  << "\n > I'll last..." << endl;
113  return false;
114  }
115  return true;
116 }
117 
118 bool NamedParameterBase::read(const std::string& fname){
119  _ppF = getFlist()->getParsedFile(fname);
120  if(0 == _ppF){
121  cout << "SERIOUS ERROR IN NamedParameterBase::read(const std::string&)"
122  << "\n > with string value = " << fname << "."
123  << "\n > Call to ParsedParameterFileList::getParsedFile"
124  << "\n > return 0 pointer. This is bad. Let's see how long"
125  << "\n > I'll last..." << endl;
126  return false;
127  }
128  return true;
129 }
130 
132  bool success=false;
133  if(0 == _ppF){
134  if((! _gotInitialised) && (! _quiet)){
135  cout << "WARNING in NamedParameterBase::setFromParsedFile():"
136  << "\n No ParsedParameterFile attached!." << endl;
137  }
138  return false;
139  }
140  const ParsedParameterLine& ppL =_ppF->find(name());
141  if(! ppL.isValid()){
142  /*if(! _quiet){
143  cout << "WARNING in NamedParameterBase::setFromParsedFile():"
144  << "\n > Can't find variable with name \"" << name() << "\""
145  << "\n > in attached ParsedParameterFile." << endl;
146  }*/
147  }else{
148  success = setFromParsedLine(ppL);
149  }
150  if(success) _gotInitialised = true;
151  return success;
152 }
153 
154 bool NamedParameterBase::setFromInitString(const std::string& str){
155  ParsedParameterLine ppl(str);
156  return this->setFromParsedLine(ppl);
157 }
158 bool NamedParameterBase::setFromInitStringNoName(const std::string& str_in){
159  std::string str = name() + "\t" + str_in;
160  ParsedParameterLine ppl(str);
161  return this->setFromParsedLine(ppl);
162 }
163 
164 std::stringstream& operator>>(std::stringstream& is
165  , MINT::NamedParameterBase& npb){
166  std::string str;
167  is >> str;
168  npb.setFromInitStringNoName(str);
169  return is;
170 }
171 
172 //
bool setPPF(ParsedParameterFile *ppF_in=0)
static std::istream * _defaultStreamPtr
virtual const std::string & name() const
static std::string removeTrailingBlanks(const std::string &s)
ParsedParameterFile * _ppF
NamedParameterStream & operator>>(NamedParameterStream &eam, std::string &ing)
static std::string _defaultFile
virtual bool setFromParsedLine(const ParsedParameterLine &line)=0
static ParsedParameterFileList * getFlist()
static std::string removeLeadingBlanks(const std::string &s)
const ParsedParameterLine & find(const std::string &name) const
static ParsedParameterFileList * getMe()
bool setFromInitString(const std::string &str)
bool setFromInitStringNoName(const std::string &str_in)
ParsedParameterFile * getParsedFile(const std::string &fname)
NamedParameterBase(const std::string &name_in="", const char *fname=0, VERBOSITY=VERBOSE)