Bayeux  3.4.1
Core Foundation library for SuperNEMO
utils.h
Go to the documentation of this file.
1 
3 #ifndef DATATOOLS_UTILS_H
4 #define DATATOOLS_UTILS_H
5 
6 // Standard Library:
7 #include <cstdlib>
8 #include <list>
9 #include <string>
10 
11 // Third party:
12 // - Boost:
13 // #include <boost/property_tree/ptree.hpp>
14 
15 // This Project:
16 #include <datatools/exception.h>
17 #include <datatools/bit_mask.h>
18 
19 namespace datatools {
20 
23  SUCCESS = EXIT_SUCCESS,
24  ERROR = EXIT_FAILURE,
25  FAILURE = EXIT_FAILURE
26  };
27 
31  COMPARE_LESS = -1,
34  };
35 
36  /* String constants */
37 
38  const std::string & empty_string();
39 
40  const std::string & none_label();
41 
42  const std::string & yes_label();
43 
44  const std::string & no_label();
45 
46  /* Single precision utility functions */
47 
50  void invalidate(float& value_);
51 
53  float invalid_real_single();
54 
57  bool is_valid(float value_);
58 
61  bool is_infinity(float value_);
62 
65  bool is_plus_infinity(float value_);
66 
69  bool is_minus_infinity(float value_);
70 
73  void plus_infinity(float& value_);
74 
77  void minus_infinity(float& value_);
78 
81  void infinity(float& value_);
82 
83  /* Double precision utility functions */
84 
87  void invalidate(double& value_);
88 
90  double invalid_real_double();
91 
93  double invalid_real();
94 
97  bool is_valid(double value_);
98 
101  bool is_infinity(double value_);
102 
105  bool is_plus_infinity(double value_);
106 
109  bool is_minus_infinity(double value_);
110 
113  bool is_normal(double value_);
114 
117  void plus_infinity(double& value_);
118 
121  void minus_infinity(double& value_);
122 
125  void infinity(double& value_);
126 
133  compare_result_type compare_real(double x1_, double x2_,
134  double abs_eps_ = 0.0, double rel_eps_ = 0.0);
135 
136  /* String utility functions */
137 
147  };
148 
150  bool name_validation(const std::string & name_, uint32_t flags_ = NV_DEFAULT);
151 
156  bool is_quoted(const std::string & text_, char q_ = '"');
157 
159  void add_quotes(const std::string & from_, std::string & to_, char q_ = '"');
160 
162  void add_quotes(std::string & text_, char q_ = '"');
163 
165  void remove_quotes(const std::string & from_, std::string & to_, char q_ = '"');
166 
168  void remove_quotes(std::string & text_, char q_ = '"');
169 
171  void remove_all_quotes(std::string & text_);
172 
173  /* Functions to expand string as path */
174 
176  bool resolve_library_info_path_keys(const std::string & library_topic_,
177  std::string & install_path_key_,
178  std::string & environ_path_key_);
179 
183  std::string fetch_path(const std::string & path_str_);
184 
186  bool fetch_path(std::string & word_, std::string & errmsg_);
187 
228  bool fetch_path_with_env(std::string& word_);
229 
231  bool fetch_path_with_env(std::string& word_, std::string & errmsg_);
232 
240  bool fetch_path_without_env(std::string& word_);
241 
246  bool fetch_path_with_env_p(std::string& word_,
247  const std::string & parent_path_);
248 
253  bool fetch_path_with_env_g(std::string& word_);
254 
259  bool fetch_path_with_env_pg(std::string& word_,
260  const std::string & parent_path_);
261 
263  bool has_global_path();
264 
266  void reset_global_path();
267 
269  void set_global_path(const std::string & gpath_);
270 
272  const std::string & get_global_path();
273 
285  std::string expand_path(const std::string& path_str_);
286 
294  void split_string(const std::string& word_,
295  const std::string& separators_,
296  std::list<std::string>& words_);
297 
298  /* Bit manipulation utility template functions */
299 
303  template<typename Integral>
304  void set_bit(Integral & number_, int pos_)
305  {
306  DT_THROW_IF(pos_ < 0 || pos_ >= (int) sizeof(Integral) * 8,
307  std::range_error,
308  "Invalid bit position [pos = " << pos_ << "]");
309  number_ |= 1 << pos_;
310  return;
311  }
312 
316  template<typename Integral>
317  void unset_bit(Integral & number_, int pos_)
318  {
319  DT_THROW_IF(pos_ < 0 || pos_ >= (int) sizeof(Integral) * 8,
320  std::range_error,
321  "Invalid bit position [pos = " << pos_ << "]");
322  number_ &= ~(1 << pos_);
323  return;
324  }
325 
326 
330  template<typename Integral>
331  void toggle_bit(Integral & number_, int pos_)
332  {
333  DT_THROW_IF(pos_ < 0 || pos_ >= (int) sizeof(Integral) * 8,
334  std::range_error,
335  "Invalid bit position [pos = " << pos_ << "]");
336  number_ ^= 1 << pos_;
337  return;
338  }
339 
340 
344  template<typename Integral>
345  bool check_bit(Integral& number_, int pos_)
346  {
347  DT_THROW_IF(pos_ < 0 || pos_ >= (int) sizeof(Integral) * 8,
348  std::range_error,
349  "Invalid bit position [pos = " << pos_ << "]");
350 
351  if (number_ & (1 << pos_)) return false;
352  return true;
353  }
354 
355 } // namespace datatools
356 
357 #endif // DATATOOLS_UTILS_H
358 
359 // Local Variables: --
360 // mode: c++ --
361 // c-file-style: "gnu" --
362 // tab-width: 2 --
363 // End: --
float invalid_real_single()
Return invalid single precision float (set at NaN):
bool is_quoted(const std::string &text_, char q_='"')
Default validation rule for object instance.
Definition: utils.h:145
const std::string & no_label()
bool is_minus_infinity(float value_)
void invalidate(float &value_)
static const uint32_t bit01
Definition: bit_mask.h:28
void plus_infinity(float &value_)
static const uint32_t bit03
Definition: bit_mask.h:30
Equal to.
Definition: utils.h:32
bool name_validation(const std::string &name_, uint32_t flags_=NV_DEFAULT)
Check if a name (object identifier) is valid using simple criteria.
Returned code in case of success.
Definition: utils.h:23
bool resolve_library_info_path_keys(const std::string &library_topic_, std::string &install_path_key_, std::string &environ_path_key_)
Extract the keys used by library information registration mechanism.
bool is_plus_infinity(float value_)
void split_string(const std::string &word_, const std::string &separators_, std::list< std::string > &words_)
static const uint32_t bit04
Definition: bit_mask.h:31
const std::string & empty_string()
compare_result_type
Comparison result code for comparable objects.
Definition: utils.h:29
void remove_quotes(const std::string &from_, std::string &to_, char q_='"')
Remove quotes from a string.
Utility macros for exception handling.
const std::string & none_label()
std::string expand_path(const std::string &path_str_)
const std::string & get_global_path()
Forbid the 'colon' character in names.
Definition: utils.h:139
Forbid the 'underscore' character in names.
Definition: utils.h:142
void toggle_bit(Integral &number_, int pos_)
Definition: utils.h:331
bool is_infinity(float value_)
void set_global_path(const std::string &gpath_)
void add_quotes(const std::string &from_, std::string &to_, char q_='"')
Add quotes to a string.
void set_bit(Integral &number_, int pos_)
Definition: utils.h:304
bool check_bit(Integral &number_, int pos_)
Definition: utils.h:345
bool fetch_path_with_env_g(std::string &word_)
bool fetch_path_with_env_pg(std::string &word_, const std::string &parent_path_)
compare_result_type compare_real(double x1_, double x2_, double abs_eps_=0.0, double rel_eps_=0.0)
Comparison cannot be performed.
Definition: utils.h:30
static const uint32_t bit02
Definition: bit_mask.h:29
Default validation rule.
Definition: utils.h:144
Returned code in case of failure.
Definition: utils.h:25
bool fetch_path_with_env_p(std::string &word_, const std::string &parent_path_)
Allow leading digit.
Definition: utils.h:143
Forbid the 'hyphen' character in names.
Definition: utils.h:141
const std::string & yes_label()
Forbid the 'dot' character in names.
Definition: utils.h:140
Default validation rule for object model.
Definition: utils.h:146
#define DT_THROW_IF(Condition, ExceptionType, Message)
Definition: exception.h:76
void remove_all_quotes(std::string &text_)
Remove all quotes from a string.
return_code_type
Return codes.
Definition: utils.h:22
Returned code in case of failure.
Definition: utils.h:24
Less than.
Definition: utils.h:31
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
bool is_valid(float value_)
Greater than.
Definition: utils.h:33
bool has_global_path()
name_validation_flags
Definition: utils.h:138
bool is_normal(double value_)
void reset_global_path()
static const uint32_t bit00
Definition: bit_mask.h:27
void minus_infinity(float &value_)
void unset_bit(Integral &number_, int pos_)
Definition: utils.h:317
double invalid_real_double()
Return invalid double precision real (set at NaN):
bool fetch_path_with_env(std::string &word_)
void infinity(float &value_)
double invalid_real()
Return invalid double precision real (set at NaN):
bool fetch_path_without_env(std::string &word_)
std::string fetch_path(const std::string &path_str_)