ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Loading...
Searching...
No Matches
map.cpp
Go to the documentation of this file.
1// Copyright (C) 2004-2005 Open Source Telecom Corporation.
2// Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
3// Copyright (C) 2015 Cherokees of Idaho.
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18//
19// As a special exception, you may use this file as part of a free software
20// library without restriction. Specifically, if other files instantiate
21// templates or use macros or inline functions from this file, or you compile
22// this file and link it with other files to produce an executable, this
23// file does not by itself cause the resulting executable to be covered by
24// the GNU General Public License. This exception does not however
25// invalidate any other reasons why the executable file might be covered by
26// the GNU General Public License.
27//
28// This exception applies only to the code released under the name GNU
29// Common C++. If you copy code from other releases into a copy of GNU
30// Common C++, as the General Public License permits, the exception does
31// not apply to the code that you add in this way. To avoid misleading
32// anyone as to the status of such modified files, you must delete
33// this exception notice from them.
34//
35// If you write modifications of your own for GNU Common C++, it is your choice
36// whether to permit this exception to apply to your modifications.
37// If you do not wish that, delete this exception notice.
38//
39
40#include <ucommon-config.h>
41#include <commoncpp/config.h>
42#include <commoncpp/export.h>
43#include <commoncpp/thread.h>
44#include <commoncpp/object.h>
45
46namespace ost {
47
49{
50 thisObject = theObject;
51 return *this;
52}
53
55{
56 if ( thisObject == NULL )
57 return *this;
58
59 if (thisObject->nextObject != NULL) {
61 }
62 else if (thisObject->table != NULL) {
63 MapObject* obj = NULL;
64 unsigned i = thisObject->table->getIndex(thisObject->idObject) + 1;
65
66
68 for ( ; obj == NULL && i < thisObject->table->range; i++)
69 obj = thisObject->table->map[i];
71
72 thisObject = obj;
73 }
74
75 return *this;
76}
77
78MapTable::MapTable(unsigned size) :
79Mutex()
80{
81 map = new MapObject *[size + 1];
82 memset(map, 0, sizeof(MapObject *) * (size + 1));
83 range = size;
84 count = 0;
85}
86
88{
89 cleanup();
90}
91
93{
94 enterMutex();
95 if(map)
96 delete[] map;
97 map = NULL;
98 leaveMutex();
99}
100
101unsigned MapTable::getIndex(const char *id)
102{
103 unsigned key = 0;
104
105 while(*id)
106 key = (key << 1) ^ (*(id++) & 0x1f);
107
108 return key % range;
109}
110
111void *MapTable::getObject(const char *id)
112{
113 if(!map)
114 return NULL;
115
116 enterMutex();
117 MapObject *obj = map[getIndex(id)];
118
119 while(obj) {
120 if(!stricmp(obj->idObject, id))
121 break;
122 obj = obj->nextObject;
123 }
124 leaveMutex();
125 return (void *)obj;
126}
127
129{
130 MapObject *obj;
131
132 if(!map)
133 return NULL;
134
135 enterMutex();
136 obj = *map;
137 for (unsigned i = 0; obj == NULL && i < range; i++)
138 obj = map[i];
139 leaveMutex();
140 return obj;
141}
142
144{
145 MapObject *obj = NULL;
146
147 if(!map)
148 return NULL;
149
150 enterMutex();
151 for (int i = range - 1; obj == NULL && i >= 0; i--)
152 obj = map[i];
153
154 if ( obj != NULL )
155 while ( obj->nextObject != NULL )
156 obj = obj->nextObject;
157 leaveMutex();
158 return obj;
159}
160
162{
163 enterMutex();
164 MapObject *obj = map[range];
165 if(obj)
166 map[range] = obj->nextObject;
167 leaveMutex();
168 return obj;
169}
170
172{
173 obj->detach();
174 enterMutex();
175 obj->nextObject = map[range];
176 map[range] = obj;
177 leaveMutex();
178}
179
181{
182 unsigned idx = getIndex(obj.idObject);
183
184 if(obj.table == this || !map)
185 return;
186
187 obj.detach();
188 enterMutex();
189 obj.nextObject = map[idx];
190 map[idx] = &obj;
191 obj.table = this;
192 count++;
193 leaveMutex();
194}
195
197{
198 addObject(obj);
199 return *this;
200}
201
203{
204 if(obj.table == this)
205 obj.detach();
206 return *this;
207}
208
209MapObject::MapObject(const char *id)
210{
211 table = NULL;
212 idObject = id;
213}
214
216{
217 MapObject *node, *prev = NULL;
218 unsigned idx;
219
220 if(!table)
221 return;
222
223 idx = table->getIndex(idObject);
224 table->enterMutex();
225 node = table->map[idx];
226
227 while(node) {
228 if(node == this)
229 break;
230 prev = node;
231 node = prev->nextObject;
232 }
233
234 if(node && !prev)
235 table->map[idx] = nextObject;
236 else if(node)
237 prev->nextObject = nextObject;
238 table->count--;
239 table->leaveMutex();
240 table = NULL;
241}
242
243} // namespace ost
The MapIndex allows linear access into a MapTable, that otherwise could have its elements being retri...
Definition: object.h:493
MapIndex & operator++()
Prefix increment operator, to be used in loops and such.
Definition: map.cpp:54
MapIndex & operator=(MapObject *theObject)
Assignment operator to avoid implicit cast.
Definition: map.cpp:48
MapObject * thisObject
Definition: object.h:494
The MapObject is a base class which can be used to make a derived class operate on a MapTable.
Definition: object.h:587
MapObject * nextObject
Definition: object.h:594
void detach(void)
Remove the object from it's current table.
Definition: map.cpp:215
MapTable * table
Definition: object.h:596
MapObject(const char *id)
Save id, mark as not using any table.
Definition: map.cpp:209
const char * idObject
Definition: object.h:595
A map table allows for entities to be mapped (hash index) onto it.
Definition: object.h:350
void addObject(MapObject &obj)
Map an object to our table.
Definition: map.cpp:180
virtual unsigned getIndex(const char *id)
Get index value from id string.
Definition: map.cpp:101
void cleanup(void)
Definition: map.cpp:92
MapTable & operator+=(MapObject &obj)
An operator to map an object to the table.
Definition: map.cpp:196
MapTable(unsigned size)
Create a map table with a specified number of slots.
Definition: map.cpp:78
virtual ~MapTable()
Destroy the table, calls cleanup.
Definition: map.cpp:87
void * getObject(const char *id)
Lookup an object by id key.
Definition: map.cpp:111
unsigned count
Definition: object.h:358
MapObject ** map
Definition: object.h:359
void * getLast()
Get the last element into table, it is returned as void * for easy re-cast.
Definition: map.cpp:143
virtual MapTable & operator-=(MapObject &obj)
This operator is virtual in case it must also add the object to a managed free list.
Definition: map.cpp:202
void * getFirst()
Get the first element into table, it is returned as void * for easy re-cast.
Definition: map.cpp:128
void addFree(MapObject *obj)
Add an object to the managed free list.
Definition: map.cpp:171
void * getFree(void)
Get next object from managed free list.
Definition: map.cpp:161
unsigned range
Definition: object.h:357
void leaveMutex(void)
Definition: thread.h:74
void enterMutex(void)
Definition: thread.h:70
Export interfaces for library interfaces.
Some object manipulation classes for smart pointers, linked lists, etc.
Common C++ thread class and sychronization objects.
int stricmp(const char *s1, const char *s2)
Definition: cpr.cpp:95
Definition: address.cpp:63