]> git.stg.codes - ssmd.git/blob - 3rdparty/snmp++/include/snmp_pp/collect.h
Fix build on osx.
[ssmd.git] / 3rdparty / snmp++ / include / snmp_pp / collect.h
1 /*_############################################################################
2   _## 
3   _##  collect.h  
4   _##
5   _##  SNMP++v3.2.25
6   _##  -----------------------------------------------
7   _##  Copyright (c) 2001-2010 Jochen Katz, Frank Fock
8   _##
9   _##  This software is based on SNMP++2.6 from Hewlett Packard:
10   _##  
11   _##    Copyright (c) 1996
12   _##    Hewlett-Packard Company
13   _##  
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. 
25   _##  
26   _##  Stuttgart, Germany, Thu Sep  2 00:07:47 CEST 2010 
27   _##  
28   _##########################################################################*/
29 /*===================================================================
30
31   Copyright (c) 1999
32   Hewlett-Packard Company
33
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.
44
45
46   SNMP++ C O L L E C T . H
47
48   COLLECTION CLASS DEFINITION
49
50   DESIGN + AUTHOR:  Peter E Mellquist
51
52   DESCRIPTION: Simple Collection classes for SNMP++ classes.
53
54 =====================================================================*/
55 // $Id: collect.h 349 2008-11-17 23:23:35Z katz $
56
57 #ifndef _COLLECTION_H_
58 #define _COLLECTION_H_
59
60 #include "snmp_pp/config_snmp_pp.h"
61
62 #ifdef SNMP_PP_NAMESPACE
63 namespace Snmp_pp {
64 #endif
65
66 #ifndef TRUE
67 #define TRUE 1
68 #endif
69
70 #ifndef FALSE
71 #define FALSE 0
72 #endif
73
74 #define MAXT 25     // elements per block
75
76 template <class T> class SnmpCollection
77 {
78   class cBlock
79   {
80     public:
81      cBlock(cBlock *p, cBlock *n) : prev(p), next(n) {};
82      T *item[MAXT];
83      cBlock *prev;
84      cBlock *next;
85   };
86
87  public:
88
89   /**
90    * Create an empty collection.
91    */
92   SnmpCollection()
93     : count(0), data(0,0) {};
94
95   /**
96    * Create a collection using a single template object.
97    */
98   SnmpCollection(const T &t)
99     : count(1), data(0, 0)
100   {
101     data.item[0] = (T*) (t.clone());
102   };
103
104   /**
105    * Create a collection with another collection (copy constructor).
106    */
107   SnmpCollection(const SnmpCollection<T> &c)
108     : count(0), data(0, 0)
109   {
110     if (c.count == 0) return;
111
112     // load up the new collection
113     cBlock *current = &data;
114     cBlock *nextBlock;
115     int cn = 0;
116
117     while (count < c.count)
118     {
119       if (cn >= MAXT)
120       {
121         nextBlock = new cBlock(current, 0);
122         current->next = nextBlock;
123         current = nextBlock;
124         cn=0;
125       }
126       T *tmp = 0;
127       c.get_element(tmp, count);
128       current->item[cn] = (T*) (tmp->clone());
129       count++;
130       cn++;
131     }
132   };
133
134   /**
135    * Destroy the collection.
136    */
137   ~SnmpCollection()
138   {
139     clear();  // just delete the data
140   };
141
142   /**
143    * Get the size of the collection.
144    */
145   int size() const
146   {
147     return count;
148   };
149
150   /**
151    * Append an item to the collection.
152    */
153   SnmpCollection& operator +=(const T &i)
154   {
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))
160     {
161       cBlock *add = new cBlock(current, 0);
162       if (!add) return *this;
163       current->next = add;
164       add->item[0] = (T*) (i.clone());
165     }
166     else
167     {
168       current->item[cn] = (T*) (i.clone());
169     }
170     count++;
171
172     return *this;
173   };
174
175   /**
176    * Assign one collection to another.
177    */
178   SnmpCollection &operator =(const SnmpCollection<T> &c)
179   {
180     if (this == &c) return *this;  // check for self assignment
181
182     clear(); // delete the data
183
184     if (c.count == 0) return *this;
185
186     // load up the new collection
187     cBlock *current = &data;
188     cBlock *nextBlock;
189     int cn = 0;
190     count = 0;
191     while (count < c.count)
192     {
193       if (cn >= MAXT)
194       {
195         nextBlock = new cBlock(current, 0);
196         current->next = nextBlock;
197         current = nextBlock;
198         cn=0;
199       }
200       T *tmp = 0;
201       c.get_element(tmp, count);
202       current->item[cn] = (T*) (tmp->clone());
203       count++;
204       cn++;
205     }
206
207     return *this;
208   };
209
210   /**
211    * Access an element in the collection.
212    *
213    * @return The requestet element or an empty element if out of bounds.
214    */
215   T operator[](const int p) const
216   {
217     if ((p < count) && (p >= 0))
218     {
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]);
225     }
226     else
227     {
228       // return an instance of nothing!!
229       T t;
230       return t;
231     }
232   };
233
234   /**
235    * Set an element in the collection.
236    *
237    * @return 0 on success and -1 on failure.
238    */
239   int set_element( const T& i, const int p)
240   {
241     if ((p < 0) || (p > count)) return -1; // not found!
242
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());
250     return 0;
251   };
252
253   /**
254    * Get an element in the collection.
255    *
256    * @return 0 on success and -1 on failure.
257    */
258   int get_element(T &t, const int p) const
259   {
260     if ((p < 0) || (p > count)) return -1; // not found!
261
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]);
268     return 0;
269   };
270
271   /**
272    * Get a pointer to an element in the collection.
273    *
274    * @return 0 on success and -1 on failure.
275    */
276   int get_element(T *&t, const int p) const
277   {
278     if ((p < 0) || (p > count)) return -1; // not found!
279
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];
286     return 0;
287   };
288
289   /**
290    * Apply an function to the entire collection, iterator.
291    */
292   void apply(void f(T&))
293   {
294     T temp;
295     for ( int z=0; z<count; z++)
296     {
297       this->get_element(temp, z);
298       f(temp);
299     }
300   };
301
302   /**
303    * Looks for an element in the collection.
304    *
305    * @return TRUE if found.
306    */
307   int find(const T& i, int &pos) const
308   {
309     T temp;
310     for (int z=0; z<count; z++)
311     {
312       this->get_element(temp, z);
313       if ( temp == i) {
314         pos = z;
315         return TRUE;
316       }
317     }
318     return FALSE;
319   };
320
321   /**
322    * Delete an element in the collection.
323    */
324   int remove(const T& i)
325   {
326     // first see if we have it
327     int pos;
328     if (find(i, pos))
329     {
330       SnmpCollection<T> newCollection;
331
332       for (int z=0; z<count; z++)
333       {
334         if (z != pos)
335         {
336           T item;
337           get_element(item, z);
338           newCollection += item;
339         }
340       }
341
342       // assign new collection to 'this'
343       operator =(newCollection);
344
345       return TRUE;
346     }
347     return FALSE;   // not found thus not removed
348   };
349
350   /**
351    * Delete all elements within the collection.
352    */
353   void clear()
354   {
355     if (count == 0) return;
356
357     cBlock *current = &data;
358     int z=0;
359     int cn=0;
360     while ( z< count)
361     {
362       if (cn >= MAXT)
363       {
364         cn =0;
365         current = current->next;
366       }
367       delete current->item[cn];
368       cn++;
369       z++;
370     }
371
372     // delete the blocks
373     while (current->next)
374       current = current->next;
375     while (current->prev)
376     {
377       current = current->prev;
378       delete current->next;
379     }
380
381     count = 0;
382     data.next=0;
383     data.prev=0;
384   };
385
386  private:
387   int count;
388   cBlock data;
389 };
390
391 #ifdef SNMP_PP_NAMESPACE
392 } // end of namespace Snmp_pp
393 #endif 
394
395 #endif  // _COLLECTION_H_
396