20#ifdef __cpp_lib_format
35#include <source_location>
37#include <CArcBaseDllMain.h>
38#include <CArcStringList.h>
40using namespace std::string_literals;
41using namespace std::string_view_literals;
60 template <
typename... Args>
77 throwFormat_t(
const std::string& sFormat,
const std::source_location& tLocation = std::source_location::current() ) :
m_format( sFormat ),
m_location( tLocation )
86 throwFormat_t( std::string_view svFormat,
const std::source_location& tLocation = std::source_location::current() ) :
m_format( svFormat ),
m_location( tLocation )
101 template <
typename... Args>
104 auto sFile = std::filesystem::path( tFormat.
m_location.file_name() ).filename().string();
106 #ifdef __cpp_lib_format
108 throw std::runtime_error( std::format( tFormat.
m_format, std::forward<Args&&>( args )... ) +
109 std::format(
" at file: {}, func: {}, line: {}",
116 std::ostringstream oss;
118 oss << tFormat.
m_format <<
" at file: " << sFile <<
", func: "
119 << tFormat.
m_location.function_name() <<
", line: "
122 throw std::runtime_error( oss.str() );
133 template <
typename... Args>
136 auto sFile = std::filesystem::path( tFormat.
m_location.file_name() ).filename().string();
138 #ifdef __cpp_lib_format
140 throw std::invalid_argument( std::format( tFormat.
m_format, std::forward<Args&&>( args )... ) +
141 std::format(
" at file: {}, func: {}, line: {}",
148 std::ostringstream oss;
150 oss << tFormat.
m_format <<
" at file: " << sFile <<
", func: "
151 << tFormat.
m_location.function_name() <<
", line: "
154 throw std::invalid_argument( oss.str() );
165 template <
typename... Args>
168 throwArcGen4OutOfRange(
const std::uint32_t uiElement,
const std::pair<std::uint32_t, std::uint32_t>&& tRange ,
const std::source_location& location = std::source_location::current() )
170 auto [uiMin, uiMax] { tRange };
172 auto sFile = std::filesystem::path( location.file_name() ).filename().string();
174 #ifdef __cpp_lib_format
176 throw std::out_of_range( std::format(
"Element [ {} ] out of range [ {} - {} ]\nTrace: ( {} {}() line: {} )"sv, uiElement, uiMin, uiMax, sFile, location.function_name(), location.line() ) );
180 std::ostringstream oss;
182 oss <<
"Element [ " << uiElement <<
" ] out of range [ " << uiMin <<
" - " << uiMax <<
" ]\nTrace: ( " << sFile <<
" " << location.function_name() <<
"() line: " << location.line() <<
" )" << std::ends;
184 throw std::out_of_range( oss.str() );
196 template <
typename... Args>
199 auto sFile = std::filesystem::path( tFormat.
m_location.file_name() ).filename().string();
201 #ifdef __cpp_lib_format
203 throw std::length_error( std::format( tFormat.
m_format, std::forward<Args&&>( args )... ) +
204 std::format(
" at file: {}, func: {}, line: {}",
211 std::ostringstream oss;
213 oss << tFormat.
m_format <<
" at file: " << sFile <<
", func: "
214 << tFormat.
m_location.function_name() <<
", line: "
217 throw std::length_error( oss.str() );
257 static void copyMemory(
void* pDest,
void* pSrc, std::size_t uiSize );
266 template<
typename T>
static std::tuple<T*,T*>
copyAndIncrement(
void* pDest,
void* pSrc,
const std::size_t uiSize )
268 T* pInserter =
reinterpret_cast<T*
>( pDest );
269 T* pReader =
reinterpret_cast<T*
>( pSrc );
271 if ( std::is_integral_v<T> )
273 if ( pInserter !=
nullptr && pReader !=
nullptr )
275 for ( std::remove_const_t<
decltype( uiSize )> i = 0; i < uiSize; i++, pInserter++, pReader++ )
277 *pInserter = *pReader;
292 return { pInserter, pReader };
301 template<
typename ErrCode>
306 std::ostringstream oss;
311 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
314 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
319 char szBuffer[ 1024 ] = {
'\0' };
321 std::int32_t iBytes = WideCharToMultiByte( CP_ACP, 0,
reinterpret_cast< LPWSTR
>( lpBuffer ), -1, szBuffer,
sizeof( szBuffer ), NULL, NULL );
328 LocalFree( lpBuffer );
332 std::ostringstream oss;
335 if ( iErrCode != -1 )
337 oss <<
"( errno: " << iErrCode <<
" ) - " << strerror( iErrCode );
345 if ( sMsg.back() ==
'\n' || sMsg.back() ==
'\r' )
347 sMsg.erase( sMsg.end() - 1, sMsg.end() );
357 if ( sMsg.back() ==
'\n' || sMsg.back() ==
'\r' )
359 sMsg.erase( sMsg.end() - 1, sMsg.end() );
379 return static_cast< std::uint32_t
>( ::GetLastError() );
382 static std::int32_t getSystemError(
void )
noexcept
384 return static_cast< std::int32_t
>( errno );
418 [[ deprecated(
"Will be removed in a future release. Use std::format() instead." ) ]]
429 static const std::filesystem::path
incrementFileName(
const std::filesystem::path& tFilePath =
"",
const std::int32_t iStartValue = -1 );
438 static std::filesystem::path
getFileName(
const std::string& sFilter =
"Gen IV Microcontroller binary file\0*.bin\0All\0*.*\0"s )
noexcept;
482 static std::string
uintToString(
const std::uint32_t uiValue )
noexcept;
499 template <
typename T>
static std::string
iterToString( T* begin, T* end,
const char zSeperator =
' ' )
501 std::ostringstream oss;
503 if ( begin ==
nullptr )
508 if ( end ==
nullptr )
513 while ( begin != end )
515 oss << cmdToString( *begin++ ) << zSeperator;
530 static std::unique_ptr<std::uint8_t[]>
readFileBytes(
const std::filesystem::path& tFilePath, std::uint32_t& uiFileLength );
541 static std::string
setDots( std::string_view svText,
const std::size_t uiMaxLength,
const char szDot =
'.' );
548 static std::string
trim(
const std::string& sText )
noexcept;
559 static std::size_t
replaceAll( std::string& sText, std::string_view svWhat, std::string_view svWith );
575 static const std::chrono::time_point<std::chrono::high_resolution_clock>
startCodeTime(
void )
noexcept;
583 template <
typename T>
584 static constexpr auto endCodeTime(
const std::chrono::time_point<std::chrono::high_resolution_clock> tStartTimePoint )
noexcept
586 return std::chrono::duration_cast< T >( std::chrono::high_resolution_clock::now() - tStartTimePoint ).count();
618 struct tFloatConverter
621 std::uint64_t uiValue;
static std::string cmdToString(const std::uint32_t uiCmd)
static std::filesystem::path getFileName(const std::string &sFilter="Gen IV Microcontroller binary file\0*.bin\0All\0*.*\0"s) noexcept
static std::tuple< T *, T * > copyAndIncrement(void *pDest, void *pSrc, const std::size_t uiSize)
static const std::string version(void)
static std::wstring convertAnsiToWide(const char *pszString)
static std::string formatString(const char *pszFmt,...)
CArcBase(const CArcBase &)=delete
CArcBase & operator=(const CArcBase &)=delete
static std::string setDots(std::string_view svText, const std::size_t uiMaxLength, const char szDot='.')
static const std::filesystem::path incrementFileName(const std::filesystem::path &tFilePath="", const std::int32_t iStartValue=-1)
static std::string iterToString(T *begin, T *end, const char zSeperator=' ')
CArcBase(CArcBase &&)=delete
static std::uint32_t getSystemError(void) noexcept
CArcBase & operator=(CArcBase &&)=delete
static constexpr auto endCodeTime(const std::chrono::time_point< std::chrono::high_resolution_clock > tStartTimePoint) noexcept
static const std::string getSystemErrorMessage(void)
static std::string trim(const std::string &sText) noexcept
static pStringList_t splitString(const std::string &sString, const char &zDelim=' ')
static float uintToFloat(const std::uint32_t uiValue)
static std::string uintToString(const std::uint32_t uiValue) noexcept
static std::string convertWideToAnsi(const wchar_t wzString[]) noexcept
static std::string convertWideToAnsi(const std::wstring &wsString)
static std::size_t replaceAll(std::string &sText, std::string_view svWhat, std::string_view svWith)
static void copyMemory(void *pDest, void *pSrc, std::size_t uiSize)
static std::unique_ptr< std::uint8_t[]> readFileBytes(const std::filesystem::path &tFilePath, std::uint32_t &uiFileLength)
static const std::chrono::time_point< std::chrono::high_resolution_clock > startCodeTime(void) noexcept
static const std::string m_sVersion
static const std::string getSystemErrorMessage(ErrCode iErrCode)
static void zeroMemory(void *pDest, std::size_t uiSize)
std::unique_ptr< arc::gen4::CArcStringList > pStringList_t
void throwArcGen4InvalidArgument(const throwFormat_t<> tFormat, Args &&... args)
void throwArcGen4Error(const throwFormat_t<> tFormat, Args &&... args)
void throwArcGen4LengthError(throwFormat_t<> tFormat, Args &&... args)
throwArcGen4OutOfRange(const std::uint32_t uiElement, const std::pair< std::uint32_t, std::uint32_t > &&tRange, const std::source_location &location=std::source_location::current())