1 #ifndef MARLINMT_UTILS_h 2 #define MARLINMT_UTILS_h 1 13 #include <unordered_map> 33 using pair = std::pair<duration_rep, duration_rep> ;
35 using nanoseconds = std::chrono::duration<duration_rep, std::nano> ;
36 using microseconds = std::chrono::duration<duration_rep, std::micro> ;
37 using milliseconds = std::chrono::duration<duration_rep, std::milli> ;
38 using seconds = std::chrono::duration<duration_rep> ;
39 using minutes = std::chrono::duration<duration_rep, std::ratio<60>> ;
51 return clock_type::now() ;
63 template <
class unit = seconds>
65 auto current =
now() ;
66 return std::chrono::duration_cast<unit>( current - since ).count() ;
87 template <
class unit = seconds>
89 return std::chrono::duration_cast<unit>( ealier - older ).count() ;
98 template <
class unit = seconds>
103 while ( timediff < crunchTime ) {
106 timediff = clock::time_difference<unit>(start,
now) ;
121 static const unsigned long long int hashinit = 14695981039346656037ull ;
124 static constexpr
unsigned long long int doByte(
unsigned long long int hash,
unsigned char val) {
125 return (hash ^ val) * 1099511628211ull ;
133 static unsigned long long int hash64(
const char *key ) {
134 unsigned char* str = (
unsigned char*)key ;
135 unsigned long long int hash = hashinit ;
136 for ( ; *str; ++str) hash = doByte(hash, *str) ;
143 template <
typename T>
145 static unsigned long long int code = hash64(
typeid(T).name()) ;
164 template <
typename T>
165 static std::string typeToString(
const T &var ) ;
172 template <
typename T>
173 static std::vector<std::string> typeToString(
const std::vector<T> &vars ) ;
180 template <
typename T>
181 static T stringToType(
const std::string &str ) ;
188 template <
typename T>
189 static std::vector<T> stringToType(
const std::vector<std::string> &strs ) ;
197 template <
typename T>
198 static std::vector<T> split(
const std::string &inputString,
const std::string &delimiter =
" " ) ;
208 template <
typename T>
209 static std::string join(
const T &input,
const std::string &delimiter =
" " ) ;
218 template <
typename T>
219 static std::string join(
const std::vector<T> &input,
const std::string &delimiter =
" " ) ;
225 template <
typename T>
227 std::ostringstream oss ;
228 if ((oss << var).fail()) {
229 throw Exception(
"Couldn't convert value to string" ) ;
245 return (var ?
"true" :
"false") ;
250 template <
typename T>
252 std::vector<std::string> result ;
253 result.reserve( vars.size() ) ;
254 for(
auto var : vars ) {
255 result.push_back( typeToString<T>( var ) ) ;
262 template <
typename T>
265 std::istringstream iss(str) ;
266 if ((iss >> t).fail()) {
267 throw Exception(
"Couldn't convert string to value" ) ;
283 if ( str ==
"true" || str ==
"1" || str ==
"on" )
return true ;
284 if ( str ==
"false" || str ==
"0" || str ==
"off" )
return false ;
285 throw Exception(
"Couldn't convert value to string" ) ;
290 template <
typename T>
292 std::vector<T> result ;
293 result.reserve( strs.size() ) ;
294 for(
auto str : strs ) {
295 result.push_back( stringToType<T>( str ) ) ;
302 template <
typename T>
303 inline std::vector<T>
StringUtil::split(
const std::string &inputString,
const std::string &delimiter) {
304 std::string::size_type lastPos = inputString.find_first_not_of(delimiter, 0);
305 std::string::size_type pos = inputString.find_first_of(delimiter, lastPos);
306 typename std::vector<T> tokens ;
307 while ((std::string::npos != pos) || (std::string::npos != lastPos)) {
308 T
value = stringToType<T>(inputString.substr(lastPos, pos - lastPos)) ;
309 tokens.push_back(value) ;
310 lastPos = inputString.find_first_not_of(delimiter, pos) ;
311 pos = inputString.find_first_of(delimiter, lastPos) ;
319 template <
typename T>
321 return typeToString( input ) ;
326 template <
typename T>
327 inline std::string
StringUtil::join(
const std::vector<T> &input,
const std::string &delimiter ) {
328 std::stringstream ss ;
329 for(
auto iter = input.begin() ; iter != input.end() ; ++iter ) {
330 ss << typeToString( *iter ) ;
331 if( std::next(iter) != input.end() ) {
340 template <
typename T>
368 template <
typename T>
371 std::ostringstream oss ;
372 if ((oss << value).fail()) {
380 std::istringstream iss(str) ;
381 if ((iss >> t).fail()) {
407 return value ?
"true" :
"false" ;
411 static const auto true_list = {
"true",
"1",
"on" } ;
412 static const auto false_list = {
"false",
"0",
"off" } ;
414 std::transform(str.begin(), str.end(), strcp.begin(),
415 [](
unsigned char c){
return std::tolower(c); } ) ;
416 if( std::find( std::begin(true_list), std::end(true_list), strcp ) != std::end(true_list) ) {
419 if( std::find( std::begin(false_list), std::end(false_list), strcp ) != std::end(false_list) ) {
428 template <
typename T>
431 std::stringstream ss ;
432 for(
auto iter = value.begin() ; iter != value.end() ; ++iter ) {
433 ss << details::convert<T>::to_string( *iter ) ;
434 if( std::next(iter) != value.end() ) {
441 static inline std::vector<T>
from_string(
const std::string &str ) {
442 std::string::size_type lastPos = str.find_first_not_of(
" ", 0);
443 std::string::size_type pos = str.find_first_of(
" ", lastPos);
444 typename std::vector<T> tokens ;
445 while ((std::string::npos != pos) || (std::string::npos != lastPos)) {
449 lastPos = str.find_first_not_of(
" ", pos) ;
450 pos = str.find_first_of(
" ", lastPos) ;
461 std::stringstream ss ;
462 for(
auto iter = value.begin() ; iter != value.end() ; ++iter ) {
464 if( std::next(iter) != value.end() ) {
471 static inline std::vector<std::string>
from_string(
const std::string &str ) {
472 std::string::size_type lastPos = str.find_first_not_of(
" ", 0);
473 std::string::size_type pos = str.find_first_of(
" ", lastPos);
474 std::vector<std::string> tokens ;
475 while ((std::string::npos != pos) || (std::string::npos != lastPos)) {
476 tokens.emplace_back( str.substr(lastPos, pos - lastPos) ) ;
477 lastPos = str.find_first_not_of(
" ", pos) ;
478 pos = str.find_first_of(
" ", lastPos) ;
486 template <
typename T>
487 inline std::vector<T>
split_string(
const std::string &str,
const std::string &delimiter,
size_t maxTokens = std::numeric_limits<std::size_t>::max()) {
488 if( 0 == maxTokens ) {
491 std::string::size_type lastPos = str.find_first_not_of(delimiter, 0) ;
492 std::string::size_type pos = str.find_first_of(delimiter, lastPos) ;
493 typename std::vector<T> tokens ;
494 while ((std::string::npos != pos) || (std::string::npos != lastPos)) {
495 if( tokens.size()+1 >= maxTokens ) {
504 lastPos = str.find_first_not_of(delimiter, pos) ;
505 pos = str.find_first_of(delimiter, lastPos) ;
513 inline std::vector<std::string>
split_string(
const std::string &str,
const std::string &delimiter,
size_t maxTokens) {
514 if( 0 == maxTokens ) {
517 std::string::size_type lastPos = str.find_first_not_of(delimiter, 0) ;
518 std::string::size_type pos = str.find_first_of(delimiter, lastPos) ;
519 std::vector<std::string> tokens ;
520 while ((std::string::npos != pos) || (std::string::npos != lastPos)) {
521 if( tokens.size()+1 >= maxTokens ) {
528 str.substr(lastPos, pos - lastPos)
530 lastPos = str.find_first_not_of(delimiter, pos) ;
531 pos = str.find_first_of(delimiter, lastPos) ;
538 template <
typename K,
typename V>
539 inline std::vector<K>
keys(
const std::map<K,V> &m ) {
540 typename std::vector<K> keyRet {} ;
541 keyRet.reserve( m.size() ) ;
542 for(
auto &kv : m ) {
543 keyRet.push_back( kv.first ) ;
550 template <
typename K,
typename V>
551 inline std::vector<K>
keys(
const std::unordered_map<K,V> &m ) {
552 typename std::vector<K> keyRet {} ;
553 keyRet.reserve( m.size() ) ;
554 for(
auto &kv : m ) {
555 keyRet.push_back( kv.first ) ;
562 template <
typename K,
typename V>
563 inline std::vector<K>
keys(
const std::vector<std::pair<K,V>> &m ) {
564 typename std::vector<K> keyRet {} ;
565 keyRet.reserve( m.size() ) ;
566 for(
auto &kv : m ) {
567 keyRet.push_back( kv.first ) ;
575 return !str.empty() && std::find_if(str.begin(), str.end(), [](
unsigned char c) {
return !std::isdigit(c); }) == str.end() ;
580 inline unsigned int nthreads(
const std::string &str ) {
584 if( str ==
"auto" ) {
585 return std::thread::hardware_concurrency() ;
593 std::transform( str.begin(), str.end(), str.begin(), [](
unsigned char c) {
return std::tolower( c ) ; } ) ;
598 template <
typename T>
599 inline T
getenv(
const std::string &var ) {
600 auto env =
::getenv( var.c_str() ) ;
601 if(
nullptr == env ) {
609 template <
typename T>
610 inline T
getenv(
const std::string &var,
const T &defVal ) {
611 auto env =
::getenv( var.c_str() ) ;
612 if(
nullptr == env ) {
622 out <<
" __ __ _ _ __ __ _______ " << std::endl ;
623 out <<
" | \\/ | | (_) | \\/ |__ __|" << std::endl ;
624 out <<
" | \\ / | __ _ _ __| |_ _ __ | \\ / | | | " << std::endl ;
625 out <<
" | |\\/| |/ _` | '__| | | '_ \\| |\\/| | | | " << std::endl ;
626 out <<
" | | | | (_| | | | | | | | | | | | | | " << std::endl ;
627 out <<
" |_| |_|\\__,_|_| |_|_|_| |_|_| |_| |_| " << std::endl ;
631 out <<
" LICENCE: GPLv3 " << std::endl ;
632 out <<
" Copyright (C), Marlin/MarlinMT Authors" << std::endl ;
static std::string to_string(const std::string &value)
bool is_number(const std::string &str)
static std::string to_string(const std::vector< std::string > &value)
static std::vector< T > from_string(const std::string &str)
unsigned int nthreads(const std::string &str)
static std::string join(const T &input, const std::string &delimiter=" ")
Weird overload for scalar types.
static std::string typeToString(const T &var)
Convert a type to string.
static std::string to_string(const std::vector< T > &value)
std::chrono::duration< duration_rep, std::micro > microseconds
T getenv(const std::string &var, const T &defVal)
constexpr unsigned long long value(const Flag_t &flag)
static std::string to_string(const bool &value)
static std::vector< std::string > from_string(const std::string &str)
static duration_rep time_difference(const time_point &older, const time_point &ealier)
Get the time difference between two time points.
static unsigned long long int typeHash64()
Generate a hash 64 from the typeid name.
std::chrono::duration< duration_rep, std::nano > nanoseconds
static bool from_string(const std::string &str)
static time_point now()
Get the current time.
std::chrono::duration< duration_rep, std::ratio< 60 > > minutes
clock class Provide a wrapper around a certain clock type in std to perform safe clock measurement in...
#define MARLINMT_THROW(message)
static T from_string(const std::string &str)
static T stringToType(const std::string &str)
Convert a variable to string.
std::chrono::duration< duration_rep > seconds
clock_type::time_point time_point
void to_lower(std::string &str)
static std::string to_string(const T &value)
static std::vector< T > split(const std::string &inputString, const std::string &delimiter=" ")
Split the string with the corresponding delimiter.
StringUtil class Simple utility class for string operations.
std::vector< K > keys(const std::vector< std::pair< K, V >> &m)
HashHelper class Helper class to generate hash 64 id.
std::chrono::steady_clock clock_type
std::vector< std::string > split_string(const std::string &str, const std::string &delimiter, size_t maxTokens)
std::pair< duration_rep, duration_rep > pair
static void crunchFor(duration_rep crunchTime)
Crunch numbers for some time.
std::chrono::duration< duration_rep, std::milli > milliseconds
static duration_rep elapsed_since(const time_point &since)
Get the elapsed time since a previous time point.
static constexpr unsigned long long int doByte(unsigned long long int hash, unsigned char val)
void print_banner(std::ostream &out)
static unsigned long long int hash64(const char *key)
Generate a hash 64 from a string.
static std::string from_string(const std::string &str)