G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsOptionList.h
Go to the documentation of this file.
1 
15 #pragma once
16 
18 #include <gsIO/gsXml.h>
19 
20 namespace gismo
21 {
22 
32 class GISMO_EXPORT gsOptionList
33 {
34 public:
35  typedef GISMO_COEFF_TYPE Real;
36 
40  std::string getString(const std::string & label) const;
42  const index_t & getInt (const std::string & label) const;
44  Real getReal (const std::string & label) const;
46  bool getSwitch(const std::string & label) const;
47 
51  std::vector<std::string> getMultiString(const std::string & gn) const;
53  std::vector<index_t> getMultiInt (const std::string & gn) const;
55  std::vector<Real> getMultiReal (const std::string & gn) const;
56 
60  std::string askString(const std::string & label, const std::string & value = "" ) const;
62  index_t askInt (const std::string & label, const index_t & value = 0 ) const;
64  Real askReal (const std::string & label, const Real & value = 0 ) const;
66  bool askSwitch(const std::string & label, const bool & value = false ) const;
67 
68  /*/// \brief Reads values of an option-group \a gn into a std::vector.
71  //std::vector<std::string> askMultiString(const std::string & gn, const std::vector<std::string> & values = std::vector<std::string>()) const;
73  //std::vector<index_t> askMultiInt (const std::string & gn, const std::vector<index_t> & values = std::vector<index_t>() ) const;
75  //std::vector<Real> askMultiReal (const std::string & gn, const std::vector<Real> & values = std::vector<Real>() ) const;*/
76 
80  void setString(const std::string & label, const std::string & value);
82  void setInt (const std::string & label, const index_t & value);
84  void setReal (const std::string & label, const Real & value);
86  void setSwitch(const std::string & label, const bool & value);
88  void toggleSwitch( const std::string & label );
89 
90  /*/// \brief Sets values of option-group \a gn from values of a std::vector.
93  //void setMultiString(const std::string & gn, const std::vector<std::string> & values );
95  //void setMultiInt (const std::string & gn, const std::vector<index_t> & values );
97  //void setMultiReal (const std::string & gn, const std::vector<Real> & values );*/
98 
105  void addString(const std::string & label, const std::string & desc, const std::string & value );
107  void addInt (const std::string & label, const std::string & desc, const index_t & value );
109  void addReal (const std::string & label, const std::string & desc, const Real & value );
111  void addSwitch(const std::string & label, const std::string & desc, const bool & value );
112 
113  /*/// \brief Adds an option-group \a gn containing values of a std::vector.
116  //void addMultiString(const std::string & label, const std::string & desc, const std::vector<std::string> & values);
121  void addMultiInt(const std::string & label, const std::string & desc, const std::vector<index_t> & values);
122  /*/// @copydoc gsOptionList::addMultiString()
123  //void addMultiReal (const std::string & label, const std::string & desc, const std::vector<Real> & values);*/
124 
126  void remove(const std::string& label);
127 
129  enum updateType {
130  ignoreIfUnknown = 0,
131  addIfUnknown = 1
132  };
133 
140  void update(const gsOptionList& other, updateType type = ignoreIfUnknown);
141 
146  gsOptionList wrapIntoGroup(const std::string & gn) const;
147 
153  gsOptionList getGroup(const std::string & gn) const;
154 
158  bool hasGlobals() const;
159 
163  bool hasGroup(const std::string & gn) const;
164 
166  std::ostream & print(std::ostream & os) const;
167 
169  size_t size() const
170  {return m_strings.size()+m_ints.size()+m_reals.size()+m_switches.size();}
171 
176  std::string type;
177  std::string label;
178  std::string desc;
179  std::string val;
180  std::ostream & print(std::ostream & os, index_t label_slot = 19) const;
181  };
182 
184  std::vector<OptionListEntry> getAllEntries() const;
185 
186  gsOptionList& operator=(const gsOptionList & other)
187  { // Note: implcitly degerated operator was buggy on some platforms
188  if (this != &other)
189  {
190  m_strings = other.m_strings;
191  m_ints = other.m_ints;
192  m_reals = other.m_reals;
193  m_switches = other.m_switches;
194  }
195  return *this;
196  }
197 
198 #if EIGEN_HAS_RVALUE_REFERENCES
199  // Note: functions cannot be defaulted/deleted in VS2013 or older
200 
201  gsOptionList() {}
202 
203  gsOptionList(const gsOptionList & other) :
204  m_strings(other.m_strings),
205  m_ints(other.m_ints),
206  m_reals(other.m_reals),
207  m_switches(other.m_switches) { }
208 
209  gsOptionList(gsOptionList && other) :
210  m_strings(std::move(other.m_strings)),
211  m_ints(std::move(other.m_ints)),
212  m_reals(std::move(other.m_reals)),
213  m_switches(std::move(other.m_switches)) { }
214 
215  gsOptionList& operator=(gsOptionList && other)
216  {
217  m_strings = std::move(other.m_strings);
218  m_ints = std::move(other.m_ints);
219  m_reals = std::move(other.m_reals);
220  m_switches = std::move(other.m_switches);
221  return *this;
222  }
223 #endif
224 
226  void swap(gsOptionList& other)
227  {
228  m_strings .swap(other.m_strings );
229  m_ints .swap(other.m_ints );
230  m_reals .swap(other.m_reals );
231  m_switches.swap(other.m_switches);
232  }
233 
234 protected:
235  index_t & getIntRef(const std::string & label);
236 
237 private:
238 
240  std::string getInfo(const std::string & label) const;
241 
243  bool exists(const std::string & label) const;
244 
246  bool isString(const std::string & label) const;
248  bool isInt(const std::string & label) const;
250  bool isReal(const std::string & label) const;
252  bool isSwitch(const std::string & label) const;
253 
254 private:
255  friend class internal::gsXml<gsOptionList>;
256 
257  // Format: std::pair<Value,Description>
258  typedef std::pair<std::string,std::string> StringOpt;
259  typedef std::pair<index_t ,std::string> IntOpt;
260  typedef std::pair<Real ,std::string> RealOpt;
261  typedef std::pair<bool ,std::string> SwitchOpt;
262 
263  // Format: std::map<Label, std::pair<Value,Description> >
264  typedef std::map<std::string,StringOpt> StringTable;
265  typedef std::map<std::string,IntOpt> IntTable;
266  typedef std::map<std::string,RealOpt> RealTable;
267  typedef std::map<std::string,SwitchOpt> SwitchTable;
268 
269  StringTable m_strings;
270  IntTable m_ints;
271  RealTable m_reals;
272  SwitchTable m_switches;
273 
274 }; // class gsOptionList
275 
277 inline std::ostream &operator<<(std::ostream &os, const gsOptionList& b)
278 { return b.print(os); }
279 
281 inline std::ostream &operator<<(std::ostream &os, const gsOptionList::OptionListEntry& b)
282 { return b.print(os); }
283 
285 inline bool operator< ( const gsOptionList::OptionListEntry& a, const gsOptionList::OptionListEntry& b )
286 { return a.label < b.label; }
287 
288 
289 namespace internal
290 {
291 
295 template<>
296 class GISMO_EXPORT gsXml<gsOptionList>
297 {
298 private:
299  gsXml();
300 public:
301  GSXML_COMMON_FUNCTIONS(gsOptionList)
302  GSXML_GET_POINTER(gsOptionList)
303  static std::string tag () { return "OptionList"; }
304  static std::string type() { return ""; }
305 
306  static void get_into(gsXmlNode * node, gsOptionList & result);
307  static gsXmlNode * put (const gsOptionList & obj, gsXmlTree & data);
308 };
309 
310 }
311 
312 #ifdef GISMO_WITH_PYBIND11
313 
317  void pybind11_init_gsOptionList(pybind11::module &m);
318 
319 #endif // GISMO_WITH_PYBIND11
320 
321 } // namespace gismo
#define GISMO_COEFF_TYPE
Definition: gsConfig.h:26
size_t size() const
Returns the length of this list of options.
Definition: gsOptionList.h:169
std::string type
Type (as string)
Definition: gsOptionList.h:176
std::string label
Label.
Definition: gsOptionList.h:177
#define index_t
Definition: gsConfig.h:32
Definition: gsOptionList.h:175
std::ostream & operator<<(std::ostream &os, const _expr< E > &b)
Stream operator for expressions.
Definition: gsExpressions.h:382
StringTable m_strings
String-valued options/parameters.
Definition: gsOptionList.h:269
updateType
Options for gsOptionList::update.
Definition: gsOptionList.h:129
std::ostream & print(std::ostream &os) const
Prints this list of options to stream os.
Definition: gsOptionList.cpp:442
std::string val
Value (as string)
Definition: gsOptionList.h:179
void swap(gsOptionList &other)
Swaps contents.
Definition: gsOptionList.h:226
Provides forward declarations of types and structs.
SwitchTable m_switches
Switches (ON/OFF) options/parameters.
Definition: gsOptionList.h:272
std::string desc
Description.
Definition: gsOptionList.h:178
IntTable m_ints
Integer-valued options/parameters.
Definition: gsOptionList.h:270
RealTable m_reals
Real-valued options/parameters.
Definition: gsOptionList.h:271
Class which holds a list of parameters/options, and provides easy access to them. ...
Definition: gsOptionList.h:32
Provides declaration of input/output XML utilities struct.