Populous: The Beginning Script 3  1.0.0
Documentation for Populous Script 3 engine
ObjectList.h
Go to the documentation of this file.
1 #pragma once
2 #include <EASTL/vector.h>
3 #include "ObjectProxy.h"
4 
5 class ObjectList;
6 
7 struct OBJLIST
8 {
10  struct OBJLIST* prev;
11  struct OBJLIST* next;
12 };
13 
15 {
17  struct OBJLIST** _curr;
18 };
19 
20 enum class ObjectListType
21 {
22  None,
23  FreeList,
24  UsedList,
25  TypeList
26 };
27 
29 {
30 public:
32  ~ObjectList();
33 
34  ObjectList(const ObjectList & cpy);
35  void setObjectListType(enum class ObjectListType _t);
36  ObjectList& operator=(ObjectList const& rhs);
37 
38  OBJLIST* insert(Thing* t);
39  void remove(Thing* t, bool OBJ3_DELETE = false); // Remove from Thing::List
40 
41  Thing* front() const;
42  Thing* tail() const;
43  Thing* pop_front();
44  Thing* pop_back();
45 
46  Thing* getNextThing(Thing* t) const; // Gets Child Pointer
47  Thing* getPreviousThing(Thing* t) const; // Gets Parent Pointer
48 
49  size_t count() const;
50  enum class ObjectListType whatListAmI() const;
51 
52  template<typename Lambda>
53  Thing* processList(Lambda&& f); // Takes a std::function<bool(Thing*t)> and calls that for each thing. `Break;` is return false. *** CAN RETURN NULLPTR! ***
54 
55  inline Thing* processList2(eastl::function<bool(Thing*)> f)
56  {
57  return processList(f);
58  }
59 
60  void reset(); // Reset the list head and tail to 0.
61  bool isEmpty() const; // Is there even a list here?s
62  OBJLIST* AmIInList(Thing * t) const;
63 
64  Thing* getNth(size_t i) const;
65 
66  eastl::vector<Thing*> toThingVector();
67  eastl::vector<ObjectProxy> toObjectProxyVector();
68 
69  template <class Archive>
70  void save(Archive& ar) const
71  {
72  ar(_count, _ListType);
73  ThingNum curr;
74  for (auto it = _start; it; it = it->next)
75  {
76  curr = it->t_thing->ThingNum;
77  ASSERT(curr > 0);
78  ar(curr);
79  }
80  }
81 
82  template <class Archive>
83  void load(Archive& ar)
84  {
85  reset();
86 
87  size_t currCount;
88  ThingNum curr;
89  ar(currCount, _ListType);
90 
91  for (auto i = 0; i < currCount; i++)
92  {
93  ar(curr);
94  ASSERT(curr > 0);
95  if (gsi.ObjectMan.isLoadingCritical())
96  {
97  gsi.ObjectMan.addObjectListToMap(this, curr);
98  }
99  else
100  {
101  ASSERT(gsi.ObjectMan.getThingNoChecks(curr));
102  auto t = gsi.ObjectMan.getThingNoChecks(curr);
104  t->FreeList = insert(t);
106  t->UsedList = insert(t);
108  t->TypeList[t->Type] = insert(t);
109  else
110  insert(t);
111  }
112  }
113  }
114 private:
115  static eastl::vector<Internal_It> _sit;
116 
118  struct OBJLIST* _start;
119  struct OBJLIST* _end;
120  size_t _count;
121 };
122 
123 template <typename Lambda>
125 {
126  if (_count)
127  {
128  auto it = _start;
129  _sit.push_back({ this, &it });
130  while (true)
131  {
132  if (!f(it->t_thing))
133  {
134  _sit.pop_back();
135  return it->t_thing;
136  }
137  if (!it || !it->next) break;
138  it = it->next;
139  }
140  _sit.pop_back();
141  }
142  return nullptr;
143 }
Thing * getNth(size_t i) const
enum ObjectListType _ListType
Definition: ObjectList.h:117
ObjectListType
Definition: ObjectList.h:20
size_t _count
Definition: ObjectList.h:120
OBJLIST * insert(Thing *t)
enum ObjectListType whatListAmI() const
struct OBJLIST * next
Definition: ObjectList.h:11
bool isEmpty() const
Thing * front() const
Thing * processList(Lambda &&f)
Definition: ObjectList.h:124
Thing * pop_front()
struct OBJLIST ** _curr
Definition: ObjectList.h:17
void load(Archive &ar)
Definition: ObjectList.h:83
OBJLIST * AmIInList(Thing *t) const
GlobalSaveItems gsi
Thing * getPreviousThing(Thing *t) const
Thing * pop_back()
void remove(Thing *t, bool OBJ3_DELETE=false)
ObjectList & operator=(ObjectList const &rhs)
ObjectList(enum class ObjectListType t=ObjectListType::None)
Thing * getNextThing(Thing *t) const
ObjectList * _ol
Definition: ObjectList.h:16
struct OBJLIST * _end
Definition: ObjectList.h:119
void reset()
struct OBJLIST * prev
Definition: ObjectList.h:10
void setObjectListType(enum class ObjectListType _t)
struct OBJLIST * _start
Definition: ObjectList.h:118
Thing * processList2(eastl::function< bool(Thing *)> f)
Definition: ObjectList.h:55
void save(Archive &ar) const
Definition: ObjectList.h:70
eastl::vector< ObjectProxy > toObjectProxyVector()
size_t count() const
Thing * t_thing
Definition: ObjectList.h:9
Thing * tail() const
eastl::vector< Thing * > toThingVector()
static eastl::vector< Internal_It > _sit
Definition: ObjectList.h:115