From 20d28e80a5c861a9d5f449ea911ab75b4f37ad0d Mon Sep 17 00:00:00 2001 From: Jef Date: Tue, 24 Sep 2024 14:54:57 +0200 Subject: Initial community commit --- Src/pfc/ptr_list.h | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 Src/pfc/ptr_list.h (limited to 'Src/pfc/ptr_list.h') diff --git a/Src/pfc/ptr_list.h b/Src/pfc/ptr_list.h new file mode 100644 index 00000000..063395bb --- /dev/null +++ b/Src/pfc/ptr_list.h @@ -0,0 +1,127 @@ +#include "mem_block.h" + +class ptr_list +{ +private: + mem_block_t data; + int count; + +protected: + void * get_raw_ptr() {return data.get_ptr();} + +public: + ptr_list() {count=0;} + int get_count() const {return count;} + void * get_item(int n) const + { + if (n>=0 && n=0;} + + void remove_item(void * ptr) + { + int idx = find_item(ptr); + if (idx>=0) remove_by_idx(idx); + } + void * remove_by_idx(int idx) + { + void * ptr = 0; + if (idx>=0 && idxcount || idx<0) idx = count; + + count++; + data.check_size(count); + int n; + for(n=count-1;n>idx;n--) + { + data[n]=data[n-1]; + } + data[idx] = ptr; + + return idx; + } + + void * operator[](int idx) const {return get_item(idx);} +}; + +template +class ptr_list_t : protected ptr_list +{ +public: + int get_count() const {return ptr_list::get_count();} + + T * get_item(int n) const {return static_cast(ptr_list::get_item(n));} + int add_item(T * ptr) {return ptr_list::add_item(static_cast(ptr));} + int find_item(T * ptr) {return ptr_list::find_item(static_cast(ptr));} + bool have_item(T * ptr) {return ptr_list::have_item(static_cast(ptr));} + void remove_item(T * ptr) {ptr_list::remove_item(static_cast(ptr));} + T * remove_by_idx(int idx) {return static_cast(ptr_list::remove_by_idx(idx));} + void remove_all() {ptr_list::remove_all();} + void * operator[](int idx) const {return get_item(idx);} + int insert_item(int idx,T* ptr) {return ptr_list::insert_item(idx,static_cast(ptr));} + + void delete_item(T * ptr) + { + remove_item(ptr); + delete ptr; + } + + void delete_by_idx(int idx) + { + T * ptr = remove_by_idx(idx); + if (ptr) delete ptr; + } + + void delete_all() + { + int n,max=get_count(); + for(n=0;n