LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
Memory.h
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 
30 #ifndef LSST_PEX_LOGGING_THRESHOLD_MEMORY_H
31 #define LSST_PEX_LOGGING_THRESHOLD_MEMORY_H
32 
33 #include <string>
34 #include <map>
35 #include <ostream>
36 #include <boost/shared_ptr.hpp>
37 #include <boost/tokenizer.hpp>
38 
40 
41 namespace lsst {
42 namespace pex {
43 namespace logging {
44 namespace threshold {
45 
46 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
47 
59 class Family {
60 public:
61 
66  Family(int defaultThreshold=INHERIT);
67 
71  ~Family();
72 
76  int getThreshold() { return _thresh; }
77 
81  void setThreshold(int threshold) { _thresh = threshold; }
82 
86  int getThresholdFor(tokenizer::iterator toptoken,
87  const tokenizer::iterator& end) const
88  {
89  const Family *nfd = findDescendant(toptoken, end, true);
90  if (nfd == 0 || nfd->_thresh == INHERIT) return _thresh;
91  return nfd->_thresh;
92  }
93 
97  void setThresholdFor(tokenizer::iterator toptoken,
98  const tokenizer::iterator& end,
99  int threshold);
100 
105  void resetThresholdFor(tokenizer::iterator toptoken,
106  const tokenizer::iterator& end);
107 
113  void printDescThresholds(std::ostream& out,
114  const std::string& prefix) const;
115 
119  void deleteDescendants();
120 
121 protected:
122 
130  Family *ensureDescendant(tokenizer::iterator toptoken,
131  const tokenizer::iterator& end);
132 
145  const Family *findDescendant(tokenizer::iterator toptoken,
146  const tokenizer::iterator& end,
147  bool orNearest=false) const;
148 
155  Family *makeDescendants(tokenizer::iterator toptoken,
156  const tokenizer::iterator& end);
157 
158 private:
159  typedef std::map<std::string, Family*> ChildMap;
160  int _thresh;
162 };
163 
171 class Memory {
172 public:
173 
174  Memory(const std::string& delims=".");
175 
179  int getThresholdFor(const std::string& name) {
180  if (name.length() == 0) return getRootThreshold();
181  tokenizer fields(name, _sep);
182  return _tree.getThresholdFor(fields.begin(), fields.end());
183  }
184 
188  void setThresholdFor(const std::string& name, int threshold) {
189  if (name.length() == 0) {
190  setRootThreshold(threshold);
191  }
192  else {
193  tokenizer fields(name, _sep);
194  _tree.setThresholdFor(fields.begin(), fields.end(), threshold);
195  }
196  }
197 
202  int getRootThreshold() { return _tree.getThreshold(); }
203 
208  void setRootThreshold(int threshold) { _tree.setThreshold(threshold); }
209 
214 
218  void printThresholds(std::ostream& out);
219 
220 
221 private:
223  boost::char_separator<char> _sep;
224 };
225 
226 }}}} // end lsst::pex::logging::threshold
227 
228 #endif // end LSST_PEX_LOGGING_THRESHOLD_MEMORY_H
Memory(const std::string &delims=".")
Definition: Memory.cc:171
void setThresholdFor(tokenizer::iterator toptoken, const tokenizer::iterator &end, int threshold)
Definition: Memory.cc:108
void setRootThreshold(int threshold)
Definition: Memory.h:208
std::map< std::string, Family * > ChildMap
Definition: Memory.h:159
table::Key< std::string > name
Definition: ApCorrMap.cc:71
int getThresholdFor(tokenizer::iterator toptoken, const tokenizer::iterator &end) const
Definition: Memory.h:86
void printDescThresholds(std::ostream &out, const std::string &prefix) const
Definition: Memory.cc:146
Family(int defaultThreshold=INHERIT)
Definition: Memory.cc:44
void setThreshold(int threshold)
Definition: Memory.h:81
definitions of reusable enumerations
void printThresholds(std::ostream &out)
Definition: Memory.cc:178
boost::tokenizer< boost::char_separator< char > > tokenizer
Definition: Memory.h:46
Family * makeDescendants(tokenizer::iterator toptoken, const tokenizer::iterator &end)
Definition: Memory.cc:92
Family * ensureDescendant(tokenizer::iterator toptoken, const tokenizer::iterator &end)
Definition: Memory.cc:48
void setThresholdFor(const std::string &name, int threshold)
Definition: Memory.h:188
void resetThresholdFor(tokenizer::iterator toptoken, const tokenizer::iterator &end)
Definition: Memory.cc:120
A hierarchical tree structure for holding mappings of names to threshold values.
Definition: Memory.h:59
int getThresholdFor(const std::string &name)
Definition: Memory.h:179
boost::char_separator< char > _sep
Definition: Memory.h:223
const Family * findDescendant(tokenizer::iterator toptoken, const tokenizer::iterator &end, bool orNearest=false) const
Definition: Memory.cc:70