All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PSet.h
Go to the documentation of this file.
1 /**
2  * \file PSet.h
3  *
4  * \ingroup Base
5  *
6  * \brief Class def header for a class flashmatch::PSet
7  *
8  * @author kazuhiro
9  */
10 
11 /** \addtogroup core_Base
12 
13  @{*/
14 #ifndef __CVFHICL_PSET_H__
15 #define __CVFHICL_PSET_H__
16 
17 #include <iostream>
18 #include <string>
19 #include <map>
20 #include "Parser.h"
21 namespace flashmatch {
22  /**
23  \class PSet
24  \brief A nested configuration parameter set holder for flashmatch framework.
25  */
26  class PSet {
27 
28  public:
29 
30  /// Default constructor
31  PSet(const std::string name="",
32  const std::string data="");
33 
34  /// Default destructor
35  virtual ~PSet(){};
36 
37  /// Copy ctor
38  PSet(const PSet& orig) : _name ( orig._name )
39  , _data_value ( orig._data_value )
40  , _data_pset ( orig._data_pset )
41  {}
42 
43  /// name getter
44  inline const std::string& name() const { return _name; }
45 
46  /// operator override
47  inline bool operator==(const PSet& rhs) const
48  {
49  if(_name != rhs.name()) return false;
50  auto const v_keys = this->value_keys();
51  if(v_keys.size() != rhs.value_keys().size()) return false;
52  for(auto const& key : v_keys) {
53  if(!rhs.contains_value(key))
54  return false;
55  if(this->get<std::string>(key) != rhs.get<std::string>(key))
56  return false;
57  }
58  auto const p_keys = this->pset_keys();
59  if(p_keys.size() != rhs.pset_keys().size()) return false;
60  for(auto const& key : p_keys) {
61  if(!rhs.contains_pset(key))
62  return false;
63  if(this->get_pset(key) != rhs.get_pset(key))
64  return false;
65  }
66  return true;
67  }
68 
69  inline bool operator!=(const PSet& rhs) const
70  { return !((*this) == rhs); }
71 
72  /// rename method
73  inline void rename(std::string name) { _name = name; }
74 
75  /// clear method
76  inline void clear()
77  { _data_value.clear(); _data_pset.clear(); }
78 
79  /// Set data contents
80  void add(const std::string& data);
81 
82  /// Insert method for a simple param
83  void add_value(std::string key, std::string value);
84 
85  /// Insert method for a PSet rep
86  void add_pset(const PSet& p);
87 
88  /// Insert method for a PSet rep
89  void add_pset(std::string key,
90  std::string pset);
91 
92  /// Dump into a text format
93  std::string dump(size_t indent_size=0) const;
94 
95  /// Dump data string
96  std::string data_string() const;
97 
98  /// Template getter
99  template <class T>
100  T get(const std::string& key) const{
101  auto iter = _data_value.find(key);
102  if( iter == _data_value.end() ) {
103  std::string msg;
104  msg = "Key does not exist: \"" + key + "\"";
105  std::cout<<dump()<<std::endl;
106  std::cerr<<msg<<std::endl;
107  throw std::exception();
108  }
109  return parser::FromString<T>((*iter).second);
110  }
111 
112  /// Template getter w/ default value
113  template <class T>
114  T get(const std::string& key, const T default_value) const{
115  auto iter = _data_value.find(key);
116  if( iter == _data_value.end() )
117  return default_value;
118  return parser::FromString<T>((*iter).second);
119  }
120 
121  /// None-template function to retrieve parameter set (deprecated)
122  const PSet& get_pset(const std::string& key) const;
123 
124  /// Returns # of parameters
125  size_t size() const;
126  /// Returns a vector of all parameter keys
127  const std::vector<std::string> keys() const;
128  /// Returns a vector of keys for key-value pairs
129  const std::vector<std::string> value_keys () const;
130  /// Returns a vector of keys for key-PSet pairs
131  const std::vector<std::string> pset_keys () const;
132  /// Check if a specified key exists for key-value pairs
133  bool contains_value (const std::string& key) const;
134  /// Check if a specified key exists for key-PSet pairs
135  bool contains_pset (const std::string& key) const;
136 
137 
138 
139  private:
140 
141  enum KeyChar_t {
147  };
148 
149  std::pair<PSet::KeyChar_t,size_t> search(const std::string& txt, const size_t start) const;
150  void strip(std::string& str, const std::string& key);
151  void rstrip(std::string& str, const std::string& key);
152  void trim_space(std::string& txt);
153  void no_space(std::string& txt);
154 
155  /// The name of this flashmatch::PSet
156  std::string _name;
157  /// Key-Value pairs
158  std::map<std::string,std::string> _data_value;
159  /// Key-PSet pairs
160  std::map<std::string,::flashmatch::PSet> _data_pset;
161 
162  };
163 
164  template<> PSet PSet::get<flashmatch::PSet>(const std::string& key) const;
165 
166 }
167 
168 #endif
169 /** @} */ // end of doxygen group
170 
bool contains_pset(const std::string &key) const
Check if a specified key exists for key-PSet pairs.
Definition: PSet.cxx:49
std::map< std::string, std::string > _data_value
Key-Value pairs.
Definition: PSet.h:158
bool operator!=(const PSet &rhs) const
Definition: PSet.h:69
const std::vector< std::string > pset_keys() const
Returns a vector of keys for key-PSet pairs.
Definition: PSet.cxx:38
BEGIN_PROLOG could also be cerr
void no_space(std::string &txt)
Definition: PSet.cxx:81
void add_pset(const PSet &p)
Insert method for a PSet rep.
Definition: PSet.cxx:147
pdgs p
Definition: selectors.fcl:22
const std::vector< std::string > keys() const
Returns a vector of all parameter keys.
Definition: PSet.cxx:22
void add_value(std::string key, std::string value)
Insert method for a simple param.
Definition: PSet.cxx:127
List of functions to type-cast std::string to an appropriate value type.
void rename(std::string name)
rename method
Definition: PSet.h:73
virtual ~PSet()
Default destructor.
Definition: PSet.h:35
const std::string & name() const
name getter
Definition: PSet.h:44
std::map< std::string,::flashmatch::PSet > _data_pset
Key-PSet pairs.
Definition: PSet.h:160
PSet(const PSet &orig)
Copy ctor.
Definition: PSet.h:38
const std::vector< std::string > value_keys() const
Returns a vector of keys for key-value pairs.
Definition: PSet.cxx:31
T get(const std::string &key) const
Template getter.
Definition: PSet.h:100
void rstrip(std::string &str, const std::string &key)
Definition: PSet.cxx:61
void trim_space(std::string &txt)
Definition: PSet.cxx:72
std::string _name
The name of this flashmatch::PSet.
Definition: PSet.h:156
bool contains_value(const std::string &key) const
Check if a specified key exists for key-value pairs.
Definition: PSet.cxx:45
std::string data_string() const
Dump data string.
Definition: PSet.cxx:359
std::string dump(size_t indent_size=0) const
Dump into a text format.
Definition: PSet.cxx:340
void add(const std::string &data)
Set data contents.
Definition: PSet.cxx:177
bool operator==(const PSet &rhs) const
operator override
Definition: PSet.h:47
std::pair< PSet::KeyChar_t, size_t > search(const std::string &txt, const size_t start) const
Definition: PSet.cxx:94
PSet(const std::string name="", const std::string data="")
Default constructor.
Definition: PSet.cxx:8
temporary value
void clear()
clear method
Definition: PSet.h:76
size_t size() const
Returns # of parameters.
Definition: PSet.cxx:19
void strip(std::string &str, const std::string &key)
Definition: PSet.cxx:54
const PSet & get_pset(const std::string &key) const
None-template function to retrieve parameter set (deprecated)
Definition: PSet.cxx:376
BEGIN_PROLOG could also be cout
A nested configuration parameter set holder for flashmatch framework.
Definition: PSet.h:26