CFx SDK Documentation  2023 SP0
SlotManager.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2017, Open Design Alliance (the "Alliance").
3 // All rights reserved.
4 //
5 // This software and its documentation and related materials are owned by
6 // the Alliance. The software may only be incorporated into application
7 // programs owned by members of the Alliance, subject to a signed
8 // Membership Agreement and Supplemental Software License Agreement with the
9 // Alliance. The structure and organization of this software are the valuable
10 // trade secrets of the Alliance and its suppliers. The software is also
11 // protected by copyright law and international treaty provisions. Application
12 // programs incorporating this software must include the following statement
13 // with their copyright notices:
14 //
15 // This application incorporates Teigha(R) software pursuant to a license
16 // agreement with Open Design Alliance.
17 // Teigha(R) Copyright (C) 2002-2017 by Open Design Alliance.
18 // All rights reserved.
19 //
20 // By use of this software, its documentation or related materials, you
21 // acknowledge and accept the above terms.
23 
24 
25 // SlotManager.h: interface for the OdSlotManager class.
26 //
28 
29 #ifndef _SLOTMANAGER_H_INCLUDED_
30 #define _SLOTMANAGER_H_INCLUDED_
31 
32 #include "TD_PackPush.h"
33 
34 #include "IntArray.h"
35 
36 
37 typedef unsigned int OdSlotId;
38 #define kOdNullSlotId OdSlotId(-1)
39 
40 
47 {
48  OdIntArray m_freeSlots;
49  unsigned int m_numSlots;
50 public:
51  OdSlotManager() : m_numSlots(0) {}
58  inline OdSlotId newSlot()
59  {
60  int res;
61  if(m_freeSlots.isEmpty())
62  {
63  res = m_numSlots++;
64  }
65  else
66  {
67  res = m_freeSlots.last();
68  m_freeSlots.removeLast();
69  }
70  return res;
71  }
72 
77  inline void freeSlot(OdSlotId slotId)
78  {
79  if(slotId+1==m_numSlots)
80  {
81  --m_numSlots;
82  }
83  else
84  {
85  m_freeSlots.append(slotId);
86  }
87  }
88 
93  inline bool contains(OdSlotId slotId) const
94  {
95  return (slotId < m_numSlots && !m_freeSlots.contains(slotId, 0));
96  }
97 
101  inline unsigned int numSlots() const
102  {
103  return m_numSlots;
104  }
105 };
106 
117 template <class TVal, class TAlloc = OdObjectsAllocator<TVal> >
118 class OdSlots : public OdArray<TVal, TAlloc>
119 {
120  void ensureSpace(OdSlotId slotId)
121  {
122  if(slotId >= this->size())
123  {
124  this->resize(slotId+1);
125  }
126  }
127 public:
129 
130  OdSlots() {}
131 
136  : OdArray<TVal, TAlloc>(physicalLength, growLength) {}
137 
141  static const TVal* emptySlotValue() { static const TVal def = TVal(); return &def; }
142 
148  const TVal& operator[](OdSlotId slotId) const
149  {
150  return (slotId < this->size() ? this->getPtr()[slotId] : *emptySlotValue());
151  }
157  TVal& operator[](OdSlotId slotId)
158  {
159  ensureSpace(slotId);
160  return this->asArrayPtr()[slotId];
161  }
170  const TVal& getAt(OdSlotId slotId) const
171  {
172  return (slotId < this->size() ? this->getPtr()[slotId] : *emptySlotValue());
173  }
180  void setAt(OdSlotId slotId, const TVal& value)
181  {
182  ensureSpace(slotId);
183  this->asArrayPtr()[slotId] = value;
184  }
185 };
186 
187 
192 template <class TVal, class TAlloc = OdObjectsAllocator<TVal> >
194  : public OdSlots<TVal, TAlloc>
195  , public OdSlotManager
196 {
197 public:
199 
201 
206  : OdSlots<TVal, TAlloc>(physicalLength, growLength) {}
207 
208 #ifdef _DEBUG
212  const TVal& operator[](OdSlotId slotId) const
213  {
214  ODA_ASSERT(OdSlotManager::contains(slotId)); // invalid slotId
215  return OdSlots<TVal, TAlloc>::operator[](slotId);
216  }
220  TVal& operator[](OdSlotId slotId)
221  {
222  ODA_ASSERT(OdSlotManager::contains(slotId)); // invalid slotId
223  return OdSlots<TVal, TAlloc>::operator[](slotId);
224  }
225 
229  const TVal& getAt(OdSlotId slotId) const
230  {
231  ODA_ASSERT(OdSlotManager::contains(slotId)); // invalid slotId
232  return OdSlots<TVal, TAlloc>::getAt(slotId);
233  }
237  void setAt(OdSlotId slotId, const TVal& value)
238  {
239  ODA_ASSERT(OdSlotManager::contains(slotId)); // invalid slotId
241  }
242 #endif //_DEBUG
243 };
244 
245 #include "TD_PackPop.h"
246 
247 #endif // #ifndef _SLOTMANAGER_H_INCLUDED_
#define ODA_ASSERT(exp)
Definition: DebugStuff.h:49
unsigned int OdSlotId
Definition: SlotManager.h:37
A::size_type size_type
Definition: OdArray.h:593
T & last()
Definition: OdArray.h:1198
bool isEmpty() const
Definition: OdArray.h:1062
OdArray & removeLast()
Definition: OdArray.h:1242
bool contains(const T &value, size_type start=0) const
Definition: OdArray.h:1043
void resize(size_type logicalLength, const TVal &value)
Definition: OdArray.h:834
size_type append(const T &value)
Definition: OdArray.h:1215
OdManagedSlots(size_type physicalLength, int growLength=8)
Definition: SlotManager.h:205
OdSlots< TVal, TAlloc >::size_type size_type
Definition: SlotManager.h:198
void freeSlot(OdSlotId slotId)
Definition: SlotManager.h:77
unsigned int numSlots() const
Definition: SlotManager.h:101
bool contains(OdSlotId slotId) const
Definition: SlotManager.h:93
OdSlotId newSlot()
Definition: SlotManager.h:58
TVal & operator[](OdSlotId slotId)
Definition: SlotManager.h:157
const TVal & getAt(OdSlotId slotId) const
Definition: SlotManager.h:170
void setAt(OdSlotId slotId, const TVal &value)
Definition: SlotManager.h:180
OdSlots(size_type physicalLength, int growLength=8)
Definition: SlotManager.h:135
OdArray< TVal, TAlloc >::size_type size_type
Definition: SlotManager.h:128
const TVal & operator[](OdSlotId slotId) const
Definition: SlotManager.h:148
static const TVal * emptySlotValue()
Definition: SlotManager.h:141
GLsizei const GLfloat * value
Definition: gles2_ext.h:302