Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_list.h
1 // ================================================================================
2 // == This file is a part of Turbo Badger. (C) 2011-2014, Emil SegerÃ¥s ==
3 // == See tb_core.h for more information. ==
4 // ================================================================================
5 
6 #ifndef TB_LIST_H
7 #define TB_LIST_H
8 
9 #include "tb_core.h"
10 namespace tb {
11 
16 {
17 public:
18  TBListBackend() : m_data(nullptr) {}
19  ~TBListBackend() { RemoveAll(); }
20  bool Reserve(int num);
21  bool GrowIfNeeded();
22  bool Add(void *data);
23  bool Add(void *data, int index);
24  void Set(void *data, int index);
25  void *Get(int index) const;
26  void *operator [] (int index) const { return Get(index); }
27  void *RemoveFast(int index);
28  void *Remove(int index);
29  void RemoveAll();
30  void Swap(int index1, int index2);
31  int Find(void *data) const;
32  int GetNumItems() const { return m_data ? m_data->num : 0; }
33  int GetCapacity() const { return m_data ? m_data->capacity : 0; }
34 private:
35  struct TBLIST_DATA {
36  int num;
37  int capacity;
38  void **list;
39  };
40  TBLIST_DATA *m_data;
41 };
42 
46 template<class T>
47 class TBListOf
48 {
49 public:
51  bool Reserve(int num) { return m_list.Reserve(num); }
52 
56  bool GrowIfNeeded() { return m_list.GrowIfNeeded(); }
57 
59  bool Add(T *data) { return m_list.Add(data); }
60 
62  bool Add(T *data, int index) { return m_list.Add(data, index); }
63 
65  void Set(T *data, int index) { m_list.Set(data, index); }
66 
68  T *Get(int index) const { return (T *) m_list.Get(index); }
69 
71  T *operator [] (int index) const { return (T *) m_list.Get(index); }
72 
76  T *RemoveFast(int index) { return (T *) m_list.RemoveFast(index); }
77 
79  T *Remove(int index) { return (T *) m_list.Remove(index); }
80 
84  void DeleteFast(int index) { delete (T *) m_list.RemoveFast(index); }
85 
87  void Delete(int index) { delete (T *) m_list.Remove(index); }
88 
90  void RemoveAll() { m_list.RemoveAll(); }
91 
93  void DeleteAll()
94  {
95  for (int i = 0; i < GetNumItems(); i++)
96  delete (T *) Get(i);
97  m_list.RemoveAll();
98  }
99 
101  void Swap(int index1, int index2) { m_list.Swap(index1, index2); }
102 
104  int Find(T *data) const { return m_list.Find(data); }
105 
107  int GetNumItems() const { return m_list.GetNumItems(); }
108 
110  int GetCapacity() const { return m_list.GetCapacity(); }
111 private:
112  TBListBackend m_list;
113 };
114 
117 template<class T>
118 class TBListAutoDeleteOf : public TBListOf<T>
119 {
120 public:
122 };
123 
124 } // namespace tb
125 
126 #endif // TB_LIST_H
int GetCapacity() const
Get the capacity of the list number of items it can hold without allocating more memory) ...
Definition: tb_list.h:110
TBList is a list (array) of pointers to any kind of objects.
Definition: tb_list.h:15
bool Add(T *data)
Add data at the end of the list.
Definition: tb_list.h:59
void RemoveAll()
Remove all items without deleding them.
Definition: tb_list.h:90
T * RemoveFast(int index)
Remove the item at position index from the list and returns the pointer.
Definition: tb_list.h:76
int GetNumItems() const
Get the number of items in the list.
Definition: tb_list.h:107
bool Reserve(int num)
Make sure there is space for at least num items in the list.
Definition: tb_list.h:51
bool GrowIfNeeded()
Make sure there is space for at least one more item in the list.
Definition: tb_list.h:56
void Delete(int index)
Deletes the item at position index after removing it from the list.
Definition: tb_list.h:87
T * Get(int index) const
Returns the content at position index.
Definition: tb_list.h:68
T * Remove(int index)
Remove the item at position index from the list and returns the pointer.
Definition: tb_list.h:79
TBListAutoDeleteOf is a list (array) of pointers to the specified object type.
Definition: tb_list.h:118
bool Add(T *data, int index)
Add data at the given index in the list.
Definition: tb_list.h:62
int Find(T *data) const
Search for the item with the given data and return the found index, or -1 if not found.
Definition: tb_list.h:104
void Swap(int index1, int index2)
Swap the items at index1 and index2.
Definition: tb_list.h:101
void Set(T *data, int index)
Replace the item at the index with the new data.
Definition: tb_list.h:65
void DeleteAll()
Remove and delete all items from the list.
Definition: tb_list.h:93
TBListOf is a list (array) of pointers to the specified object type.
Definition: tb_list.h:47
void DeleteFast(int index)
Deletes the item at position index after removing it from the list.
Definition: tb_list.h:84
T * operator[](int index) const
Returns the content at position index.
Definition: tb_list.h:71