LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Component.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
35 #include <map>
36 #include <boost/tokenizer.hpp>
37 
39 
40 using namespace lsst::pex::logging;
41 
42 /*****************************************************************************/
45 Component::Component(const std::string &name,
46  int verbosity
47  ) : _name(new std::string(name)),
48  _verbosity(verbosity),
49  _subcomp(*new(comp_map)) {
50 }
51 
55  // Note from rlp: memory leak! map members not deleted.
56  delete &_subcomp;
57  delete _name;
58 }
59 
62 void Component::add(const std::string &name,
63  int verbosity,
64  const std::string &separator
65  ) {
66  //
67  // Prepare to parse name
68  //
69  boost::char_separator<char> sep(separator.c_str());
70  tokenizer components(name, sep);
71  tokenizer::iterator token = components.begin();
72  const tokenizer::iterator end = components.end();
73 
74  if (token == end) { // "" or "."
75  this->setVerbosity(verbosity);
76  } else {
77  add(token, end, verbosity);
78  }
79 }
80 
83 void Component::add(tokenizer::iterator token,
84  const tokenizer::iterator end,
85  int verbosity
86  ) {
87  const std::string cpt0 = *token++; // first component of name
88  /*
89  * Does first part of path match this verbosity?
90  */
91  if (*_name == cpt0) { // a match
92  if (token == end) { // name has no more components
93  _verbosity = verbosity;
94  } else {
95  add(token, end, verbosity);
96  }
97 
98  return;
99  }
100  /*
101  * Look for a match for cpt0 in this verbosity's subcomps
102  */
103  comp_map::iterator iter = _subcomp.find(cpt0);
104  if (iter != _subcomp.end()) {
105  if (token == end) {
106  iter->second->_verbosity = verbosity;
107  } else {
108  iter->second->add(token, end, verbosity);
109  }
110 
111  return;
112  }
113  /*
114  * No match; add cpt0 to this verbosity
115  */
116  Component *fcpt0 = new Component(cpt0);
117  _subcomp[*fcpt0->_name] = fcpt0;
118 
119  if (token == end) {
120  fcpt0->_verbosity = verbosity;
121  } else {
122  fcpt0->add(token, end, verbosity);
123  }
124 }
125 
126 /*****************************************************************************/
130  ) {
131  if (_verbosity > highest) {
132  highest = _verbosity;
133  }
134 
135  for (comp_map::iterator iter = _subcomp.begin();
136  iter != _subcomp.end(); iter++) {
137  highest = iter->second->highestVerbosity(highest);
138  }
139 
140  return highest;
141 }
142 
143 /*****************************************************************************/
146 int Component::getVerbosity(tokenizer::iterator token,
147  const tokenizer::iterator end,
148  int defaultVerbosity
149  ) {
150  const std::string cpt0 = *token++; // first component of name
151  /*
152  * Look for a match for cpt0 in this Component's subcomps
153  */
154  comp_map::iterator iter = _subcomp.find(cpt0);
155  if (iter != _subcomp.end()) {
156  int verbosity = iter->second->_verbosity;
157  if (verbosity == INHERIT_VERBOSITY) {
158  verbosity = defaultVerbosity;
159  }
160 
161  if (token == end) { // there was only one component
162  ; // so save the function call
163  } else {
164  verbosity = iter->second->getVerbosity(token, end, verbosity);
165  }
166 
167  return (verbosity == INHERIT_VERBOSITY) ? defaultVerbosity : verbosity;
168  }
169  /*
170  * No match. This is as far as she goes
171  */
172  return _verbosity;
173 }
174 
179 int Component::getVerbosity(const std::string &name,
180  const std::string &separator
181  ) {
182  //
183  // Prepare to parse name
184  //
185  boost::char_separator<char> sep(separator.c_str());
186  tokenizer components(name, sep);
187  tokenizer::iterator token = components.begin();
188 
189  if (token == components.end()) {
190  return _verbosity;
191  }
192 
193  return getVerbosity(token, components.end(), _verbosity);
194 }
195 
198 void Component::printVerbosity(std::ostream &fp,
199  int depth
200  ) {
201  if (_subcomp.empty() && _verbosity == INHERIT_VERBOSITY) {
202  return;
203  }
204  //
205  // Print this verbosity
206  //
207  for (int i = 0; i < depth; i++) {
208  fp << ' ';
209  }
210 
211  std::string &name = *_name;
212  if (name.length() == 0) name = "(root)";
213  fp << name;
214  for (int i = 0; i < 20 - depth - static_cast<int>(name.size()); i++) {
215  fp << ' ';
216  }
217 
218  if (_verbosity != INHERIT_VERBOSITY) {
219  fp << _verbosity;
220  }
221  fp << "\n";
222  //
223  // And other levels of the hierarchy too
224  //
225  for (comp_map::iterator iter = _subcomp.begin();
226  iter != _subcomp.end(); iter++) {
227  iter->second->printVerbosity(fp, depth + 1);
228  }
229 }
230 
int iter
int highestVerbosity(int highest=0)
Definition: Component.cc:129
table::Key< std::string > name
Definition: ApCorrMap.cc:71
std::string const & _name
Definition: Mask.cc:678
int getVerbosity(const std::string &name, const std::string &separator)
Definition: Component.cc:179
boost::tokenizer< boost::char_separator< char > > tokenizer
Definition: Component.h:78
Create a component in the verbosity tree (deprecated).
Definition: Component.h:62
void add(const std::string &name, int verbosity, const std::string &separator)
Definition: Component.cc:62
void setVerbosity(int verbosity)
Definition: Component.h:75
void printVerbosity(std::ostream &fp, int depth=0)
Definition: Component.cc:198
std::map< const std::string, Component * > comp_map
Definition: Component.h:79
Defines the (deprecated) Component class.
Component(const std::string &name=".", int verbosity=INHERIT_VERBOSITY)
Definition: Component.cc:45
table::Key< table::Array< int > > components