CFx SDK Documentation 2024 SP0
Loading...
Searching...
No Matches
SlotManager.h
Go to the documentation of this file.
1
2// Copyright (C) 2002-2022, 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 Open Design Alliance software pursuant to a license
16// agreement with Open Design Alliance.
17// Open Design Alliance Copyright (C) 2002-2022 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
37typedef unsigned int OdSlotId;
38#define kOdNullSlotId OdSlotId(-1)
39
40
47{
48 OdIntArray m_freeSlots;
49 unsigned int m_numSlots;
50public:
51 OdSlotManager() : m_numSlots(0) {}
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
117template <class TVal, class TAlloc = OdObjectsAllocator<TVal> >
118class 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 }
127public:
129
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
192template <class TVal, class TAlloc = OdObjectsAllocator<TVal> >
194 : public OdSlots<TVal, TAlloc>
195 , public OdSlotManager
196{
197public:
199
204
209 : OdSlots<TVal, TAlloc>(physicalLength, growLength) {}
210
211#ifdef _DEBUG
215 const TVal& operator[](OdSlotId slotId) const
216 {
217 ODA_ASSERT(OdSlotManager::contains(slotId)); // invalid slotId
219 }
223 TVal& operator[](OdSlotId slotId)
224 {
225 ODA_ASSERT(OdSlotManager::contains(slotId)); // invalid slotId
227 }
228
232 const TVal& getAt(OdSlotId slotId) const
233 {
234 ODA_ASSERT(OdSlotManager::contains(slotId)); // invalid slotId
235 return OdSlots<TVal, TAlloc>::getAt(slotId);
236 }
240 void setAt(OdSlotId slotId, const TVal& value)
241 {
242 ODA_ASSERT(OdSlotManager::contains(slotId)); // invalid slotId
244 }
245#endif //_DEBUG
246};
247
248#include "TD_PackPop.h"
249
250#endif // #ifndef _SLOTMANAGER_H_INCLUDED_
#define ODA_ASSERT(exp)
Definition: DebugStuff.h:57
unsigned int OdSlotId
Definition: SlotManager.h:37
const T * asArrayPtr() const
Definition: OdArray.h:1590
size_type physicalLength() const
Definition: OdArray.h:1569
bool contains(const ConstForPtrT &value, size_type start=0) const
Definition: OdArray.h:1524
OdArray & removeLast()
Definition: OdArray.h:1894
bool isEmpty() const
Definition: OdArray.h:1547
size_type size() const
Definition: OdArray.h:1247
typename A::size_type size_type
Definition: OdArray.h:831
const T * getPtr() const
Definition: OdArray.h:1600
T & last()
Definition: OdArray.h:1710
void resize(size_type logicalLength, const T &value)
Definition: OdArray.h:1185
size_type append(const T &value)
Definition: OdArray.h:1725
int growLength() const
Definition: OdArray.h:1580
OdManagedSlots(size_type physicalLength, int growLength=8)
Definition: SlotManager.h:208
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
static const TVal * emptySlotValue()
Definition: SlotManager.h:141
void setAt(OdSlotId slotId, const TVal &value)
Definition: SlotManager.h:180
OdSlots(size_type physicalLength, int growLength=8)
Definition: SlotManager.h:135
const TVal & getAt(OdSlotId slotId) const
Definition: SlotManager.h:170
OdArray< TVal, TAlloc >::size_type size_type
Definition: SlotManager.h:128
const TVal & operator[](OdSlotId slotId) const
Definition: SlotManager.h:148
GLsizei const GLfloat * value
Definition: gles2_ext.h:302