SNMP++  3.3.4
collect.h
Go to the documentation of this file.
1 /*_############################################################################
2  _##
3  _## collect.h
4  _##
5  _## SNMP++ v3.3
6  _## -----------------------------------------------
7  _## Copyright (c) 2001-2013 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  _##########################################################################*/
27 /*===================================================================
28 
29  Copyright (c) 1999
30  Hewlett-Packard Company
31 
32  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
33  Permission to use, copy, modify, distribute and/or sell this software
34  and/or its documentation is hereby granted without fee. User agrees
35  to display the above copyright notice and this license notice in all
36  copies of the software and any documentation of the software. User
37  agrees to assume all liability for the use of the software; Hewlett-Packard
38  makes no representations about the suitability of this software for any
39  purpose. It is provided "AS-IS without warranty of any kind,either express
40  or implied. User hereby grants a royalty-free license to any and all
41  derivatives based upon this software code base.
42 
43 
44  SNMP++ C O L L E C T . H
45 
46  COLLECTION CLASS DEFINITION
47 
48  DESIGN + AUTHOR: Peter E Mellquist
49 
50  DESCRIPTION: Simple Collection classes for SNMP++ classes.
51 
52 =====================================================================*/
53 // $Id: collect.h 2359 2013-05-09 20:07:01Z fock $
54 
55 #ifndef _COLLECTION_H_
56 #define _COLLECTION_H_
57 
58 #include "snmp_pp/config_snmp_pp.h"
59 
60 #ifdef SNMP_PP_NAMESPACE
61 namespace Snmp_pp {
62 #endif
63 
64 #define MAXT 25 // elements per block
65 
66 template <class T> class SnmpCollection
67 {
68  class cBlock
69  {
70  public:
71  cBlock(cBlock *p, cBlock *n) : prev(p), next(n) {};
72  T *item[MAXT];
75  };
76 
77  public:
78 
79  /**
80  * Create an empty collection.
81  */
83  : count(0), data(0,0) {};
84 
85  /**
86  * Create a collection using a single template object.
87  */
88  SnmpCollection(const T &t)
89  : count(1), data(0, 0)
90  {
91  data.item[0] = (T*) (t.clone());
92  };
93 
94  /**
95  * Create a collection with another collection (copy constructor).
96  */
98  : count(0), data(0, 0)
99  {
100  if (c.count == 0) return;
101 
102  // load up the new collection
103  cBlock *current = &data;
104  cBlock *nextBlock;
105  int cn = 0;
106 
107  while (count < c.count)
108  {
109  if (cn >= MAXT)
110  {
111  nextBlock = new cBlock(current, 0);
112  current->next = nextBlock;
113  current = nextBlock;
114  cn=0;
115  }
116  T *tmp = 0;
117  c.get_element(tmp, count);
118  current->item[cn] = (T*) (tmp->clone());
119  count++;
120  cn++;
121  }
122  };
123 
124  /**
125  * Destroy the collection.
126  */
128  {
129  clear(); // just delete the data
130  };
131 
132  /**
133  * Get the size of the collection.
134  */
135  int size() const
136  {
137  return count;
138  };
139 
140  /**
141  * Append an item to the collection.
142  */
143  SnmpCollection& operator +=(const T &i)
144  {
145  cBlock *current = &data;
146  int cn = (int) count % MAXT;
147  while (current->next)
148  current = current->next;
149  if ((count > 0) && ((count % MAXT) == 0))
150  {
151  cBlock *add = new cBlock(current, 0);
152  if (!add) return *this;
153  current->next = add;
154  add->item[0] = (T*) (i.clone());
155  }
156  else
157  {
158  current->item[cn] = (T*) (i.clone());
159  }
160  count++;
161 
162  return *this;
163  };
164 
165  /**
166  * Assign one collection to another.
167  */
168  SnmpCollection &operator =(const SnmpCollection<T> &c)
169  {
170  if (this == &c) return *this; // check for self assignment
171 
172  clear(); // delete the data
173 
174  if (c.count == 0) return *this;
175 
176  // load up the new collection
177  cBlock *current = &data;
178  cBlock *nextBlock;
179  int cn = 0;
180  count = 0;
181  while (count < c.count)
182  {
183  if (cn >= MAXT)
184  {
185  nextBlock = new cBlock(current, 0);
186  current->next = nextBlock;
187  current = nextBlock;
188  cn=0;
189  }
190  T *tmp = 0;
191  c.get_element(tmp, count);
192  current->item[cn] = (T*) (tmp->clone());
193  count++;
194  cn++;
195  }
196 
197  return *this;
198  };
199 
200  /**
201  * Access an element in the collection.
202  *
203  * @return The requestet element or an empty element if out of bounds.
204  */
205  T operator[](const int p) const
206  {
207  if ((p < count) && (p >= 0))
208  {
209  cBlock const *current = &data;
210  int bn = (int) (p / MAXT);
211  int cn = (int) p % MAXT;
212  for (int z=0; z<bn; z++)
213  current = current->next;
214  return *(current->item[cn]);
215  }
216  else
217  {
218  // return an instance of nothing!!
219  T t;
220  return t;
221  }
222  };
223 
224  /**
225  * Set an element in the collection.
226  *
227  * @return 0 on success and -1 on failure.
228  */
229  int set_element( const T& i, const int p)
230  {
231  if ((p < 0) || (p > count)) return -1; // not found!
232 
233  cBlock *current = &data;
234  int bn = (int) p / MAXT;
235  int cn = (int) p % MAXT;
236  for (int z=0; z<bn; z++)
237  current = current->next;
238  delete current->item[cn];
239  current->item[cn] = (T*) (i.clone());
240  return 0;
241  };
242 
243  /**
244  * Get an element in the collection.
245  *
246  * @return 0 on success and -1 on failure.
247  */
248  int get_element(T &t, const int p) const
249  {
250  if ((p < 0) || (p > count)) return -1; // not found!
251 
252  cBlock const *current = &data;
253  int bn = (int) p / MAXT;
254  int cn = (int) p % MAXT;
255  for (int z=0; z<bn; z++)
256  current = current->next;
257  t = *(current->item[cn]);
258  return 0;
259  };
260 
261  /**
262  * Get a pointer to an element in the collection.
263  *
264  * @return 0 on success and -1 on failure.
265  */
266  int get_element(T *&t, const int p) const
267  {
268  if ((p < 0) || (p > count)) return -1; // not found!
269 
270  cBlock const *current = &data;
271  int bn = (int) p / MAXT;
272  int cn = (int) p % MAXT;
273  for (int z=0; z<bn; z++)
274  current = current->next;
275  t = current->item[cn];
276  return 0;
277  };
278 
279  /**
280  * Apply an function to the entire collection, iterator.
281  */
282  void apply(void f(T&))
283  {
284  T temp;
285  for ( int z=0; z<count; z++)
286  {
287  this->get_element(temp, z);
288  f(temp);
289  }
290  };
291 
292  /**
293  * Looks for an element in the collection.
294  *
295  * @return TRUE if found.
296  */
297  int find(const T& i, int &pos) const
298  {
299  T temp;
300  for (int z=0; z<count; z++)
301  {
302  this->get_element(temp, z);
303  if ( temp == i) {
304  pos = z;
305  return true;
306  }
307  }
308  return false;
309  };
310 
311  /**
312  * Delete an element in the collection.
313  */
314  int remove(const T& i)
315  {
316  // first see if we have it
317  int pos;
318  if (find(i, pos))
319  {
320  SnmpCollection<T> newCollection;
321 
322  for (int z=0; z<count; z++)
323  {
324  if (z != pos)
325  {
326  T item;
327  get_element(item, z);
328  newCollection += item;
329  }
330  }
331 
332  // assign new collection to 'this'
333  operator =(newCollection);
334 
335  return true;
336  }
337  return false; // not found thus not removed
338  };
339 
340  /**
341  * Delete all elements within the collection.
342  */
343  void clear()
344  {
345  if (count == 0) return;
346 
347  cBlock *current = &data;
348  int z=0;
349  int cn=0;
350  while ( z< count)
351  {
352  if (cn >= MAXT)
353  {
354  cn =0;
355  current = current->next;
356  }
357  delete current->item[cn];
358  cn++;
359  z++;
360  }
361 
362  // delete the blocks
363  while (current->next)
364  current = current->next;
365  while (current->prev)
366  {
367  current = current->prev;
368  delete current->next;
369  }
370 
371  count = 0;
372  data.next=0;
373  data.prev=0;
374  };
375 
376  private:
377  int count;
379 };
380 
381 #ifdef SNMP_PP_NAMESPACE
382 } // end of namespace Snmp_pp
383 #endif
384 
385 #endif // _COLLECTION_H_
386 
cBlock data
Definition: collect.h:378
int size() const
Get the size of the collection.
Definition: collect.h:135
int set_element(const T &i, const int p)
Set an element in the collection.
Definition: collect.h:229
SnmpCollection(const T &t)
Create a collection using a single template object.
Definition: collect.h:88
int find(const T &i, int &pos) const
Looks for an element in the collection.
Definition: collect.h:297
~SnmpCollection()
Destroy the collection.
Definition: collect.h:127
int get_element(T *&t, const int p) const
Get a pointer to an element in the collection.
Definition: collect.h:266
SnmpCollection(const SnmpCollection< T > &c)
Create a collection with another collection (copy constructor).
Definition: collect.h:97
int get_element(T &t, const int p) const
Get an element in the collection.
Definition: collect.h:248
void apply(void f(T &))
Apply an function to the entire collection, iterator.
Definition: collect.h:282
T operator[](const int p) const
Access an element in the collection.
Definition: collect.h:205
void clear()
Delete all elements within the collection.
Definition: collect.h:343
#define MAXT
Definition: collect.h:64
SnmpCollection()
Create an empty collection.
Definition: collect.h:82