|
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 #ifndef __CCOMPONENTMANAGER_H__ |
|
21 #define __CCOMPONENTMANAGER_H__ |
|
22 |
|
23 /**************************************************************************************** |
|
24 * |
|
25 * System Includes |
|
26 * |
|
27 ***************************************************************************************/ |
|
28 #include <stdio.h> |
|
29 #include <assert.h> |
|
30 |
|
31 /**************************************************************************************** |
|
32 * |
|
33 * Local Includes |
|
34 * |
|
35 ***************************************************************************************/ |
|
36 |
|
37 /**************************************************************************************** |
|
38 * |
|
39 * Class CComponentManager |
|
40 * |
|
41 ***************************************************************************************/ |
|
42 template <class T> |
|
43 class CComponentManager |
|
44 { |
|
45 public: |
|
46 CComponentManager( int aMaxComponents ) |
|
47 { |
|
48 // check params |
|
49 assert( aMaxComponents > 0 ); |
|
50 |
|
51 // set the state |
|
52 iComponentCount = 0; |
|
53 iMaxComponents = aMaxComponents; |
|
54 iComponentList = (T**)calloc( iMaxComponents, sizeof(T*) ); |
|
55 assert( iComponentList != NULL ); |
|
56 } |
|
57 |
|
58 ~CComponentManager() |
|
59 { |
|
60 assert( iComponentCount == 0 ); |
|
61 free( iComponentList ); |
|
62 } |
|
63 |
|
64 int CreateInstance() |
|
65 { |
|
66 int i; |
|
67 |
|
68 // make sure there is room |
|
69 if( iComponentCount == iMaxComponents ) { |
|
70 return ERR_CANNOT_CREATE_NEW_INSTANCE; |
|
71 } |
|
72 |
|
73 // find a free index |
|
74 for( i = 0; i < iMaxComponents; i++ ) { |
|
75 if( iComponentList[i] == NULL ) |
|
76 break; |
|
77 } |
|
78 assert( i < iMaxComponents ); |
|
79 |
|
80 // create the new instance |
|
81 iComponentList[i] = new T(); |
|
82 assert( iComponentList[i] != NULL ); |
|
83 |
|
84 // set the key |
|
85 (iComponentList[i])->SetKey( i ); |
|
86 |
|
87 // update the count |
|
88 iComponentCount++; |
|
89 |
|
90 // done - return the key |
|
91 return i; |
|
92 } |
|
93 |
|
94 int DeleteInstance( int aInstanceKey ) |
|
95 { |
|
96 int is_key_valid; |
|
97 |
|
98 // check that the key is valid |
|
99 is_key_valid = IsValidKey( aInstanceKey ); |
|
100 assert( is_key_valid != 0 ); |
|
101 |
|
102 // verify the key |
|
103 assert( (iComponentList[aInstanceKey])->GetKey() == aInstanceKey ); |
|
104 |
|
105 // delete the instace |
|
106 delete (iComponentList[aInstanceKey]); |
|
107 iComponentList[aInstanceKey] = NULL; |
|
108 iComponentCount--; |
|
109 |
|
110 // done |
|
111 return ERR_NONE; |
|
112 } |
|
113 |
|
114 int IsValidKey( int aInstanceKey ) |
|
115 { |
|
116 return ((aInstanceKey >= 0) && (aInstanceKey < iMaxComponents) && (iComponentList[aInstanceKey] != NULL) ); |
|
117 } |
|
118 |
|
119 T *GetInstance( int aInstanceKey ) |
|
120 { |
|
121 // validate the param |
|
122 if( (aInstanceKey < 0) || (aInstanceKey >= iMaxComponents) ) { |
|
123 return NULL; |
|
124 } |
|
125 return iComponentList[aInstanceKey]; |
|
126 } |
|
127 |
|
128 int GetInstanceCount() |
|
129 { |
|
130 return iComponentCount; |
|
131 } |
|
132 |
|
133 int DeleteAllInstances() |
|
134 { |
|
135 int i, err; |
|
136 for( i = 0; i < iMaxComponents; i++ ) { |
|
137 if( iComponentList[i] != NULL ) { |
|
138 err = DeleteInstance( i ); |
|
139 assert( err == ERR_NONE ); |
|
140 } |
|
141 } |
|
142 assert( iComponentCount == 0 ); |
|
143 return ERR_NONE; |
|
144 } |
|
145 |
|
146 |
|
147 private: |
|
148 int iComponentCount; |
|
149 int iMaxComponents; |
|
150 T **iComponentList; |
|
151 }; |
|
152 |
|
153 #endif //__CCOMPONENTMANAGER_H__ |