summaryrefslogtreecommitdiff
path: root/source/utilities/ObjectList.h
blob: 61511045525ff05fd03430e9865effff9af24452 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*******************************************************************************
*
*    File name: ObjectList.h
*     Language: Visual C++
*  Description: Template class for manage list of object
*
*
* Copyright (C) ST-Ericsson SA 2011
* License terms: 3-clause BSD license
*
*******************************************************************************/

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                     File ObjectList.h

#ifndef __OBJECT_LIST_H__
#define __OBJECT_LIST_H__

#pragma once
#if defined(_WIN32)
#include "WinApiWrappers.h"
#elif defined(__linux__)
#include "LinuxApiWrappers.h"
#include <stdio.h>
#include <stdlib.h>
#else
#error "Unknown target"
#endif

#define ID_STRING_LEN   25

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                            Template class CObjectList

template < class TemplateClass > class CObjectList
{
public:
    CObjectList();
    virtual ~CObjectList();

    int Add(TemplateClass *pObject, const char *szObjectId, int nSubPart = -1);
    TemplateClass *Find(const char *szObjectId, int nSubPart = -1);
    int Release(TemplateClass *pObject);
    int GetNumberOfInstances(TemplateClass *pObject);

protected:
    struct TObjectList {
        TemplateClass *pObject;
        char szObjectId[ 25 + 1 ];
        int nSubPart;
        int nCounter;
        TObjectList *pNextObject;
    } *m_pObjectList;
    CCriticalSectionObject m_CriticalSectionObject;
};


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                         CObjectList()
// Constructor
//
template < class TemplateClass >
CObjectList< TemplateClass >::CObjectList()
{
    m_pObjectList = NULL;
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                        ~CObjectList()
// Destructor
//
template < class TemplateClass >
CObjectList< TemplateClass >::~CObjectList()
{
    CLockCS Lock(m_CriticalSectionObject);
    TObjectList *pCurrentObject;

    // Delete all object that is left.
    while (m_pObjectList != NULL) {
        pCurrentObject = m_pObjectList;
        m_pObjectList = pCurrentObject->pNextObject;
        free(pCurrentObject);
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                 Add()
// FUNCTION
//    Add() -- Add object ID to the object list
//
// INPUT
//    szId    - ID of the object
//    pObject - Object to be assigned to the ID
//
// RESULT
//     0: Success
//     1: ID string is to long
//    -1: Unable to expand the list.
//    -2: SubParts doesn't match each other.
//
// NOTES
//    If no record with the same ID string exist, a new record will be added and the object pointer will be set.
//    If a record with the same ID string already exists in the array, the instance counter will be incremented.
//

template < class TemplateClass >
int CObjectList< TemplateClass >::Add(TemplateClass *pObject, const char *szObjectId, int nSubPart)
{
    CLockCS Lock(m_CriticalSectionObject);
    TObjectList *pCurrentObject;

    if (m_pObjectList == NULL) {
        m_pObjectList = (TObjectList *)malloc(sizeof(TObjectList));

        if (m_pObjectList == NULL) {
            return -1;
        }

        m_pObjectList->pObject = pObject;
        strcpy_s(m_pObjectList->szObjectId, szObjectId);
        m_pObjectList->nSubPart = nSubPart;
        m_pObjectList->nCounter = 1;
        m_pObjectList->pNextObject = NULL;
    } else { // Is the object allready created ?
        pCurrentObject = m_pObjectList;

        while (((strcmp(pCurrentObject->szObjectId, szObjectId) != 0) || (pCurrentObject->nSubPart != nSubPart)) && (pCurrentObject->pNextObject != NULL)) {
            pCurrentObject = pCurrentObject->pNextObject;
        }

        if ((_stricmp(pCurrentObject->szObjectId, szObjectId) == 0) && (pCurrentObject->nSubPart = nSubPart)) {
            pCurrentObject->nCounter++;    // Found the object!
        } else {
            // Add a new object to the end of the list.
            pCurrentObject->pNextObject = (TObjectList *)malloc(sizeof(TObjectList));

            if (pCurrentObject->pNextObject == NULL) {
                return -1;
            }

            pCurrentObject->pNextObject->pObject = pObject;
            strcpy_s(pCurrentObject->pNextObject->szObjectId, szObjectId);
            pCurrentObject->pNextObject->nSubPart = nSubPart;
            pCurrentObject->pNextObject->nCounter = 1;
            pCurrentObject->pNextObject->pNextObject = NULL;
        }
    }

    return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                             Release()
// FUNCTION
//    Release() -- Release reference to object from the object list
//
// INPUT
//    pObject - Object to release
//
// RESULT
//    The number of instances left for this object.
//    If negative the object doesn't exist in the list.
//
// NOTES
//    The instance counter for the entry is decremented.
//    If the instance counter reaches 0, the entry will be deleted from the list.
//

template < class TemplateClass >
int CObjectList< TemplateClass >::Release(TemplateClass *pObject)
{
    CLockCS Lock(m_CriticalSectionObject);
    TObjectList *pCurrentObject, *pLastObject;

    // Find the right object to delete.
    if (m_pObjectList != NULL) {
        pCurrentObject = m_pObjectList;
        pLastObject = NULL;

        while ((pCurrentObject->pObject != pObject) && (pCurrentObject->pNextObject != NULL)) {
            pLastObject = pCurrentObject;
            pCurrentObject = pCurrentObject->pNextObject;
        }

        if (pCurrentObject->pObject == pObject) {
            if (pCurrentObject->nCounter > 1) {
                pCurrentObject->nCounter--;
                return pCurrentObject->nCounter;
            } else {
                // Remove the current record in the global object table.
                if (pLastObject == NULL) {
                    m_pObjectList = pCurrentObject->pNextObject;
                } else {
                    pLastObject->pNextObject = pCurrentObject->pNextObject;
                }

                free(pCurrentObject);
                return 0;
            }
        } else {
            return -1;
        }
    } else {
        return -1;
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                Find()
// FUNCTION
//    Find() -- Search for an object ID in the object list
//
// INPUT
//    szObjectId - ID of object
//
// RESULT
//    The object pointer to the object assigned to the ID or
//    NULL if the ID was not found
//

template < class TemplateClass >
TemplateClass *CObjectList< TemplateClass >::Find(const char *szObjectId, int nSubPart)
{
    CLockCS Lock(m_CriticalSectionObject);
    TObjectList *pCurrentObject;

    // Find the right object
    if (m_pObjectList != NULL) {
        pCurrentObject = m_pObjectList;

        while (((strcmp(pCurrentObject->szObjectId, szObjectId) != 0) || (pCurrentObject->nSubPart != nSubPart)) && (pCurrentObject->pNextObject != NULL)) {
            pCurrentObject = pCurrentObject->pNextObject;
        }

        if ((_stricmp(pCurrentObject->szObjectId, szObjectId) == 0) && (pCurrentObject->nSubPart = nSubPart)) {
            return pCurrentObject->pObject;
        } else {
            return NULL;
        }
    } else {
        return NULL;
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                GetNumberOfInstances()
// FUNCTION
//    GetNumberOfInstances() -- Search for an object ID in the object list
//
// INPUT
//    szObjectId - ID of object
//
// RESULT
//    The number of instances of the object assigned to the ID.
//    0 if the ID was not found.
//

template < class TemplateClass >
int CObjectList< TemplateClass >::GetNumberOfInstances(TemplateClass *pObject)
{
    CLockCS Lock(m_CriticalSectionObject);
    TObjectList *pCurrentObject;

    // Find the right object
    if (m_pObjectList != NULL) {
        pCurrentObject = m_pObjectList;

        while ((pCurrentObject->pObject != pObject) && (pCurrentObject->pNextObject != NULL)) {
            pCurrentObject = pCurrentObject->pNextObject;
        }

        if (pCurrentObject->pObject == pObject) {
            return pCurrentObject->nCounter;
        } else {
            return 0;
        }
    } else {
        return 0;
    }
}

#endif

//                                                                                              End of file ObjectList.h
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////