252 lines
13 KiB
C++
252 lines
13 KiB
C++
#pragma once
|
|
|
|
#ifdef __cplusplus
|
|
# include <unistd.h>
|
|
# include <stdint.h>
|
|
|
|
namespace baseutils
|
|
{
|
|
template<typename type_t> inline type_t getNearestMultiple(type_t p_value, size_t p_multiple)
|
|
{
|
|
uint64_t address = static_cast<uint64_t>(p_value);
|
|
uint64_t tmp = address % p_multiple;
|
|
return(static_cast<type_t>(tmp ? address + p_multiple - tmp : address));
|
|
}
|
|
|
|
inline char* getNearestMultiple(char* p_value, size_t p_multiple)
|
|
{
|
|
uint64_t address = reinterpret_cast<uint64_t>(reinterpret_cast<void*>(p_value));
|
|
uint64_t tmp = address % p_multiple;
|
|
return(reinterpret_cast<char*>(tmp ? address + p_multiple - tmp : address));
|
|
}
|
|
|
|
inline char const* getNearestMultiple(char const* p_value, size_t p_multiple)
|
|
{
|
|
uint64_t address = reinterpret_cast<uint64_t>(reinterpret_cast<void const*>(p_value));
|
|
uint64_t tmp = address % p_multiple;
|
|
return(reinterpret_cast<char const*>(tmp ? address + p_multiple - tmp : address));
|
|
}
|
|
|
|
template<typename type_t> inline type_t roundUp(type_t p_value, size_t p_multiple)
|
|
{
|
|
size_t remains = p_value % p_multiple;
|
|
if(remains)
|
|
{
|
|
p_value += static_cast<type_t>(p_multiple - remains);
|
|
}
|
|
return(p_value);
|
|
}
|
|
|
|
# if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
|
|
template<typename T, bool unsigned_t = T(0) < T(-1)> struct IsUnsigned
|
|
{
|
|
static const bool value = unsigned_t;
|
|
};
|
|
|
|
template<typename T> bool isUnsigned(T __attribute__((unused))& p_args)
|
|
{
|
|
return(IsUnsigned<T>::value);
|
|
}
|
|
|
|
template<typename T> struct NumericLimits;
|
|
|
|
template<> struct NumericLimits<unsigned char>
|
|
{
|
|
static const uint8_t smallest = static_cast<uint8_t>(0);
|
|
static const uint8_t biggest = static_cast<uint8_t>(0xFF);
|
|
static const uint8_t size = static_cast<uint8_t>(1);
|
|
static const bool isUnsigned = true;
|
|
static const bool isSigned = false;
|
|
};
|
|
|
|
template<> struct NumericLimits<signed char>
|
|
{
|
|
static const int8_t smallest = static_cast<int8_t>(0x80);
|
|
static const int8_t biggest = static_cast<int8_t>(0x7F);
|
|
static const uint8_t size = static_cast<uint8_t>(1);
|
|
static const bool isUnsigned = false;
|
|
static const bool isSigned = true;
|
|
};
|
|
|
|
template<> struct NumericLimits<unsigned short int>
|
|
{
|
|
static const uint16_t smallest = static_cast<uint16_t>(0);
|
|
static const uint16_t biggest = static_cast<uint16_t>(0xFFFF);
|
|
static const uint8_t size = static_cast<uint8_t>(2);
|
|
static const bool isUnsigned = true;
|
|
static const bool isSigned = false;
|
|
};
|
|
|
|
template<> struct NumericLimits<signed short int>
|
|
{
|
|
static const int16_t smallest = static_cast<int16_t>(0x8000);
|
|
static const int16_t biggest = static_cast<int16_t>(0x7FFF);
|
|
static const uint8_t size = static_cast<uint8_t>(2);
|
|
static const bool isUnsigned = false;
|
|
static const bool isSigned = true;
|
|
};
|
|
|
|
template<> struct NumericLimits<unsigned int>
|
|
{
|
|
static const uint32_t smallest = static_cast<uint32_t>(0);
|
|
static const uint32_t biggest = static_cast<uint32_t>(0xFFFFFFFFU);
|
|
static const uint8_t size = static_cast<uint8_t>(4);
|
|
static const bool isUnsigned = true;
|
|
static const bool isSigned = false;
|
|
};
|
|
|
|
template<> struct NumericLimits<signed int>
|
|
{
|
|
static const int32_t smallest = static_cast<int32_t>(0x80000000);
|
|
static const int32_t biggest = static_cast<int32_t>(0x7FFFFFFF);
|
|
static const uint8_t size = static_cast<uint8_t>(4);
|
|
static const bool isUnsigned = false;
|
|
static const bool isSigned = true;
|
|
};
|
|
|
|
template<> struct NumericLimits<unsigned long int>
|
|
{
|
|
static const uint64_t smallest = static_cast<uint64_t>(0);
|
|
static const uint64_t biggest = static_cast<uint64_t>(0xFFFFFFFFFFFFFFFFULL);
|
|
static const uint8_t size = static_cast<uint8_t>(8);
|
|
static const bool isUnsigned = true;
|
|
static const bool isSigned = false;
|
|
};
|
|
|
|
template<> struct NumericLimits<signed long int>
|
|
{
|
|
static const int64_t smallest = static_cast<int64_t>(0x8000000000000000);
|
|
static const int64_t biggest = static_cast<int64_t>(0x7FFFFFFFFFFFFFFF);
|
|
static const uint8_t size = static_cast<uint8_t>(8);
|
|
static const bool isUnsigned = false;
|
|
static const bool isSigned = true;
|
|
};
|
|
|
|
template<> struct NumericLimits<unsigned long long int>
|
|
{
|
|
static const uint64_t smallest = static_cast<uint64_t>(0);
|
|
static const uint64_t biggest = static_cast<uint64_t>(0xFFFFFFFFFFFFFFFFULL);
|
|
static const uint8_t size = static_cast<uint8_t>(8);
|
|
static const bool isUnsigned = true;
|
|
static const bool isSigned = false;
|
|
};
|
|
|
|
template<> struct NumericLimits<signed long long int>
|
|
{
|
|
static const int64_t smallest = static_cast<int64_t>(0x8000000000000000);
|
|
static const int64_t biggest = static_cast<int64_t>(0x7FFFFFFFFFFFFFFF);
|
|
static const uint8_t size = static_cast<uint8_t>(8);
|
|
static const bool isUnsigned = false;
|
|
static const bool isSigned = true;
|
|
};
|
|
|
|
template<typename T> inline T smallest(T __attribute__((unused))& p_args)
|
|
{
|
|
return(NumericLimits<T>::smallest);
|
|
}
|
|
|
|
template<typename T> inline T biggest(T __attribute__((unused))& p_args)
|
|
{
|
|
return(NumericLimits<T>::biggest);
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------------------------------
|
|
// _ _ _
|
|
// _ __ (_)_ __ | |_| |_ ___ _ __ ______ _
|
|
// | '_ \| | '_ \| __| __/ _ \| '_ \|_ / _` |
|
|
// | |_) | | | | | |_| || (_) | | | |/ / (_| |
|
|
// | .__/|_|_| |_|\__|\__\___/|_| |_/___\__,_|
|
|
// |_|
|
|
--------------------------------------------------------------------------------------------------*/
|
|
|
|
template<typename T> char* pinttonza(char* po_dest, T p_val)
|
|
{
|
|
unsigned long long int val;
|
|
if(baseutils::NumericLimits<T>::isSigned and p_val < 0)
|
|
{
|
|
val = static_cast<unsigned long long int>(-static_cast<long long int>(p_val));
|
|
*po_dest++ = '-';
|
|
}
|
|
else
|
|
{
|
|
val = static_cast<unsigned long long int>(p_val);
|
|
}
|
|
|
|
#ifndef __clang_analyzer__
|
|
switch(val)
|
|
{
|
|
case 10000000000000000000ULL ... 18446744073709551615ULL: *po_dest++ = static_cast<char>( (val / 10000000000000000000ULL) + '0');
|
|
case 1000000000000000000ULL ... 9999999999999999999ULL: *po_dest++ = static_cast<char>(((val % 10000000000000000000ULL) / 1000000000000000000ULL) + '0');
|
|
case 100000000000000000ULL ... 999999999999999999ULL: *po_dest++ = static_cast<char>(((val % 1000000000000000000ULL) / 100000000000000000ULL) + '0');
|
|
case 10000000000000000ULL ... 99999999999999999ULL: *po_dest++ = static_cast<char>(((val % 100000000000000000ULL) / 10000000000000000ULL) + '0');
|
|
case 1000000000000000ULL ... 9999999999999999ULL: *po_dest++ = static_cast<char>(((val % 10000000000000000ULL) / 1000000000000000ULL) + '0');
|
|
case 100000000000000ULL ... 999999999999999ULL: *po_dest++ = static_cast<char>(((val % 1000000000000000ULL) / 100000000000000ULL) + '0');
|
|
case 10000000000000ULL ... 99999999999999ULL: *po_dest++ = static_cast<char>(((val % 100000000000000ULL) / 10000000000000ULL) + '0');
|
|
case 1000000000000ULL ... 9999999999999ULL: *po_dest++ = static_cast<char>(((val % 10000000000000ULL) / 1000000000000ULL) + '0');
|
|
case 100000000000ULL ... 999999999999ULL: *po_dest++ = static_cast<char>(((val % 1000000000000ULL) / 100000000000ULL) + '0');
|
|
case 10000000000ULL ... 99999999999ULL: *po_dest++ = static_cast<char>(((val % 100000000000ULL) / 10000000000ULL) + '0');
|
|
case 1000000000ULL ... 9999999999ULL: *po_dest++ = static_cast<char>(((val % 10000000000ULL) / 1000000000ULL) + '0');
|
|
case 100000000ULL ... 999999999ULL: *po_dest++ = static_cast<char>(((val % 1000000000ULL) / 100000000ULL) + '0');
|
|
case 10000000ULL ... 99999999ULL: *po_dest++ = static_cast<char>(((val % 100000000ULL) / 10000000ULL) + '0');
|
|
case 1000000ULL ... 9999999ULL: *po_dest++ = static_cast<char>(((val % 10000000ULL) / 1000000ULL) + '0');
|
|
case 100000ULL ... 999999ULL: *po_dest++ = static_cast<char>(((val % 1000000ULL) / 100000ULL) + '0');
|
|
case 10000ULL ... 99999ULL: *po_dest++ = static_cast<char>(((val % 100000ULL) / 10000ULL) + '0');
|
|
case 1000ULL ... 9999ULL: *po_dest++ = static_cast<char>(((val % 10000ULL) / 1000ULL) + '0');
|
|
case 100ULL ... 999ULL: *po_dest++ = static_cast<char>(((val % 1000ULL) / 100ULL) + '0');
|
|
case 10ULL ... 99ULL: *po_dest++ = static_cast<char>(((val % 100ULL) / 10ULL) + '0');
|
|
default: *po_dest++ = static_cast<char>( (val % 10ULL) + '0'); break;
|
|
}
|
|
#endif
|
|
return(po_dest);
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------------------------------
|
|
// _ _ _
|
|
// (_)_ __ | |_| |_ ___ _ __ ______ _
|
|
// | | '_ \| __| __/ _ \| '_ \|_ / _` |
|
|
// | | | | | |_| || (_) | | | |/ / (_| |
|
|
// |_|_| |_|\__|\__\___/|_| |_/___\__,_|
|
|
//
|
|
--------------------------------------------------------------------------------------------------*/
|
|
|
|
template<typename T> size_t inttonza(char* po_dest, T p_val)
|
|
{
|
|
return(static_cast<size_t>(pinttonza(po_dest, p_val) - po_dest));
|
|
}
|
|
|
|
/*------------------------------------------------------------------------------------------
|
|
// _ _ _
|
|
// (_)_ __ | |_| |_ ___ __ _
|
|
// | | '_ \| __| __/ _ \ / _` |
|
|
// | | | | | |_| || (_) | (_| |
|
|
// |_|_| |_|\__|\__\___/ \__,_|
|
|
//
|
|
------------------------------------------------------------------------------------------*/
|
|
|
|
template<typename T> size_t inttoa(char* po_dest, T p_val)
|
|
{
|
|
char* ptr = pinttonza(po_dest, p_val);
|
|
*ptr = '\0';
|
|
return(static_cast<size_t>(ptr - po_dest));
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------------------------------
|
|
// _ _ _
|
|
// _ __ (_)_ __ | |_| |_ ___ __ _
|
|
// | '_ \| | '_ \| __| __/ _ \ / _` |
|
|
// | |_) | | | | | |_| || (_) | (_| |
|
|
// | .__/|_|_| |_|\__|\__\___/ \__,_|
|
|
// |_|
|
|
--------------------------------------------------------------------------------------------------*/
|
|
|
|
template<typename T> char* pinttoa(char* po_dest, T p_val)
|
|
{
|
|
char* ptr = pinttonza(po_dest, p_val);
|
|
*ptr = '\0';
|
|
return(ptr);
|
|
}
|
|
# endif
|
|
}
|
|
|
|
#endif
|