diff --git a/.gitignore b/.gitignore index a1decab8..67e5983d 100644 --- a/.gitignore +++ b/.gitignore @@ -403,3 +403,8 @@ test/syntax_check/bgcc test/vs2022/Debug MSVC C++23 test/vs2022/Debug MSVC C++23 - No STL .vs +examples/UniquePtrWithPool/CMakeFiles +examples/UniquePtrWithPool/cmake_install.cmake +examples/UniquePtrWithPool/Makefile +examples/UniquePtrWithPool/CMakeCache.txt +examples/UniquePtrWithPool/UniquePtrWithPool diff --git a/examples/UniquePtrWithPool/CmakeLists.txt b/examples/UniquePtrWithPool/CmakeLists.txt new file mode 100644 index 00000000..a7e41b0a --- /dev/null +++ b/examples/UniquePtrWithPool/CmakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.10) +project(UniquePtrWithPool LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# Add source files +add_executable(UniquePtrWithPool Main.cpp) + +target_include_directories(UniquePtrWithPool PRIVATE ../../include) diff --git a/examples/UniquePtrWithPool/Main.cpp b/examples/UniquePtrWithPool/Main.cpp new file mode 100644 index 00000000..1b267c48 --- /dev/null +++ b/examples/UniquePtrWithPool/Main.cpp @@ -0,0 +1,51 @@ +#include + +#include "etl/memory.h" +#include "etl/pool.h" + +struct S +{ + S(int a_, double b_) + : a(a_), b(b_) + { + } + + int a; + double b; +}; + +// Custom deleter that returns S* to the pool +struct Deleter +{ + Deleter(etl::pool& p) + : pool(&p) {} + + void operator()(S* ptr) const + { + if (ptr) + { + std::cout << "Releasing S(" << ptr->a << ", " << ptr->b << ") back to pool." << std::endl; + pool->release(ptr); + } + } + + etl::pool* pool; +}; + +int main() +{ + etl::pool pool; + Deleter pool_deleter(pool); + using Unique = etl::unique_ptr; + + Unique us1(pool.create(1, 2), pool_deleter); + std::cout << "Created S(" << us1->a << ", " << us1->b << ") from pool." << std::endl; + + { + Unique us2(pool.create(3, 4), pool_deleter); + std::cout << "Created S(" << us2->a << ", " << us2->b << ") from pool." << std::endl; + } + + Unique us3(pool.create(5, 6), pool_deleter); + std::cout << "Created S(" << us3->a << ", " << us3->b << ") from pool." << std::endl; +} diff --git a/examples/UniquePtrWithPool/UniquePtrWithPool.sln b/examples/UniquePtrWithPool/UniquePtrWithPool.sln new file mode 100644 index 00000000..24a9e577 --- /dev/null +++ b/examples/UniquePtrWithPool/UniquePtrWithPool.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36408.4 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "UniquePtrWithPool", "UniquePtrWithPool.vcxproj", "{B9ED2FDF-2529-4315-9AEF-02A98B804DEC}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B9ED2FDF-2529-4315-9AEF-02A98B804DEC}.Debug|x64.ActiveCfg = Debug|x64 + {B9ED2FDF-2529-4315-9AEF-02A98B804DEC}.Debug|x64.Build.0 = Debug|x64 + {B9ED2FDF-2529-4315-9AEF-02A98B804DEC}.Debug|x86.ActiveCfg = Debug|Win32 + {B9ED2FDF-2529-4315-9AEF-02A98B804DEC}.Debug|x86.Build.0 = Debug|Win32 + {B9ED2FDF-2529-4315-9AEF-02A98B804DEC}.Release|x64.ActiveCfg = Release|x64 + {B9ED2FDF-2529-4315-9AEF-02A98B804DEC}.Release|x64.Build.0 = Release|x64 + {B9ED2FDF-2529-4315-9AEF-02A98B804DEC}.Release|x86.ActiveCfg = Release|Win32 + {B9ED2FDF-2529-4315-9AEF-02A98B804DEC}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D21FA849-65C8-4A81-BD9D-B38BFBD73494} + EndGlobalSection +EndGlobal diff --git a/examples/UniquePtrWithPool/UniquePtrWithPool.vcxproj b/examples/UniquePtrWithPool/UniquePtrWithPool.vcxproj new file mode 100644 index 00000000..184bf5b3 --- /dev/null +++ b/examples/UniquePtrWithPool/UniquePtrWithPool.vcxproj @@ -0,0 +1,133 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {b9ed2fdf-2529-4315-9aef-02a98b804dec} + UniquePtrWithPool + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ../../include/;%(AdditionalIncludeDirectories) + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ../../include/;%(AdditionalIncludeDirectories) + + + Console + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + + + + + \ No newline at end of file