diff --git a/CMakeLists.txt b/CMakeLists.txt index ef87c3d..06ba38f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -110,7 +110,7 @@ SET ( cppcore_container_src SOURCE_GROUP( code FILES ${cppcore_src} ) SOURCE_GROUP( code\\common FILES ${cppcore_common_src} ) SOURCE_GROUP( code\\container FILES ${cppcore_container_src} ) -SOURCE_GROUP( code\\IO FILES ${cppcore_io_src} ) +SOURCE_GROUP( code\\io FILES ${cppcore_io_src} ) SOURCE_GROUP( code\\memory FILES ${cppcore_memory_src} ) SOURCE_GROUP( code\\random FILES ${cppcore_random_src} ) @@ -139,12 +139,17 @@ IF( CPPCORE_BUILD_UNITTESTS ) SET( cppcore_container_test_src test/container/TArrayTest.cpp + test/container/TAlgorithmTest.cpp test/container/THashMapTest.cpp test/container/TListTest.cpp test/container/TQueueTest.cpp test/container/TStaticArrayTest.cpp ) + SET( cppcore_io_test_src + test/io/FileSystemTest.cpp + ) + SET( cppcore_memory_test_src test/memory/TStackAllocatorTest.cpp test/memory/TPoolAllocatorTest.cpp @@ -152,14 +157,15 @@ IF( CPPCORE_BUILD_UNITTESTS ) ) SET( cppcore_random_test_src - test/Random/RandomGeneratorTest.cpp + test/random/RandomGeneratorTest.cpp ) - SOURCE_GROUP( code FILES ${cppcore_test_src} ) - SOURCE_GROUP( code\\common FILES ${cppcore_common_test_src} ) - SOURCE_GROUP( code\\container FILES ${cppcore_container_test_src} ) - SOURCE_GROUP( code\\memory FILES ${cppcore_memory_test_src} ) - SOURCE_GROUP( code\\random FILES ${cppcore_random_test_src} ) + SOURCE_GROUP(code FILES ${cppcore_test_src} ) + SOURCE_GROUP(code\\common FILES ${cppcore_common_test_src} ) + SOURCE_GROUP(code\\io FILES ${cppcore_io_test_src} ) + SOURCE_GROUP(code\\container FILES ${cppcore_container_test_src} ) + SOURCE_GROUP(code\\memory FILES ${cppcore_memory_test_src} ) + SOURCE_GROUP(code\\random FILES ${cppcore_random_test_src} ) # Prevent overriding the parent project's compiler/linker # settings on Windows @@ -179,6 +185,7 @@ IF( CPPCORE_BUILD_UNITTESTS ) ADD_EXECUTABLE( cppcore_unittest ${cppcore_test_src} ${cppcore_common_test_src} + ${cppcore_io_test_src} ${cppcore_memory_test_src} ${cppcore_random_test_src} ${cppcore_container_test_src} diff --git a/code/Random/RandomGenerator.cpp b/code/Random/RandomGenerator.cpp index 4692e4c..b5a3f97 100644 --- a/code/Random/RandomGenerator.cpp +++ b/code/Random/RandomGenerator.cpp @@ -91,15 +91,15 @@ unsigned int mersenne_twister() { } RandomGenerator::RandomGenerator( GeneratorType type ) noexcept : - m_type( type ) { - ::srand(static_cast(time(nullptr))); + mType( type ) { + ::srand( static_cast(time(nullptr))); } int RandomGenerator::get( int lower, int upper ) { int ret( 0 ); - if ( GeneratorType::Standard == m_type ) { + if ( GeneratorType::Standard == mType ) { ret = ::rand() % upper + lower; - } else if (GeneratorType::MersenneTwister == m_type) { + } else if (GeneratorType::MersenneTwister == mType) { ret = mersenne_twister() % upper + lower; } diff --git a/contrib/googletest-1.15.2/googletest/generated/gmock.pc b/contrib/googletest-1.15.2/googletest/generated/gmock.pc index cf23988..72457e0 100644 --- a/contrib/googletest-1.15.2/googletest/generated/gmock.pc +++ b/contrib/googletest-1.15.2/googletest/generated/gmock.pc @@ -1,5 +1,5 @@ -libdir=C:/Program Files (x86)/osre/lib -includedir=C:/Program Files (x86)/osre/include +libdir=C:/Program Files (x86)/cppcore/lib +includedir=C:/Program Files (x86)/cppcore/include Name: gmock Description: GoogleMock (without main() function) diff --git a/contrib/googletest-1.15.2/googletest/generated/gmock_main.pc b/contrib/googletest-1.15.2/googletest/generated/gmock_main.pc index 3dadafc..c7badcf 100644 --- a/contrib/googletest-1.15.2/googletest/generated/gmock_main.pc +++ b/contrib/googletest-1.15.2/googletest/generated/gmock_main.pc @@ -1,5 +1,5 @@ -libdir=C:/Program Files (x86)/osre/lib -includedir=C:/Program Files (x86)/osre/include +libdir=C:/Program Files (x86)/cppcore/lib +includedir=C:/Program Files (x86)/cppcore/include Name: gmock_main Description: GoogleMock (with main() function) diff --git a/contrib/googletest-1.15.2/googletest/generated/gtest.pc b/contrib/googletest-1.15.2/googletest/generated/gtest.pc index d512a14..fb449b7 100644 --- a/contrib/googletest-1.15.2/googletest/generated/gtest.pc +++ b/contrib/googletest-1.15.2/googletest/generated/gtest.pc @@ -1,5 +1,5 @@ -libdir=C:/Program Files (x86)/osre/lib -includedir=C:/Program Files (x86)/osre/include +libdir=C:/Program Files (x86)/cppcore/lib +includedir=C:/Program Files (x86)/cppcore/include Name: gtest Description: GoogleTest (without main() function) diff --git a/contrib/googletest-1.15.2/googletest/generated/gtest_main.pc b/contrib/googletest-1.15.2/googletest/generated/gtest_main.pc index 0d18961..86c6b34 100644 --- a/contrib/googletest-1.15.2/googletest/generated/gtest_main.pc +++ b/contrib/googletest-1.15.2/googletest/generated/gtest_main.pc @@ -1,5 +1,5 @@ -libdir=C:/Program Files (x86)/osre/lib -includedir=C:/Program Files (x86)/osre/include +libdir=C:/Program Files (x86)/cppcore/lib +includedir=C:/Program Files (x86)/cppcore/include Name: gtest_main Description: GoogleTest (with main() function) diff --git a/contrib/googletest-1.15.2/googletest/gtest.dir/Debug/gtest.lib.recipe b/contrib/googletest-1.15.2/googletest/gtest.dir/Debug/gtest.lib.recipe index d203f96..c4a5ed7 100644 --- a/contrib/googletest-1.15.2/googletest/gtest.dir/Debug/gtest.lib.recipe +++ b/contrib/googletest-1.15.2/googletest/gtest.dir/Debug/gtest.lib.recipe @@ -2,7 +2,7 @@ - C:\develop\projects\osre\x64\Debug\ZERO_CHECK + C:\develop\projects\cppcore\x64\Debug\ZERO_CHECK diff --git a/contrib/googletest-1.15.2/googletest/gtest_main.dir/Debug/gtest_main.lib.recipe b/contrib/googletest-1.15.2/googletest/gtest_main.dir/Debug/gtest_main.lib.recipe index d203f96..c4a5ed7 100644 --- a/contrib/googletest-1.15.2/googletest/gtest_main.dir/Debug/gtest_main.lib.recipe +++ b/contrib/googletest-1.15.2/googletest/gtest_main.dir/Debug/gtest_main.lib.recipe @@ -2,7 +2,7 @@ - C:\develop\projects\osre\x64\Debug\ZERO_CHECK + C:\develop\projects\cppcore\x64\Debug\ZERO_CHECK diff --git a/include/cppcore/Container/TAlgorithm.h b/include/cppcore/Container/TAlgorithm.h new file mode 100644 index 0000000..b9ef97f --- /dev/null +++ b/include/cppcore/Container/TAlgorithm.h @@ -0,0 +1,42 @@ +/*----------------------------------------------------------------------------------------------- +The MIT License (MIT) + +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 +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 +#include + +namespace cppcore { + +/// @brief Function to calulate the distance between two iterators. +/// @tparam TIt The container iterator +/// @param begin The first container iterator +/// @param end The second container iterator +/// @return The distance between the two iterators. +template +inline size_t distance(TIt begin, TIt end) { + assert(begin < end); + const size_t distance = end - begin; + return distance; +} + +} // namespace cppcore diff --git a/include/cppcore/Container/TArray.h b/include/cppcore/Container/TArray.h index ea90e05..b7bc463 100644 --- a/include/cppcore/Container/TArray.h +++ b/include/cppcore/Container/TArray.h @@ -28,6 +28,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. namespace cppcore { namespace Details { +//------------------------------------------------------------------------------------------------- inline static size_t getGrowing(size_t size) { if (0u == size) { return 0u; @@ -60,14 +61,6 @@ class TArray { /// The index address type using array_size_type = size_t; - /// @enum SortedMode - /// @brief This enum describes the sorting mode of the array. - enum class SortMode { - Unsorted, ///< No sorting performed. - Ascending, ///< The array is sorted ascending. - Descending ///< The array is sorted descending. - }; - public: /// @brief The default class constructor. TArray(); @@ -154,10 +147,10 @@ class TArray { /// @return true, if the array is empty, false if not. bool isEmpty() const; - /// @brief Search for a given item in the array. + /// @brief Performs a linear search for a given item in the array. /// @param[in] item The item to look for. /// @return An iterator showing to the position will be returned. - Iterator find(const T &item); + Iterator linearSearch(const T &item); /// @brief The array will be cleared, destructor of the items will be called. void clear(); @@ -175,7 +168,7 @@ class TArray { /// @brief Will return the data pointer. /// @return The data pointer. - T data() const; + T *data() const; /// @brief The [] operator. T &operator[](array_size_type idx) const; @@ -188,42 +181,28 @@ class TArray { private: TAlloc mAllocator; - SortMode m_Sorted; - size_t m_Size; - size_t m_Capacity; - T *m_pData; + size_t mSize = 0u; + size_t mCapacity = 0u; + T *mData = nullptr; }; template -inline TArray::TArray() : - mAllocator(), - m_Sorted(SortMode::Unsorted), - m_Size(0u), - m_Capacity(0u), - m_pData(nullptr) { +inline TArray::TArray() { // empty } template -inline TArray::TArray(size_t size) : - m_Sorted(SortMode::Unsorted), - m_Size(0u), - m_Capacity(0u), - m_pData(nullptr) { +inline TArray::TArray(size_t size) { const size_t capa = Details::getGrowing(size); reserve(capa); resize(size); } template -inline TArray::TArray(const TArray &rhs) : - m_Sorted(rhs.m_Sorted), - m_Size(0u), - m_Capacity(0u), - m_pData(nullptr) { - resize(rhs.m_Size); - for (size_t i = 0u; i < m_Size; ++i) { - m_pData[i] = rhs.m_pData[i]; +inline TArray::TArray(const TArray &rhs) { + resize(rhs.mSize); + for (size_t i = 0u; i < mSize; ++i) { + mData[i] = rhs.mData[i]; } } @@ -234,38 +213,38 @@ inline TArray::~TArray() { template inline void TArray::add(const T &value) { - if (m_Size + 1 > m_Capacity) { - const size_t newcapa = Details::getGrowing(m_Size + 1); - reserve(m_Capacity + newcapa); + if (mSize + 1 > mCapacity) { + const size_t newcapa = Details::getGrowing(mSize + 1); + reserve(mCapacity + newcapa); } - m_pData[m_Size] = value; - ++m_Size; + mData[mSize] = value; + ++mSize; } template inline void TArray::add(const T *newValues, size_t numItems) { - if (0 == numItems) { + if (0 == numItems || newValues == nullptr) { return; } - const size_t newCapa = m_Size + numItems; - if (newCapa > m_Capacity) { - reserve(m_Capacity + newCapa); + const size_t newCapa = mSize + numItems; + if (newCapa > mCapacity) { + reserve(mCapacity + newCapa); } for (size_t i = 0; i < numItems; ++i) { - m_pData[m_Size] = newValues[i]; - m_Size++; + mData[mSize] = newValues[i]; + ++mSize; } } template inline void TArray::remove(size_t index) { - assert(index < m_Size); + assert(index < mSize); - if (index == m_Size - 1) { + if (index == mSize - 1) { // Destroy the last index destroy(index); - --m_Size; + --mSize; } else { // Remove an index in the middle this->move(index + 1, index); @@ -279,7 +258,7 @@ inline void TArray::remove(Iterator it) { // Look out for the index long pos = -1; Iterator currentIt = this->begin(); - for (size_t i = 0; i < m_Size; ++i) { + for (size_t i = 0; i < mSize; ++i) { if (it == currentIt) { pos = static_cast(i); break; @@ -307,35 +286,35 @@ inline void TArray::set(const T &value) { } for (size_t i = 0; i < size(); ++i) { - m_pData[i] = value; + mData[i] = value; } } template inline void TArray::destroy(size_t index) { - T *pElem = &(m_pData[index]); + T *pElem = &(mData[index]); pElem->~T(); } template inline T &TArray::front() { - assert(m_Size > 0); + assert(mSize > 0); - return m_pData[0]; + return mData[0]; } template inline T &TArray::back() { - assert(m_Size > 0); + assert(mSize > 0); - return (m_pData[m_Size - 1]); + return (mData[mSize - 1]); } template inline const T &TArray::back() const { - assert(m_Size > 0); + assert(mSize > 0); - return (m_pData[m_Size - 1]); + return (mData[mSize - 1]); } template @@ -344,16 +323,16 @@ inline void TArray::move(size_t fromIdx, size_t toIdx) { return; } - const size_t numElements = m_Size - fromIdx; + const size_t numElements = mSize - fromIdx; const size_t newSize = toIdx + numElements; - while (m_Capacity < newSize) { - resize(m_Capacity + Details::getGrowing(newSize - m_Capacity)); + while (mCapacity < newSize) { + resize(mCapacity + Details::getGrowing(newSize - mCapacity)); } size_t index = 0u; if (fromIdx > toIdx) { for (size_t i = 0; i < numElements; ++i) { - m_pData[toIdx + i] = m_pData[fromIdx + i]; + mData[toIdx + i] = mData[fromIdx + i]; } for (size_t i = toIdx; i < fromIdx + 1; --i) { @@ -362,7 +341,7 @@ inline void TArray::move(size_t fromIdx, size_t toIdx) { } } else { for (size_t i = numElements - 1; i != 0; --i) { - m_pData[toIdx + i] = m_pData[fromIdx + i]; + mData[toIdx + i] = mData[fromIdx + i]; } for (size_t i = 0; i < numElements; i++) { @@ -371,68 +350,68 @@ inline void TArray::move(size_t fromIdx, size_t toIdx) { } } - m_Size = toIdx + numElements; + mSize = toIdx + numElements; } template inline void TArray::reserve(size_t capacity) { - if (capacity <= m_Capacity) { + if (capacity <= mCapacity) { return; } - T *pTmp = nullptr; - if (m_Size > 0u) { - pTmp = mAllocator.alloc(m_Size); - for (size_t i = 0u; i < m_Size; ++i) { - pTmp[i] = m_pData[i]; + T *tmp = nullptr; + if (mSize > 0u) { + tmp = mAllocator.alloc(mSize); + for (size_t i = 0u; i < mSize; ++i) { + tmp[i] = mData[i]; } } - if (m_pData) { - mAllocator.release(m_pData); - m_pData = nullptr; + if (mData) { + mAllocator.release(mData); + mData = nullptr; } - m_pData = mAllocator.alloc(capacity); - m_Capacity = capacity; + mData = mAllocator.alloc(capacity); + mCapacity = capacity; - if (m_Size > 0u) { - for (size_t i = 0u; i < m_Size; ++i) { - m_pData[i] = pTmp[i]; + if (mSize > 0u) { + for (size_t i = 0u; i < mSize; ++i) { + mData[i] = tmp[i]; } - mAllocator.release(pTmp); + mAllocator.release(tmp); } } template inline void TArray::resize(size_t size) { - size_t oldSize = m_Size; - T *pTmp = nullptr; + const size_t oldSize = mSize; + T *tmp = nullptr; // Store older items - if (m_Size > 0u && m_Capacity < size) { - pTmp = mAllocator.alloc(m_Size * sizeof(T)); - for (size_t i = 0u; i < m_Size; ++i) { - pTmp[i] = m_pData[i]; + if (mSize > 0u && mCapacity < size) { + tmp = mAllocator.alloc(mSize * sizeof(T)); + for (size_t i = 0u; i < mSize; ++i) { + tmp[i] = mData[i]; } - mAllocator.release(m_pData); - m_pData = nullptr; - m_Capacity = 0u; + mAllocator.release(mData); + mData = nullptr; + mCapacity = 0u; } // Realloc memory - if (size > m_Capacity) { - m_pData = mAllocator.alloc(size * sizeof(T)); - if (pTmp) { + if (size > mCapacity) { + mData = mAllocator.alloc(size * sizeof(T)); + if (tmp) { for (size_t i = 0u; i < oldSize; ++i) { - m_pData[i] = pTmp[i]; + mData[i] = tmp[i]; } - mAllocator.release(pTmp); + mAllocator.release(tmp); } - m_Capacity = size; + mCapacity = size; } - m_Size = size; + mSize = size; } template @@ -440,34 +419,31 @@ inline void TArray::resize(size_t size, T val) { resize(size); if (0u != size) { for (size_t i = 0u; i < size; ++i) { - m_pData[i] = val; + mData[i] = val; } } } template inline size_t TArray::size() const { - return m_Size; + return mSize; } template inline size_t TArray::capacity() const { - return m_Capacity; + return mCapacity; } template inline bool TArray::isEmpty() const { - return (0u == m_Size); + return (0u == mSize); } template -inline typename TArray::Iterator - TArray::find(const T &item) { - if (m_Sorted == SortMode::Unsorted) { - for (Iterator it = begin(); it != end(); ++it) { - if (item == *it) { - return it; - } +inline typename TArray::Iterator TArray::linearSearch(const T &item) { + for (Iterator it = begin(); it != end(); ++it) { + if (item == *it) { + return it; } } @@ -476,10 +452,10 @@ inline typename TArray::Iterator template inline void TArray::clear() { - mAllocator.release(m_pData); - m_pData = nullptr; - m_Size = 0u; - m_Capacity = 0u; + mAllocator.release(mData); + mData = nullptr; + mSize = 0u; + mCapacity = 0u; } template @@ -488,28 +464,28 @@ inline typename TArray::Iterator TArray::begin() { return end(); } - return &m_pData[0]; + return &mData[0]; } template inline typename TArray::Iterator TArray::end() { - T *tmp = m_pData; - tmp += m_Size; + T *tmp = mData; + tmp += mSize; return tmp; } template -inline T TArray::data() const { - return m_pData; +inline T *TArray::data() const { + return mData; } template inline T &TArray::operator[](size_t idx) const { - assert(idx < m_Size); - assert(nullptr != m_pData); + assert(idx < mSize); + assert(nullptr != mData); - return m_pData[idx]; + return mData[idx]; } template @@ -520,12 +496,12 @@ inline TArray &TArray::operator=(const TArray & resize(other.size()); if (!other.isEmpty()) { - assert(nullptr != other.m_pData); - if (m_pData) { + assert(nullptr != other.mData); + if (mData) { for (size_t i = 0u; i < other.size(); ++i) { - m_pData[i] = other.m_pData[i]; + mData[i] = other.mData[i]; } - m_Size = other.m_Size; + mSize = other.mSize; } } @@ -534,12 +510,12 @@ inline TArray &TArray::operator=(const TArray & template inline bool TArray::operator == (const TArray &rhs) const { - if (rhs.m_Size != m_Size) { + if (rhs.mSize != mSize) { return false; } - for (size_t i = 0u; i < m_Size; ++i) { - if (m_pData[i] != rhs.m_pData[i]) { + for (size_t i = 0u; i < mSize; ++i) { + if (mData[i] != rhs.mData[i]) { return false; } } diff --git a/include/cppcore/Container/THashMap.h b/include/cppcore/Container/THashMap.h index e594d38..bb20a2f 100644 --- a/include/cppcore/Container/THashMap.h +++ b/include/cppcore/Container/THashMap.h @@ -55,7 +55,6 @@ class THashMap { /// @brief Marker for unset node keys. static constexpr unsigned int UnsetNode = 999999999; -public: /// @brief The class constructor. /// @param init [in] The initial size for the hash. explicit THashMap(size_t init = InitSize); @@ -114,27 +113,24 @@ class THashMap { private: struct Node { - T m_key; - U m_value; - Node *m_next; + T mKey = UnsetNode; + U mValue; + Node *mNext; Node(); - ~Node(); + ~Node() = default; void append(T key, const U &value); bool remove(T key); void releaseList(); }; - Node **m_buffer; - size_t m_numItems; - size_t m_buffersize; + Node **mBuffer = nullptr; + size_t mNumItems = 0u; + size_t mBuffersize = 0u; }; template -inline THashMap::THashMap(size_t initSize) : - m_buffer(nullptr), - m_numItems(0u), - m_buffersize(0u) { +inline THashMap::THashMap(size_t initSize) { init(initSize); } @@ -145,76 +141,76 @@ inline THashMap::~THashMap() { template inline size_t THashMap::size() const { - return m_numItems; + return mNumItems; } template inline size_t THashMap::capacity() const { - return m_buffersize; + return mBuffersize; } template inline bool THashMap::isEmpty() const { - return (0u == m_numItems); + return (0u == mNumItems); } template inline void THashMap::init(size_t init) { - m_buffer = new Node *[init]; + mBuffer = new Node *[init]; for (size_t i = 0; i < init; i++) { - m_buffer[i] = nullptr; + mBuffer[i] = nullptr; } - m_buffersize = init; + mBuffersize = init; } template inline void THashMap::clear() { - for (size_t i = 0; i < m_buffersize; ++i) { - if (nullptr != m_buffer[i]) { - m_buffer[i]->releaseList(); + for (size_t i = 0; i < mBuffersize; ++i) { + if (nullptr != mBuffer[i]) { + mBuffer[i]->releaseList(); } } - delete[] m_buffer; - m_buffer = nullptr; - m_numItems = 0; - m_buffersize = 0; + delete[] mBuffer; + mBuffer = nullptr; + mNumItems = 0; + mBuffersize = 0; } template inline void THashMap::insert(const T &key, const U &value) { - const T hash = Hash::toHash(key, static_cast(m_buffersize)); - if (nullptr == m_buffer[hash]) { + const T hash = Hash::toHash(key, static_cast(mBuffersize)); + if (nullptr == mBuffer[hash]) { Node *node = new Node; - node->m_key = key; - node->m_value = value; - m_buffer[hash] = node; + node->mKey = key; + node->mValue = value; + mBuffer[hash] = node; } else { - m_buffer[hash]->append(key, value); + mBuffer[hash]->append(key, value); } - ++m_numItems; + ++mNumItems; } template inline bool THashMap::remove(const T &key) { - const T hash = Hash::toHash(key, (unsigned int)m_buffersize); - if (nullptr == m_buffer[hash]) { + const T hash = Hash::toHash(key, static_cast(mBuffersize)); + if (nullptr == mBuffer[hash]) { return false; } bool found = false; - Node *current = m_buffer[hash]; - if (current->m_key == key) { - Node *next = current->m_next; - m_buffer[hash] = next; + Node *current = mBuffer[hash]; + if (current->mKey == key) { + Node *next = current->mNext; + mBuffer[hash] = next; delete current; - --m_numItems; + --mNumItems; found = true; return true; } found = current->remove(key); if (found) { - --m_numItems; + --mNumItems; } return found; @@ -223,26 +219,26 @@ inline bool THashMap::remove(const T &key) { template inline bool THashMap::hasKey(const T &key) const { // no buffer, so no items stored - if (0 == m_buffersize) { + if (0 == mBuffersize) { return false; } - const T hash = THash::toHash(key, (unsigned int)m_buffersize); - const Node *node = m_buffer[hash]; + const T hash = THash::toHash(key, (unsigned int)mBuffersize); + const Node *node = mBuffer[hash]; if (nullptr == node) { return false; } - if (node->m_key == key) { + if (node->mKey == key) { return true; } - if (nullptr != node->m_next) { - Node *next = node->m_next; + if (nullptr != node->mNext) { + Node *next = node->mNext; while (nullptr != next) { - if (next->m_key == key) { + if (next->mKey == key) { return true; } - next = next->m_next; + next = next->mNext; } } @@ -251,21 +247,21 @@ inline bool THashMap::hasKey(const T &key) const { template inline bool THashMap::getValue(const T &key, U &value) const { - const size_t pos = Hash::toHash(key, (unsigned int) m_buffersize); - if (m_buffer[pos]->m_key == key) { - value = m_buffer[pos]->m_value; + const size_t pos = Hash::toHash(key, (unsigned int) mBuffersize); + if (mBuffer[pos]->mKey == key) { + value = mBuffer[pos]->mValue; return true; } - Node *node = m_buffer[pos]; - Node *next = node->m_next; - while (next->m_key != key) { - next = next->m_next; + Node *node = mBuffer[pos]; + Node *next = node->mNext; + while (next->mKey != key) { + next = next->mNext; if (nullptr == next) { return false; } } - value = next->m_value; + value = next->mValue; return true; } @@ -273,80 +269,74 @@ inline bool THashMap::getValue(const T &key, U &value) const { template inline U &THashMap::operator[](const T &key) const { static U dummy; - const unsigned int pos = Hash::toHash(key, m_buffersize); - if (m_buffer[pos]->m_key == key) { - return m_buffer[pos]->m_value; + const unsigned int pos = Hash::toHash(key, mBuffersize); + if (mBuffer[pos]->mKey == key) { + return mBuffer[pos]->mValue; } - Node next = m_buffer[pos]->m_next; - while (next->m_key != key) { - next = next->m_next; + Node *next = mBuffer[pos]->mNext; + while (next->mKey != key) { + next = next->mNext; if (nullptr == next) { return dummy; } } - return next->m_value; + return next->mValue; } template inline THashMap::Node::Node() : - m_key(UnsetNode), - m_value(), - m_next(nullptr) { + mValue(), + mNext(nullptr) { // empty } -template -inline THashMap::Node::~Node() { - m_next = nullptr; -} - template inline void THashMap::Node::append(T key, const U &value) { - if (m_key == UnsetNode) { - m_key = key; - m_value = value; + if (mKey == UnsetNode) { + mKey = key; + mValue = value; return; } - Node *current = m_next; + Node *current = mNext; if (nullptr != current) { // already collisions, traverse - while (current->m_next) { - current = current->m_next; + while (current->mNext) { + current = current->mNext; } - current->m_next = new Node; - current->m_next->m_key = key; - current->m_next->m_value = value; + current->mNext = new Node; + current->mNext->mKey = key; + current->mNext->mValue = value; } else { // first collision - m_next = new Node; - current = m_next; - current->m_key = key; - current->m_value = value; + mNext = new Node; + current = mNext; + current->mKey = key; + current->mValue = value; } } template inline bool THashMap::Node::remove(T key) { - if (nullptr == m_next) { + if (nullptr == mNext) { return false; } bool found = false; - Node *current = m_next, *prev = nullptr; + Node *current = mNext, *prev = nullptr; while (nullptr != current) { - if (current->m_key == key) { + if (current->mKey == key) { if (nullptr != prev) { - prev->m_next = current->m_next; + prev->mNext = current->mNext; delete current; } found = true; break; } prev = current; - current = current->m_next; + current = current->mNext; } return found; @@ -354,14 +344,14 @@ inline bool THashMap::Node::remove(T key) { template inline void THashMap::Node::releaseList() { - if (nullptr == m_next) { + if (nullptr == mNext) { return; } - Node *current = m_next; + Node *current = mNext; while (nullptr != current) { Node *tmp = current; - current = current->m_next; + current = current->mNext; delete tmp; } } diff --git a/include/cppcore/Container/TList.h b/include/cppcore/Container/TList.h index 41b8fff..2a16cde 100644 --- a/include/cppcore/Container/TList.h +++ b/include/cppcore/Container/TList.h @@ -97,10 +97,10 @@ class TList { Iterator end() const; /// @brief Compare operator. - bool operator==(const TList &rOther) const; + bool operator == (const TList &rOther) const; /// @brief Assignment operator. - TList &operator=(const TList &rOther); + TList &operator = (const TList &rOther); //--------------------------------------------------------------------------------------------- /// @class Iterator @@ -108,43 +108,43 @@ class TList { //--------------------------------------------------------------------------------------------- class Iterator { public: - /// The default class constructor. + /// The default class constructor. Iterator(); - /// Constructor with a node instance. + /// Constructor with a node instance. Iterator(Node *pNode); - /// The copy constructor. + /// The copy constructor. Iterator(const Iterator &rOther); - /// The destructor, non virtual. - ~Iterator(); - /// The assignment operator. + /// The destructor, non virtual. + ~Iterator() = default; + /// The assignment operator. Iterator &operator=(const Iterator &rOther); - /// The compare operator. + /// The compare operator. bool operator==(const Iterator &rOther) const; - /// The not equal operator. + /// The not equal operator. bool operator!=(const Iterator &rOther) const; - /// The post increment operator. + /// The post increment operator. const Iterator &operator++(int); - /// The pre-increment operator. + /// The pre-increment operator. Iterator &operator++(); - /// The post decrement operator. + /// The post decrement operator. const Iterator &operator--(int); - /// The pre-decrement operator. + /// The pre-decrement operator. Iterator &operator--(); - /// The -> operator. + /// The -> operator. T *operator->() const; - /// The dereference operator. + /// The dereference operator. T &operator*() const; private: - Node *m_pNode; + Node *mNode; }; private: class Node { public: - T m_Item; - Node *m_pPrev; - Node *m_pNext; + T mItem; + Node *mPrev; + Node *mNext; Node(const T &rItem); void setNext(Node *pNode); @@ -154,24 +154,24 @@ class TList { T &getItem() const; }; - Node *m_pFist; - Node *m_pLast; - size_t m_size; + Node *mFist; + Node *mLast; + size_t mSize; }; template inline TList::TList() noexcept : - m_pFist(nullptr), - m_pLast(nullptr), - m_size(0) { + mFist(nullptr), + mLast(nullptr), + mSize(0) { // empty } template inline TList::TList(const TList &rhs) : - m_pFist(nullptr), - m_pLast(nullptr), - m_size(0) { + mFist(nullptr), + mLast(nullptr), + mSize(0) { copyFrom(rhs); } @@ -183,104 +183,104 @@ inline TList::~TList() { template inline void TList::copyFrom(const TList &rhs) { clear(); - Node *pCurrent = rhs.m_pFist; - if (nullptr == pCurrent) { + Node *current = rhs.mFist; + if (nullptr == current) { return; } - Node *pPrevNode = nullptr; + Node *prevNode = nullptr; for (Iterator it = rhs.begin(); it != rhs.end(); ++it) { - m_pLast = new Node(pCurrent->m_Item); - if (!m_pFist) { - m_pFist = m_pLast; + mLast = new Node(current->mItem); + if (!mFist) { + mFist = mLast; } - if (nullptr != pPrevNode) { - m_pLast->setPrev(pPrevNode); - pPrevNode->setNext(m_pLast); + if (nullptr != prevNode) { + mLast->setPrev(prevNode); + prevNode->setNext(mLast); } - pPrevNode = m_pLast; - pCurrent = pCurrent->getNext(); + prevNode = mLast; + current = current->getNext(); } - m_size = rhs.m_size; + mSize = rhs.mSize; } template inline typename TList::Iterator TList::addFront(const T &item) { - Node *pNode = new Node(item); - if (nullptr == m_pFist) { - m_pFist = pNode; - m_pLast = pNode; + Node *node = new Node(item); + if (nullptr == mFist) { + mFist = node; + mLast = node; } else { - pNode->setNext(m_pFist); - m_pFist->setPrev(pNode); - m_pFist = pNode; + node->setNext(mFist); + mFist->setPrev(node); + mFist = node; } - ++m_size; + ++mSize; - return Iterator(pNode); + return Iterator(node); } template inline typename TList::Iterator TList::addBack(const T &item) { - Node *pNode = new Node(item); - if (nullptr == m_pFist) { - m_pFist = pNode; - m_pLast = pNode; + Node *node = new Node(item); + if (nullptr == mFist) { + mFist = node; + mLast = node; } else { - pNode->setPrev(m_pLast); - m_pLast->setNext(pNode); - m_pLast = pNode; + node->setPrev(mLast); + mLast->setNext(node); + mLast = node; } - ++m_size; + ++mSize; - return Iterator(pNode); + return Iterator(node); } template inline void TList::removeFront() { - Node *pTmp = m_pFist->getNext(); - if (pTmp) { - pTmp->setPrev(nullptr); + Node *tmp = mFist->getNext(); + if (tmp) { + tmp->setPrev(nullptr); } - delete m_pFist; - m_pFist = pTmp; - --m_size; + delete mFist; + mFist = tmp; + --mSize; } template inline void TList::removeBack() { - Node *pLast = m_pLast->getPrev(); - pLast->setNext(nullptr); - delete m_pLast; - m_pLast = pLast; - --m_size; + Node *last = mLast->getPrev(); + last->setNext(nullptr); + delete mLast; + mLast = last; + --mSize; } template inline T &TList::front() const { - assert(nullptr != m_pFist); + assert(nullptr != mFist); - return (m_pFist->m_Item); + return (mFist->mItem); } template inline T &TList::back() const { - assert(nullptr != m_pLast); + assert(nullptr != mLast); - return (m_pLast->m_Item); + return (mLast->mItem); } template inline size_t TList::size() const { - return m_size; + return mSize; } template inline bool TList::isEmpty() const { - return (0 == m_size); + return (0u == mSize); } template @@ -289,16 +289,16 @@ inline void TList::clear() { return; } - Node *pCurrent = m_pFist; - while (m_pFist->getNext()) { - pCurrent = m_pFist; - m_pFist = m_pFist->getNext(); - delete pCurrent; + Node *current = mFist; + while (mFist->getNext()) { + current = mFist; + mFist = mFist->getNext(); + delete current; } - delete m_pFist; - m_pFist = nullptr; - m_pLast = nullptr; - m_size = 0; + delete mFist; + mFist = nullptr; + mLast = nullptr; + mSize = 0; } template @@ -307,7 +307,7 @@ inline typename TList::Iterator TList::begin() const { return end(); } - return Iterator(m_pFist); + return Iterator(mFist); } template @@ -317,7 +317,7 @@ inline typename TList::Iterator TList::end() const { template inline bool TList::operator == (const TList &rhs) const { - if (m_size != rhs.m_size) { + if (mSize != rhs.mSize) { return false; } @@ -347,57 +347,52 @@ inline TList &TList::operator=(const TList &rhs template inline TList::Node::Node(const T &item) : - m_Item(item), - m_pPrev(nullptr), - m_pNext(nullptr) { + mItem(item), + mPrev(nullptr), + mNext(nullptr) { // empty } template inline void TList::Node::setNext(Node *pNode) { - m_pNext = pNode; + mNext = pNode; } template inline typename TList::Node *TList::Node::getNext() const { - return m_pNext; + return mNext; } template -inline void TList::Node::setPrev(Node *pNode) { - m_pPrev = pNode; +inline void TList::Node::setPrev(Node *node) { + mPrev = node; } template inline typename TList::Node *TList::Node::getPrev() const { - return m_pPrev; + return mPrev; } template inline T &TList::Node::getItem() const { - return m_Item; + return mItem; } template inline TList::Iterator::Iterator() : - m_pNode(nullptr) { + mNode(nullptr) { // empty } template -inline TList::Iterator::Iterator(Node *pNode) : - m_pNode(pNode) { +inline TList::Iterator::Iterator(Node *node) : + mNode(node) { // empty } template inline TList::Iterator::Iterator(const Iterator &rhs) : - m_pNode(rhs.m_pNode) { - // empty -} - -template -inline TList::Iterator::~Iterator() { + mNode(rhs.mNode) { // empty } @@ -407,71 +402,71 @@ inline typename TList::Iterator &TList::Iterator::operator return *this; } - m_pNode = rhs.m_pNode; + mNode = rhs.mNode; return *this; } template inline bool TList::Iterator::operator==(const Iterator &rhs) const { - return (m_pNode == rhs.m_pNode); + return (mNode == rhs.mNode); } template inline bool TList::Iterator::operator!=(const Iterator &rhs) const { - return (m_pNode != rhs.m_pNode); + return (mNode != rhs.mNode); } template inline const typename TList::Iterator &TList::Iterator::operator++(int) { - assert(nullptr != m_pNode); + assert(nullptr != mNode); - Iterator inst(m_pNode); - m_pNode = m_pNode->getNext(); + Iterator inst(mNode); + mNode = mNode->getNext(); return inst; } template inline typename TList::Iterator &TList::Iterator::operator++() { - assert(nullptr != m_pNode); + assert(nullptr != mNode); - m_pNode = m_pNode->getNext(); + mNode = mNode->getNext(); return *this; } template inline typename TList::Iterator const &TList::Iterator::operator--(int) { - assert(nullptr != m_pNode); + assert(nullptr != mNode); - Iterator inst(m_pNode); - m_pNode = m_pNode->getPrev(); + Iterator inst(mNode); + mNode = mNode->getPrev(); return inst; } template inline typename TList::Iterator &TList::Iterator::operator--() { - assert(nullptr != m_pNode); + assert(nullptr != mNode); - m_pNode = m_pNode->getPrev(); + mNode = mNode->getPrev(); return *this; } template inline T *TList::Iterator::operator->() const { - assert(nullptr != m_pNode); + assert(nullptr != mNode); - return &(m_pNode->m_Item); + return &(mNode->mItem); } template inline T &TList::Iterator::operator*() const { - assert(nullptr != m_pNode); + assert(nullptr != mNode); - return m_pNode->m_Item; + return mNode->mItem; } } // Namespace cppcore diff --git a/include/cppcore/Container/TQueue.h b/include/cppcore/Container/TQueue.h index 34a55fb..6c40f12 100644 --- a/include/cppcore/Container/TQueue.h +++ b/include/cppcore/Container/TQueue.h @@ -78,18 +78,18 @@ class TQueue { bool operator == ( const TQueue &rhs ) const; private: - TList m_QueueData; + TList mQueueData; }; template inline TQueue::TQueue() noexcept : - m_QueueData() { + mQueueData() { // empty } template inline TQueue::TQueue( const TQueue &rhs ) : - m_QueueData( rhs.m_QueueData ) { + mQueueData( rhs.mQueueData ) { // empty } @@ -100,13 +100,13 @@ inline TQueue::~TQueue() { template inline void TQueue::enqueue( const T &item ) { - m_QueueData.addBack( item ); + mQueueData.addBack( item ); } template inline bool TQueue::dequeue( T &item ) { - item = m_QueueData.front(); - m_QueueData.removeFront(); + item = mQueueData.front(); + mQueueData.removeFront(); if ( isEmpty() ) { return false; } @@ -116,22 +116,22 @@ inline bool TQueue::dequeue( T &item ) { template inline T TQueue::front() { - return m_QueueData.front(); + return mQueueData.front(); } template inline bool TQueue::isEmpty() const { - return m_QueueData.isEmpty(); + return mQueueData.isEmpty(); } template inline size_t TQueue::size( ) { - return m_QueueData.size(); + return mQueueData.size(); } template inline void TQueue::clear() { - m_QueueData.clear(); + mQueueData.clear(); } template @@ -140,14 +140,14 @@ inline TQueue &TQueue::operator = ( const TQueue inline bool TQueue::operator == ( const TQueue &rhs ) const { - return m_QueueData == rhs.m_QueueData; + return mQueueData == rhs.mQueueData; } } // Namespace cppcore diff --git a/include/cppcore/Container/TStaticArray.h b/include/cppcore/Container/TStaticArray.h index 94423cb..d07546e 100644 --- a/include/cppcore/Container/TStaticArray.h +++ b/include/cppcore/Container/TStaticArray.h @@ -75,65 +75,65 @@ class TStaticArray { TStaticArray &operator=(const TStaticArray &rhs); private: - T m_array[len]; - size_t m_len; + T mArray[len]; + size_t mLen; }; template inline TStaticArray::TStaticArray() : - m_len(len) { + mLen(len) { // empty } template inline TStaticArray::TStaticArray(T initValue) : - m_len(len) { + mLen(len) { memset(initValue); } template inline TStaticArray::TStaticArray(const TStaticArray &rhs) : - m_len(rhs.m_len) { - for (size_t i = 0; i < m_len; ++i) { - m_array[i] = rhs.m_array[i]; + mLen(rhs.mLen) { + for (size_t i = 0; i < mLen; ++i) { + mArray[i] = rhs.mArray[i]; } } template inline void TStaticArray::set(size_t index, T value) { - assert(index < m_len); + assert(index < mLen); - m_array[index] = value; + mArray[index] = value; } template inline void TStaticArray::memset(T value) { - for (size_t i = 0; i < m_len; ++i) { - m_array[i] = value; + for (size_t i = 0; i < mLen; ++i) { + mArray[i] = value; } } template inline size_t TStaticArray::size() const { - return m_len; + return mLen; } template inline T TStaticArray::operator[](size_t index) const { assert(index < m_len); - return m_array[index]; + return mArray[index]; } template inline T &TStaticArray::operator[](size_t index) { - return m_array[index]; + return mArray[index]; } template inline bool TStaticArray::operator==(const TStaticArray &rhs) const { - for (size_t i = 0; i < m_len; ++i) { - if (m_array[i] != rhs.m_array[i]) { + for (size_t i = 0; i < mLen; ++i) { + if (mArray[i] != rhs.mArray[i]) { return false; } } @@ -142,13 +142,13 @@ inline bool TStaticArray::operator==(const TStaticArray &rhs) co } template -inline TStaticArray &TStaticArray::operator=(const TStaticArray &rhs) { +inline TStaticArray &TStaticArray::operator = (const TStaticArray &rhs) { if (*this == rhs) { return *this; } - for (unsigned int i = 0; i < m_len; ++i) { - m_array[i] = rhs.m_array[i]; + for (unsigned int i = 0; i < mLen; ++i) { + mArray[i] = rhs.mArray[i]; } return *this; diff --git a/include/cppcore/IO/FileSystem.h b/include/cppcore/IO/FileSystem.h index befdae5..f7bfd89 100644 --- a/include/cppcore/IO/FileSystem.h +++ b/include/cppcore/IO/FileSystem.h @@ -37,8 +37,7 @@ struct FSSpace { long inUse; ///< The used capacity /// @brief The default class constructor. - FSSpace() : - capacity(0L), free(0L), inUse(0L) { + FSSpace() : capacity(0L), free(0L), inUse(0L) { // empty } }; @@ -61,42 +60,41 @@ class FileSystem { FSSpace *getFreeDiskSpace(); private: - const char *m_drive; - FSSpace m_fsSpace; + const char *mDrive; + FSSpace mFsSpace; }; inline FileSystem::FileSystem(const char *location) : - m_drive(location), - m_fsSpace() { + mDrive(location), + mFsSpace() { // empty } inline void FileSystem::refresh() { - if (m_drive == nullptr) { + if (mDrive == nullptr) { return; } #ifdef _WIN32 - PULARGE_INTEGER freeByteAvailable = 0, totalNumberOfBytes = 0, totalNumberOfFreeBytes = 0; - BOOL result = ::GetDiskFreeSpaceEx(m_drive, freeByteAvailable, totalNumberOfBytes, totalNumberOfFreeBytes); + ULARGE_INTEGER freeByteAvailable, totalNumberOfBytes, totalNumberOfFreeBytes; + BOOL result = ::GetDiskFreeSpaceEx(mDrive, &freeByteAvailable, &totalNumberOfBytes, &totalNumberOfFreeBytes); if (TRUE == result) { - ::memcpy(&m_fsSpace.capacity, &totalNumberOfBytes->QuadPart, sizeof(PULARGE_INTEGER)); - ::memcpy(&m_fsSpace.free, &freeByteAvailable->QuadPart, sizeof(PULARGE_INTEGER)); - ULONGLONG res = totalNumberOfBytes->QuadPart - freeByteAvailable->QuadPart; - ::memcpy(&m_fsSpace.inUse, &res, sizeof(PULARGE_INTEGER)); + mFsSpace.capacity = totalNumberOfBytes.QuadPart / (1024 * 1024 * 1024); + mFsSpace.free = freeByteAvailable.QuadPart / (1024 * 1024 * 1024); + mFsSpace.inUse = mFsSpace.capacity - mFsSpace.free; } #else struct statvfs stats; - statvfs(m_drive, &stats); - m_fsSpace.capacity = stats.f_bsize; - m_fsSpace.free = stats.f_bsize * stats.f_bfree; - m_fsSpace.inUse = m_fsSpace.capacity - m_fsSpace.free; + statvfs(mDrive, &stats); + mFsSpace.capacity = stats.f_bsize; + mFsSpace.free = stats.f_bsize * stats.f_bfree; + mFsSpace.inUse = mFsSpace.capacity - mFsSpace.free; #endif } inline FSSpace *FileSystem::getFreeDiskSpace() { refresh(); - return &m_fsSpace; + return &mFsSpace; } } // Namespace cppcore diff --git a/include/cppcore/Memory/TDefaultAllocator.h b/include/cppcore/Memory/TDefaultAllocator.h index 64bbe13..583e3f4 100644 --- a/include/cppcore/Memory/TDefaultAllocator.h +++ b/include/cppcore/Memory/TDefaultAllocator.h @@ -37,10 +37,10 @@ template class TDefaultAllocator { public: /// @brief The default class constructor. - TDefaultAllocator(); + TDefaultAllocator() = default; /// @brief The class destructor. - ~TDefaultAllocator(); + ~TDefaultAllocator() = default; /// @brief Will allocate the number of insances. /// @param[in] size Size of instances to allocate. @@ -79,16 +79,6 @@ class TDefaultAllocator { CPPCORE_NONE_COPYING(TDefaultAllocator) }; -template -inline TDefaultAllocator::TDefaultAllocator() { - // empty -} - -template -inline TDefaultAllocator::~TDefaultAllocator() { - // empty -} - template inline T *TDefaultAllocator::alloc(size_t size) { return new T[size]; diff --git a/include/cppcore/Memory/TPoolAllocator.h b/include/cppcore/Memory/TPoolAllocator.h index 427bf32..d212776 100644 --- a/include/cppcore/Memory/TPoolAllocator.h +++ b/include/cppcore/Memory/TPoolAllocator.h @@ -100,67 +100,66 @@ class TPoolAllocator { private: struct Pool { - size_t m_poolsize; - T *m_pool; - size_t m_currentIdx; - Pool *m_next; + size_t mPoolsize; + T *mPool; + size_t mCurrentIdx; + Pool *mNext; Pool() : - m_poolsize(0u), - m_pool(nullptr), - m_currentIdx(0u), - m_next(nullptr) { + mPoolsize(0u), + mPool(nullptr), + mCurrentIdx(0u), + mNext(nullptr) { // empty } Pool(size_t numItems, Pool *prev) : - m_poolsize(numItems), - m_pool(nullptr), - m_currentIdx(0u), - m_next(prev) { - m_pool = new T[m_poolsize]; + mPoolsize(numItems), + mPool(nullptr), + mCurrentIdx(0u), + mNext(prev) { + mPool = new T[mPoolsize]; } ~Pool() { - delete[] m_pool; - m_pool = nullptr; + delete[] mPool; } CPPCORE_NONE_COPYING(Pool) }; Pool *getFreePool() { - Pool *current = m_freeList; - if (nullptr != m_freeList) { - m_freeList = m_freeList->m_next; + Pool *current = mFreeList; + if (nullptr != mFreeList) { + mFreeList = mFreeList->mNext; } return current; } - Pool *m_first; - Pool *m_current; - Pool *m_freeList; - size_t m_capacity; + Pool *mFirst; + Pool *mCurrent; + Pool *mFreeList; + size_t mCapacity; }; template inline TPoolAllocator::TPoolAllocator() : - m_first(nullptr), - m_current(nullptr), - m_freeList(nullptr), - m_capacity(0L) { + mFirst(nullptr), + mCurrent(nullptr), + mFreeList(nullptr), + mCapacity(0L) { // empty } template inline TPoolAllocator::TPoolAllocator(size_t numItems) : - m_first(nullptr), - m_current(nullptr), - m_freeList(nullptr), - m_capacity(0L) { - m_first = new Pool(numItems, nullptr); - m_capacity += numItems; - m_current = m_first; + mFirst(nullptr), + mCurrent(nullptr), + mFreeList(nullptr), + mCapacity(0L) { + mFirst = new Pool(numItems, nullptr); + mCapacity += numItems; + mCurrent = mFirst; } template @@ -170,117 +169,117 @@ inline TPoolAllocator::~TPoolAllocator() { template inline T *TPoolAllocator::alloc() { - if (nullptr == m_current) { + if (nullptr == mCurrent) { return nullptr; } - if (m_current->m_currentIdx == m_current->m_poolsize) { - resize(m_current->m_poolsize); + if (mCurrent->mCurrentIdx == mCurrent->mPoolsize) { + resize(mCurrent->mPoolsize); } - T *ptr = &m_current->m_pool[m_current->m_currentIdx]; - m_current->m_currentIdx++; + T *ptr = &mCurrent->mPool[mCurrent->mCurrentIdx]; + mCurrent->mCurrentIdx++; return ptr; } template inline void TPoolAllocator::release() { - if (nullptr == m_current) { + if (nullptr == mCurrent) { return; } - Pool *current = m_first; + Pool *current = mFirst; while (nullptr != current) { - current->m_currentIdx = 0; - current = current->m_next; + current->mCurrentIdx = 0; + current = current->mNext; } - m_freeList = m_first->m_next; - m_current = m_first; + mFreeList = mFirst->mNext; + mCurrent = mFirst; } template inline void TPoolAllocator::reserve(size_t size) { clear(); - m_first = new Pool(size, nullptr); - m_current = m_first; + mFirst = new Pool(size, nullptr); + mCurrent = mFirst; - m_current->m_pool = new T[size]; - m_current->m_poolsize = size; + mCurrent->mPool = new T[size]; + mCurrent->mPoolsize = size; - m_capacity = size; + mCapacity = size; } template inline void TPoolAllocator::clear() { - if (nullptr == m_current) { + if (nullptr == mCurrent) { return; } - Pool *next = m_first; + Pool *next = mFirst; while (nullptr != next) { Pool *current = next; - next = current->m_next; + next = current->mNext; delete current; } - m_current = nullptr; - m_freeList = nullptr; + mCurrent = nullptr; + mFreeList = nullptr; } template inline size_t TPoolAllocator::capacity() const { - return m_capacity; + return mCapacity; } template inline size_t TPoolAllocator::reservedMem() const { - return m_capacity * sizeof(T); + return mCapacity * sizeof(T); } template inline size_t TPoolAllocator::freeMem() const { - if (nullptr == m_current) { + if (nullptr == mCurrent) { return 0L; } - return (m_current->m_poolsize - m_current->m_currentIdx); + return (mCurrent->mPoolsize - mCurrent->mCurrentIdx); } template inline void TPoolAllocator::dumpAllocations(std::string &allocs) { allocs.clear(); allocs += "Number allocations = "; - allocs += std::to_string(m_current->m_currentIdx); + allocs += std::to_string(mCurrent->mCurrentIdx); allocs += "\n"; } template inline void TPoolAllocator::resize(size_t growSize) { - if (nullptr != m_current) { - if (growSize < m_current->m_poolsize) { + if (nullptr != mCurrent) { + if (growSize < mCurrent->mPoolsize) { return; } } - if (nullptr == m_first) { - m_first = new Pool(growSize, nullptr); - m_current = m_first; - m_capacity += m_current->m_poolsize; + if (nullptr == mFirst) { + mFirst = new Pool(growSize, nullptr); + mCurrent = mFirst; + mCapacity += mCurrent->mPoolsize; } else { Pool *pool = getFreePool(); if (nullptr == pool) { pool = new Pool(growSize, nullptr); - m_capacity += growSize; + mCapacity += growSize; } - m_current->m_next = pool; - m_current = m_current->m_next; + mCurrent->mNext = pool; + mCurrent = mCurrent->mNext; } } template void TPoolAllocator::reset() { - m_current = m_first; + mCurrent = mFirst; } } // namespace cppcore diff --git a/include/cppcore/Memory/TStackAllocator.h b/include/cppcore/Memory/TStackAllocator.h index b556727..ece9b0c 100644 --- a/include/cppcore/Memory/TStackAllocator.h +++ b/include/cppcore/Memory/TStackAllocator.h @@ -93,29 +93,29 @@ class TStackAllocator { using byte_t = unsigned char; struct Header { - size_t m_size; + size_t mSize; }; - size_t m_capacity; - size_t m_top; - size_t m_numAllocs; - byte_t *m_data; + size_t mCapacity; + size_t mTop; + size_t mNumAllocs; + byte_t *mData; }; template inline TStackAllocator::TStackAllocator() : - m_capacity(0), - m_top(0), - m_numAllocs(0), - m_data(nullptr) { + mCapacity(0), + mTop(0), + mNumAllocs(0), + mData(nullptr) { // empty } template inline TStackAllocator::TStackAllocator(size_t initSize) : - m_capacity(0), - m_top(0), - m_numAllocs(0), - m_data(nullptr) { + mCapacity(0), + mTop(0), + mNumAllocs(0), + mData(nullptr) { reserve(initSize); } @@ -131,20 +131,20 @@ inline T *TStackAllocator::alloc(size_t size) { } const size_t range(size * sizeof(T) + sizeof(Header)); - const size_t limit(m_top + range); - if (limit > m_capacity) { + const size_t limit(mTop + range); + if (limit > mCapacity) { return nullptr; } // head - Header *header = (Header *)(&m_data[m_top]); - header->m_size = size * sizeof(T); - m_top += sizeof(Header); + Header *header = (Header *)(&mData[mTop]); + header->mSize = size * sizeof(T); + mTop += sizeof(Header); // data - T *ptr = (T *)(&m_data[m_top]); - m_top += header->m_size; - m_numAllocs++; + T *ptr = (T *)(&mData[mTop]); + mTop += header->mSize; + mNumAllocs++; return ptr; } @@ -159,60 +159,60 @@ inline bool TStackAllocator::release(T *ptr) { byte_t *tmp = (byte_t *)ptr; tmp = tmp - sizeof(Header); Header *head = (Header *)tmp; - const size_t allocSize = head->m_size; + const size_t allocSize = head->mSize; // data - m_top -= allocSize; - m_top -= sizeof(Header); - m_numAllocs--; + mTop -= allocSize; + mTop -= sizeof(Header); + mNumAllocs--; return true; } template inline void TStackAllocator::reserve(size_t size) { - if (size > (m_capacity)) { + if (size > mCapacity) { clear(); - m_capacity = size * sizeof(T); - m_data = new byte_t[m_capacity]; + mCapacity = size * sizeof(T); + mData = new byte_t[mCapacity]; } } template inline void TStackAllocator::clear() { - delete[] m_data; - m_data = nullptr; - m_capacity = 0; - m_top = 0; - m_numAllocs = 0; + delete[] mData; + mData = nullptr; + mCapacity = 0; + mTop = 0; + mNumAllocs = 0; } template inline void TStackAllocator::reset() { - m_top = 0; - m_numAllocs = 0; + mTop = 0; + mNumAllocs = 0; } template inline size_t TStackAllocator::capacity() const { - return m_capacity; + return mCapacity; } template inline size_t TStackAllocator::reservedMem() const { - return m_top; + return mTop; } template inline size_t TStackAllocator::freeMem() const { - return (m_capacity - m_top); + return (mCapacity - mTop); } template inline void TStackAllocator::dumpAllocations(std::string &allocs) { allocs.clear(); allocs += "Number allocations = "; - allocs += std::to_string(m_numAllocs); + allocs += std::to_string(mNumAllocs); allocs += "\n"; } diff --git a/include/cppcore/Random/RandomGenerator.h b/include/cppcore/Random/RandomGenerator.h index cd40644..797af28 100644 --- a/include/cppcore/Random/RandomGenerator.h +++ b/include/cppcore/Random/RandomGenerator.h @@ -60,7 +60,7 @@ class DLL_CPPCORE_EXPORT RandomGenerator { RandomGenerator &operator = (const RandomGenerator &) = delete; private: - GeneratorType m_type; + GeneratorType mType; }; } // Namespace cppcore diff --git a/test/container/TAlgorithmTest.cpp b/test/container/TAlgorithmTest.cpp new file mode 100644 index 0000000..4f61509 --- /dev/null +++ b/test/container/TAlgorithmTest.cpp @@ -0,0 +1,24 @@ +#include +#include +#include + +#include "gtest/gtest.h" + +#include + +using namespace cppcore; + +class TAlgorithmTest : public testing::Test {}; + +TEST_F(TAlgorithmTest, distanceTest) { + TArray theArray; + theArray.add(1.0f); + theArray.add(3.0f); + theArray.add(2.0f); + + size_t dist = distance(theArray.begin(), theArray.end()); + EXPECT_EQ(dist, 3u); + + dist = distance(theArray.begin()+1, theArray.end()); + EXPECT_EQ(dist, 2u); +} diff --git a/test/container/TArrayTest.cpp b/test/container/TArrayTest.cpp index 617eca1..d4fc005 100644 --- a/test/container/TArrayTest.cpp +++ b/test/container/TArrayTest.cpp @@ -33,8 +33,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. using namespace cppcore; // Some test-data -static const size_t ArraySize = 4; -static const float ArrayData[ ArraySize ] = { +static constexpr size_t ArraySize = 4; +static constexpr float ArrayData[ ArraySize ] = { 0.0f, 1.0f, 2.0f, @@ -168,7 +168,7 @@ TEST_F( TArrayTest, removeItTest) { TArray arrayInstance; arrayInstance.add( 1.0f ); EXPECT_EQ( 1u, arrayInstance.size() ); - TArray::Iterator it = arrayInstance.find( 1.0f ); + TArray::Iterator it = arrayInstance.linearSearch( 1.0f ); EXPECT_NE( arrayInstance.end(), it ); arrayInstance.remove( it ); @@ -278,7 +278,7 @@ TEST_F( TArrayTest, findTest ) { arrayInstance.add( 3.0f ); EXPECT_EQ( 4u, arrayInstance.size() ); - TArray::Iterator it = arrayInstance.find( 1.0f ); + TArray::Iterator it = arrayInstance.linearSearch( 1.0f ); EXPECT_NE( it, arrayInstance.end() ); EXPECT_EQ( *it, 1.0f ); } diff --git a/test/container/TStaticArrayTest.cpp b/test/container/TStaticArrayTest.cpp index 0a7fe18..03ac3f9 100644 --- a/test/container/TStaticArrayTest.cpp +++ b/test/container/TStaticArrayTest.cpp @@ -94,3 +94,15 @@ TEST_F(TStaticArrayTest, string_Test) { } } +TEST_F(TStaticArrayTest, copyTest) { + TStaticArray arr; + for (size_t i = 0; i < 4; ++i) { + arr[i] = 1; + } + + TStaticArray copy_arr = arr; + for (size_t i = 0; i < 4; ++i) { + EXPECT_EQ(copy_arr[i], arr[i]); + } +} + diff --git a/test/io/FileSystemTest.cpp b/test/io/FileSystemTest.cpp new file mode 100644 index 0000000..8885e53 --- /dev/null +++ b/test/io/FileSystemTest.cpp @@ -0,0 +1,39 @@ +/* +------------------------------------------------------------------------------------------------- +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 + +using namespace cppcore; + +#include + +using namespace cppcore; + +class FileSystemTest : public testing::Test {}; + +TEST_F(FileSystemTest, getFreeDiskSpaceTest) { + FileSystem fs("c:\\"); + FSSpace *space = fs.getFreeDiskSpace(); + EXPECT_NE(space, nullptr); +} diff --git a/test/Random/RandomGeneratorTest.cpp b/test/random/RandomGeneratorTest.cpp similarity index 100% rename from test/Random/RandomGeneratorTest.cpp rename to test/random/RandomGeneratorTest.cpp