A library for working with phylogenetic and population genetic data.
v0.27.0
options.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_CORE_OPTIONS_H_
2 #define GENESIS_UTILS_CORE_OPTIONS_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2019 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
34 #include <random>
35 #include <string>
36 #include <utility>
37 #include <vector>
38 
39 namespace genesis {
40 namespace utils {
41 
42 // =================================================================================================
43 // Options
44 // =================================================================================================
45 
49 class Options
50 {
51 public:
52 
53  // -------------------------------------------------------------------------
54  // Instance
55  // -------------------------------------------------------------------------
56 
60  inline static Options& get() {
61  // Meyers-Singleton
62  static Options instance;
63  return instance;
64  }
65 
66  // -------------------------------------------------------------------------
67  // Command Line
68  // -------------------------------------------------------------------------
69 
73  inline std::vector<std::string> command_line() const
74  {
75  return command_line_;
76  }
77 
81  std::string command_line_string() const;
82 
89  void command_line( int const argc, char const* const* argv );
90 
91  // -------------------------------------------------------------------------
92  // Number of Threads
93  // -------------------------------------------------------------------------
94 
98  inline unsigned int number_of_threads() const
99  {
100  return number_of_threads_;
101  }
102 
112  void number_of_threads( unsigned int number );
113 
120  unsigned int guess_number_of_threads( bool use_openmp = true ) const;
121 
125  bool hyperthreads_enabled() const;
126 
127  // -------------------------------------------------------------------------
128  // Random Seed & Engine
129  // -------------------------------------------------------------------------
130 
134  inline unsigned long random_seed() const
135  {
136  return random_seed_;
137  }
138 
145  void random_seed (const unsigned long seed);
146 
158  inline std::default_random_engine& random_engine()
159  {
160  return random_engine_;
161  }
162 
163  // -------------------------------------------------------------------------
164  // Misc Options
165  // -------------------------------------------------------------------------
166 
172  inline bool allow_file_overwriting() const
173  {
174  return allow_file_overwriting_;
175  }
176 
188  inline void allow_file_overwriting( bool value )
189  {
190  allow_file_overwriting_ = value;
191  }
192 
193  // -------------------------------------------------------------------------
194  // Object Info
195  // -------------------------------------------------------------------------
196 
212  inline void print_object_infos( bool value )
213  {
214  print_obj_infos_ = value;
215  }
216 
222  inline bool print_object_infos() const
223  {
224  return print_obj_infos_;
225  }
226 
243  inline void print_object_gists( long value )
244  {
245  print_obj_gists_ = value;
246  }
247 
253  inline long print_object_gists() const
254  {
255  return print_obj_gists_;
256  }
257 
258  // -------------------------------------------------------------------------
259  // Run Time Environment
260  // -------------------------------------------------------------------------
261 
266  static bool stdin_is_terminal();
267 
272  static bool stdout_is_terminal();
273 
278  static bool stderr_is_terminal();
279 
284  static std::pair<int, int> terminal_size();
285 
286  // -------------------------------------------------------------------------
287  // Compile Time Environment
288  // -------------------------------------------------------------------------
289 
293  static bool is_debug();
294 
298  static bool is_release();
299 
303  static std::string build_type();
304 
308  static bool is_little_endian();
309 
313  static bool is_big_endian();
314 
320  static std::string platform();
321 
327  static std::string compiler_family();
328 
334  static std::string compiler_version();
335 
339  static std::string cpp_version();
340 
347  static std::string compile_date_time();
348 
352  static bool using_pthreads();
353 
357  static bool using_openmp();
358 
362  static bool using_zlib();
363 
364  // -------------------------------------------------------------------------
365  // Overview
366  // -------------------------------------------------------------------------
367 
371  std::string info() const;
372 
376  std::string info_compile_time() const;
377 
381  std::string info_run_time() const;
382 
383  // -------------------------------------------------------------------------
384  // Data Members
385  // -------------------------------------------------------------------------
386 
387 private:
388 
389  std::vector<std::string> command_line_;
390  unsigned int number_of_threads_;
391 
392  unsigned long random_seed_;
393  std::default_random_engine random_engine_;
394 
395  bool allow_file_overwriting_ = false;
396 
397  bool print_obj_infos_ = true;
398  long print_obj_gists_ = 0;
399 
400  // -------------------------------------------------------------------------
401  // Hidden Class Members
402  // -------------------------------------------------------------------------
403 
404 private:
405 
406  // Everything private, as it is a singleton.
407 
411  Options();
412 
413  ~Options() = default;
414 
415  Options( const Options& ) = delete;
416  Options( Options&& ) = delete;
417  Options& operator= ( const Options& ) = delete;
418  Options& operator= ( Options&& ) = delete;
419 
420 };
421 
422 } // namespace utils
423 } // namespace genesis
424 
425 #endif // include guard
genesis::utils::Options::info
std::string info() const
Return a list with compile time and run time options with their values.
Definition: options.cpp:410
genesis::utils::Options::allow_file_overwriting
void allow_file_overwriting(bool value)
Set whether Genesis is allowed to overwrite files when outputting data.
Definition: options.hpp:188
genesis::utils::Options::compiler_version
static std::string compiler_version()
Return the compiler version that was used to compile genesis.
Definition: options.cpp:339
genesis::utils::Options::info_run_time
std::string info_run_time() const
Return a list of run time options.
Definition: options.cpp:434
genesis::utils::Options::random_seed
unsigned long random_seed() const
Returns the random seed that was used to initialize the engine.
Definition: options.hpp:134
genesis::utils::Options::platform
static std::string platform()
Return the platform under which genesis was compiled.
Definition: options.cpp:299
genesis::utils::Options::guess_number_of_threads
unsigned int guess_number_of_threads(bool use_openmp=true) const
Try to guess the number of hardware threads of the current system.
Definition: options.cpp:149
genesis::utils::Options::command_line
std::vector< std::string > command_line() const
Returns an array of strings containing the program's command line arguments.
Definition: options.hpp:73
genesis::utils::Options::is_debug
static bool is_debug()
Return whether the binary was compiled with build type DEBUG.
Definition: options.cpp:258
genesis::utils::Options::info_compile_time
std::string info_compile_time() const
Return a list of compile time options.
Definition: options.cpp:419
genesis::utils::Options::stderr_is_terminal
static bool stderr_is_terminal()
Return true iff the standard error stream is a terminal, and false if not, i.e., if it is a file or a...
Definition: options.cpp:225
genesis::utils::Options::terminal_size
static std::pair< int, int > terminal_size()
Return the width and height of the terminal that is used to run the program, in number of columns and...
Definition: options.cpp:234
genesis::utils::Options::stdout_is_terminal
static bool stdout_is_terminal()
Return true iff the standard output stream is a terminal, and false if not, i.e., if it is a file or ...
Definition: options.cpp:216
genesis::utils::Options::compile_date_time
static std::string compile_date_time()
Return the date and time when genesis was compiled.
Definition: options.cpp:374
genesis::utils::Options::hyperthreads_enabled
bool hyperthreads_enabled() const
Try to get whether hyperthreads are enabled in the current system.
Definition: options.cpp:133
genesis::utils::Options::compiler_family
static std::string compiler_family()
Return the compiler family (name) that was used to compile genesis.
Definition: options.cpp:316
genesis::utils::Options::using_openmp
static bool using_openmp()
Return whether the binary was compiled using OpenMP.
Definition: options.cpp:388
genesis::utils::Options::command_line_string
std::string command_line_string() const
Returns a string containing the program's command line arguments.
Definition: options.cpp:88
genesis::utils::Options::print_object_infos
bool print_object_infos() const
Get whether an object info one-liner is printed when using the operator <<.
Definition: options.hpp:222
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::Options
Simple Options class for application-wide configuration and settings.
Definition: options.hpp:49
genesis::utils::Options::stdin_is_terminal
static bool stdin_is_terminal()
Return true iff the standard input stream is a terminal, and false if not, i.e., if it is a file or a...
Definition: options.cpp:206
genesis::utils::Options::random_engine
std::default_random_engine & random_engine()
Returns the default engine for random number generation.
Definition: options.hpp:158
genesis::utils::Options::number_of_threads
unsigned int number_of_threads() const
Returns the number of threads.
Definition: options.hpp:98
genesis::utils::Options::print_object_gists
long print_object_gists() const
Get whether an object gist is printed when using the operator <<.
Definition: options.hpp:253
genesis::utils::Options::print_object_gists
void print_object_gists(long value)
Set whether an object gist is printed when using the operator << that is defined for many (container)...
Definition: options.hpp:243
genesis::utils::Options::is_release
static bool is_release()
Return whether the binary was compiled with build type RELEASE.
Definition: options.cpp:267
genesis::utils::Options::is_little_endian
static bool is_little_endian()
Return whether the system uses little endian memory.
Definition: options.cpp:287
genesis::utils::Options::using_pthreads
static bool using_pthreads()
Return whether the binary was compiled using Pthreads.
Definition: options.cpp:379
genesis::utils::Options::get
static Options & get()
Returns a single instance of this class.
Definition: options.hpp:60
genesis::utils::Options::build_type
static std::string build_type()
Return the build type that was used to compile the binary, i.e., "debug" or "release".
Definition: options.cpp:276
genesis::utils::Options::print_object_infos
void print_object_infos(bool value)
Set whether an object info one-liner is printed when using the operator << that is defined for many c...
Definition: options.hpp:212
genesis::utils::Options::is_big_endian
static bool is_big_endian()
Return whether the system uses big endian memory.
Definition: options.cpp:293
genesis::utils::Options::cpp_version
static std::string cpp_version()
Return the CPP version that was used to compile genesis.
Definition: options.cpp:365
genesis::utils::Options::allow_file_overwriting
bool allow_file_overwriting() const
Get whether Genesis is allowed to overwrite files when outputting data.
Definition: options.hpp:172
genesis::utils::Options::using_zlib
static bool using_zlib()
Return whether the binary was compiled using zlib.
Definition: options.cpp:397