Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

collect1.h

Go to the documentation of this file.
00001 /*_############################################################################
00002   _## 
00003   _##  collect1.h  
00004   _##
00005   _##  SNMP++v3.2.15
00006   _##  -----------------------------------------------
00007   _##  Copyright (c) 2001-2004 Jochen Katz, Frank Fock
00008   _##
00009   _##  This software is based on SNMP++2.6 from Hewlett Packard:
00010   _##  
00011   _##    Copyright (c) 1996
00012   _##    Hewlett-Packard Company
00013   _##  
00014   _##  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
00015   _##  Permission to use, copy, modify, distribute and/or sell this software 
00016   _##  and/or its documentation is hereby granted without fee. User agrees 
00017   _##  to display the above copyright notice and this license notice in all 
00018   _##  copies of the software and any documentation of the software. User 
00019   _##  agrees to assume all liability for the use of the software; 
00020   _##  Hewlett-Packard and Jochen Katz make no representations about the 
00021   _##  suitability of this software for any purpose. It is provided 
00022   _##  "AS-IS" without warranty of any kind, either express or implied. User 
00023   _##  hereby grants a royalty-free license to any and all derivatives based
00024   _##  upon this software code base. 
00025   _##  
00026   _##  Stuttgart, Germany, Tue Jan  4 21:42:42 CET 2005 
00027   _##  
00028   _##########################################################################*/
00029 // $Id: collect1.h,v 1.5 2004/09/05 18:59:48 katz Exp $
00030 #ifdef SNMP_PP_NAMESPACE
00031 namespace Snmp_pp {
00032 #endif
00033 
00034 template <class T> class SnmpCollection
00035 {
00036   class cBlock
00037   {
00038     public:
00039      cBlock(cBlock *p, cBlock *n) : prev(p), next(n) {};
00040      T *item[MAXT];
00041      cBlock *prev;
00042      cBlock *next;
00043   };
00044 
00045  public:
00046 
00047   /**
00048    * Create an empty collection.
00049    */
00050   SnmpCollection()
00051     : count(0), data(0,0) {};
00052 
00053   /**
00054    * Create a collection using a single template object.
00055    */
00056   SnmpCollection(const T &t)
00057     : count(1), data(0, 0)
00058   {
00059     data.item[0] = (T*) (t.clone());
00060   };
00061 
00062   /**
00063    * Create a collection with another collection (copy constructor).
00064    */
00065   SnmpCollection(const SnmpCollection<T> &c)
00066     : count(0), data(0, 0)
00067   {
00068     if (c.count == 0) return;
00069 
00070     // load up the new collection
00071     cBlock *current = &data;
00072     cBlock *nextBlock;
00073     int cn = 0;
00074 
00075     while (count < c.count)
00076     {
00077       if (cn >= MAXT)
00078       {
00079         nextBlock = new cBlock(current, 0);
00080         current->next = nextBlock;
00081         current = nextBlock;
00082         cn=0;
00083       }
00084       T *tmp;
00085       c.get_element(tmp, count);
00086       current->item[cn] = (T*) (tmp->clone());
00087       count++;
00088       cn++;
00089     }
00090   };
00091 
00092   /**
00093    * Destroy the collection.
00094    */
00095   ~SnmpCollection()
00096   {
00097     clear();  // just delete the data
00098   };
00099 
00100   /**
00101    * Get the size of the collection.
00102    */
00103   int size() const
00104   {
00105     return count;
00106   };
00107 
00108   /**
00109    * Append an item to the collection.
00110    */
00111   SnmpCollection& operator +=(const T &i)
00112   {
00113     cBlock *current = &data;
00114     int cn = (int) count % MAXT;
00115     while (current->next)
00116       current = current->next;
00117     if ((count > 0) && ((count % MAXT) == 0))
00118     {
00119       cBlock *add = new cBlock(current, 0);
00120       if (!add) return *this;
00121       current->next = add;
00122       add->item[0] = (T*) (i.clone());
00123     }
00124     else
00125     {
00126       current->item[cn] = (T*) (i.clone());
00127     }
00128     count++;
00129 
00130     return *this;
00131   };
00132 
00133   /**
00134    * Assign one collection to another.
00135    */
00136   SnmpCollection &operator =(const SnmpCollection<T> &c)
00137   {
00138     if (this == &c) return *this;  // check for self assignment
00139 
00140     clear(); // delete the data
00141 
00142     if (c.count == 0) return *this;
00143 
00144     // load up the new collection
00145     cBlock *current = &data;
00146     cBlock *nextBlock;
00147     int cn = 0;
00148     count = 0;
00149     while (count < c.count)
00150     {
00151       if (cn >= MAXT)
00152       {
00153         nextBlock = new cBlock(current, 0);
00154         current->next = nextBlock;
00155         current = nextBlock;
00156         cn=0;
00157       }
00158       T *tmp;
00159       c.get_element(tmp, count);
00160       current->item[cn] = (T*) (tmp->clone());
00161       count++;
00162       cn++;
00163     }
00164 
00165     return *this;
00166   };
00167 
00168   /**
00169    * Access an element in the collection.
00170    *
00171    * @return The requestet element or an empty element if out of bounds.
00172    */
00173   T operator[](const int p) const
00174   {
00175     if ((p < count) && (p >= 0))
00176     {
00177       cBlock const *current = &data;
00178       int bn = (int) (p / MAXT);
00179       int cn = (int) p % MAXT;
00180       for (int z=0; z<bn; z++)
00181         current = current->next;
00182       return *(current->item[cn]);
00183     }
00184     else
00185     {
00186       // return an instance of nothing!!
00187       T t;
00188       return t;
00189     }
00190   };
00191 
00192   /**
00193    * Set an element in the collection.
00194    *
00195    * @return 0 on success and -1 on failure.
00196    */
00197   int set_element( const T& i, const int p)
00198   {
00199     if ((p < 0) || (p > count)) return -1; // not found!
00200 
00201     cBlock *current = &data;
00202     int bn = (int) p / MAXT;
00203     int cn = (int) p % MAXT;
00204     for (int z=0; z<bn; z++)
00205       current = current->next;
00206     delete current->item[cn];
00207     current->item[cn] = (T*) (i.clone());
00208     return 0;
00209   };
00210 
00211   /**
00212    * Get an element in the collection.
00213    *
00214    * @return 0 on success and -1 on failure.
00215    */
00216   int get_element(T &t, const int p) const
00217   {
00218     if ((p < 0) || (p > count)) return -1; // not found!
00219 
00220     cBlock const *current = &data;
00221     int bn = (int) p / MAXT;
00222     int cn = (int) p % MAXT;
00223     for (int z=0; z<bn; z++)
00224       current = current->next;
00225     t = *(current->item[cn]);
00226     return 0;
00227   };
00228 
00229   /**
00230    * Get a pointer to an element in the collection.
00231    *
00232    * @return 0 on success and -1 on failure.
00233    */
00234   int get_element(T *&t, const int p) const
00235   {
00236     if ((p < 0) || (p > count)) return -1; // not found!
00237 
00238     cBlock const *current = &data;
00239     int bn = (int) p / MAXT;
00240     int cn = (int) p % MAXT;
00241     for (int z=0; z<bn; z++)
00242       current = current->next;
00243     t = current->item[cn];
00244     return 0;
00245   };
00246 
00247   /**
00248    * Apply an function to the entire collection, iterator.
00249    */
00250   void apply(void f(T&))
00251   {
00252     T temp;
00253     for ( int z=0; z<count; z++)
00254     {
00255       this->get_element(temp, z);
00256       f(temp);
00257     }
00258   };
00259 
00260   /**
00261    * Looks for an element in the collection.
00262    *
00263    * @return TRUE if found.
00264    */
00265   int find(const T& i, int &pos) const
00266   {
00267     T temp;
00268     for (int z=0; z<count; z++)
00269     {
00270       this->get_element(temp, z);
00271       if ( temp == i) {
00272         pos = z;
00273         return TRUE;
00274       }
00275     }
00276     return FALSE;
00277   };
00278 
00279   /**
00280    * Delete an element in the collection.
00281    */
00282   int remove(const T& i)
00283   {
00284     // first see if we have it
00285     int pos;
00286     if (find(i, pos))
00287     {
00288       SnmpCollection<T> newCollection;
00289 
00290       for (int z=0; z<count; z++)
00291       {
00292         if (z != pos)
00293         {
00294           T item;
00295           get_element(item, z);
00296           newCollection += item;
00297         }
00298       }
00299 
00300       // assign new collection to 'this'
00301       operator =(newCollection);
00302 
00303       return TRUE;
00304     }
00305     return FALSE;   // not found thus not removed
00306   };
00307 
00308   /**
00309    * Delete all elements within the collection.
00310    */
00311   void clear()
00312   {
00313     if (count == 0) return;
00314 
00315     cBlock *current = &data;
00316     int z=0;
00317     int cn=0;
00318     while ( z< count)
00319     {
00320       if (cn >= MAXT)
00321       {
00322         cn =0;
00323         current = current->next;
00324       }
00325       delete current->item[cn];
00326       cn++;
00327       z++;
00328     }
00329 
00330     // delete the blocks
00331     while (current->next)
00332       current = current->next;
00333     while (current->prev)
00334     {
00335       current = current->prev;
00336       delete current->next;
00337     }
00338 
00339     count = 0;
00340     data.next=0;
00341     data.prev=0;
00342   };
00343 
00344  private:
00345   int count;
00346   cBlock data;
00347 };
00348 
00349 #ifdef SNMP_PP_NAMESPACE
00350 }; // end of namespace Snmp_pp
00351 #endif 

Generated on Tue Jan 4 22:42:13 2005 for SNMP++ by doxygen 1.3.2