0
|
1 |
/*
|
|
2 |
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
#ifndef __CCOMPONENTMANAGER_H__
|
|
22 |
#define __CCOMPONENTMANAGER_H__
|
|
23 |
|
|
24 |
/****************************************************************************************
|
|
25 |
*
|
|
26 |
* System Includes
|
|
27 |
*
|
|
28 |
***************************************************************************************/
|
|
29 |
#include <stdio.h>
|
|
30 |
#include <assert.h>
|
|
31 |
|
|
32 |
/****************************************************************************************
|
|
33 |
*
|
|
34 |
* Local Includes
|
|
35 |
*
|
|
36 |
***************************************************************************************/
|
|
37 |
|
|
38 |
/****************************************************************************************
|
|
39 |
*
|
|
40 |
* Class CComponentManager
|
|
41 |
*
|
|
42 |
***************************************************************************************/
|
|
43 |
template <class T>
|
|
44 |
class CComponentManager
|
|
45 |
{
|
|
46 |
public:
|
|
47 |
CComponentManager( int aMaxComponents )
|
|
48 |
{
|
|
49 |
// check params
|
|
50 |
assert( aMaxComponents > 0 );
|
|
51 |
|
|
52 |
// set the state
|
|
53 |
iComponentCount = 0;
|
|
54 |
iMaxComponents = aMaxComponents;
|
|
55 |
iComponentList = (T**)calloc( iMaxComponents, sizeof(T*) );
|
|
56 |
assert( iComponentList != NULL );
|
|
57 |
}
|
|
58 |
|
|
59 |
~CComponentManager()
|
|
60 |
{
|
|
61 |
assert( iComponentCount == 0 );
|
|
62 |
free( iComponentList );
|
|
63 |
}
|
|
64 |
|
|
65 |
int CreateInstance()
|
|
66 |
{
|
|
67 |
int i;
|
|
68 |
|
|
69 |
// make sure there is room
|
|
70 |
if( iComponentCount == iMaxComponents ) {
|
|
71 |
return ERR_CANNOT_CREATE_NEW_INSTANCE;
|
|
72 |
}
|
|
73 |
|
|
74 |
// find a free index
|
|
75 |
for( i = 0; i < iMaxComponents; i++ ) {
|
|
76 |
if( iComponentList[i] == NULL )
|
|
77 |
break;
|
|
78 |
}
|
|
79 |
assert( i < iMaxComponents );
|
|
80 |
|
|
81 |
// create the new instance
|
|
82 |
iComponentList[i] = new T();
|
|
83 |
assert( iComponentList[i] != NULL );
|
|
84 |
|
|
85 |
// set the key
|
|
86 |
(iComponentList[i])->SetKey( i );
|
|
87 |
|
|
88 |
// update the count
|
|
89 |
iComponentCount++;
|
|
90 |
|
|
91 |
// done - return the key
|
|
92 |
return i;
|
|
93 |
}
|
|
94 |
|
|
95 |
int DeleteInstance( int aInstanceKey )
|
|
96 |
{
|
|
97 |
int is_key_valid;
|
|
98 |
|
|
99 |
// check that the key is valid
|
|
100 |
is_key_valid = IsValidKey( aInstanceKey );
|
|
101 |
assert( is_key_valid != 0 );
|
|
102 |
|
|
103 |
// verify the key
|
|
104 |
assert( (iComponentList[aInstanceKey])->GetKey() == aInstanceKey );
|
|
105 |
|
|
106 |
// delete the instace
|
|
107 |
delete (iComponentList[aInstanceKey]);
|
|
108 |
iComponentList[aInstanceKey] = NULL;
|
|
109 |
iComponentCount--;
|
|
110 |
|
|
111 |
// done
|
|
112 |
return ERR_NONE;
|
|
113 |
}
|
|
114 |
|
|
115 |
int IsValidKey( int aInstanceKey )
|
|
116 |
{
|
|
117 |
return ((aInstanceKey >= 0) && (aInstanceKey < iMaxComponents) && (iComponentList[aInstanceKey] != NULL) );
|
|
118 |
}
|
|
119 |
|
|
120 |
T *GetInstance( int aInstanceKey )
|
|
121 |
{
|
|
122 |
// validate the param
|
|
123 |
if( (aInstanceKey < 0) || (aInstanceKey >= iMaxComponents) ) {
|
|
124 |
return NULL;
|
|
125 |
}
|
|
126 |
return iComponentList[aInstanceKey];
|
|
127 |
}
|
|
128 |
|
|
129 |
int GetInstanceCount()
|
|
130 |
{
|
|
131 |
return iComponentCount;
|
|
132 |
}
|
|
133 |
|
|
134 |
int DeleteAllInstances()
|
|
135 |
{
|
|
136 |
int i, err;
|
|
137 |
for( i = 0; i < iMaxComponents; i++ ) {
|
|
138 |
if( iComponentList[i] != NULL ) {
|
|
139 |
err = DeleteInstance( i );
|
|
140 |
assert( err == ERR_NONE );
|
|
141 |
}
|
|
142 |
}
|
|
143 |
assert( iComponentCount == 0 );
|
|
144 |
return ERR_NONE;
|
|
145 |
}
|
|
146 |
|
|
147 |
|
|
148 |
private:
|
|
149 |
int iComponentCount;
|
|
150 |
int iMaxComponents;
|
|
151 |
T **iComponentList;
|
|
152 |
};
|
|
153 |
|
|
154 |
#endif //__CCOMPONENTMANAGER_H__
|