xc
FactorsMap.h
1 // -*-c++-*-
2 //----------------------------------------------------------------------------
3 // xc utils library; general purpose classes and functions.
4 //
5 // Copyright (C) Luis C. PĂ©rez Tato
6 //
7 // XC utils is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // This software is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program.
19 // If not, see <http://www.gnu.org/licenses/>.
20 //----------------------------------------------------------------------------
21 //FactorsMap.h
22 //Contenedor de coeficientes de simultaneidad de acciones.
23 
24 #ifndef FACTORSMAP_H
25 #define FACTORSMAP_H
26 
27 #include "utility/kernel/CommandEntity.h"
28 #include <map>
29 #include "utility/utils/misc_utils/colormod.h"
30 
31 
32 namespace cmb_acc {
33 
34 class LoadCombinationVector;
35 
37 //
39 template <class Factors>
40 class FactorsMap: public CommandEntity, public std::map<std::string,Factors>
41  {
42  public:
43  typedef typename std::map<std::string,Factors>::const_iterator const_iterator;
44  typedef typename std::map<std::string,Factors>::iterator iterator;
45  private:
46  static Factors default_factors;
47  bool exists(const std::string &) const;
48  Factors *getPtrCoefs(const std::string &);
49  Factors *crea_coefs(const std::string &);
50  void print_err_not_found(const std::string &, const std::string &) const;
51  public:
52  FactorsMap(void);
53  const Factors *getPtrCoefs(const std::string &) const;
54  const Factors &BuscaCoefs(const std::string &) const;
55  void insert(const std::string &,const Factors &);
56  std::deque<std::string> getNames(void) const;
57  boost::python::list getKeys(void) const;
58  };
59 
60 template <class Factors>
62 
64 template <class Factors>
65 bool FactorsMap<Factors>::exists(const std::string &name) const
66  { return (this->find(name)!=this->end()); }
67 
69 template <class Factors>
70 void FactorsMap<Factors>::print_err_not_found(const std::string &functionName, const std::string &name) const
71  {
72  std::cerr << Color::red << getClassName() << "::" << functionName
73  << "; factors with name: '"
74  << name << "' not found." << std::endl
75  << " candidates are: ";
76  const std::deque<std::string> candidates= this->getNames();
77  if(!candidates.empty())
78  {
79  std::deque<std::string>::const_iterator i= candidates.begin();
80  std::cerr << *i;
81  i++;
82  for(;i!=candidates.end();i++)
83  std::cerr << ", " << *i;
84  std::cerr << Color::def << std::endl;
85  }
86  }
87 
89 template <class Factors>
90 std::deque<std::string> FactorsMap<Factors>::getNames(void) const
91  {
92  std::deque<std::string> retval;
93  for(const_iterator i= this->begin();i!= this->end();i++)
94  retval.push_back(i->first);
95  return retval;
96  }
97 
99 template <class Factors>
100 boost::python::list FactorsMap<Factors>::getKeys(void) const
101  {
102  boost::python::list retval;
103  for(const_iterator i=this->begin();i!=this->end();i++)
104  retval.append((*i).first);
105  return retval;
106  }
107 
110 template <class Factors>
111 Factors *FactorsMap<Factors>::getPtrCoefs(const std::string &name)
112  {
113  Factors *retval= nullptr;
114  if(this->exists(name))
115  retval= &((*this)[name]);
116  else
117  this->print_err_not_found(__FUNCTION__,name);
118  return retval;
119  }
120 
122 template <class Factors>
123 const Factors &FactorsMap<Factors>::BuscaCoefs(const std::string &name) const
124  {
125  if(this->exists(name))
126  return this->find(name)->second;
127  else
128  {
129  this->print_err_not_found(__FUNCTION__,name);
130  return default_factors;
131  }
132  }
133 
135 template <class Factors>
136 const Factors *FactorsMap<Factors>::getPtrCoefs(const std::string &name) const
137  {
138  const Factors *retval= nullptr;
139  if(this->exists(name))
140  retval= &(this->find(name)->second);
141  else
142  this->print_err_not_found(__FUNCTION__,name);
143  return retval;
144  }
145 
147 template <class Factors>
149  : CommandEntity() {}
150 
152 template <class Factors>
153 Factors *FactorsMap<Factors>::crea_coefs(const std::string &name)
154  {
155  Factors *retval= nullptr;
156  if(this->exists(name))
157  retval= &(this->find(name)->second);
158  else //los coeficientes son nuevos.
159  {
160  Factors tmp;
161  (*this)[name]= tmp;
162  retval= getPtrCoefs(name);
163  }
164  return retval;
165  }
166 
170 template <class Factors>
171 void FactorsMap<Factors>::insert(const std::string &name,const Factors &c)
172  { (*this)[name]= c; }
173 
174 } // fin namespace cmb_acc
175 
176 #endif
Routines to generate combinations of actions.
Almacena todas las familias de acciones.
Definition: Factors.h:34
Combination factor container.
Definition: FactorsMap.h:40
FactorsMap(void)
Default constructor.
Definition: FactorsMap.h:148
boost::python::list getKeys(void) const
Return the names of the factor families.
Definition: FactorsMap.h:100
virtual std::string getClassName(void) const
Returns demangled class name.
Definition: EntityWithOwner.cc:90
Objet that can execute python scripts.
Definition: CommandEntity.h:40
const Factors & BuscaCoefs(const std::string &) const
Return the factors identified by name.
Definition: FactorsMap.h:123
std::deque< std::string > getNames(void) const
Return the names of the factor families.
Definition: FactorsMap.h:90
void insert(const std::string &, const Factors &)
Inserts the coefficients.
Definition: FactorsMap.h:171