OpenClonk
C4ObjectList.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* Dynamic object list */
19 
20 #ifndef INC_C4ObjectList
21 #define INC_C4ObjectList
22 
23 #include "object/C4Id.h"
24 
26 {
27 public:
30 };
31 
33 {
34 public:
35  virtual void OnObjectRemove(C4ObjectList * list, C4ObjectLink * link) {};
36  virtual void OnObjectAdded(C4ObjectList * list, C4ObjectLink * link) {};
37  virtual void OnObjectRename(C4ObjectList * list, C4ObjectLink * link) {};
38  virtual void OnObjectContainerChanged(C4Object *obj, C4Object *old_container, C4Object *new_container) {};
39  virtual ~C4ObjectListChangeListener() = default;
40 };
41 
43 
45 {
46 public:
47  C4ObjectList();
48  C4ObjectList(const C4ObjectList &list);
49  virtual ~C4ObjectList();
50 
52  int Mass;
53  std::list<int32_t> *pEnumerated;
54 
56 
57  // An iterator which survives if an object is removed from the list
58  class iterator
59  {
60  public:
61  ~iterator();
62  iterator& operator++ (); // prefix ++
63  iterator operator++ (int); // postfix ++
64  iterator(const iterator & iter);
65  C4Object * operator* ();
66  bool operator== (const iterator & iter) const;
67  bool operator!= (const iterator & iter) const;
68  // advance until end reached or link with target Obj found - return false in former case, true in latter
69  bool find(C4Object* target);
70  // return whether the iterator has reached the end
71  bool atEnd() const;
72  // advance the iterator position, either going forward or backward depending on the reverse flag
73  bool advance();
74  // reset the iterator to either the beginning or the end of the list, depending on the reverse flag
75  bool reset();
76 
77  iterator& operator=(const iterator & iter);
78  private:
79  iterator(const C4ObjectList & list, const C4ObjectLink * link, bool reverse);
80  const C4ObjectList & List;
81  // instead of pointing to the current link make a copy of it
82  C4ObjectLink link;
83  iterator * Next;
84  bool reverse;
85  friend class C4ObjectList;
86  };
87  iterator begin() const;
88  const iterator end() const;
89 
90  // Helper object returned by reverse() - allows for iterating the C4ObjectList in reverse order, still using the for (x : list) syntax
92  {
93  private:
94  const C4ObjectList& list;
95  public:
96  ReverseView(const C4ObjectList& list): list(list) {}
97  // return an iterator at the end of the list
98  iterator begin() const;
99  // return an iterator at the position before the first item in the list
100  iterator end() const;
101  };
102 
103  // Return a temporary object allowing for reverse-order iteration of the C4ObjectList
104  const ReverseView reverse() const { return ReverseView(*this); }
105 
106  virtual void Default();
107  virtual void Clear();
108  void Sort();
109  void Copy(const C4ObjectList &list);
110  void DrawIfCategory(C4TargetFacet &cgo, int player, uint32_t dwCategory, bool invert); // draw all objects that match dwCategory (or don't match if invert)
111  void Draw(C4TargetFacet &cgo, int player, int MinPlane, int MaxPlane); // draw all objects
112  void DrawSelectMark(C4TargetFacet &cgo) const;
113  void CloseMenus();
114  void UpdateGraphics(bool graphics_changed);
115  void UpdateFaces(bool update_shape);
116  void ClearInfo(C4ObjectInfo *info);
117 
118  typedef int SortProc(C4Object *, C4Object *);
119 
120  virtual bool Add(C4Object *new_obj, SortType sort_type, C4ObjectList *sorted_list = nullptr);
121  bool AddSortCustom(C4Object *new_obj, SortProc &pSortProc);
122  virtual bool Remove(C4Object *obj);
123 
124  virtual bool AssignInfo();
125  virtual bool ValidateOwners();
126  StdStrBuf GetNameList(C4DefList &defs) const;
127  bool IsClear() const;
128  bool DenumeratePointers();
129  bool Write(char *szTarget);
130  void CompileFunc(StdCompiler *pComp, C4ValueNumbers * = nullptr);
131  void CompileFunc(StdCompiler *pComp, bool skip_player_objects, C4ValueNumbers *);
132  void Denumerate(C4ValueNumbers *);
133 
134  bool IsContained(const C4Object *obj) const;
135  int ClearPointers(C4Object *obj);
136  int ObjectCount(C4ID id=C4ID::None) const;
137  int MassCount();
138  int ListIDCount(int32_t dwCategory) const;
139 
140  C4Object* GetObject(int index = 0) const;
141  C4Object* GetFirstObject() const { return First ? First->Obj : nullptr; }
142  C4Object* GetLastObject() const { return Last ? Last->Obj : nullptr; }
143  C4Object* Find(C4Def * def, int owner = ANY_OWNER, DWORD dwOCF = OCF_All);
144  C4Object* FindOther(C4ID id, int owner = ANY_OWNER);
145 
146  const C4ObjectLink* GetLink(const C4Object *obj) const;
148  { return const_cast<C4ObjectLink*>(const_cast<const C4ObjectList*>(this)->GetLink(obj)); }
149 
150  C4ID GetListID(int32_t dwCategory, int index) const;
151 
152  bool ShiftContents(C4Object *new_first); // cycle list so new_first is at front
153 
154  void DeleteObjects(); // delete all objects and links
155 
156  void UpdateScriptPointers(); // update pointers to C4AulScript *
157 
158  bool CheckSort(C4ObjectList *list); // check that all objects of this list appear in the other list in the same order
159  void CheckCategorySort(); // assertwhether sorting by category is done right
160 
161 protected:
162  virtual void InsertLinkBefore(C4ObjectLink *link, C4ObjectLink *before_link);
163  virtual void InsertLink(C4ObjectLink *link, C4ObjectLink *after_link);
164  virtual void RemoveLink(C4ObjectLink *link);
165  mutable iterator * FirstIter{nullptr};
166  iterator * AddIter(iterator * iter) const;
167  void RemoveIter(iterator * iter) const;
168 
169  friend class iterator;
170 };
171 
173 {
174 public:
178  ~C4NotifyingObjectList() override = default;
179 protected:
180  void InsertLinkBefore(C4ObjectLink *link, C4ObjectLink *before_link) override;
181  void InsertLink(C4ObjectLink *link, C4ObjectLink *after_link) override;
182  void RemoveLink(C4ObjectLink *link) override;
183 };
184 
185 // This iterator is used to return objects of same ID and picture as grouped.
186 // It's taking advantage of the fact that object lists are sorted by ID.
187 // Used by functions such as C4ObjectList::DrawIDList, or the menu-filling of
188 // activation/selling menus
190 {
191 private:
192  C4ObjectList & rList; // iterated list
193  C4ObjectList::iterator pCurr; // link to last returned object
194  C4ObjectList::iterator pCurrID; // link to head of link group with same ID
195 
196  C4ObjectListIterator(const C4ObjectListIterator &rCopy) = delete; // no copy ctor
197 public:
198  C4ObjectListIterator(C4ObjectList &rList) : rList(rList), pCurr(rList.end()), pCurrID(rList.begin()) {} // ctor
199  C4Object *GetNext(int32_t *piCount); // get next object; return nullptr if end is reached
200 };
201 
202 #endif
const int ANY_OWNER
Definition: C4Constants.h:138
const uint32_t OCF_All
Definition: C4Constants.h:78
C4ObjectLink * Prev
Definition: C4ObjectList.h:29
C4ObjectListChangeListener & ObjectListChangeListener
C4ObjectLink * Next
Definition: C4ObjectList.h:29
C4Object * Obj
Definition: C4ObjectList.h:28
uint32_t DWORD
Definition: C4Def.h:99
Definition: C4Id.h:26
static const C4ID None
Definition: C4Id.h:39
C4NotifyingObjectList(const C4ObjectList &list)
Definition: C4ObjectList.h:177
C4NotifyingObjectList(const C4NotifyingObjectList &list)=default
void InsertLink(C4ObjectLink *link, C4ObjectLink *after_link) override
~C4NotifyingObjectList() override=default
void InsertLinkBefore(C4ObjectLink *link, C4ObjectLink *before_link) override
C4NotifyingObjectList()=default
void RemoveLink(C4ObjectLink *link) override
ReverseView(const C4ObjectList &list)
Definition: C4ObjectList.h:96
iterator & operator=(const iterator &iter)
iterator(const iterator &iter)
bool find(C4Object *target)
bool operator!=(const iterator &iter) const
bool operator==(const iterator &iter) const
virtual void OnObjectContainerChanged(C4Object *obj, C4Object *old_container, C4Object *new_container)
Definition: C4ObjectList.h:38
virtual void OnObjectRename(C4ObjectList *list, C4ObjectLink *link)
Definition: C4ObjectList.h:37
virtual void OnObjectRemove(C4ObjectList *list, C4ObjectLink *link)
Definition: C4ObjectList.h:35
virtual void OnObjectAdded(C4ObjectList *list, C4ObjectLink *link)
Definition: C4ObjectList.h:36
virtual ~C4ObjectListChangeListener()=default
bool DenumeratePointers()
virtual void Clear()
void UpdateFaces(bool update_shape)
void UpdateGraphics(bool graphics_changed)
void UpdateScriptPointers()
bool ShiftContents(C4Object *new_first)
virtual void Default()
C4ObjectLink * First
Definition: C4ObjectList.h:51
virtual bool AssignInfo()
int ListIDCount(int32_t dwCategory) const
C4Object * GetLastObject() const
Definition: C4ObjectList.h:142
bool AddSortCustom(C4Object *new_obj, SortProc &pSortProc)
void Copy(const C4ObjectList &list)
C4Object * GetFirstObject() const
Definition: C4ObjectList.h:141
virtual void InsertLinkBefore(C4ObjectLink *link, C4ObjectLink *before_link)
int SortProc(C4Object *, C4Object *)
Definition: C4ObjectList.h:118
iterator begin() const
void Draw(C4TargetFacet &cgo, int player, int MinPlane, int MaxPlane)
iterator * AddIter(iterator *iter) const
bool Write(char *szTarget)
bool IsClear() const
void DrawSelectMark(C4TargetFacet &cgo) const
const C4ObjectLink * GetLink(const C4Object *obj) const
virtual bool Add(C4Object *new_obj, SortType sort_type, C4ObjectList *sorted_list=nullptr)
friend class iterator
Definition: C4ObjectList.h:169
virtual bool ValidateOwners()
C4ObjectLink * Last
Definition: C4ObjectList.h:51
virtual void RemoveLink(C4ObjectLink *link)
iterator * FirstIter
Definition: C4ObjectList.h:165
C4ID GetListID(int32_t dwCategory, int index) const
const ReverseView reverse() const
Definition: C4ObjectList.h:104
C4Object * Find(C4Def *def, int owner=ANY_OWNER, DWORD dwOCF=OCF_All)
virtual bool Remove(C4Object *obj)
virtual void InsertLink(C4ObjectLink *link, C4ObjectLink *after_link)
void CheckCategorySort()
int ObjectCount(C4ID id=C4ID::None) const
void CompileFunc(StdCompiler *pComp, C4ValueNumbers *=nullptr)
void ClearInfo(C4ObjectInfo *info)
C4ObjectLink * GetLink(const C4Object *obj)
Definition: C4ObjectList.h:147
StdStrBuf GetNameList(C4DefList &defs) const
std::list< int32_t > * pEnumerated
Definition: C4ObjectList.h:53
int ClearPointers(C4Object *obj)
bool IsContained(const C4Object *obj) const
void DeleteObjects()
C4Object * FindOther(C4ID id, int owner=ANY_OWNER)
bool CheckSort(C4ObjectList *list)
void DrawIfCategory(C4TargetFacet &cgo, int player, uint32_t dwCategory, bool invert)
void Denumerate(C4ValueNumbers *)
const iterator end() const
virtual ~C4ObjectList()
C4Object * GetObject(int index=0) const
void RemoveIter(iterator *iter) const
C4Object * GetNext(int32_t *piCount)
C4ObjectListIterator(C4ObjectList &rList)
Definition: C4ObjectList.h:198