OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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 * pList, C4ObjectLink * pLnk) {};
36  virtual void OnObjectAdded(C4ObjectList * pList, C4ObjectLink * pLnk) {};
37  virtual void OnObjectRename(C4ObjectList * pList, C4ObjectLink * pLnk) {};
38  virtual void OnObjectContainerChanged(C4Object *obj, C4Object *old_container, C4Object *new_container) {};
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 * pLink, 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 &rList);
110  void DrawIfCategory(C4TargetFacet &cgo, int iPlayer, uint32_t dwCat, bool fInvert); // draw all objects that match dwCat (or don't match if fInvert)
111  void Draw(C4TargetFacet &cgo, int iPlayer, int MinPlane, int MaxPlane); // draw all objects
112  void DrawSelectMark(C4TargetFacet &cgo) const;
113  void CloseMenus();
114  void UpdateGraphics(bool fGraphicsChanged);
115  void UpdateFaces(bool bUpdateShape);
116  void ClearInfo(C4ObjectInfo *pInfo);
117 
118  typedef int SortProc(C4Object *, C4Object *);
119 
120  virtual bool Add(C4Object *nObj, SortType eSort, C4ObjectList *pLstSorted = nullptr);
121  bool AddSortCustom(C4Object *nObj, SortProc &pSortProc);
122  virtual bool Remove(C4Object *pObj);
123 
124  virtual bool AssignInfo();
125  virtual bool ValidateOwners();
126  StdStrBuf GetNameList(C4DefList &rDefs) const;
127  bool IsClear() const;
128  bool DenumeratePointers();
129  bool Write(char *szTarget);
130  void CompileFunc(StdCompiler *pComp, C4ValueNumbers * = 0);
131  void CompileFunc(StdCompiler *pComp, bool fSkipPlayerObjects, C4ValueNumbers *);
132  void Denumerate(C4ValueNumbers *);
133 
134  bool IsContained(const C4Object *pObj) const;
135  int ClearPointers(C4Object *pObj);
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 iOwner=ANY_OWNER, DWORD dwOCF=OCF_All);
144  C4Object* FindOther(C4ID id, int iOwner=ANY_OWNER);
145 
146  const C4ObjectLink* GetLink(const C4Object *pObj) const;
148  { return const_cast<C4ObjectLink*>(const_cast<const C4ObjectList*>(this)->GetLink(pObj)); }
149 
150  C4ID GetListID(int32_t dwCategory, int Index) const;
151 
152  bool ShiftContents(C4Object *pNewFirst); // cycle list so pNewFirst 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 *pList); // 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 *pLink, C4ObjectLink *pBefore);
163  virtual void InsertLink(C4ObjectLink *pLink, C4ObjectLink *pAfter);
164  virtual void RemoveLink(C4ObjectLink *pLnk);
165  mutable iterator * FirstIter;
166  iterator * AddIter(iterator * iter) const;
167  void RemoveIter(iterator * iter) const;
168 
169  friend class iterator;
170 };
171 
173 {
174 public:
179 protected:
180  virtual void InsertLinkBefore(C4ObjectLink *pLink, C4ObjectLink *pBefore);
181  virtual void InsertLink(C4ObjectLink *pLink, C4ObjectLink *pAfter);
182  virtual void RemoveLink(C4ObjectLink *pLnk);
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); // 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
virtual void OnObjectRename(C4ObjectList *pList, C4ObjectLink *pLnk)
Definition: C4ObjectList.h:37
virtual ~C4ObjectListChangeListener()
Definition: C4ObjectList.h:39
C4NotifyingObjectList(const C4ObjectList &List)
Definition: C4ObjectList.h:177
virtual void InsertLinkBefore(C4ObjectLink *pLink, C4ObjectLink *pBefore)
C4Object * GetNext(int32_t *piCount)
virtual bool Add(C4Object *nObj, SortType eSort, C4ObjectList *pLstSorted=nullptr)
void UpdateFaces(bool bUpdateShape)
bool DenumeratePointers()
C4Object * GetFirstObject() const
Definition: C4ObjectList.h:141
bool ShiftContents(C4Object *pNewFirst)
C4ObjectListChangeListener & ObjectListChangeListener
virtual void InsertLinkBefore(C4ObjectLink *pLink, C4ObjectLink *pBefore)
void ClearInfo(C4ObjectInfo *pInfo)
virtual void RemoveLink(C4ObjectLink *pLnk)
iterator * FirstIter
Definition: C4ObjectList.h:165
C4ObjectLink * First
Definition: C4ObjectList.h:51
bool operator==(const iterator &iter) const
const int ANY_OWNER
Definition: C4Constants.h:139
void Copy(const C4ObjectList &rList)
StdStrBuf GetNameList(C4DefList &rDefs) const
void RemoveIter(iterator *iter) const
void Draw(C4TargetFacet &cgo, int iPlayer, int MinPlane, int MaxPlane)
ReverseView(const C4ObjectList &list)
Definition: C4ObjectList.h:96
bool AddSortCustom(C4Object *nObj, SortProc &pSortProc)
void DeleteObjects()
virtual bool ValidateOwners()
iterator begin() const
virtual ~C4ObjectList()
const C4ObjectLink * GetLink(const C4Object *pObj) const
iterator(const iterator &iter)
iterator & operator=(const iterator &iter)
void DrawIfCategory(C4TargetFacet &cgo, int iPlayer, uint32_t dwCat, bool fInvert)
void CompileFunc(StdCompiler *pComp, C4ValueNumbers *=0)
C4ObjectLink * Next
Definition: C4ObjectList.h:29
void UpdateScriptPointers()
void UpdateGraphics(bool fGraphicsChanged)
static const C4ID None
Definition: C4Id.h:42
virtual void OnObjectAdded(C4ObjectList *pList, C4ObjectLink *pLnk)
Definition: C4ObjectList.h:36
void CheckCategorySort()
virtual void InsertLink(C4ObjectLink *pLink, C4ObjectLink *pAfter)
Definition: C4Def.h:100
int ListIDCount(int32_t dwCategory) const
virtual void InsertLink(C4ObjectLink *pLink, C4ObjectLink *pAfter)
C4Object * GetObject(int Index=0) const
C4NotifyingObjectList(const C4NotifyingObjectList &List)
Definition: C4ObjectList.h:176
Definition: C4Id.h:28
C4ObjectLink * Prev
Definition: C4ObjectList.h:29
iterator * AddIter(iterator *iter) const
virtual bool AssignInfo()
bool operator!=(const iterator &iter) const
const ReverseView reverse() const
Definition: C4ObjectList.h:104
C4Object * FindOther(C4ID id, int iOwner=ANY_OWNER)
C4ObjectListIterator(C4ObjectList &rList)
Definition: C4ObjectList.h:198
int ObjectCount(C4ID id=C4ID::None) const
const iterator end() const
bool find(C4Object *target)
C4ID GetListID(int32_t dwCategory, int Index) const
iterator begin() const
C4Object * Find(C4Def *def, int iOwner=ANY_OWNER, DWORD dwOCF=OCF_All)
int SortProc(C4Object *, C4Object *)
Definition: C4ObjectList.h:118
const uint32_t OCF_All
Definition: C4Constants.h:78
virtual void OnObjectContainerChanged(C4Object *obj, C4Object *old_container, C4Object *new_container)
Definition: C4ObjectList.h:38
void Denumerate(C4ValueNumbers *)
virtual ~C4NotifyingObjectList()
Definition: C4ObjectList.h:178
C4ObjectLink * Last
Definition: C4ObjectList.h:51
int ClearPointers(C4Object *pObj)
virtual void Default()
void DrawSelectMark(C4TargetFacet &cgo) const
bool IsContained(const C4Object *pObj) const
virtual void OnObjectRemove(C4ObjectList *pList, C4ObjectLink *pLnk)
Definition: C4ObjectList.h:35
C4ObjectLink * GetLink(const C4Object *pObj)
Definition: C4ObjectList.h:147
virtual void Clear()
bool IsClear() const
std::list< int32_t > * pEnumerated
Definition: C4ObjectList.h:53
C4Object * GetLastObject() const
Definition: C4ObjectList.h:142
uint32_t DWORD
bool CheckSort(C4ObjectList *pList)
C4Object * Obj
Definition: C4ObjectList.h:28
bool Write(char *szTarget)
virtual void RemoveLink(C4ObjectLink *pLnk)
virtual bool Remove(C4Object *pObj)