Loading [MathJax]/extensions/tex2jax.js
Parallel numerical verification of the σ_odd problem  October 6, 2018
All Classes Namespaces Files Functions Variables Typedefs Macros
helper.hpp
Go to the documentation of this file.
1 /* -*- coding: latin-1 -*- */
2 /** \file common/helper/helper.hpp (September 14, 2018)
3  * \brief
4  * Some generic helper functions for programs.
5  *
6  * GPLv3 --- Copyright (C) 2017, 2018 Olivier Pirson
7  * http://www.opimedia.be/
8  */
9 
10 #ifndef PROGS_SRC_COMMON_HELPER_HELPER_HPP_
11 #define PROGS_SRC_COMMON_HELPER_HELPER_HPP_
12 
13 // \cond
14 #include <chrono>
15 #include <iostream>
16 #include <string>
17 // \endcond
18 
19 
20 
21 /* ****************
22  * Private macros *
23  ******************/
24 
25 /** \brief
26  * From "Variadic macros tricks"
27  * https://codecraft.co/2014/11/25/variadic-macros-tricks/
28  */
29 #define GET_OVERLOAD_(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, NAME, ...) NAME
30 
31 
32 #define TSV1_(a) { \
33  std::cerr << a << std::endl; \
34  }
35 
36 #define TSV2_(a, b) { \
37  std::cerr << a << '\t' << b << std::endl; \
38  }
39 
40 #define TSV3_(a, b, c) { \
41  std::cerr << a << '\t' << b << '\t' << c << std::endl; \
42  }
43 
44 #define TSV4_(a, b, c, d) { \
45  std::cerr << a << '\t' << b << '\t' << c << '\t' << d << std::endl; \
46  }
47 
48 #define TSV5_(a, b, c, d, e) { \
49  std::cerr << a << '\t' << b << '\t' << c << '\t' << d << '\t' << e << std::endl; \
50  }
51 
52 #define TSV6_(a, b, c, d, e, f) { \
53  std::cerr << a << '\t' << b << '\t' << c << '\t' << d << '\t' << e << '\t' \
54  << f << std::endl; \
55  }
56 
57 #define TSV7_(a, b, c, d, e, f, g) { \
58  std::cerr << a << '\t' << b << '\t' << c << '\t' << d << '\t' << e << '\t' \
59  << f << '\t' << g << std::endl; \
60  }
61 
62 #define TSV8_(a, b, c, d, e, f, g, h) { \
63  std::cerr << a << '\t' << b << '\t' << c << '\t' << d << '\t' << e << '\t' \
64  << f << '\t' << g << '\t' << h << std::endl; \
65  }
66 
67 #define TSV9_(a, b, c, d, e, f, g, h, i) { \
68  std::cerr << a << '\t' << b << '\t' << c << '\t' << d << '\t' << e << '\t' \
69  << f << '\t' << g << '\t' << h << '\t' << i << std::endl; \
70  }
71 
72 #define TSV10_(a, b, c, d, e, f, g, h, i, j) { \
73  std::cerr << a << '\t' << b << '\t' << c << '\t' << d << '\t' << e << '\t' \
74  << f << '\t' << g << '\t' << h << '\t' << i << '\t' << j << std::endl; \
75  }
76 
77 
78 
79 /* ********
80  * Macros *
81  **********/
82 
83 /** \brief
84  * Print to stdcerr the element of the list l, separated by comma.
85  *
86  * (Useful during development phase.)
87  *
88  * @param l iterable
89  */
90 #define PRINT_LIST(l) { \
91  std::cerr << '(' << l.size() << ')'; \
92  for (auto x : l) { \
93  std::cerr << ',' << x; \
94  } \
95  std::cerr << std::endl; \
96  }
97 
98 
99 /** \brief
100  * Convert the value of the macro x to a literal string.
101  *
102  * @param x macro
103  */
104 #define STR(x) STRINGIFY(x)
105 
106 /** \brief
107  * Convert the name of the macro x to a literal string.
108  *
109  * @param x macro
110  */
111 #define STRINGIFY(x) #x
112 
113 
114 
115 /** \brief
116  * Some generic helper functions for programs.
117  */
118 namespace helper {
119 
120  /* ************
121  * Prototypes *
122  **************/
123 
124  /** \brief
125  * Return the path composed by path1/path2.
126  *
127  * @param path1 a valid path
128  * @param path2 a valid relative path
129  */
130  std::string
131  concat_path(std::string path1, std::string path2);
132 
133 
134  /** \brief
135  * Return a string with the duration expressed in milliseconds, seconds, minutes and hours,
136  * separated by tabulation.
137  *
138  * @return "_ms\t= _s\t= _m\t= _h"
139  */
140  std::string
141  duration_ms_to_string(double duration_ms);
142 
143 
144  /** \brief
145  * Return a string with the duration expressed in milliseconds, seconds, minutes and hours,
146  * separated by tabulation.
147  *
148  * @return "_ms\t= _s\t= _m\t= _h"
149  */
150  std::string
151  duration_to_string(std::chrono::duration<double> duration_second);
152 
153 
154  /** \brief
155  * Read the file and return its content to a string.
156  * If failed then print a error message and exit.
157  */
158  std::string
159  file_to_string(std::string filename);
160 
161 
162  /** \brief
163  * Return argv[i] converted in string.
164  *
165  * If (help_and_exit_function != nullptr) and (i is not a valid index)
166  * then call help_and_exit_function().
167  */
168  std::string
169  get_string(int argc, const char* const argv[], unsigned int i,
170  void (*help_and_exit_function)() = nullptr);
171 
172 
173  /** \brief
174  * Return argv[i] converted in integer.
175  *
176  * If (help_and_exit_function != nullptr) and (i is not a valid index)
177  * then call help_and_exit_function().
178  */
179  unsigned int
180  get_uint(int argc, const char* const argv[], unsigned int i,
181  void (*help_and_exit_function)() = nullptr);
182 
183 
184  /** \brief
185  * Return argv[i] converted in integer.
186  *
187  * If (help_and_exit_function != nullptr) and (i is not a valid index)
188  * then call help_and_exit_function().
189  */
190  unsigned long
191  get_ulong(int argc, const char* const argv[], unsigned int i,
192  void (*help_and_exit_function)() = nullptr);
193 
194 
195  /** \brief
196  * Return true iff the file (or directory) exists.
197  */
198  bool
199  is_file_exists(std::string filename);
200 
201 
202  /** \brief
203  * Print to stdcout the intern configuration of the compiler.
204  */
205  void
207 
208 
209  /** \brief
210  * If s terminates by a null character
211  * then return a copy of s without this last character,
212  * else return s.
213  */
214  std::string
215  remove_last_if_null(const std::string &s);
216 
217 
218  /** \brief
219  * Return the string "true" if b, else "false"
220  */
221  std::string
222  to_string(bool b);
223 
224 } // namespace helper
225 
226 #endif // PROGS_SRC_COMMON_HELPER_HELPER_HPP_
227 
228 
229 
230 /* ***********************
231  * Macro always included *
232  *************************/
233 
234 /** \brief
235  * Print to stdout all parameters separated by tabulation.
236  *
237  * If a NTSV macro is defined
238  * then do nothing.
239  *
240  * (Useful during development phase.)
241  */
242 #undef TSV
243 
244 #ifdef NTSV
245 #define TSV(...)
246 #else
247 #define TSV(...) GET_OVERLOAD_(__VA_ARGS__, \
248  TSV10_, TSV9_, TSV8_, TSV7_, TSV6_, TSV5_, \
249  TSV4_, TSV3_, TSV2_, TSV1_)(__VA_ARGS__)
250 #endif
251 
252 
253 
254 // To avoid cpplint warning
255 #ifndef PROGS_SRC_COMMON_HELPER_HELPER_HPP_
256 #endif // PROGS_SRC_COMMON_HELPER_HELPER_HPP_
Some generic helper functions for programs.
Definition: helper.cpp:16
std::string remove_last_if_null(const std::string &s)
If s terminates by a null character then return a copy of s without this last character, else return s.
Definition: helper.cpp:169
std::string duration_ms_to_string(double duration_ms)
Return a string with the duration expressed in milliseconds, seconds, minutes and hours...
Definition: helper.cpp:48
unsigned int get_uint(int argc, const char *const argv[], unsigned int i, void(*help_and_exit_function)())
Return argv[i] converted in integer.
Definition: helper.cpp:108
std::string duration_to_string(std::chrono::duration< double > duration_second)
Return a string with the duration expressed in milliseconds, seconds, minutes and hours...
Definition: helper.cpp:61
bool is_file_exists(std::string filename)
Return true iff the file (or directory) exists.
Definition: helper.cpp:140
std::string get_string(int argc, const char *const argv[], unsigned int i, void(*help_and_exit_function)())
Return argv[i] converted in string.
Definition: helper.cpp:92
std::string concat_path(std::string path1, std::string path2)
Return the path composed by path1/path2.
Definition: helper.cpp:29
std::string file_to_string(std::string filename)
Read the file and return its content to a string. If failed then print a error message and exit...
Definition: helper.cpp:75
void print_intern_config_compiler()
Print to stdcout the intern configuration of the compiler.
Definition: helper.cpp:148
unsigned long get_ulong(int argc, const char *const argv[], unsigned int i, void(*help_and_exit_function)())
Return argv[i] converted in integer.
Definition: helper.cpp:124
std::string to_string(bool b)
Return the string "true" if b, else "false".
Definition: helper.cpp:177