SNMP++  3.3.11
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$
54 
55 #ifndef _SNMP_COLLECT_H_
56 #define _SNMP_COLLECT_H_
57 
58 #include <libsnmp.h>
59 #include "snmp_pp/config_snmp_pp.h"
60 
61 #ifdef SNMP_PP_NAMESPACE
62 namespace Snmp_pp {
63 #endif
64 
65 #define MAXT 25 // elements per block
66 
67 template <class T> class SnmpCollection
68 {
69  class cBlock
70  {
71  public:
72  cBlock(cBlock *p, cBlock *n) : prev(p), next(n) {};
73  T *item[MAXT];
76  };
77 
78  public:
79 
80  /**
81  * Create an empty collection.
82  */
84  : count(0), data(0,0) {}
85 
86  /**
87  * Create a collection using a single template object.
88  */
89  SnmpCollection(const T &t)
90  : count(1), data(0, 0)
91  {
92  data.item[0] = (T*) (t.clone());
93  }
94 
95  /**
96  * Create a collection with another collection (copy constructor).
97  */
99  : count(0), data(0, 0)
100  {
101  if (c.count == 0) return;
102 
103  // load up the new collection
104  cBlock *current = &data;
105  cBlock *nextBlock;
106  int cn = 0;
107 
108  while (count < c.count)
109  {
110  if (cn >= MAXT)
111  {
112  nextBlock = new cBlock(current, 0);
113  current->next = nextBlock;
114  current = nextBlock;
115  cn=0;
116  }
117  T *tmp = 0;
118  c.get_element(tmp, count);
119  current->item[cn] = (T*) (tmp->clone());
120  count++;
121  cn++;
122  }
123  }
124 
125  /**
126  * Destroy the collection.
127  */
129  {
130  clear(); // just delete the data
131  }
132 
133  /**
134  * Get the size of the collection.
135  */
136  int size() const
137  {
138  return count;
139  }
140 
141  /**
142  * Append an item to the collection.
143  */
144  SnmpCollection& operator +=(const T &i)
145  {
146  cBlock *current = &data;
147  int cn = (int) count % MAXT;
148  while (current->next)
149  current = current->next;
150  if ((count > 0) && ((count % MAXT) == 0))
151  {
152  cBlock *add = new cBlock(current, 0);
153  if (!add) return *this;
154  current->next = add;
155  add->item[0] = (T*) (i.clone());
156  }
157  else
158  {
159  current->item[cn] = (T*) (i.clone());
160  }
161  count++;
162 
163  return *this;
164  }
165 
166  /**
167  * Assign one collection to another.
168  */
169  SnmpCollection &operator =(const SnmpCollection<T> &c)
170  {
171  if (this == &c) return *this; // check for self assignment
172 
173  clear(); // delete the data
174 
175  if (c.count == 0) return *this;
176 
177  // load up the new collection
178  cBlock *current = &data;
179  cBlock *nextBlock;
180  int cn = 0;
181  count = 0;
182  while (count < c.count)
183  {
184  if (cn >= MAXT)
185  {
186  nextBlock = new cBlock(current, 0);
187  current->next = nextBlock;
188  current = nextBlock;
189  cn=0;
190  }
191  T *tmp = 0;
192  c.get_element(tmp, count);
193  current->item[cn] = (T*) (tmp->clone());
194  count++;
195  cn++;
196  }
197 
198  return *this;
199  }
200 
201  /**
202  * Access an element in the collection.
203  *
204  * @return The requestet element or an empty element if out of bounds.
205  */
206  T operator[](const int p) const
207  {
208  if ((p < count) && (p >= 0))
209  {
210  cBlock const *current = &data;
211  int bn = (int) (p / MAXT);
212  int cn = (int) p % MAXT;
213  for (int z=0; z<bn; z++)
214  current = current->next;
215  return *(current->item[cn]);
216  }
217  else
218  {
219  // return an instance of nothing!!
220  T t;
221  return t;
222  }
223  }
224 
225  /**
226  * Set an element in the collection.
227  *
228  * @return 0 on success and -1 on failure.
229  */
230  int set_element( const T& i, const int p)
231  {
232  if ((p < 0) || (p > count)) return -1; // not found!
233 
234  cBlock *current = &data;
235  int bn = (int) p / MAXT;
236  int cn = (int) p % MAXT;
237  for (int z=0; z<bn; z++)
238  current = current->next;
239  delete current->item[cn];
240  current->item[cn] = (T*) (i.clone());
241  return 0;
242  }
243 
244  /**
245  * Get an element in the collection.
246  *
247  * @return 0 on success and -1 on failure.
248  */
249  int get_element(T &t, const int p) const
250  {
251  if ((p < 0) || (p > count)) return -1; // not found!
252 
253  cBlock const *current = &data;
254  int bn = (int) p / MAXT;
255  int cn = (int) p % MAXT;
256  for (int z=0; z<bn; z++)
257  current = current->next;
258  t = *(current->item[cn]);
259  return 0;
260  }
261 
262  /**
263  * Get a pointer to an element in the collection.
264  *
265  * @return 0 on success and -1 on failure.
266  */
267  int get_element(T *&t, const int p) const
268  {
269  if ((p < 0) || (p > count)) return -1; // not found!
270 
271  cBlock const *current = &data;
272  int bn = (int) p / MAXT;
273  int cn = (int) p % MAXT;
274  for (int z=0; z<bn; z++)
275  current = current->next;
276  t = current->item[cn];
277  return 0;
278  }
279 
280  /**
281  * Apply an function to the entire collection, iterator.
282  */
283  void apply(void f(T&))
284  {
285  T temp;
286  for ( int z=0; z<count; z++)
287  {
288  this->get_element(temp, z);
289  f(temp);
290  }
291  }
292 
293  /**
294  * Looks for an element in the collection.
295  *
296  * @return TRUE if found.
297  */
298  int find(const T& i, int &pos) const
299  {
300  T temp;
301  for (int z=0; z<count; z++)
302  {
303  this->get_element(temp, z);
304  if ( temp == i) {
305  pos = z;
306  return true;
307  }
308  }
309  return false;
310  }
311 
312  /**
313  * Delete an element in the collection.
314  */
315  int remove(const T& i)
316  {
317  // first see if we have it
318  int pos;
319  if (find(i, pos))
320  {
321  SnmpCollection<T> newCollection;
322 
323  for (int z=0; z<count; z++)
324  {
325  if (z != pos)
326  {
327  T item;
328  get_element(item, z);
329  newCollection += item;
330  }
331  }
332 
333  // assign new collection to 'this'
334  operator =(newCollection);
335 
336  return true;
337  }
338  return false; // not found thus not removed
339  }
340 
341  /**
342  * Delete all elements within the collection.
343  */
344  void clear()
345  {
346  if (count == 0) return;
347 
348  cBlock *current = &data;
349  int z=0;
350  int cn=0;
351  while ( z< count)
352  {
353  if (cn >= MAXT)
354  {
355  cn =0;
356  current = current->next;
357  }
358  delete current->item[cn];
359  cn++;
360  z++;
361  }
362 
363  // delete the blocks
364  while (current->next)
365  current = current->next;
366  while (current->prev)
367  {
368  current = current->prev;
369  delete current->next;
370  }
371 
372  count = 0;
373  data.next=0;
374  data.prev=0;
375  }
376 
377  private:
378  int count;
380 };
381 
382 #ifdef SNMP_PP_NAMESPACE
383 } // end of namespace Snmp_pp
384 #endif
385 
386 #endif // _SNMP_COLLECT_H_
387 
cBlock data
Definition: collect.h:379
int set_element(const T &i, const int p)
Set an element in the collection.
Definition: collect.h:230
SnmpCollection(const T &t)
Create a collection using a single template object.
Definition: collect.h:89
T operator[](const int p) const
Access an element in the collection.
Definition: collect.h:206
int size() const
Get the size of the collection.
Definition: collect.h:136
~SnmpCollection()
Destroy the collection.
Definition: collect.h:128
int get_element(T &t, const int p) const
Get an element in the collection.
Definition: collect.h:249
SnmpCollection(const SnmpCollection< T > &c)
Create a collection with another collection (copy constructor).
Definition: collect.h:98
int find(const T &i, int &pos) const
Looks for an element in the collection.
Definition: collect.h:298
void apply(void f(T &))
Apply an function to the entire collection, iterator.
Definition: collect.h:283
int get_element(T *&t, const int p) const
Get a pointer to an element in the collection.
Definition: collect.h:267
void clear()
Delete all elements within the collection.
Definition: collect.h:344
#define MAXT
Definition: collect.h:65
SnmpCollection()
Create an empty collection.
Definition: collect.h:83