OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
contain.h
Go to the documentation of this file.
1//------------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1993, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Definition of container classes used and made available by OWL
7//------------------------------------------------------------------------------
8
9#if !defined(OWL_CONTAIN_H)
10#define OWL_CONTAIN_H
11
12#include <owl/private/defs.h>
13#if defined(BI_HAS_PRAGMA_ONCE)
14# pragma once
15#endif
16
17#include <owl/defs.h>
18#include <owl/template.h>
19
20//class TStandardAllocator;
21
22namespace owl {
23
24//------------------------------------------------------------------------------
25//class _OWLCLASS TStandardAllocator;
26//template <class T, class T1> class TPtrArrayIterator;
27//template <class T, class P, class A> class TTypedArray;
28//template <class T, class T1> class TObjArrayIterator;
29//template <class T> class TObjectArray;
30//template <class T> class TSortedObjectArray;
31//template <class T, class T1> class TMapNode;
32
41typedef int TInt; // for portability
42
45
46#if defined(_OWLDLL) || defined(BI_APP_DLL)
47//
48// Export templates when building ObjectWindows DLL and provide import
49// declaration of DLL instance for users of the class.
50//
51//template class _OWLCLASS TMapNode<string,string>;
52//template class _OWLCLASS TObjectArray< TMapNode<string,string> >;
53//template class _OWLCLASS TSortedObjectArray< TMapNode<string,string> >;
54#endif // _OWLDLL || BI_APP_DLL
55
56//------------------------------------------------------------------------------
57
58//
59/// Maximum number of entries in each Vector
60//
61const uint MAXENTRIES = uint((uint32(UINT_MAX) - sizeof(uint))/
62 sizeof(void*));
63
64#include <owl/preclass.h>
65//
66/// \class TCollection
67// ~~~~~ ~~~~~~~~~~~
68/// Simple template holding pointers to Ts used internally by
69/// ObjectWindows Controls
70//
71template <class T> class TCollection {
72 public:
73 TCollection(uint aLimit, bool shldDel = true);
75
76 T& operator [](uint index) const;
77 int GetCount() const;
78
79 int Append(T* item);
80 void InsertAt(uint index, T* item);
81 void RemoveAt(uint index);
82 void RemoveAll();
83 void FreeAt(uint index);
84 void FreeAll();
85
86 protected:
87 void SetLimit(uint aLimit);
88
89 private:
92
93 void FreeItem(T* item);
94 T** items; ///< Pointer to array of T*
95 uint count; ///< Number of items
96 uint limit; ///< Size array is allocated for
97 bool shouldDelete; ///< Should free in destructor
98};
99
100#include <owl/posclass.h>
101
102
103//------------------------------------------------------------------------------
104// Inline implementations
105//
106
107//
108template<class T>
110: items(0), count(0), limit(0), shouldDelete(shldDel)
111{
113}
114
115//
116template<class T>
118{
119 if (shouldDelete)
120 FreeAll();
121 SetLimit(0);
122}
123
124//
125template<class T>
127{
128 return count;
129}
130
131//
132template<class T>
134{
135 count = 0;
136}
137
138//
139template<class T>
141{
142 PRECONDITION(index < count);
143 return *items[index];
144}
145
146//
147template<class T>
149{
150 InsertAt(count, item);
151 return count-1;
152}
153
154//
155template<class T>
156void TCollection<T>::InsertAt(uint index, T* item)
157{
158 PRECONDITION(index <= count);
159 if (count == limit)
160 SetLimit(count*2);
161 if (count-index != 0)
162 memmove(reinterpret_cast<void*>(&items[index+1]), reinterpret_cast<void*>(&items[index]),
163 (count-index)*sizeof(T*));
164 items[index] = item;
165 count++;
166}
167
168//
169template<class T>
171{
172 PRECONDITION(index < count);
173 if (index != count-1)
174 memmove(reinterpret_cast<void*>(&items[index]), reinterpret_cast<void*>(&items[index+1]),
175 (count-(index+1))*sizeof(T*));
176 count--;
177}
178
179//
180template<class T>
182{
183 T& item = operator[](index);
184 RemoveAt(index);
185 FreeItem(&item);
186}
187
188//
189template<class T>
191{
192 for (uint i = 0; i < count; i++)
193 FreeItem(&(operator[](i)));
194 count = 0;
195}
196
197//
198template<class T>
199void TCollection<T>::FreeItem(T* item)
200{
201 delete item;
202}
203
204//
205template<class T>
207{
208 if (aLimit < count)
209 aLimit = count;
211
212 if (aLimit != limit) {
213 T **aItems;
214
215 if (aLimit == 0)
216 aItems = 0;
217 else {
218 aItems = new T *[aLimit];
219 if (count != 0)
220 memcpy(aItems, items, count*sizeof(T*));
221 }
222 delete [] items;
223 items = aItems;
224 limit = aLimit;
225 }
226}
227
228
229} // OWL namespace
230
231
232#endif // OWL_CONTAIN_H
#define CHECK(condition)
Definition checks.h:239
#define PRECONDITION(condition)
Definition checks.h:227
Simple template holding pointers to Ts used internally by ObjectWindows Controls.
Definition contain.h:71
void RemoveAt(uint index)
Definition contain.h:170
int Append(T *item)
Definition contain.h:148
void FreeAt(uint index)
Definition contain.h:181
TCollection(uint aLimit, bool shldDel=true)
Definition contain.h:109
void InsertAt(uint index, T *item)
Definition contain.h:156
void SetLimit(uint aLimit)
Definition contain.h:206
T & operator[](uint index) const
Definition contain.h:140
void RemoveAll()
Definition contain.h:133
int GetCount() const
Definition contain.h:126
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
TTypedArray< int, int, TStandardAllocator > TIntArray
Definition contain.h:33
TObjectArray< tstring > TStringArray
Definition contain.h:37
TPtrArrayIterator< LPARAM, TLParamArray > TLParamArrayIterator
Definition contain.h:44
TSortedObjectArray< tstring > TSortedStringArray
Definition contain.h:39
int TInt
Definition contain.h:41
TPtrArrayIterator< uint32, TUint32Array > TUint32ArrayIterator
Definition contain.h:36
unsigned long uint32
Definition number.h:34
TTypedArray< uint32, uint32, TStandardAllocator > TUint32Array
Definition contain.h:35
TObjArrayIterator< tstring, TSortedStringArray > TSortedStringArrayIterator
Definition contain.h:40
TObjArrayIterator< tstring, TStringArray > TStringArrayIterator
Definition contain.h:38
TTypedArray< LPARAM, LPARAM, TStandardAllocator > TLParamArray
Definition contain.h:43
TPtrArrayIterator< int, TIntArray > TIntArrayIterator
Definition contain.h:34
unsigned int uint
Definition number.h:25
const uint MAXENTRIES
Maximum number of entries in each Vector.
Definition contain.h:61
General definitions used by all ObjectWindows programs.
Definition of container classes used and made available by OWL.