Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
LUPI.hpp
Go to the documentation of this file.
1/*
2# <<BEGIN-copyright>>
3# Copyright 2019, Lawrence Livermore National Security, LLC.
4# This file is part of the gidiplus package (https://github.com/LLNL/gidiplus).
5# gidiplus is licensed under the MIT license (see https://opensource.org/licenses/MIT).
6# SPDX-License-Identifier: MIT
7# <<END-copyright>>
8*/
9
10#ifndef LUPI_hpp_included
11#define LUPI_hpp_included 1
12
13#include <sys/stat.h>
14#include <string>
15#include <vector>
16#include <list>
17#include <map>
18#include <stdexcept>
19#include <iostream>
20#ifndef _WIN32
21 #include <time.h>
22 #include <sys/time.h>
23#endif
24
25#include <LUPI_defines.hpp>
27
28#define LUPI_XML_verionEncoding "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
29
30#ifndef LUPI_PATH_MAX
31#define LUPI_PATH_MAX ( 4 * 4096 )
32#endif
33
34#if defined (GIDIP_HAVE_COMPILER_FLOATING_POINT_EXCEPTIONS)
35void LUPI_FPE_enable( char const *a_file, int a_line );
36void LUPI_FPE_disable_and_clear( char const *a_file, int a_line );
37void LUPI_FPE_test( char const *a_file, int a_line );
38#endif
39
40namespace LUPI {
41
42#ifdef _WIN32
43#define LUPI_FILE_SEPARATOR "\\"
44#else
45#define LUPI_FILE_SEPARATOR "/"
46#endif
47
48#define GNDS_formatVersion_1_10Chars "1.10"
49#define GNDS_formatVersion_2_0Chars "2.0"
50#define GNDS_formatVersion_2_0_LLNL_4Chars "2.0.LLNL_4"
51#define GNDS_formatVersion_2_1Chars "2.1"
52
53void deprecatedFunction( std::string const &a_functionName, std::string const &a_replacementName, std::string const &a_asOf );
54
55/*
56============================================================
57====================== FormatVersion =======================
58============================================================
59*/
61
62 private:
63 std::string m_format; /**< The GNDS format version. */
64 int m_major; /**< The GNDS format major value as an integer. */
65 int m_minor; /**< The GNDS format minor value as an integer. */
66 std::string m_patch; /**< The GNDS format patch string. This will be an empty string except for unofficial formats. */
67
68 public:
70 FormatVersion( std::string const &a_formatVersion );
71 FormatVersion( FormatVersion const &a_formatVersion );
72 FormatVersion &operator=( FormatVersion const &a_rhs );
73
74 std::string const &format( ) const { return( m_format ); }
75 int major( ) const { return( m_major ); }
76 int minor( ) const { return( m_minor ); }
77 std::string const &patch( ) const { return( m_patch ); }
78
79 bool setFormat( std::string const &a_formatVersion );
80 bool supported( ) const ;
81};
82
83/*
84============================================================
85========================= Exception ========================
86============================================================
87*/
88class Exception : public std::runtime_error {
89
90 public :
91 explicit Exception( std::string const &a_message );
92};
93
94/*
95============================================================
96================== StatusMessageReporting ==================
97============================================================
98*/
99
101
102 public:
103 enum class Status { ok, info, warning, error };
104
105 private:
107
108 public:
111
112 statusMessageReporting *smr( ) { return( &m_smr ); }
113 bool isOk( ) { return( smr_isOk( &m_smr ) ); }
114 bool isInfo( ) { return( smr_isInfo( &m_smr ) ); }
115 bool isWarning( ) { return( smr_isWarning( &m_smr ) ); }
116 bool isError( ) { return( smr_isError( &m_smr ) ); }
117 void clear( ) { smr_release( &m_smr ); }
118 std::string constructMessage( std::string a_prefix, int a_reports = 1, bool a_clear = false );
119 std::string constructFullMessage( std::string const &a_prefix, int a_reports = 1, bool a_clear = false );
120};
121
122/*
123============================================================
124====================== ArgumentParser ======================
125============================================================
126*/
127
129
130class ArgumentBase;
131
133
134 private:
135 std::string m_codeName; /**< The name of the code that is using **ArgumentParser**. */
136 std::string m_descriptor; /**< The descriptor that is printed when help (i.e., '-h') is entered. */
137 std::vector<ArgumentBase *> m_arguments; /**< The list of arguments (positional and optional) supported. */
138
139 void add2( ArgumentBase *a_argumentBase );
140
141 public:
142 ArgumentParser( std::string const &a_codeName, std::string const &a_descriptor = "" );
144
145 std::string const &codeName( ) const { return( m_codeName ); }
146 std::string const &descriptor( ) const { return( m_descriptor ); }
147 template<typename T> T *add( std::string const &a_name, std::string const &a_descriptor, int a_minimumNeeded = 1, int a_maximumNeeded = 1 );
148 ArgumentBase *add( ArgumentType a_argumentType, std::string const &a_name, std::string const &a_descriptor,
149 int a_minimumNeeded = -2, int a_maximumNeeded = -2 );
150 void addAlias( std::string const &a_name, std::string const &a_alias );
151 void addAlias( ArgumentBase const * const a_argumentBase, std::string const &a_alias );
152 bool hasName( std::string const &a_name ) const ;
153 bool isOptionalArgument( std::string const &a_name ) const ;
154 void parse( int a_argc, char **a_argv, bool a_printArguments = true );
155 template<typename T> T *get( std::size_t a_name );
156 void help( ) const ;
157 void usage( ) const ;
158 virtual void printStatus( std::string a_indent ) const ;
159};
160
161/* *********************************************************************************************************//**
162 * Creates a new argument, adds the argument to *this* and returns a pointer the the newly created argument.
163 *
164 * @param a_name [in] The name of the argument.
165 * @param a_descriptor [in] The argument's description, displayed when the help option is enetered.
166 * @param a_minimumNeeded [in] The minimum number of required time *this* argument must be entered.
167 * @param a_maximumNeeded [in] The maximum number of required time *this* argument must be entered.
168 *
169 * @return A pointer to the created argument.
170 ***********************************************************************************************************/
171
172template<typename T> T *ArgumentParser::add( std::string const &a_name, std::string const &a_descriptor, int a_minimumNeeded, int a_maximumNeeded ) {
173
174 T *argument = new T( a_name, a_descriptor, a_minimumNeeded, a_maximumNeeded );
175 add2( argument );
176
177 return( argument );
178}
179
180/*
181============================================================
182======================= ArgumentBase =======================
183============================================================
184*/
185
187
188 private:
189 ArgumentType m_argumentType; /**< The enum for arguent type of *this*. */
190 std::vector<std::string> m_names; /**< The allowed names for *this*. */
191 std::string m_descriptor; /**< The desciption printed help. */
192 int m_minimumNeeded; /**< Minimum number of times *this* argument is required on the command line. */
193 int m_maximumNeeded; /**< Maximum number of times *this* argument is required on the command line. */
194 std::size_t m_counts; /**< The number of time this argument was entered on the command line. */
195 std::vector<std::string> m_values; /**< list of values entered for this argument. Only used for types Store, Append and Positional. */
196
197 void addAlias( std::string const &a_name ); /**< Adds the alias *a_name* to *this*. */
198 virtual std::string printStatus2( ) const ; /**< For internal use. Called by method **printStatus**. */
199 virtual void printStatus3( std::string const &a_indent ) const ;
200
201 friend void ArgumentParser::addAlias( std::string const &a_name, std::string const &a_alias );
202
203 public:
204 ArgumentBase( ArgumentType a_argumentType, std::string const &a_name, std::string const &a_descriptor, int a_minimumNeeded, int a_maximumNeeded );
205 virtual ~ArgumentBase( ) = 0 ;
206
207 ArgumentType argumentType( ) const { return( m_argumentType ); }
208 std::string const &name( ) const { return( m_names[0] ); }
209 std::vector<std::string> const &names( ) { return( m_names ); }
210 bool hasName( std::string const &a_name ) const ;
211 std::string const &descriptor( ) const { return( m_descriptor ); }
212 int minimumNeeded( ) const { return( m_minimumNeeded ); }
213 int maximumNeeded( ) const { return( m_maximumNeeded ); }
214 std::size_t counts( ) const { return( m_counts ); }
215
216 virtual std::string const &value( std::size_t a_index = 0 ) const ;
217 std::vector<std::string> const &values( ) const { return( m_values ); }
218 virtual bool isOptionalArgument( ) const { return( true ); }
219 virtual bool requiresAValue( ) const { return( false ); }
220 virtual int parse( ArgumentParser const &a_argumentParser, int a_index, int a_argc, char **a_argv );
221 std::string usage( bool a_requiredOption ) const ;
222 void printStatus( std::string a_indent ) const ;
223};
224
225/*
226============================================================
227======================= OptionBoolean ======================
228============================================================
229*/
230
232
233 private:
234 bool m_default;
235
236 public:
237 OptionBoolean( ArgumentType a_argumentType, std::string const &a_name, std::string const &a_descriptor, bool a_default );
238 virtual ~OptionBoolean( ) = 0 ;
239
240 bool _default( ) const { return( m_default ); }
241 std::string printStatus2( ) const ;
242};
243
244/*
245============================================================
246======================== OptionTrue ========================
247============================================================
248*/
249
250class OptionTrue : public OptionBoolean {
251
252 public:
253 OptionTrue( std::string const &a_name, std::string const &a_descriptor = "", int a_minimumNeeded = 0, int a_maximumNeeded = -1 );
255};
256
257/*
258============================================================
259======================= OptionFalse ========================
260============================================================
261*/
262
264
265 public:
266 OptionFalse( std::string const &a_name, std::string const &a_descriptor = "", int a_minimumNeeded = 0, int a_maximumNeeded = -1 );
268};
269
270/*
271============================================================
272====================== OptionCounter =======================
273============================================================
274*/
275
277
278 public:
279 OptionCounter( std::string const &a_name, std::string const &a_descriptor = "", int a_minimumNeeded = 0, int a_maximumNeeded = -1 );
281
282 std::string printStatus2( ) const ;
283};
284
285/*
286============================================================
287======================= OptionStore ========================
288============================================================
289*/
290
291class OptionStore : public ArgumentBase {
292
293 public:
294 OptionStore( std::string const &a_name, std::string const &a_descriptor = "", int a_minimumNeeded = 0, int a_maximumNeeded = -1 );
296
297 std::string const &value( std::size_t a_index = 0 ) const ;
298 bool requiresAValue( ) const { return( true ); }
299 void printStatus3( std::string const &a_indent ) const ;
300};
301
302/*
303============================================================
304======================= OptionAppend =======================
305============================================================
306*/
307
309
310 public:
311 OptionAppend( std::string const &a_name, std::string const &a_descriptor = "", int a_minimumNeeded = 0, int a_maximumNeeded = -1 );
313
314 bool requiresAValue( ) const { return( true ); }
315 void printStatus3( std::string const &a_indent ) const ;
316};
317
318/*
319============================================================
320======================== Positional ========================
321============================================================
322*/
323
324class Positional : public ArgumentBase {
325
326 public:
327 Positional( std::string const &a_name, std::string const &a_descriptor = "", int a_minimumNeeded = 1, int a_maximumNeeded = 1 );
329
330 bool isOptionalArgument( ) const { return( false ); }
331 bool requiresAValue( ) const { return( true ); }
332 void printStatus3( std::string const &a_indent ) const ;
333};
334
335/*
336============================================================
337======================== DeltaTime =========================
338============================================================
339*/
340
341#ifndef _WIN32
342
343#define LUPI_DeltaTime_toStringFormatIncremental "incremental: CPU %8.3fs, wall %8.3fs"
344#define LUPI_DeltaTime_toStringFormatTotal "total: CPU %8.3fs, wall %8.3fs"
345
347
348 private:
349 double m_CPU_time;
350 double m_wallTime;
351 double m_CPU_timeIncremental;
352 double m_wallTimeIncremental;
353
354 public:
355 DeltaTime( );
356 DeltaTime( double a_CPU_time, double a_wallTime, double a_CPU_timeIncremental, double a_wallTimeIncremental );
357 DeltaTime( DeltaTime const &deltaTime );
359
360 double CPU_time( ) const { return( m_CPU_time ); }
361 double wallTime( ) const { return( m_wallTime ); }
362 double CPU_timeIncremental( ) const { return( m_CPU_timeIncremental ); }
363 double wallTimeIncremental( ) const { return( m_wallTimeIncremental ); }
364 std::string toString( std::string a_formatIncremental = LUPI_DeltaTime_toStringFormatIncremental,
365 std::string a_format = LUPI_DeltaTime_toStringFormatTotal, std::string a_sep = "; " );
366};
367
368/*
369============================================================
370========================== Timer ===========================
371============================================================
372*/
373
374class Timer {
375
376 private:
377 clock_t m_CPU_time;
378 struct timeval m_wallTime;
379 clock_t m_CPU_timeIncremental;
380 struct timeval m_wallTimeIncremental;
381
382 public:
383 Timer( );
384 ~Timer( ) { }
385
388 void reset( );
389};
390
391#endif // End of not _WIN32 defined.
392
393namespace FileInfo { // Should be using std::filesystem stuff but this requires C++ 17.
394
395std::string realPath( std::string const &a_path );
396std::string _basename( std::string const &a_path );
397std::string basenameWithoutExtension( std::string const &a_path );
398std::string _dirname( std::string const &a_path );
399bool exists( std::string const &a_path );
400bool isDirectory( std::string const &a_path );
401bool createDirectories( std::string const &a_path );
402
403/*
404============================================================
405========================= FileStat =========================
406============================================================
407*/
408class FileStat {
409
410 private:
411 std::string m_path; /**< The path that is stat-ed. */
412 struct stat m_stat; /**< The stat for the path. */
413
414 public:
415 FileStat( std::string const &a_path );
416
417 std::string const &path( ) const { return( m_path ); } /**< Returns a reference to the **m_path** member. */
418 struct stat const &statRef( ) const { return( m_stat ); } /**< Returns a reference to the **m_stat** member. */
419
420 bool exists( );
421 bool isDirectory( ) const { return( ( m_stat.st_mode & S_IFMT ) == S_IFDIR ); } /**< Returns *true* if the path is a directory and *false* otherwise. */
422 bool isRegularFile( ) const { return( ( m_stat.st_mode & S_IFMT ) == S_IFREG ); } /**< Returns *true* if the path is a regular file and *false* otherwise. */
423};
424
425} // End of namespace FileInfo.
426
427// Miscellaneous functions
428
429namespace Misc {
430
431std::string stripString( std::string const &a_string, bool a_left = true, bool a_right = true );
432std::vector<std::string> splitString( std::string const &a_string, char a_delimiter, bool a_strip = false );
433std::vector<std::string> splitString( std::string const &a_string, std::string const &a_delimiter, bool a_strip = false );
434std::string joinStrings( std::string const &a_sep, std::vector<std::string> a_strings );
435std::string replaceString( std::string const &a_string, std::string const &a_old, std::string const &a_new, bool a_all );
436std::vector<std::string> splitXLinkString( std::string const &a_string );
437bool stringToInt( std::string const &a_string, int &a_value );
438bool stringToSize_t( std::string const &a_string, std::size_t &a_value );
439
440std::string argumentsToString( char const *a_format, ... );
441std::string doubleToString3( char const *a_format, double a_value, bool a_reduceBits = false );
442std::string doubleToShortestString( double a_value, int a_significantDigits = 15, int a_favorEFormBy = 0 );
443
444void printCommand( std::string const &a_indent, int a_argc, char **a_argv );
445
446} // End of namespace Misc.
447
448} // End of namespace LUPI.
449
450#endif // LUPI_hpp_included
#define LUPI_DeltaTime_toStringFormatTotal
Definition LUPI.hpp:344
#define LUPI_DeltaTime_toStringFormatIncremental
Definition LUPI.hpp:343
virtual bool requiresAValue() const
Definition LUPI.hpp:219
ArgumentType argumentType() const
Definition LUPI.hpp:207
std::vector< std::string > const & names()
Definition LUPI.hpp:209
std::vector< std::string > const & values() const
Definition LUPI.hpp:217
virtual bool isOptionalArgument() const
Definition LUPI.hpp:218
bool hasName(std::string const &a_name) const
std::string const & descriptor() const
Definition LUPI.hpp:211
virtual std::string const & value(std::size_t a_index=0) const
std::size_t counts() const
Definition LUPI.hpp:214
ArgumentBase(ArgumentType a_argumentType, std::string const &a_name, std::string const &a_descriptor, int a_minimumNeeded, int a_maximumNeeded)
virtual int parse(ArgumentParser const &a_argumentParser, int a_index, int a_argc, char **a_argv)
std::string const & name() const
Definition LUPI.hpp:208
void printStatus(std::string a_indent) const
int maximumNeeded() const
Definition LUPI.hpp:213
std::string usage(bool a_requiredOption) const
int minimumNeeded() const
Definition LUPI.hpp:212
virtual void printStatus(std::string a_indent) const
std::string const & codeName() const
Definition LUPI.hpp:145
T * add(std::string const &a_name, std::string const &a_descriptor, int a_minimumNeeded=1, int a_maximumNeeded=1)
Definition LUPI.hpp:172
T * get(std::size_t a_name)
ArgumentParser(std::string const &a_codeName, std::string const &a_descriptor="")
bool hasName(std::string const &a_name) const
void parse(int a_argc, char **a_argv, bool a_printArguments=true)
bool isOptionalArgument(std::string const &a_name) const
void addAlias(std::string const &a_name, std::string const &a_alias)
std::string const & descriptor() const
Definition LUPI.hpp:146
double CPU_timeIncremental() const
Definition LUPI.hpp:362
double CPU_time() const
Definition LUPI.hpp:360
std::string toString(std::string a_formatIncremental=LUPI_DeltaTime_toStringFormatIncremental, std::string a_format=LUPI_DeltaTime_toStringFormatTotal, std::string a_sep="; ")
Definition LUPI_times.cc:72
double wallTimeIncremental() const
Definition LUPI.hpp:363
double wallTime() const
Definition LUPI.hpp:361
Exception(std::string const &a_message)
Definition LUPI_misc.cc:51
FileStat(std::string const &a_path)
Definition LUPI_file.cc:195
bool isDirectory() const
Definition LUPI.hpp:421
struct stat const & statRef() const
Definition LUPI.hpp:418
bool isRegularFile() const
Definition LUPI.hpp:422
std::string const & path() const
Definition LUPI.hpp:417
int minor() const
Definition LUPI.hpp:76
std::string const & format() const
Definition LUPI.hpp:74
FormatVersion & operator=(FormatVersion const &a_rhs)
int major() const
Definition LUPI.hpp:75
bool setFormat(std::string const &a_formatVersion)
std::string const & patch() const
Definition LUPI.hpp:77
void printStatus3(std::string const &a_indent) const
OptionAppend(std::string const &a_name, std::string const &a_descriptor="", int a_minimumNeeded=0, int a_maximumNeeded=-1)
bool requiresAValue() const
Definition LUPI.hpp:314
bool _default() const
Definition LUPI.hpp:240
std::string printStatus2() const
OptionBoolean(ArgumentType a_argumentType, std::string const &a_name, std::string const &a_descriptor, bool a_default)
OptionCounter(std::string const &a_name, std::string const &a_descriptor="", int a_minimumNeeded=0, int a_maximumNeeded=-1)
std::string printStatus2() const
OptionFalse(std::string const &a_name, std::string const &a_descriptor="", int a_minimumNeeded=0, int a_maximumNeeded=-1)
std::string const & value(std::size_t a_index=0) const
OptionStore(std::string const &a_name, std::string const &a_descriptor="", int a_minimumNeeded=0, int a_maximumNeeded=-1)
void printStatus3(std::string const &a_indent) const
bool requiresAValue() const
Definition LUPI.hpp:298
OptionTrue(std::string const &a_name, std::string const &a_descriptor="", int a_minimumNeeded=0, int a_maximumNeeded=-1)
void printStatus3(std::string const &a_indent) const
Positional(std::string const &a_name, std::string const &a_descriptor="", int a_minimumNeeded=1, int a_maximumNeeded=1)
bool requiresAValue() const
Definition LUPI.hpp:331
bool isOptionalArgument() const
Definition LUPI.hpp:330
statusMessageReporting * smr()
Definition LUPI.hpp:112
std::string constructFullMessage(std::string const &a_prefix, int a_reports=1, bool a_clear=false)
std::string constructMessage(std::string a_prefix, int a_reports=1, bool a_clear=false)
DeltaTime deltaTime()
DeltaTime deltaTimeAndReset()
std::string basenameWithoutExtension(std::string const &a_path)
Definition LUPI_file.cc:99
std::string _basename(std::string const &a_path)
Definition LUPI_file.cc:82
bool exists(std::string const &a_path)
Definition LUPI_file.cc:129
std::string realPath(std::string const &a_path)
Definition LUPI_file.cc:63
bool createDirectories(std::string const &a_path)
Definition LUPI_file.cc:165
std::string _dirname(std::string const &a_path)
Definition LUPI_file.cc:112
bool isDirectory(std::string const &a_path)
Definition LUPI_file.cc:146
std::string doubleToShortestString(double a_value, int a_significantDigits=15, int a_favorEFormBy=0)
Definition LUPI_misc.cc:349
std::vector< std::string > splitXLinkString(std::string const &a_string)
Definition LUPI_misc.cc:206
std::string replaceString(std::string const &a_string, std::string const &a_old, std::string const &a_new, bool a_all)
Definition LUPI_misc.cc:183
std::string joinStrings(std::string const &a_sep, std::vector< std::string > a_strings)
Definition LUPI_misc.cc:158
void printCommand(std::string const &a_indent, int a_argc, char **a_argv)
Definition LUPI_misc.cc:367
std::vector< std::string > splitString(std::string const &a_string, char a_delimiter, bool a_strip=false)
Definition LUPI_misc.cc:103
std::string argumentsToString(char const *a_format,...)
Definition LUPI_misc.cc:305
bool stringToSize_t(std::string const &a_string, std::size_t &a_value)
Definition LUPI_misc.cc:283
std::string stripString(std::string const &a_string, bool a_left=true, bool a_right=true)
Definition LUPI_misc.cc:68
std::string doubleToString3(char const *a_format, double a_value, bool a_reduceBits=false)
Definition LUPI_misc.cc:327
bool stringToInt(std::string const &a_string, int &a_value)
Definition LUPI_misc.cc:260
Definition LUPI.hpp:40
ArgumentType
Definition LUPI.hpp:128
void deprecatedFunction(std::string const &a_functionName, std::string const &a_replacementName, std::string const &a_asOf)
int smr_isInfo(statusMessageReporting const *smr)
void smr_release(statusMessageReporting *smr)
int smr_isOk(statusMessageReporting const *smr)
int smr_isWarning(statusMessageReporting const *smr)
int smr_isError(statusMessageReporting const *smr)