24#ifdef __cpp_lib_format
39#include <source_location>
41#include <CArcBaseDllMain.h>
42#include <CArcStringList.h>
44using namespace std::string_literals;
45using namespace std::string_view_literals;
64 template <
typename... Args>
81 throwFormat_t(
const std::string& sFormat,
const std::source_location& tLocation = std::source_location::current() ) :
m_format( sFormat ),
m_location( tLocation )
90 throwFormat_t( std::string_view svFormat,
const std::source_location& tLocation = std::source_location::current() ) :
m_format( svFormat ),
m_location( tLocation )
105 template <
typename... Args>
108 auto sFile = std::filesystem::path( tFormat.
m_location.file_name() ).filename().string();
110 #ifdef __cpp_lib_format
112 throw std::runtime_error( std::format( tFormat.
m_format, std::forward<Args&&>( args )... ) +
113 std::format(
" at file: {}, func: {}, line: {}",
120 std::ostringstream oss;
122 oss << tFormat.
m_format <<
" at file: " << sFile <<
", func: "
123 << tFormat.
m_location.function_name() <<
", line: "
126 throw std::runtime_error( oss.str() );
137 template <
typename... Args>
140 auto sFile = std::filesystem::path( tFormat.
m_location.file_name() ).filename().string();
142 #ifdef __cpp_lib_format
144 throw std::invalid_argument( std::format( tFormat.
m_format, std::forward<Args&&>( args )... ) +
145 std::format(
" at file: {}, func: {}, line: {}",
152 std::ostringstream oss;
154 oss << tFormat.
m_format <<
" at file: " << sFile <<
", func: "
155 << tFormat.
m_location.function_name() <<
", line: "
158 throw std::invalid_argument( oss.str() );
169 template <
typename... Args>
172 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() )
174 auto [uiMin, uiMax] { tRange };
176 auto sFile = std::filesystem::path( location.file_name() ).filename().string();
178 #ifdef __cpp_lib_format
180 throw std::out_of_range( std::format(
"Element [ {} ] out of range [ {} - {} ]\nTrace: ( {} {}() line: {} )"sv, uiElement, uiMin, uiMax, sFile, location.function_name(), location.line() ) );
184 std::ostringstream oss;
186 oss <<
"Element [ " << uiElement <<
" ] out of range [ " << uiMin <<
" - " << uiMax <<
" ]\nTrace: ( " << sFile <<
" " << location.function_name() <<
"() line: " << location.line() <<
" )" << std::ends;
188 throw std::out_of_range( oss.str() );
200 template <
typename... Args>
203 auto sFile = std::filesystem::path( tFormat.
m_location.file_name() ).filename().string();
205 #ifdef __cpp_lib_format
207 throw std::length_error( std::format( tFormat.
m_format, std::forward<Args&&>( args )... ) +
208 std::format(
" at file: {}, func: {}, line: {}",
215 std::ostringstream oss;
217 oss << tFormat.
m_format <<
" at file: " << sFile <<
", func: "
218 << tFormat.
m_location.function_name() <<
", line: "
221 throw std::length_error( oss.str() );
260 static void copyMemory(
void* pDest,
void* pSrc, std::size_t uiSize );
269 template<
typename T>
static std::tuple<T*,T*>
copyAndIncrement(
void* pDest,
void* pSrc,
const std::size_t uiSize )
271 T* pInserter =
reinterpret_cast<T*
>( pDest );
272 T* pReader =
reinterpret_cast<T*
>( pSrc );
274 if ( std::is_integral_v<T> )
276 if ( pInserter !=
nullptr && pReader !=
nullptr )
278 for ( std::remove_const_t<
decltype( uiSize )> i = 0; i < uiSize; i++, pInserter++, pReader++ )
280 *pInserter = *pReader;
295 return { pInserter, pReader };
304 template<
typename ErrCode>
310 std::ostringstream oss;
315 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
318 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
323 char szBuffer[ 1024 ] = {
'\0' };
325 std::int32_t iBytes = WideCharToMultiByte( CP_ACP, 0,
reinterpret_cast< LPWSTR
>( lpBuffer ), -1, szBuffer,
sizeof( szBuffer ), NULL, NULL );
332 LocalFree( lpBuffer );
336 std::ostringstream oss;
339 if ( iErrCode != -1 )
341 oss <<
"( errno: " << iErrCode <<
" ) - " << strerror( iErrCode );
349 if ( sMsg.back() ==
'\n' || sMsg.back() ==
'\r' )
351 sMsg.erase( sMsg.end() - 1, sMsg.end() );
361 if ( sMsg.back() ==
'\n' || sMsg.back() ==
'\r' )
363 sMsg.erase( sMsg.end() - 1, sMsg.end() );
383 return static_cast< std::uint32_t
>( ::GetLastError() );
386 static std::int32_t getSystemError(
void )
noexcept
388 return static_cast< std::int32_t
>( errno );
422 [[ deprecated(
"Will be removed in a future release. Use std::format() instead." ) ]]
433 static const std::filesystem::path
incrementFileName(
const std::filesystem::path& tFilePath =
"",
const std::int32_t iStartValue = -1 );
485 template <
typename T>
static std::string
iterToString( T* begin, T* end,
const char zSeperator =
' ' )
487 std::ostringstream oss;
489 if ( begin ==
nullptr )
494 if ( end ==
nullptr )
499 while ( begin != end )
501 oss << cmdToString( *begin++ ) << zSeperator;
516 static std::string
setDots(
const std::string& sText,
const std::size_t uiMaxLength,
const char szDot =
'.' );
523 static std::string
trim(
const std::string& sText )
noexcept;
530 static const std::chrono::time_point<std::chrono::high_resolution_clock>
startCodeTime(
void )
noexcept;
538 template <
typename T>
539 static constexpr auto endCodeTime(
const std::chrono::time_point<std::chrono::high_resolution_clock> tStartTimePoint )
noexcept
541 return std::chrono::duration_cast< T >( std::chrono::high_resolution_clock::now() - tStartTimePoint ).count();
static std::string cmdToString(const std::uint32_t uiCmd)
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 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 std::string setDots(const std::string &sText, const std::size_t uiMaxLength, const char szDot='.')
static std::string convertWideToAnsi(const wchar_t wzString[]) noexcept
static std::string convertWideToAnsi(const std::wstring &wsString)
static void copyMemory(void *pDest, void *pSrc, std::size_t uiSize)
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())