\doxysection{object\+\_\+pool.\+h} \hypertarget{object__pool_8h_source}{}\label{object__pool_8h_source} \begin{DoxyCode}{0} \DoxyCodeLine{00001\ \textcolor{comment}{//\ =====================================================================================================================}} \DoxyCodeLine{00002\ \textcolor{comment}{//\ \ open-\/cpp-\/utils,\ an\ open-\/source\ cpp\ library\ with\ data\ structures\ that\ extend\ the\ STL.}} \DoxyCodeLine{00003\ \textcolor{comment}{//\ \ Copyright\ (C)\ 2024\ \ Medusa\ Slockbower}} \DoxyCodeLine{00004\ \textcolor{comment}{//}} \DoxyCodeLine{00005\ \textcolor{comment}{//\ \ This\ program\ is\ free\ software:\ you\ can\ redistribute\ it\ and/or\ modify}} \DoxyCodeLine{00006\ \textcolor{comment}{//\ \ it\ under\ the\ terms\ of\ the\ GNU\ General\ Public\ License\ as\ published\ by}} \DoxyCodeLine{00007\ \textcolor{comment}{//\ \ the\ Free\ Software\ Foundation,\ either\ version\ 3\ of\ the\ License,\ or}} \DoxyCodeLine{00008\ \textcolor{comment}{//\ \ (at\ your\ option)\ any\ later\ version.}} \DoxyCodeLine{00009\ \textcolor{comment}{//}} \DoxyCodeLine{00010\ \textcolor{comment}{//\ \ This\ program\ is\ distributed\ in\ the\ hope\ that\ it\ will\ be\ useful,}} \DoxyCodeLine{00011\ \textcolor{comment}{//\ \ but\ WITHOUT\ ANY\ WARRANTY;\ without\ even\ the\ implied\ warranty\ of}} \DoxyCodeLine{00012\ \textcolor{comment}{//\ \ MERCHANTABILITY\ or\ FITNESS\ FOR\ A\ PARTICULAR\ PURPOSE.\ \ See\ the}} \DoxyCodeLine{00013\ \textcolor{comment}{//\ \ GNU\ General\ Public\ License\ for\ more\ details.}} \DoxyCodeLine{00014\ \textcolor{comment}{//}} \DoxyCodeLine{00015\ \textcolor{comment}{//\ \ You\ should\ have\ received\ a\ copy\ of\ the\ GNU\ General\ Public\ License}} \DoxyCodeLine{00016\ \textcolor{comment}{//\ \ along\ with\ this\ program.\ \ If\ not,\ see\ .}} \DoxyCodeLine{00017\ \textcolor{comment}{//\ =====================================================================================================================}} \DoxyCodeLine{00018\ } \DoxyCodeLine{00019\ \textcolor{preprocessor}{\#ifndef\ OPEN\_CPP\_UTILS\_OBJECT\_POOL\_H}} \DoxyCodeLine{00020\ \textcolor{preprocessor}{\#define\ OPEN\_CPP\_UTILS\_OBJECT\_POOL\_H}} \DoxyCodeLine{00021\ } \DoxyCodeLine{00022\ \textcolor{preprocessor}{\#include\ "{}dynarray.h"{}}} \DoxyCodeLine{00023\ \textcolor{preprocessor}{\#include\ "{}optional.h"{}}} \DoxyCodeLine{00024\ } \DoxyCodeLine{00025\ \textcolor{keyword}{namespace\ }open\_cpp\_utils} \DoxyCodeLine{00026\ \{} \DoxyCodeLine{00027\ } \DoxyCodeLine{00032\ \textcolor{keyword}{template}<\textcolor{keyword}{typename}\ T>} \DoxyCodeLine{00033\ \textcolor{keyword}{class\ }\mbox{\hyperlink{classopen__cpp__utils_1_1object__list}{object\_list}}} \DoxyCodeLine{00034\ \{} \DoxyCodeLine{00035\ \textcolor{comment}{//\ Typedefs\ ============================================================================================================}} \DoxyCodeLine{00036\ } \DoxyCodeLine{00037\ \textcolor{keyword}{public}:} \DoxyCodeLine{00038\ \ \ \ \ \textcolor{keyword}{using\ }value\_type\ \ \ \ \ \ =\ T;} \DoxyCodeLine{00039\ \ \ \ \ \textcolor{keyword}{using\ }pointer\ \ \ \ \ \ \ \ \ =\ T*;} \DoxyCodeLine{00040\ \ \ \ \ \textcolor{keyword}{using\ }const\_pointer\ \ \ =\ \textcolor{keyword}{const}\ T*;} \DoxyCodeLine{00041\ \ \ \ \ \textcolor{keyword}{using\ }reference\ \ \ \ \ \ \ =\ T\&;} \DoxyCodeLine{00042\ \ \ \ \ \textcolor{keyword}{using\ }const\_reference\ =\ \textcolor{keyword}{const}\ T\&;} \DoxyCodeLine{00043\ } \DoxyCodeLine{00044\ \ \ \ \ \textcolor{keyword}{using\ }uuid\_type\ \ =\ uint64\_t;} \DoxyCodeLine{00045\ } \DoxyCodeLine{00046\ \textcolor{keyword}{private}:} \DoxyCodeLine{00047\ \ \ \ \ \textcolor{keyword}{using\ }\mbox{\hyperlink{classopen__cpp__utils_1_1optional}{node}}\ =\ \mbox{\hyperlink{classopen__cpp__utils_1_1optional}{optional}};} \DoxyCodeLine{00048\ } \DoxyCodeLine{00049\ } \DoxyCodeLine{00050\ \textcolor{comment}{//\ Functions\ ===========================================================================================================}} \DoxyCodeLine{00051\ } \DoxyCodeLine{00052\ \textcolor{keyword}{public}:} \DoxyCodeLine{00053\ } \DoxyCodeLine{00054\ \textcolor{comment}{//\ Constructors\ \&\ Destructor\ -\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/}} \DoxyCodeLine{00055\ } \DoxyCodeLine{00056\ \ \ \ \ \mbox{\hyperlink{classopen__cpp__utils_1_1object__list}{object\_list}}()\ =\ \textcolor{keywordflow}{default};} \DoxyCodeLine{00057\ \ \ \ \ \mbox{\hyperlink{classopen__cpp__utils_1_1object__list}{object\_list}}(\textcolor{keyword}{const}\ \mbox{\hyperlink{classopen__cpp__utils_1_1object__list}{object\_list}}\&\ other)\ =\ \textcolor{keywordflow}{default};} \DoxyCodeLine{00058\ \ \ \ \ \mbox{\hyperlink{classopen__cpp__utils_1_1object__list}{object\_list}}(\mbox{\hyperlink{classopen__cpp__utils_1_1object__list}{object\_list}}\&\&\ other)\ =\ \textcolor{keywordflow}{default};} \DoxyCodeLine{00059\ \ \ \ \ \mbox{\hyperlink{classopen__cpp__utils_1_1object__list}{\string~object\_list}}()\ =\ \textcolor{keywordflow}{default};} \DoxyCodeLine{00060\ } \DoxyCodeLine{00061\ } \DoxyCodeLine{00062\ \textcolor{comment}{//\ Modifiers\ -\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/}} \DoxyCodeLine{00063\ } \DoxyCodeLine{00064\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ size()\textcolor{keyword}{\ const\ }\{\ \textcolor{keywordflow}{return}\ data\_.size();\ \}} \DoxyCodeLine{00065\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ capacity()\textcolor{keyword}{\ const\ }\{\ \textcolor{keywordflow}{return}\ data\_.capacity();\ \}} \DoxyCodeLine{00066\ } \DoxyCodeLine{00067\ } \DoxyCodeLine{00068\ \textcolor{comment}{//\ Modifiers\ -\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/}} \DoxyCodeLine{00069\ } \DoxyCodeLine{00070\ \ \ \ \ \textcolor{keywordtype}{void}\ clear()\ \{\ data\_.clear();\ freed\_.clear();\ \}} \DoxyCodeLine{00071\ } \DoxyCodeLine{00072\ \ \ \ \ uuid\_type\ insert(const\_reference\&\ value);} \DoxyCodeLine{00073\ \ \ \ \ \textcolor{keywordtype}{void}\ \ \ \ \ \ erase(uuid\_type\ \textcolor{keywordtype}{id});} \DoxyCodeLine{00074\ } \DoxyCodeLine{00075\ \ \ \ \ \mbox{\hyperlink{classopen__cpp__utils_1_1object__list}{object\_list}}\&\ operator=(\textcolor{keyword}{const}\ \mbox{\hyperlink{classopen__cpp__utils_1_1object__list}{object\_list}}\&)\ =\ \textcolor{keywordflow}{default};} \DoxyCodeLine{00076\ \ \ \ \ \mbox{\hyperlink{classopen__cpp__utils_1_1object__list}{object\_list}}\&\ operator=(\mbox{\hyperlink{classopen__cpp__utils_1_1object__list}{object\_list}}\&\&)\ =\ \textcolor{keywordflow}{default};} \DoxyCodeLine{00077\ } \DoxyCodeLine{00078\ } \DoxyCodeLine{00079\ \textcolor{comment}{//\ Accessors\ -\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/}} \DoxyCodeLine{00080\ } \DoxyCodeLine{00081\ \ \ \ \ reference\ \ \ \ \ \ \ operator[](uuid\_type\ \textcolor{keywordtype}{id})\ \ \ \ \ \ \ \{\ assert(data\_[\textcolor{keywordtype}{id}]());\ \textcolor{keywordflow}{return}\ data\_[id];\ \}} \DoxyCodeLine{00082\ \ \ \ \ const\_reference\ operator[](uuid\_type\ \textcolor{keywordtype}{id})\textcolor{keyword}{\ const\ }\{\ assert(data\_[\textcolor{keywordtype}{id}]());\ \textcolor{keywordflow}{return}\ data\_[id];\ \}} \DoxyCodeLine{00083\ \ \ \ \ \textcolor{keywordtype}{bool}\ \ \ \ \ \ \ \ \ \ \ \ operator()(uuid\_type\ \textcolor{keywordtype}{id})\textcolor{keyword}{\ const\ }\{\ \textcolor{keywordflow}{return}\ data\_[id]();\ \}} \DoxyCodeLine{00084\ } \DoxyCodeLine{00085\ \ \ \ \ \textcolor{keyword}{typename}\ dynarray::iterator\ begin()\ \{\ \textcolor{keywordflow}{return}\ data\_.begin();\ \}} \DoxyCodeLine{00086\ \ \ \ \ \textcolor{keyword}{typename}\ dynarray::iterator\ end()\ \ \ \{\ \textcolor{keywordflow}{return}\ data\_.end();\ \}} \DoxyCodeLine{00087\ } \DoxyCodeLine{00088\ } \DoxyCodeLine{00089\ \textcolor{comment}{//\ Variables\ ===========================================================================================================}} \DoxyCodeLine{00090\ } \DoxyCodeLine{00091\ \textcolor{keyword}{private}:} \DoxyCodeLine{00092\ \ \ \ \ dynarray\ \ \ \ \ \ data\_;} \DoxyCodeLine{00093\ \ \ \ \ dynarray\ freed\_;} \DoxyCodeLine{00094\ \};} \DoxyCodeLine{00095\ } \DoxyCodeLine{00096\ \textcolor{keyword}{template}<\textcolor{keyword}{typename}\ T>} \DoxyCodeLine{00097\ \textcolor{keyword}{typename}\ object\_list::uuid\_type\ \mbox{\hyperlink{classopen__cpp__utils_1_1object__list}{object\_list::insert}}(const\_reference\ value)} \DoxyCodeLine{00098\ \{} \DoxyCodeLine{00099\ \ \ \ \ \textcolor{keywordflow}{if}(freed\_.empty())\ \{\ data\_.push\_back(value);\ \textcolor{keywordflow}{return}\ data\_.size()\ -\/\ 1;\ \}} \DoxyCodeLine{00100\ } \DoxyCodeLine{00101\ \ \ \ \ uuid\_type\ \textcolor{keywordtype}{id}\ =\ freed\_.back();\ freed\_.pop\_back();} \DoxyCodeLine{00102\ \ \ \ \ data\_[id]\ =\ value;} \DoxyCodeLine{00103\ \ \ \ \ \textcolor{keywordflow}{return}\ id;} \DoxyCodeLine{00104\ \}} \DoxyCodeLine{00105\ } \DoxyCodeLine{00106\ \textcolor{keyword}{template}<\textcolor{keyword}{typename}\ T>} \DoxyCodeLine{00107\ \textcolor{keywordtype}{void}\ object\_list::erase(uuid\_type\ \textcolor{keywordtype}{id})} \DoxyCodeLine{00108\ \{} \DoxyCodeLine{00109\ \ \ \ \ data\_[id].reset();} \DoxyCodeLine{00110\ \ \ \ \ freed\_.push\_back(\textcolor{keywordtype}{id});} \DoxyCodeLine{00111\ \}} \DoxyCodeLine{00112\ } \DoxyCodeLine{00113\ \}\ \textcolor{comment}{//\ open\_cpp\_utils}} \DoxyCodeLine{00114\ } \DoxyCodeLine{00115\ \textcolor{preprocessor}{\#endif\ }\textcolor{comment}{//\ OPEN\_CPP\_UTILS\_OBJECT\_POOL\_H}} \end{DoxyCode}