1 /*_############################################################################
6 _## -----------------------------------------------
7 _## Copyright (c) 2001-2010 Jochen Katz, Frank Fock
9 _## This software is based on SNMP++2.6 from Hewlett Packard:
11 _## Copyright (c) 1996
12 _## Hewlett-Packard Company
14 _## ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
15 _## Permission to use, copy, modify, distribute and/or sell this software
16 _## and/or its documentation is hereby granted without fee. User agrees
17 _## to display the above copyright notice and this license notice in all
18 _## copies of the software and any documentation of the software. User
19 _## agrees to assume all liability for the use of the software;
20 _## Hewlett-Packard and Jochen Katz make no representations about the
21 _## suitability of this software for any purpose. It is provided
22 _## "AS-IS" without warranty of any kind, either express or implied. User
23 _## hereby grants a royalty-free license to any and all derivatives based
24 _## upon this software code base.
26 _## Stuttgart, Germany, Thu Sep 2 00:07:47 CEST 2010
28 _##########################################################################*/
29 /*===================================================================
32 Hewlett-Packard Company
34 ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
35 Permission to use, copy, modify, distribute and/or sell this software
36 and/or its documentation is hereby granted without fee. User agrees
37 to display the above copyright notice and this license notice in all
38 copies of the software and any documentation of the software. User
39 agrees to assume all liability for the use of the software; Hewlett-Packard
40 makes no representations about the suitability of this software for any
41 purpose. It is provided "AS-IS without warranty of any kind,either express
42 or implied. User hereby grants a royalty-free license to any and all
43 derivatives based upon this software code base.
46 SNMP++ C O L L E C T . H
48 COLLECTION CLASS DEFINITION
50 DESIGN + AUTHOR: Peter E Mellquist
52 DESCRIPTION: Simple Collection classes for SNMP++ classes.
54 =====================================================================*/
55 // $Id: collect.h 349 2008-11-17 23:23:35Z katz $
57 #ifndef _COLLECTION_H_
58 #define _COLLECTION_H_
60 #include "snmp_pp/config_snmp_pp.h"
62 #ifdef SNMP_PP_NAMESPACE
74 #define MAXT 25 // elements per block
76 template <class T> class SnmpCollection
81 cBlock(cBlock *p, cBlock *n) : prev(p), next(n) {};
90 * Create an empty collection.
93 : count(0), data(0,0) {};
96 * Create a collection using a single template object.
98 SnmpCollection(const T &t)
99 : count(1), data(0, 0)
101 data.item[0] = (T*) (t.clone());
105 * Create a collection with another collection (copy constructor).
107 SnmpCollection(const SnmpCollection<T> &c)
108 : count(0), data(0, 0)
110 if (c.count == 0) return;
112 // load up the new collection
113 cBlock *current = &data;
117 while (count < c.count)
121 nextBlock = new cBlock(current, 0);
122 current->next = nextBlock;
127 c.get_element(tmp, count);
128 current->item[cn] = (T*) (tmp->clone());
135 * Destroy the collection.
139 clear(); // just delete the data
143 * Get the size of the collection.
151 * Append an item to the collection.
153 SnmpCollection& operator +=(const T &i)
155 cBlock *current = &data;
156 int cn = (int) count % MAXT;
157 while (current->next)
158 current = current->next;
159 if ((count > 0) && ((count % MAXT) == 0))
161 cBlock *add = new cBlock(current, 0);
162 if (!add) return *this;
164 add->item[0] = (T*) (i.clone());
168 current->item[cn] = (T*) (i.clone());
176 * Assign one collection to another.
178 SnmpCollection &operator =(const SnmpCollection<T> &c)
180 if (this == &c) return *this; // check for self assignment
182 clear(); // delete the data
184 if (c.count == 0) return *this;
186 // load up the new collection
187 cBlock *current = &data;
191 while (count < c.count)
195 nextBlock = new cBlock(current, 0);
196 current->next = nextBlock;
201 c.get_element(tmp, count);
202 current->item[cn] = (T*) (tmp->clone());
211 * Access an element in the collection.
213 * @return The requestet element or an empty element if out of bounds.
215 T operator[](const int p) const
217 if ((p < count) && (p >= 0))
219 cBlock const *current = &data;
220 int bn = (int) (p / MAXT);
221 int cn = (int) p % MAXT;
222 for (int z=0; z<bn; z++)
223 current = current->next;
224 return *(current->item[cn]);
228 // return an instance of nothing!!
235 * Set an element in the collection.
237 * @return 0 on success and -1 on failure.
239 int set_element( const T& i, const int p)
241 if ((p < 0) || (p > count)) return -1; // not found!
243 cBlock *current = &data;
244 int bn = (int) p / MAXT;
245 int cn = (int) p % MAXT;
246 for (int z=0; z<bn; z++)
247 current = current->next;
248 delete current->item[cn];
249 current->item[cn] = (T*) (i.clone());
254 * Get an element in the collection.
256 * @return 0 on success and -1 on failure.
258 int get_element(T &t, const int p) const
260 if ((p < 0) || (p > count)) return -1; // not found!
262 cBlock const *current = &data;
263 int bn = (int) p / MAXT;
264 int cn = (int) p % MAXT;
265 for (int z=0; z<bn; z++)
266 current = current->next;
267 t = *(current->item[cn]);
272 * Get a pointer to an element in the collection.
274 * @return 0 on success and -1 on failure.
276 int get_element(T *&t, const int p) const
278 if ((p < 0) || (p > count)) return -1; // not found!
280 cBlock const *current = &data;
281 int bn = (int) p / MAXT;
282 int cn = (int) p % MAXT;
283 for (int z=0; z<bn; z++)
284 current = current->next;
285 t = current->item[cn];
290 * Apply an function to the entire collection, iterator.
292 void apply(void f(T&))
295 for ( int z=0; z<count; z++)
297 this->get_element(temp, z);
303 * Looks for an element in the collection.
305 * @return TRUE if found.
307 int find(const T& i, int &pos) const
310 for (int z=0; z<count; z++)
312 this->get_element(temp, z);
322 * Delete an element in the collection.
324 int remove(const T& i)
326 // first see if we have it
330 SnmpCollection<T> newCollection;
332 for (int z=0; z<count; z++)
337 get_element(item, z);
338 newCollection += item;
342 // assign new collection to 'this'
343 operator =(newCollection);
347 return FALSE; // not found thus not removed
351 * Delete all elements within the collection.
355 if (count == 0) return;
357 cBlock *current = &data;
365 current = current->next;
367 delete current->item[cn];
373 while (current->next)
374 current = current->next;
375 while (current->prev)
377 current = current->prev;
378 delete current->next;
391 #ifdef SNMP_PP_NAMESPACE
392 } // end of namespace Snmp_pp
395 #endif // _COLLECTION_H_