mirror of
https://github.com/KartKrewDev/RingRacers.git
synced 2025-12-23 08:22:58 +00:00
Add srb2::MobjList, similar to std::forward_list
This commit is contained in:
parent
aba94d205b
commit
b5809b6ed2
1 changed files with 86 additions and 0 deletions
86
src/mobj_list.hpp
Normal file
86
src/mobj_list.hpp
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
// DR. ROBOTNIK'S RING RACERS
|
||||
//-----------------------------------------------------------------------------
|
||||
// Copyright (C) 2023 by James Robert Roman
|
||||
//
|
||||
// This program is free software distributed under the
|
||||
// terms of the GNU General Public License, version 2.
|
||||
// See the 'LICENSE' file for more details.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef mobj_list_hpp
|
||||
#define mobj_list_hpp
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "cxxutil.hpp"
|
||||
#include "mobj.hpp"
|
||||
#include "mobj_list_view.hpp"
|
||||
|
||||
namespace srb2
|
||||
{
|
||||
|
||||
// Requires:
|
||||
// void T::next(T*)
|
||||
// T* T::next() const
|
||||
template <typename T, mobj_t*& Head>
|
||||
struct MobjList
|
||||
{
|
||||
static_assert(std::is_convertible_v<T, mobj_t>);
|
||||
|
||||
MobjList() {}
|
||||
|
||||
T* front() const { return static_cast<T*>(Head); }
|
||||
bool empty() const { return !front(); }
|
||||
|
||||
void push_front(T* ptr)
|
||||
{
|
||||
ptr->next(front());
|
||||
front(ptr);
|
||||
}
|
||||
|
||||
void erase(T* node)
|
||||
{
|
||||
if (front() == node)
|
||||
{
|
||||
front(node->next());
|
||||
node->next(nullptr);
|
||||
return;
|
||||
}
|
||||
|
||||
auto view = this->view();
|
||||
auto end = view.end();
|
||||
auto it = view.begin();
|
||||
|
||||
SRB2_ASSERT(it != end);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
T* prev = *it;
|
||||
|
||||
it++;
|
||||
|
||||
if (it == end)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (*it == node)
|
||||
{
|
||||
prev->next(node->next());
|
||||
node->next(nullptr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto begin() const { return view().begin(); }
|
||||
auto end() const { return view().end(); }
|
||||
|
||||
private:
|
||||
void front(T* ptr) { Mobj::ManagedPtr {Head} = ptr; }
|
||||
auto view() const { return MobjListView(front(), [](T* node) { return node->next(); }); }
|
||||
};
|
||||
|
||||
}; // namespace srb2
|
||||
|
||||
#endif/*mobj_list_hpp*/
|
||||
Loading…
Add table
Reference in a new issue