CFx SDK Documentation  2020SP3
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
209 
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_
OdArray::removeLast
OdArray & removeLast()
Definition: OdArray.h:1242
OdSlots::setAt
void setAt(OdSlotId slotId, const TVal &value)
Definition: SlotManager.h:180
OdSlots::emptySlotValue
static const TVal * emptySlotValue()
Definition: SlotManager.h:141
OdManagedSlots
Definition: SlotManager.h:196
OdSlots::operator[]
TVal & operator[](OdSlotId slotId)
Definition: SlotManager.h:157
OdManagedSlots::OdManagedSlots
OdManagedSlots(size_type physicalLength, int growLength=8)
Definition: SlotManager.h:205
OdSlotManager::contains
bool contains(OdSlotId slotId) const
Definition: SlotManager.h:93
OdArray< int, OdMemoryAllocator< int > >
TD_PackPop.h
IntArray.h
OdArray::contains
bool contains(const T &value, size_type start=0) const
Definition: OdArray.h:1043
OdArray< TVal, OdObjectsAllocator< TVal > >::getPtr
const TVal * getPtr() const
Definition: OdArray.h:1102
OdArray::last
T & last()
Definition: OdArray.h:1198
OdArray< TVal, OdObjectsAllocator< TVal > >::asArrayPtr
const TVal * asArrayPtr() const
Definition: OdArray.h:1094
OdSlots::getAt
const TVal & getAt(OdSlotId slotId) const
Definition: SlotManager.h:170
OdArray< TVal, OdObjectsAllocator< TVal > >::size
size_type size() const
Definition: OdArray.h:893
OdSlots::OdSlots
OdSlots(size_type physicalLength, int growLength=8)
Definition: SlotManager.h:135
OdArray< TVal, OdObjectsAllocator< TVal > >::physicalLength
size_type physicalLength() const
Definition: OdArray.h:1078
OdArray::append
size_type append(const T &value)
Definition: OdArray.h:1215
OdArray< TVal, OdObjectsAllocator< TVal > >::resize
void resize(size_type logicalLength, const TVal &value)
Definition: OdArray.h:834
OdArray< TVal, OdObjectsAllocator< TVal > >::growLength
int growLength() const
Definition: OdArray.h:1086
TD_PackPush.h
OdArray::size_type
A::size_type size_type
Definition: OdArray.h:593
OdSlotId
unsigned int OdSlotId
Definition: SlotManager.h:37
OdSlotManager::numSlots
unsigned int numSlots() const
Definition: SlotManager.h:101
OdSlotManager::freeSlot
void freeSlot(OdSlotId slotId)
Definition: SlotManager.h:77
ODA_ASSERT
#define ODA_ASSERT(exp)
Definition: DebugStuff.h:49
OdSlotManager::OdSlotManager
OdSlotManager()
Definition: SlotManager.h:51
OdSlotManager
Definition: SlotManager.h:47
value
GLsizei const GLfloat * value
Definition: gles2_ext.h:302
OdSlots
Definition: SlotManager.h:119
OdSlotManager::newSlot
OdSlotId newSlot()
Definition: SlotManager.h:58
OdSlots::OdSlots
OdSlots()
Definition: SlotManager.h:130
OdSlots::operator[]
const TVal & operator[](OdSlotId slotId) const
Definition: SlotManager.h:148
OdManagedSlots::size_type
OdSlots< TVal, TAlloc >::size_type size_type
Definition: SlotManager.h:198
OdManagedSlots::OdManagedSlots
OdManagedSlots()
Definition: SlotManager.h:200
OdSlots::size_type
OdArray< TVal, TAlloc >::size_type size_type
Definition: SlotManager.h:128
OdArray::isEmpty
bool isEmpty() const
Definition: OdArray.h:1062