UtilsLite
Utilities for C++ programming
Loading...
Searching...
No Matches
Utils.hxx
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*\
2 | |
3 | Copyright (C) 2017 |
4 | |
5 | , __ , __ |
6 | /|/ \ /|/ \ |
7 | | __/ _ ,_ | __/ _ ,_ |
8 | | \|/ / | | | | \|/ / | | | |
9 | |(__/|__/ |_/ \_/|/|(__/|__/ |_/ \_/|/ |
10 | /| /| |
11 | \| \| |
12 | |
13 | Enrico Bertolazzi |
14 | Dipartimento di Ingegneria Industriale |
15 | Università degli Studi di Trento |
16 | email: enrico.bertolazzi@unitn.it |
17 | |
18\*--------------------------------------------------------------------------*/
19
20//
21// file: Utils.hxx
22//
23
24// select computer architecture
25#if defined(__APPLE__) && defined(__MACH__)
26 // osx architecture
27 #define UTILS_OS_OSX 1
28 #if defined(__i386__)
29 #define UTILS_ARCH32 1
30 #elif defined(__x86_64__)
31 #define UTILS_ARCH64 1
32 #endif
33#elif defined(__unix__)
34 // linux architecture
35 #define UTILS_OS_LINUX 1
36 #if defined(__i386__)
37 #define UTILS_ARCH32 1
38 #elif defined(__x86_64__)
39 #define UTILS_ARCH64 1
40 #endif
41#elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64)
42 // windows architecture
43 #define UTILS_OS_WINDOWS 1
44 // mingw subsystem
45 #if defined(__MINGW64__)
46 #define UTILS_OS_MINGW 1
47 #define UTILS_ARCH64 1
48 #elif defined(__MINGW32__)
49 #define UTILS_OS_MINGW 1
50 #define UTILS_ARCH32 1
51 #else
52 #if defined(_M_X64) || defined(_M_AMD64) || defined(_WIN64) || defined(WIN64)
53 #define UTILS_ARCH64 1
54 #else
55 #define UTILS_ARCH32 1
56 #endif
57 #endif
58 // windows headers, order matters!
59 #include <Winsock2.h>
60 #include <Windows.h>
61 #include <Ws2tcpip.h>
62 #include <iptypes.h>
63 #include <Iphlpapi.h>
64 // --------------------
65 #include <tchar.h>
66 #include <stdio.h>
67#else
68 #error "unsupported OS!"
69#endif
70
71// check if compiler is C++11
72#if (defined(_MSC_VER) && _MSC_VER >= 1800) || \
73 (defined(__cplusplus) && __cplusplus > 199711L)
74
75 #if (defined(__cplusplus) && __cplusplus <= 201103L)
76 #define UTILS_DEFAULT {}
77 #else
78 #define UTILS_DEFAULT = default
79 #endif
80#else
81 #error "Lapack Wrapper must be compiled using C++ >= C++11"
82#endif
83
84#define UTILS_PURE_VIRTUAL = 0
85#ifndef UTILS_OS_WINDOWS
86 #define UTILS_OVERRIDE override
87 #define UTILS_CONSTEXPR constexpr
88#else
89 #define UTILS_OVERRIDE
90 #define UTILS_CONSTEXPR
91#endif
92
93// STL
94#include <cassert>
95#include <iterator>
96#include <utility> // For std::move(), std::forward()
97#include <algorithm>
98#include <type_traits> // For std::remove_reference()
99#include <functional> // For std::bind()
100#include <cctype>
101
102#include <string>
103#include <string_view>
104#include <vector>
105#include <list>
106#include <map>
107#include <limits>
108
109// I/O
110#include <iostream>
111#include <iomanip>
112#include <sstream>
113#include <cstdlib>
114#include <filesystem>
115
116// C/C++
117#include <cstddef>
118#include <cmath>
119#include <cstdint>
120#include <stdexcept>
121#include <memory>
122
123// disable mingw-std-threads for mingw on MATLAB
124#if defined(__MINGW32__) || defined(__MINGW64__) && !defined(MATLAB_MEX_FILE )
125 #include <_mingw.h>
126 #if defined(__MINGW64_VERSION_MAJOR)
127 #if __MINGW64_VERSION_MAJOR < 8
128 #define UTILS_USE_MINGW_PORTABLE_THREADS
129 #endif
130 #endif
131 #if defined(__MINGW32_VERSION_MAJOR)
132 #if __MINGW32_VERSION_MAJORs < 8
133 #define UTILS_USE_MINGW_PORTABLE_THREADS
134 #endif
135 #endif
136#endif
137
138#ifdef UTILS_USE_MINGW_PORTABLE_THREADS
139 #include "mingw-std-threads/mingw.future.h"
140 #include "mingw-std-threads/mingw.mutex.h"
141 #include "mingw-std-threads/mingw.invoke.h"
142 #include "mingw-std-threads/mingw.shared_mutex.h"
143 #include "mingw-std-threads/mingw.thread.h"
144 #include "mingw-std-threads/mingw.condition_variable.h"
145#else
146 #include <future>
147 #include <mutex>
148 #include <shared_mutex>
149 #include <thread>
150 #include <condition_variable>
151 #include <atomic>
152#endif
153
154#ifdef _MSC_VER
155 // Workaround for visual studio
156 #ifdef max
157 #undef max
158 #endif
159 #ifdef min
160 #undef min
161 #endif
162#endif
163
164namespace Utils {
165 #ifndef DOXYGEN_SHOULD_SKIP_THIS
166 using string = std::string;
167 using string_view = std::string_view;
168 using ostream_type = std::basic_ostream<char>;
169 using istream_type = std::basic_istream<char>;
170 #endif
171}
172
173#include "rang.hxx"
174#include "Console.hxx"
175#include "Malloc.hxx"
176#include "Numbers.hxx"
177#include "TicToc.hxx"
178#include "Quaternion.hxx"
179#include "Table.hxx"
180#include "Token.hxx"
181
182// order must be preserved
183#include "ThreadUtils.hxx"
184#include "ThreadPoolBase.hxx"
185#include "ThreadPool0.hxx"
186#include "ThreadPool1.hxx"
187#include "ThreadPool2.hxx"
188#include "ThreadPool3.hxx"
189#include "ThreadPool4.hxx"
190#include "ThreadPool5.hxx"
191// -----------------------
192
193namespace Utils {
194
195 /*\
196 :|: _
197 :|: | |__ __ _ ___ ___ _ _ __ _ _ __ ___
198 :|: | '_ \/ _` (_-</ -_) ' \/ _` | ' \/ -_)
199 :|: |_.__/\__,_/__/\___|_||_\__,_|_|_|_\___|
200 :|:
201 \*/
202
203 inline
204 string
205 get_basename( string_view path ) {
206 namespace fs = std::filesystem;
207 return fs::path(path).parent_path().string();
208 }
209
210 inline
211 string
212 get_filename( string_view path ) {
213 namespace fs = std::filesystem;
214 return fs::path(path).filename().string();
215 }
216
217 inline
218 string
219 get_extension( string_view path ) {
220 namespace fs = std::filesystem;
221 return fs::path(path).extension().string();
222 }
223
228
230
239 bool get_environment( string_view ename, string & res );
240
242
252 void set_environment( string_view ename, string_view newval, bool overwrite );
253
255
262 void get_MAC_address( std::map<string,string> & addr );
263
265
269
271
277 void get_IP_address( std::vector<string> & addr );
278
280
284
286
290
292
296
298
302 bool check_if_file_exists( string_view fname );
303
305
309 bool check_if_dir_exists( string_view dirname );
310
312
320 bool make_directory( string_view dirname, unsigned mode = 0777 );
321
322 string get_date();
323 string get_day_time();
326 // End of OS group
328
329 template <typename T_int, typename T_real>
330 void
332 T_int npts,
333 T_real const X[],
334 T_real & x,
335 T_int & last_interval,
336 bool closed,
337 bool can_extend
338 );
339
340 #ifndef DOXYGEN_SHOULD_SKIP_THIS
341 extern template void search_interval(
342 int32_t npts,
343 float const X[],
344 float & x,
345 int32_t & last_interval,
346 bool closed,
347 bool can_extend
348 );
349
350 extern template void search_interval(
351 int32_t npts,
352 double const X[],
353 double & x,
354 int32_t & last_interval,
355 bool closed,
356 bool can_extend
357 );
358
359 extern template void search_interval(
360 int64_t npts,
361 float const X[],
362 float & x,
363 int64_t & last_interval,
364 bool closed,
365 bool can_extend
366 );
367
368 extern template void search_interval(
369 int64_t npts,
370 double const X[],
371 double & x,
372 int64_t & last_interval,
373 bool closed,
374 bool can_extend
375 );
376 #endif
377
378 template <typename T_int, typename T_real>
379 inline
380 void
382 T_int npts,
383 T_real const X[],
384 T_real & x,
385 T_int & last_interval,
386 bool closed,
387 bool can_extend
388 ) {
389 search_interval( npts, X, x, last_interval, closed, can_extend );
390 }
391
392 static inline void to_upper ( string & str ) { std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) { return std::toupper(c); }); }
393 static inline void to_lower ( string & str ) { std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) { return std::tolower(c); }); }
394 static inline bool is_lower ( string_view s ) { return std::all_of( s.begin(), s.end(), islower ); }
395 static inline bool is_upper ( string_view s ) { return std::all_of( s.begin(), s.end(), isupper ); }
396 static inline bool is_alpha ( string_view s ) { return std::all_of( s.begin(), s.end(), isalpha ); }
397 static inline bool is_alphanum ( string_view s ) { return std::all_of( s.begin(), s.end(), isalnum ); }
398 static inline bool is_digits ( string_view s ) { return std::all_of( s.begin(), s.end(), isdigit ); }
399 static inline bool is_xdigits ( string_view s ) { return std::all_of( s.begin(), s.end(), isxdigit ); }
400
401 // https://stackoverflow.com/questions/11376288/fast-computing-of-log2-for-64-bit-integers
402 static
403 inline
404 unsigned
405 iLog2( uint32_t N ) {
406 static unsigned const tab32[32] = {
407 0, 9, 1, 10, 13, 21, 2, 29,
408 11, 14, 16, 18, 22, 25, 3, 30,
409 8, 12, 20, 28, 15, 17, 24, 7,
410 19, 27, 23, 6, 26, 5, 4, 31
411 };
412 N |= N >> 1;
413 N |= N >> 2;
414 N |= N >> 4;
415 N |= N >> 8;
416 N |= N >> 16;
417 return tab32[uint32_t(N*0x07C4ACDD)>>27];
418 }
419
420 static
421 inline
422 unsigned
423 iLog2( uint64_t N ) {
424 static unsigned const tab64[64] = {
425 63, 0, 58, 1, 59, 47, 53, 2,
426 60, 39, 48, 27, 54, 33, 42, 3,
427 61, 51, 37, 40, 49, 18, 28, 20,
428 55, 30, 34, 11, 43, 14, 22, 4,
429 62, 57, 46, 52, 38, 26, 32, 41,
430 50, 36, 17, 19, 29, 10, 13, 21,
431 56, 45, 25, 31, 35, 16, 9, 12,
432 44, 24, 15, 8, 23, 7, 6, 5
433 };
434 N |= N >> 1;
435 N |= N >> 2;
436 N |= N >> 4;
437 N |= N >> 8;
438 N |= N >> 16;
439 N |= N >> 32;
440 return tab64[uint64_t((N - (N>>1))*0x07EDD5E59A4E28C2) >> 58];
441 }
442
443 string progress_bar( double progress, int width );
444 void progress_bar( ostream_type &, double progress, int width, string_view msg );
445 void progress_bar2( ostream_type &, double progress, int width, string_view msg );
446
447}
448
449//
450// eof: Utils.hxx
451//
Console utility for formatted output with different message levels.
void get_IP_address(std::vector< string > &addr)
Fetches the IP addresses of the system.
string get_executable_path_name()
Retrieves the full path to the current executable.
string get_log_date_time()
string get_home_directory()
Retrieves the home directory of the current user.
void get_MAC_address(std::map< string, string > &addr)
Retrieves the MAC addresses of network interfaces.
bool check_if_dir_exists(string_view dirname)
Checks if a directory exists.
string get_user_name()
Retrieves the username of the current user.
string get_host_name()
Retrieves the hostname of the system.
string get_date()
string get_day_time_and_date()
void set_environment(string_view ename, string_view newval, bool overwrite)
Sets the value of an environment variable.
bool get_environment(string_view ename, string &res)
Fetches the value of an environment variable.
bool check_if_file_exists(string_view fname)
Checks if a file exists.
bool make_directory(string_view dirname, unsigned mode=0777)
Creates a directory if it does not exist.
string get_day_time()
Definition SystemUtils.cc:39
std::basic_istream< char > istream_type
Type for input stream.
Definition Console.hxx:27
std::basic_ostream< char > ostream_type
Type for output stream.
Definition Console.hxx:28
string get_extension(string_view path)
Definition Utils.hxx:219
std::string string
Type for string.
Definition Console.hxx:29
void searchInterval(T_int npts, T_real const X[], T_real &x, T_int &last_interval, bool closed, bool can_extend)
Definition Utils.hxx:381
void progress_bar2(ostream_type &, double progress, int width, string_view msg)
Outputs an enhanced text-based progress bar to the specified output stream.
Definition Utils_progress_bar.cc:95
string get_filename(string_view path)
Definition Utils.hxx:212
void search_interval(T_int npts, T_real const X[], T_real &x, T_int &last_interval, bool closed, bool can_extend)
string progress_bar(double progress, int width)
Generates a text-based progress bar as a string.
Definition Utils_progress_bar.cc:47
string get_basename(string_view path)
Definition Utils.hxx:205