Skip to content

Commit

Permalink
Add some updates to allocators and containers
Browse files Browse the repository at this point in the history
  • Loading branch information
wopss committed Jan 28, 2025
1 parent 588936e commit 508e3b3
Show file tree
Hide file tree
Showing 11 changed files with 1,607 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ project(
# -----------------------------------------------------------------------------
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
elseif(CMAKE_CXX_STANDARD LESS 20)
message(FATAL_ERROR "RED4ext.SDK requires C++20 or higher.")
endif()

set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(PROJECT_IS_TOP_LEVEL)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
endif()
Expand Down
135 changes: 135 additions & 0 deletions examples/accessing_properties/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,143 @@ void PrintScannerStatus(RED4ext::IScriptable* aContext, RED4ext::CStackFrame* aF
}
}

#include <RED4ext/Containers/DynArray.hpp>

RED4EXT_C_EXPORT void RED4EXT_CALL RegisterTypes()
{
// https://github.com/oneapi-src/oneTBB/blob/master/include/oneapi/tbb/memory_pool.h
// https://stackoverflow.com/questions/826569/compelling-examples-of-custom-c-allocators
// https://stackoverflow.com/questions/657783/how-does-intel-tbbs-scalable-allocator-work

{
std::vector<int /*, std::pmr::polymorphic_allocator<int>*/> vector;
vector.clear();
std::cout << vector.size() << std::endl;

std::uninitialized_copy(vector.begin(), vector.end(), vector.data());
std::uninitialized_move(vector.begin(), vector.end(), vector.data());
}

{
RED4ext::Containers::DynArray<int, RED4ext::Allocator<int, RED4ext::Memory::PoolRTTIFunction>>
DynArrayDefaultAlloc;

constexpr auto max_size = DynArrayDefaultAlloc.MaxSize();
std::cout << max_size << std::endl;

auto capacity = DynArrayDefaultAlloc.Capacity();
std::cout << capacity << std::endl;

DynArrayDefaultAlloc.Reserve(1025);

capacity = DynArrayDefaultAlloc.Capacity();
std::cout << capacity << std::endl;

for (auto i : DynArrayDefaultAlloc)
{
std::cout << i << " ";
}
std::cout << std::endl;

for (const auto i : DynArrayDefaultAlloc)
{
std::cout << i << " ";
}
std::cout << std::endl;

for (auto& i : DynArrayDefaultAlloc)
{
std::cout << i << " ";
}
std::cout << std::endl;

for (const auto& i : DynArrayDefaultAlloc)
{
std::cout << i << " ";
}
std::cout << std::endl;
}

{
RED4ext::Containers::DynArray<int> DynArray({1, 2, 3});
}

{
RED4ext::Containers::DynArray<int> DynArray1({1, 2, 3});
RED4ext::Containers::DynArray<int> DynArray2(DynArray1);
}

{
RED4ext::Containers::DynArray<int> DynArray1({1, 2, 3});
RED4ext::Containers::DynArray<int> DynArray2(std::move(DynArray1));
}

{
RED4ext::Containers::DynArray<int> DynArray(1);
}

{
RED4ext::Containers::DynArray<int> DynArray(1, 123);
}

{
RED4ext::Containers::DynArray<int> DynArray(1, 123);
DynArray.ShrinkToFit();
DynArray.Clear();
DynArray.PopBack();
DynArray.Resize(10);
DynArray.Resize(123, 4);
DynArray.PushBack(1);
DynArray.PushBack(std::move(2));
DynArray.EmplaceBack(3);
DynArray.Assign(3, 5);
DynArray.Erase(DynArray.begin());
DynArray.Erase(DynArray.begin(), DynArray.end());
}

{
RED4ext::Containers::DynArray<int*> DynArray;
DynArray.ShrinkToFit();
DynArray.Clear();
DynArray.PopBack();
}

{
RED4ext::Allocator<int, RED4ext::Memory::PoolAI> allocator;
auto allocate = allocator.Allocate(12);
auto a = allocator.Reallocate(allocate, 1234);
allocator.Deallocate(a);

auto allocateAtLeast = allocator.AllocateAtLeast(255);
allocator.Deallocate(allocateAtLeast);
}

{
constexpr RED4ext::Allocator<int, RED4ext::Memory::PoolAI> allocator;
constexpr RED4ext::Allocator<void*, RED4ext::Memory::PoolAI_Attitudes> allocator2;
constexpr RED4ext::Allocator<char, RED4ext::Memory::PoolAI> allocator3;

{
constexpr auto equal = (allocator == allocator2);
std::cout << equal << std::endl;
}

{
constexpr auto equal = (allocator == allocator3);
std::cout << equal << std::endl;
}
}

{
std::allocator<int> allocator;
auto a = allocator.allocate(12);
}

{
// std::allocator_traits<RED4ext::Allocator<int, RED4ext::Memory::PoolAI>> triats;
// std::allocator_traits<RED4ext::Allocator<int,
// RED4ext::Memory::PoolAI>>::select_on_container_copy_construction triats.allocate(10);
}
}

RED4EXT_C_EXPORT void RED4EXT_CALL PostRegisterTypes()
Expand Down
10 changes: 10 additions & 0 deletions include/RED4ext/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

#include <cstddef>

#if defined(_MSVC_LANG) && _MSVC_LANG > __cplusplus
#define RED4EXT_CPLUSPLUS _MSVC_LANG
#else
#define RED4EXT_CPLUSPLUS __cplusplus
#endif

#ifndef RED4EXT_HAS_CPP23
#define RED4EXT_HAS_CPP23 (RED4EXT_CPLUSPLUS > 202002L)
#endif

#ifdef RED4EXT_STATIC_LIB
#undef RED4EXT_HEADER_ONLY
#define RED4EXT_INLINE
Expand Down
Loading

0 comments on commit 508e3b3

Please sign in to comment.