diff --git a/CMakeLists.txt b/CMakeLists.txt index 7fd06b2..1610a6a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,7 +76,6 @@ SET ( cppcore_src SET ( cppcore_common_src include/cppcore/Common/Hash.h include/cppcore/Common/TStringBase.h - include/cppcore/Common/TSharedPtr.h include/cppcore/Common/Variant.h include/cppcore/Common/TBitField.h include/cppcore/Common/TOptional.h @@ -136,7 +135,6 @@ IF( CPPCORE_BUILD_UNITTESTS ) test/common/VariantTest.cpp test/common/TBitFieldTest.cpp test/common/TOptionalTest.cpp - test/common/TSharedPtrTest.cpp ) SET( cppcore_container_test_src diff --git a/code/Random/RandomGenerator.cpp b/code/Random/RandomGenerator.cpp index 7780132..4692e4c 100644 --- a/code/Random/RandomGenerator.cpp +++ b/code/Random/RandomGenerator.cpp @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in @@ -28,11 +28,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. namespace cppcore { -static const unsigned int N = 624; -static const unsigned int M = 397; +static constexpr unsigned int N = 624; +static constexpr unsigned int M = 397; -static void mersenne_twister_vector_init( unsigned int *seedPoints, size_t len ) { - assert( nullptr != seedPoints ); +static bool mersenne_twister_vector_init( unsigned int *seedPoints, size_t len ) { + if (seedPoints == nullptr) { + return false; + } const unsigned int mult = 1812433253ul; unsigned int seed = 5489ul; @@ -40,9 +42,15 @@ static void mersenne_twister_vector_init( unsigned int *seedPoints, size_t len ) seedPoints[ i ] = seed; seed = mult * (seed ^ (seed >> 30)) + (static_cast(i) + 1); } + + return true; } -static void mersenne_twister_vector_update(unsigned int* const p) { +static bool mersenne_twister_vector_update(unsigned int* const p) { + if (p == nullptr) { + return false; + } + static const unsigned int A[ 2 ] = { 0, 0x9908B0DF }; unsigned int i=0; for (; i < N - M; i++) { @@ -52,6 +60,8 @@ static void mersenne_twister_vector_update(unsigned int* const p) { p[i] = p[i + (M - N)] ^ (((p[i] & 0x80000000) | (p[i + 1] & 0x7FFFFFFF)) >> 1) ^ A[p[i + 1] & 1]; } p[N - 1] = p[M - 1] ^ (((p[N - 1] & 0x80000000) | (p[0] & 0x7FFFFFFF)) >> 1) ^ A[p[0] & 1]; + + return true; } unsigned int mersenne_twister() { @@ -60,13 +70,15 @@ unsigned int mersenne_twister() { // readout index static int idx = N + 1; + bool ok = true; if (static_cast(idx) >= N) { if (static_cast(idx) > N) { - mersenne_twister_vector_init(vector, N); + ok &= mersenne_twister_vector_init(vector, N); } - mersenne_twister_vector_update(vector); + ok &= mersenne_twister_vector_update(vector); idx = 0; } + assert(ok); unsigned int e = vector[ idx++ ]; // Tempering @@ -80,7 +92,7 @@ unsigned int mersenne_twister() { RandomGenerator::RandomGenerator( GeneratorType type ) noexcept : m_type( type ) { - ::srand( static_cast(time(nullptr))); + ::srand(static_cast(time(nullptr))); } int RandomGenerator::get( int lower, int upper ) { diff --git a/include/cppcore/Common/Hash.h b/include/cppcore/Common/Hash.h index f41af5a..4c3c4aa 100644 --- a/include/cppcore/Common/Hash.h +++ b/include/cppcore/Common/Hash.h @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2014-2024 Kim Kulling +Copyright (c) 2014-2025 Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/include/cppcore/Common/TSharedPtr.h b/include/cppcore/Common/TSharedPtr.h deleted file mode 100644 index fb1acbc..0000000 --- a/include/cppcore/Common/TSharedPtr.h +++ /dev/null @@ -1,163 +0,0 @@ -/*----------------------------------------------------------------------------------------------- -The MIT License (MIT) - -Copyright (c) 2014-2024 Kim Kulling - -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software is furnished to do so, -subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR -COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER -IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------------------------*/ -#pragma once - -#include - -namespace cppcore { - -//------------------------------------------------------------------------------------------------- -/// @class TSharedPtr -/// @ingroup CPPCore -/// -/// @brief This class a shared pointer implementation. -//------------------------------------------------------------------------------------------------- -template -class TSharedPtr { -public: - typedef void (*deleterFunc)(T *ptr); - - TSharedPtr(); - explicit TSharedPtr(T *ptr, deleterFunc func = nullptr); - TSharedPtr(const TSharedPtr &rhs); - ~TSharedPtr(); - void reset(T *ptr, deleterFunc func = nullptr); - void clear(); - unsigned int getRefs() const; - T *operator->() const; - T &operator*() const; - TSharedPtr &operator=(const TSharedPtr &rhs); - bool operator==(const TSharedPtr &rhs) const; - bool operator!=(const TSharedPtr &rhs) const; - -private: - struct PtrType { - unsigned int m_refs; - T *m_ptr; - deleterFunc m_delFunc; - - PtrType(T *ptr, deleterFunc func) : - m_refs(0), m_ptr(ptr), m_delFunc(func) { - if (nullptr != m_ptr) { - ++m_refs; - } - } - - ~PtrType() { - if (nullptr == m_delFunc) { - delete m_ptr; - } else { - m_delFunc(m_ptr); - } - } - - PtrType() = delete; - PtrType(const PtrType &rhs) = delete; - }; - PtrType *m_ptrType; -}; - -template -inline TSharedPtr::TSharedPtr() : - m_ptrType(nullptr) { - // empty -} - -template -inline TSharedPtr::TSharedPtr(T *ptr, deleterFunc func) : - m_ptrType(nullptr) { - m_ptrType = new PtrType(ptr, func); -} - -template -inline TSharedPtr::TSharedPtr(const TSharedPtr &rhs) : - m_ptrType( rhs.m_ptrType ) { - if (nullptr != m_ptrType) { - m_ptrType->m_refs++; - } -} - -template -inline TSharedPtr::~TSharedPtr() { - clear(); -} - -template -inline void TSharedPtr::reset(T *ptr, deleterFunc func) { - if (nullptr != m_ptrType) { - clear(); - } - m_ptrType = new PtrType(ptr, func); -} - -template -inline void TSharedPtr::clear() { - if (nullptr == m_ptrType) { - return; - } - - m_ptrType->m_refs--; - if (0 == m_ptrType->m_refs) { - delete m_ptrType; - } - m_ptrType = nullptr; -} - -template -inline unsigned int TSharedPtr::getRefs() const { - return m_ptrType->m_refs; -} - -template -inline T *TSharedPtr::operator->() const { - return m_ptrType->m_ptr; -} - -template -inline T &TSharedPtr::operator*() const { - return *m_ptrType->m_ptr; -} - -template -inline TSharedPtr &TSharedPtr::operator=(const TSharedPtr &rhs) { - clear(); - m_ptrType = rhs.m_ptrType; - m_ptrType->m_refs++; - - return *this; -} - -template -inline bool TSharedPtr::operator==(const TSharedPtr &rhs) const { - if (rhs.m_ptrType == m_ptrType) { - return true; - } - return false; -} - -template -inline bool TSharedPtr::operator!=(const TSharedPtr &rhs) const { - return !(*this == rhs); -} - -} // Namespace cppcore diff --git a/include/cppcore/IO/FileSystem.h b/include/cppcore/IO/FileSystem.h index 79f11fd..befdae5 100644 --- a/include/cppcore/IO/FileSystem.h +++ b/include/cppcore/IO/FileSystem.h @@ -22,10 +22,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -----------------------------------------------------------------------------------------------*/ #pragma once -#ifdef WIN32 -#include +#ifdef _WIN32 +# include #else -#include +# include #endif namespace cppcore { @@ -75,7 +75,7 @@ inline void FileSystem::refresh() { if (m_drive == nullptr) { return; } -#ifdef WIN32 +#ifdef _WIN32 PULARGE_INTEGER freeByteAvailable = 0, totalNumberOfBytes = 0, totalNumberOfFreeBytes = 0; BOOL result = ::GetDiskFreeSpaceEx(m_drive, freeByteAvailable, totalNumberOfBytes, totalNumberOfFreeBytes); if (TRUE == result) { diff --git a/test/common/TSharedPtrTest.cpp b/test/common/TSharedPtrTest.cpp deleted file mode 100644 index 210637c..0000000 --- a/test/common/TSharedPtrTest.cpp +++ /dev/null @@ -1,61 +0,0 @@ -/*----------------------------------------------------------------------------------------------- -The MIT License (MIT) - -Copyright (c) 2014-2024 Kim Kulling - -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software is furnished to do so, -subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR -COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER -IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------------------------*/ -#include - -#include - -using namespace ::cppcore; - -class TSharedPtrTest : public ::testing::Test { - // empty -}; - -TEST_F( TSharedPtrTest, createInstance_success ) { - bool ok( true ); - try { - int *ptr = new int; - TSharedPtr myPtr( ptr ); - } catch ( ... ) { - ok = false; - } - EXPECT_TRUE( ok ); -} - -TEST_F( TSharedPtrTest, copyPtr_success ) { - int *ptr = new int; - TSharedPtr myPtr1( ptr ); - TSharedPtr myPtr2( myPtr1 ); - unsigned int refs1 = myPtr2.getRefs(); - EXPECT_EQ( 2U, refs1 ); - - myPtr2.clear(); - unsigned int refs2 = myPtr1.getRefs(); - EXPECT_EQ( refs2, refs1-1 ); -} - -TEST_F( TSharedPtrTest, resetPtr_success ) { - int *ptr = new int; - TSharedPtr myPtr1( ptr ); - - myPtr1.reset( new int ); -}