servlib/prophet/PointerList.h
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2006 Baylor University
       
     3  *
       
     4  *    Licensed under the Apache License, Version 2.0 (the "License");
       
     5  *    you may not use this file except in compliance with the License.
       
     6  *    You may obtain a copy of the License at
       
     7  *
       
     8  *        http://www.apache.org/licenses/LICENSE-2.0
       
     9  *
       
    10  *    Unless required by applicable law or agreed to in writing, software
       
    11  *    distributed under the License is distributed on an "AS IS" BASIS,
       
    12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13  *    See the License for the specific language governing permissions and
       
    14  *    limitations under the License.
       
    15  */
       
    16 
       
    17 #ifndef _PROPHET_POINTER_LIST_H_
       
    18 #define _PROPHET_POINTER_LIST_H_
       
    19 
       
    20 #include <vector>
       
    21 
       
    22 namespace prophet
       
    23 {
       
    24 
       
    25 /**
       
    26  * Auto deletes pointers in list destructor
       
    27  * This object assumes ownership for member pointers
       
    28  * Creates copies of members instead of copies of pointers to members
       
    29  */
       
    30 template<class T>
       
    31 class PointerList : public std::vector<T*>
       
    32 {
       
    33 public: 
       
    34     typedef std::vector<T*> List;
       
    35     typedef typename std::vector<T*>::iterator iterator;
       
    36     typedef typename std::vector<T*>::const_iterator const_iterator;
       
    37 
       
    38     /**
       
    39      * Default constructor
       
    40      */
       
    41     PointerList()
       
    42         : std::vector<T*>() {}
       
    43 
       
    44     /**
       
    45      * Copy constructor
       
    46      */
       
    47     PointerList(const PointerList& a)
       
    48         : std::vector<T*>()
       
    49     {
       
    50         clear();
       
    51         copy_from(a);
       
    52     }
       
    53 
       
    54     /**
       
    55      * Destructor
       
    56      */
       
    57     virtual ~PointerList()
       
    58     {
       
    59         clear();
       
    60     }
       
    61 
       
    62     /**
       
    63      * Assignment operator creates deep copy, not pointer copy
       
    64      */
       
    65     PointerList& operator= (const PointerList& a)
       
    66     {
       
    67         clear();
       
    68         copy_from(a);
       
    69         return *this;
       
    70     }
       
    71 
       
    72     /**
       
    73      * Deletes member pointed to by iterator, then removes pointer
       
    74      */
       
    75     void erase(iterator i)
       
    76     {
       
    77         delete (*i);
       
    78         List::erase(i);
       
    79     }
       
    80 
       
    81     /**
       
    82      * Delete all member variables, then remove pointers from
       
    83      * container
       
    84      */
       
    85     void clear()
       
    86     {
       
    87         free();
       
    88         List::clear();
       
    89     }
       
    90 
       
    91 protected:
       
    92     /**
       
    93      * Free memory pointed to by member variables
       
    94      */
       
    95     void free()
       
    96     {
       
    97         for(iterator i = List::begin();
       
    98             i != List::end();
       
    99             i++)
       
   100         {
       
   101             delete *i;
       
   102         }
       
   103     }
       
   104     /**
       
   105      * Utility function to perform deep copy from peer object
       
   106      */
       
   107     void copy_from(const PointerList& a)
       
   108     {
       
   109         for(const_iterator i = a.begin();
       
   110             i != a.end();
       
   111             i++)
       
   112         {
       
   113             push_back(new T(**i));
       
   114         }
       
   115     }
       
   116 }; // template PointerList<T>
       
   117 
       
   118 }; // namespace prophet
       
   119 
       
   120 #endif // _PROPHET_POINTER_LIST_H_