2023-09-17 00:18:56 -04:00
|
|
|
#ifndef MXLIST_H
|
|
|
|
#define MXLIST_H
|
|
|
|
|
2023-11-19 07:23:30 -05:00
|
|
|
#include "mxcollection.h"
|
2023-09-17 00:18:56 -04:00
|
|
|
#include "mxcore.h"
|
2023-10-24 19:38:27 -04:00
|
|
|
#include "mxtypes.h"
|
2023-09-17 00:18:56 -04:00
|
|
|
|
2023-10-24 19:24:29 -04:00
|
|
|
template <class T>
|
|
|
|
class MxList;
|
|
|
|
template <class T>
|
|
|
|
class MxListCursor;
|
|
|
|
|
2023-10-02 09:51:43 -04:00
|
|
|
template <class T>
|
2023-10-24 19:38:27 -04:00
|
|
|
class MxListEntry {
|
2023-09-17 00:18:56 -04:00
|
|
|
public:
|
2023-10-24 19:38:27 -04:00
|
|
|
MxListEntry() {}
|
|
|
|
MxListEntry(T p_obj, MxListEntry* p_prev)
|
|
|
|
{
|
|
|
|
m_obj = p_obj;
|
|
|
|
m_prev = p_prev;
|
|
|
|
m_next = NULL;
|
|
|
|
}
|
|
|
|
MxListEntry(T p_obj, MxListEntry* p_prev, MxListEntry* p_next)
|
|
|
|
{
|
|
|
|
m_obj = p_obj;
|
|
|
|
m_prev = p_prev;
|
|
|
|
m_next = p_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
T GetValue() { return this->m_obj; }
|
2023-11-06 08:04:51 -05:00
|
|
|
MxListEntry* GetNext() { return m_next; }
|
|
|
|
MxListEntry* GetPrev() { return m_prev; }
|
2023-10-24 19:38:27 -04:00
|
|
|
|
2023-11-06 08:04:51 -05:00
|
|
|
void SetValue(T p_obj) { m_obj = p_obj; }
|
|
|
|
void SetNext(MxListEntry* p_next) { m_next = p_next; }
|
|
|
|
void SetPrev(MxListEntry* p_prev) { m_prev = p_prev; }
|
2023-10-24 19:38:27 -04:00
|
|
|
|
2023-10-02 09:51:43 -04:00
|
|
|
private:
|
2023-10-24 19:38:27 -04:00
|
|
|
T m_obj;
|
|
|
|
MxListEntry* m_prev;
|
|
|
|
MxListEntry* m_next;
|
2023-09-17 00:18:56 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// SIZE 0x18
|
|
|
|
template <class T>
|
2023-11-19 07:23:30 -05:00
|
|
|
class MxList : protected MxCollection<T> {
|
2023-09-17 00:18:56 -04:00
|
|
|
public:
|
2023-10-24 19:38:27 -04:00
|
|
|
MxList()
|
|
|
|
{
|
|
|
|
m_last = NULL;
|
|
|
|
m_first = NULL;
|
|
|
|
}
|
2023-09-17 00:18:56 -04:00
|
|
|
|
2023-11-09 10:52:58 -05:00
|
|
|
virtual ~MxList() override;
|
2023-09-17 06:38:50 -04:00
|
|
|
|
2023-12-13 05:48:14 -05:00
|
|
|
void Append(T p_obj) { InsertEntry(p_obj, this->m_last, NULL); };
|
2023-12-23 10:42:30 -05:00
|
|
|
void Prepend(T p_obj) { InsertEntry(p_obj, NULL, this->m_first); };
|
2023-12-11 16:35:50 -05:00
|
|
|
void DeleteAll(MxBool p_destroy = TRUE);
|
2023-10-24 19:38:27 -04:00
|
|
|
MxU32 GetCount() { return this->m_count; }
|
|
|
|
|
|
|
|
friend class MxListCursor<T>;
|
2023-12-20 20:09:05 -05:00
|
|
|
using MxCollection<T>::SetDestroy;
|
2023-09-17 06:38:50 -04:00
|
|
|
|
2023-09-17 00:18:56 -04:00
|
|
|
protected:
|
2023-11-19 07:23:30 -05:00
|
|
|
MxListEntry<T>* m_first; // 0x10
|
|
|
|
MxListEntry<T>* m_last; // 0x14
|
2023-09-17 06:38:50 -04:00
|
|
|
|
2023-12-13 05:48:14 -05:00
|
|
|
void DeleteEntry(MxListEntry<T>*);
|
|
|
|
MxListEntry<T>* InsertEntry(T, MxListEntry<T>*, MxListEntry<T>*);
|
2023-09-17 06:38:50 -04:00
|
|
|
};
|
|
|
|
|
2023-12-20 20:09:05 -05:00
|
|
|
template <class T>
|
|
|
|
class MxPtrList : public MxList<T*> {
|
|
|
|
public:
|
|
|
|
MxPtrList(MxBool p_ownership) { SetOwnership(p_ownership); }
|
|
|
|
|
|
|
|
static void Destroy(T* p_obj) { delete p_obj; };
|
|
|
|
|
2024-01-06 12:56:15 -05:00
|
|
|
void SetOwnership(MxBool p_ownership)
|
|
|
|
{
|
|
|
|
MxCollection<T*>::SetDestroy(p_ownership ? MxPtrList<T>::Destroy : MxCollection<T*>::Destroy);
|
|
|
|
}
|
2023-12-20 20:09:05 -05:00
|
|
|
};
|
|
|
|
|
2023-09-17 06:38:50 -04:00
|
|
|
template <class T>
|
2023-10-24 19:38:27 -04:00
|
|
|
class MxListCursor : public MxCore {
|
2023-09-17 06:38:50 -04:00
|
|
|
public:
|
2023-10-24 19:38:27 -04:00
|
|
|
MxListCursor(MxList<T>* p_list)
|
|
|
|
{
|
|
|
|
m_list = p_list;
|
|
|
|
m_match = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
MxBool Find(T p_obj);
|
|
|
|
void Detach();
|
2023-10-31 11:30:13 -04:00
|
|
|
void Destroy();
|
2024-01-06 12:06:50 -05:00
|
|
|
MxBool Next();
|
2023-10-24 19:38:27 -04:00
|
|
|
MxBool Next(T& p_obj);
|
2024-01-06 12:06:50 -05:00
|
|
|
MxBool Prev();
|
2023-12-28 23:37:21 -05:00
|
|
|
MxBool Prev(T& p_obj);
|
2023-10-31 11:30:13 -04:00
|
|
|
MxBool Current(T& p_obj);
|
2023-12-22 14:05:42 -05:00
|
|
|
MxBool First(T& p_obj);
|
|
|
|
MxBool Last(T& p_obj);
|
2023-10-31 11:30:13 -04:00
|
|
|
MxBool HasMatch() { return m_match != NULL; }
|
2023-10-24 19:38:27 -04:00
|
|
|
void SetValue(T p_obj);
|
2023-12-28 23:37:21 -05:00
|
|
|
MxBool Head()
|
|
|
|
{
|
|
|
|
m_match = m_list->m_first;
|
|
|
|
return m_match != NULL;
|
|
|
|
}
|
|
|
|
MxBool Tail()
|
|
|
|
{
|
|
|
|
m_match = m_list->m_last;
|
|
|
|
return m_match != NULL;
|
|
|
|
}
|
2023-10-24 19:38:27 -04:00
|
|
|
void Reset() { m_match = NULL; }
|
2023-10-31 11:30:13 -04:00
|
|
|
void Prepend(T p_newobj);
|
2023-09-17 06:38:50 -04:00
|
|
|
|
2023-12-11 16:35:50 -05:00
|
|
|
// TODO: Probably shouldn't exist
|
|
|
|
void NextFragment()
|
|
|
|
{
|
|
|
|
if (m_match)
|
|
|
|
m_match = m_match->GetNext();
|
|
|
|
}
|
|
|
|
|
2023-09-17 06:38:50 -04:00
|
|
|
private:
|
2023-12-28 23:37:21 -05:00
|
|
|
MxList<T>* m_list; // 0x08
|
|
|
|
MxListEntry<T>* m_match; // 0x0c
|
2023-09-17 06:38:50 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
2023-12-07 14:14:49 -05:00
|
|
|
class MxPtrListCursor : public MxListCursor<T*> {
|
2023-09-17 06:38:50 -04:00
|
|
|
public:
|
2023-12-07 14:14:49 -05:00
|
|
|
MxPtrListCursor(MxPtrList<T>* p_list) : MxListCursor<T*>(p_list){};
|
2023-09-17 00:18:56 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
MxList<T>::~MxList()
|
2023-09-25 13:08:19 -04:00
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
DeleteAll();
|
2023-09-25 13:08:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2023-12-11 16:35:50 -05:00
|
|
|
inline void MxList<T>::DeleteAll(MxBool p_destroy)
|
2023-09-17 00:18:56 -04:00
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
for (MxListEntry<T>* t = m_first;;) {
|
|
|
|
if (!t)
|
|
|
|
break;
|
|
|
|
|
2023-11-06 08:04:51 -05:00
|
|
|
MxListEntry<T>* next = t->GetNext();
|
2023-12-11 16:35:50 -05:00
|
|
|
|
|
|
|
if (p_destroy)
|
|
|
|
this->m_customDestructor(t->GetValue());
|
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
delete t;
|
|
|
|
t = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->m_count = 0;
|
|
|
|
m_last = NULL;
|
|
|
|
m_first = NULL;
|
2023-09-17 00:18:56 -04:00
|
|
|
}
|
|
|
|
|
2023-10-07 14:03:15 -04:00
|
|
|
template <class T>
|
2023-12-13 05:48:14 -05:00
|
|
|
inline MxListEntry<T>* MxList<T>::InsertEntry(T p_newobj, MxListEntry<T>* p_prev, MxListEntry<T>* p_next)
|
2023-10-07 14:03:15 -04:00
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
MxListEntry<T>* newEntry = new MxListEntry<T>(p_newobj, p_prev, p_next);
|
2023-10-07 14:03:15 -04:00
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
if (p_prev)
|
2023-11-06 08:04:51 -05:00
|
|
|
p_prev->SetNext(newEntry);
|
2023-10-24 19:38:27 -04:00
|
|
|
else
|
|
|
|
this->m_first = newEntry;
|
2023-10-07 14:03:15 -04:00
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
if (p_next)
|
2023-11-06 08:04:51 -05:00
|
|
|
p_next->SetPrev(newEntry);
|
2023-10-24 19:38:27 -04:00
|
|
|
else
|
|
|
|
this->m_last = newEntry;
|
2023-10-07 14:03:15 -04:00
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
this->m_count++;
|
2023-10-25 18:02:49 -04:00
|
|
|
return newEntry;
|
2023-10-07 14:03:15 -04:00
|
|
|
}
|
|
|
|
|
2023-09-17 06:38:50 -04:00
|
|
|
template <class T>
|
2023-12-13 05:48:14 -05:00
|
|
|
inline void MxList<T>::DeleteEntry(MxListEntry<T>* p_match)
|
2023-09-17 06:38:50 -04:00
|
|
|
{
|
2023-12-13 05:48:14 -05:00
|
|
|
if (p_match->GetPrev())
|
|
|
|
p_match->GetPrev()->SetNext(p_match->GetNext());
|
2023-10-24 19:38:27 -04:00
|
|
|
else
|
2023-12-13 05:48:14 -05:00
|
|
|
m_first = p_match->GetNext();
|
2023-09-17 06:38:50 -04:00
|
|
|
|
2023-12-13 05:48:14 -05:00
|
|
|
if (p_match->GetNext())
|
|
|
|
p_match->GetNext()->SetPrev(p_match->GetPrev());
|
2023-10-24 19:38:27 -04:00
|
|
|
else
|
2023-12-13 05:48:14 -05:00
|
|
|
m_last = p_match->GetPrev();
|
2023-09-17 06:38:50 -04:00
|
|
|
|
2023-12-13 05:48:14 -05:00
|
|
|
delete p_match;
|
2023-10-24 19:38:27 -04:00
|
|
|
this->m_count--;
|
2023-09-17 06:38:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2023-10-02 09:51:43 -04:00
|
|
|
inline MxBool MxListCursor<T>::Find(T p_obj)
|
2023-09-17 06:38:50 -04:00
|
|
|
{
|
2023-11-06 08:04:51 -05:00
|
|
|
for (m_match = m_list->m_first; m_match && m_list->Compare(m_match->GetValue(), p_obj);
|
|
|
|
m_match = m_match->GetNext())
|
2023-10-24 19:38:27 -04:00
|
|
|
;
|
2023-09-17 06:38:50 -04:00
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
return m_match != NULL;
|
2023-09-17 06:38:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline void MxListCursor<T>::Detach()
|
|
|
|
{
|
2023-12-23 10:42:30 -05:00
|
|
|
if (m_match) {
|
|
|
|
m_list->DeleteEntry(m_match);
|
|
|
|
m_match = NULL;
|
|
|
|
}
|
2023-09-17 06:38:50 -04:00
|
|
|
}
|
|
|
|
|
2023-10-31 11:30:13 -04:00
|
|
|
template <class T>
|
|
|
|
inline void MxListCursor<T>::Destroy()
|
|
|
|
{
|
2023-11-06 08:04:51 -05:00
|
|
|
if (m_match) {
|
|
|
|
m_list->m_customDestructor(m_match->GetValue());
|
2023-12-23 10:42:30 -05:00
|
|
|
m_list->DeleteEntry(m_match);
|
|
|
|
m_match = NULL;
|
2023-11-06 08:04:51 -05:00
|
|
|
}
|
2023-10-31 11:30:13 -04:00
|
|
|
}
|
|
|
|
|
2024-01-06 12:06:50 -05:00
|
|
|
template <class T>
|
|
|
|
inline MxBool MxListCursor<T>::Next()
|
|
|
|
{
|
|
|
|
if (!m_match)
|
|
|
|
m_match = m_list->m_first;
|
|
|
|
else
|
|
|
|
m_match = m_match->GetNext();
|
|
|
|
|
|
|
|
return m_match != NULL;
|
|
|
|
}
|
|
|
|
|
2023-09-17 06:38:50 -04:00
|
|
|
template <class T>
|
2023-10-02 09:51:43 -04:00
|
|
|
inline MxBool MxListCursor<T>::Next(T& p_obj)
|
2023-09-17 06:38:50 -04:00
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
if (!m_match)
|
|
|
|
m_match = m_list->m_first;
|
|
|
|
else
|
2023-11-06 08:04:51 -05:00
|
|
|
m_match = m_match->GetNext();
|
2023-09-17 06:38:50 -04:00
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
if (m_match)
|
|
|
|
p_obj = m_match->GetValue();
|
2023-09-17 06:38:50 -04:00
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
return m_match != NULL;
|
2023-09-17 06:38:50 -04:00
|
|
|
}
|
|
|
|
|
2024-01-06 12:06:50 -05:00
|
|
|
template <class T>
|
|
|
|
inline MxBool MxListCursor<T>::Prev()
|
|
|
|
{
|
|
|
|
if (!m_match)
|
|
|
|
m_match = m_list->m_last;
|
|
|
|
else
|
|
|
|
m_match = m_match->GetPrev();
|
|
|
|
|
|
|
|
return m_match != NULL;
|
|
|
|
}
|
|
|
|
|
2023-12-28 23:37:21 -05:00
|
|
|
template <class T>
|
|
|
|
inline MxBool MxListCursor<T>::Prev(T& p_obj)
|
|
|
|
{
|
|
|
|
if (!m_match)
|
|
|
|
m_match = m_list->m_last;
|
|
|
|
else
|
|
|
|
m_match = m_match->GetPrev();
|
|
|
|
|
|
|
|
if (m_match)
|
|
|
|
p_obj = m_match->GetValue();
|
|
|
|
|
|
|
|
return m_match != NULL;
|
|
|
|
}
|
|
|
|
|
2023-10-31 11:30:13 -04:00
|
|
|
template <class T>
|
|
|
|
inline MxBool MxListCursor<T>::Current(T& p_obj)
|
|
|
|
{
|
|
|
|
if (m_match)
|
|
|
|
p_obj = m_match->GetValue();
|
|
|
|
|
|
|
|
return m_match != NULL;
|
|
|
|
}
|
|
|
|
|
2023-12-22 14:05:42 -05:00
|
|
|
template <class T>
|
|
|
|
inline MxBool MxListCursor<T>::First(T& p_obj)
|
|
|
|
{
|
|
|
|
m_match = m_list->m_first;
|
|
|
|
if (m_match)
|
|
|
|
p_obj = m_match->GetValue();
|
|
|
|
|
|
|
|
return m_match != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline MxBool MxListCursor<T>::Last(T& p_obj)
|
|
|
|
{
|
|
|
|
m_match = m_list->m_last;
|
|
|
|
if (m_match)
|
|
|
|
p_obj = m_match->GetValue();
|
|
|
|
|
|
|
|
return m_match != NULL;
|
|
|
|
}
|
|
|
|
|
2023-09-19 23:00:34 -04:00
|
|
|
template <class T>
|
2023-10-02 09:51:43 -04:00
|
|
|
inline void MxListCursor<T>::SetValue(T p_obj)
|
2023-09-19 23:00:34 -04:00
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
if (m_match)
|
2023-11-06 08:04:51 -05:00
|
|
|
m_match->SetValue(p_obj);
|
2023-09-19 23:00:34 -04:00
|
|
|
}
|
|
|
|
|
2023-10-31 11:30:13 -04:00
|
|
|
template <class T>
|
|
|
|
inline void MxListCursor<T>::Prepend(T p_newobj)
|
|
|
|
{
|
|
|
|
if (m_match)
|
2023-12-13 05:48:14 -05:00
|
|
|
m_list->InsertEntry(p_newobj, m_match->GetPrev(), m_match);
|
2023-10-31 11:30:13 -04:00
|
|
|
}
|
|
|
|
|
2023-09-19 23:00:34 -04:00
|
|
|
#endif // MXLIST_H
|