LCOV - code coverage report
Current view: top level - nsas - nsas_entry.h (source / functions) Hit Total Coverage
Test: report.info Lines: 13 20 65.0 %
Date: 2012-05-15 Functions: 15 33 45.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 3 24 12.5 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
       2                 :            : //
       3                 :            : // Permission to use, copy, modify, and/or distribute this software for any
       4                 :            : // purpose with or without fee is hereby granted, provided that the above
       5                 :            : // copyright notice and this permission notice appear in all copies.
       6                 :            : //
       7                 :            : // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
       8                 :            : // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
       9                 :            : // AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
      10                 :            : // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
      11                 :            : // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
      12                 :            : // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
      13                 :            : // PERFORMANCE OF THIS SOFTWARE.
      14                 :            : 
      15                 :            : #ifndef __NSAS_ENTRY_H
      16                 :            : #define __NSAS_ENTRY_H
      17                 :            : 
      18                 :            : #include <boost/enable_shared_from_this.hpp>
      19                 :            : #include <iostream>
      20                 :            : 
      21                 :            : #include <exceptions/exceptions.h>
      22                 :            : #include <util/lru_list.h>
      23                 :            : 
      24                 :            : #include "hash_key.h"
      25                 :            : #include "hash_table.h"
      26                 :            : 
      27                 :            : namespace isc {
      28                 :            : namespace nsas {
      29                 :            : 
      30                 :            : /// \brief Invalid Iterator
      31                 :            : ///
      32                 :            : /// Thrown if an attempt was made to access the iterator - the pointer into
      33                 :            : /// the LRU list where this element is located - when it is marked as invalid.
      34                 :          0 : class InvalidLruIterator : public isc::Exception {
      35                 :            : public:
      36                 :          0 :     InvalidLruIterator(const char* file, size_t line, const char* what) :
      37 [ #  # ][ #  # ]:          0 :         Exception(file, line, what)
      38                 :          0 :     {}
      39                 :            : };
      40                 :            : 
      41                 :            : /// \brief Element of NSAS Internal Lists
      42                 :            : ///
      43                 :            : /// This defines an element of the NSAS lists.  All elements stored in these
      44                 :            : /// lists *MUST* be derived from this object.
      45                 :            : ///
      46                 :            : /// The class provides two properties:
      47                 :            : ///
      48                 :            : /// # The method hashKey(), which returns a hash key associated with the
      49                 :            : /// object.
      50                 :            : /// # Storage for a pointer into the LRU list, used to quickly locate the
      51                 :            : /// element when it is being "touched".
      52                 :            : ///
      53                 :            : /// Although it would be possible to require classes stored in the list
      54                 :            : /// to have particular methods (and so eliminate the inheritance), this
      55                 :            : /// would require the implementor to know something about the list and to
      56                 :            : /// provide the appropriate logic.
      57                 :            : ///
      58                 :            : /// Unfortunately, using a base class does not simplify the definition of
      59                 :            : /// the list classes (by allowing the list to be defined as a list
      60                 :            : /// of base class objects), as the lists are a list of shared pointers to
      61                 :            : /// objects, not a list of pointers to object.  Arguments are shared
      62                 :            : /// pointers, but a shared pointer to a base class is not a subclass of a
      63                 :            : /// shared pointer to a derived class.  For this reason, the type of element
      64                 :            : /// being stored is a template parameter.
      65                 :            : ///
      66                 :            : /// This class is inherited from boost::enable_shared_from_this class
      67                 :            : /// So within a member function a shared_ptr to current object can be obtained
      68                 :            : template <typename T>
      69                 :            : class NsasEntry : public boost::enable_shared_from_this <T> {
      70                 :            : public:
      71                 :            : 
      72                 :            :     /// \brief Default Constructor
      73                 :            :     ///
      74                 :            :     /// Ensures that the handle into the LRU list is invalid.
      75                 :       1341 :     NsasEntry() : valid_(false)
      76                 :            :     {}
      77                 :            : 
      78                 :            :     /// \brief Virtual Destructor
      79                 :          0 :     virtual ~NsasEntry()
      80                 :          0 :     {}
      81                 :            : 
      82                 :            :     /// Copy constructor and assignment operator OK for this class
      83                 :            : 
      84                 :            :     /// \brief Hash Key
      85                 :            :     ///
      86                 :            :     /// Returns the hash key for this element.
      87                 :            :     ///
      88                 :            :     /// TODO: Consider returning a reference to an internal object, for speed
      89                 :            :     virtual HashKey hashKey() const = 0;
      90                 :            : 
      91                 :            :     /// \brief Sets the iterator of the object
      92                 :            :     ///
      93                 :            :     /// Sets the iterator of an object and, as a side effect, marks it as valid.
      94                 :            :     ///
      95                 :            :     /// \param iterator Iterator of this element in the list
      96                 :       2363 :     virtual void setLruIterator(typename isc::util::LruList<T>::iterator iterator) {
      97                 :       2363 :         iterator_ = iterator;
      98                 :       2363 :         valid_ = true;
      99                 :       2363 :     }
     100                 :            : 
     101                 :            :     /// \brief Return Iterator
     102                 :            :     ///
     103                 :            :     /// \return iterator Iterator of this element in the list.
     104                 :            :     ///
     105                 :            :     /// \exception InvalidLruIterator Thrown if the iterator is not valid.
     106                 :       2154 :     virtual typename isc::util::LruList<T>::iterator getLruIterator() const {
     107 [ -  + ][ -  + ]:       2154 :         if (! valid_) {
                 [ -  + ]
     108 [ #  # ][ #  # ]:          0 :             isc_throw(InvalidLruIterator,
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     109                 :            :                 "pointer of element into LRU list was not valid");
     110                 :            :         }
     111                 :       2154 :         return iterator_;
     112                 :            :     }
     113                 :            : 
     114                 :            :     /// \brief Iterator Valid
     115                 :            :     ///
     116                 :            :     /// \return true if the stored iterator is valid.
     117                 :       2159 :     virtual bool iteratorValid() const {
     118                 :       2159 :         return valid_;
     119                 :            :     }
     120                 :            : 
     121                 :            :     /// \brief Invalidate Iterator
     122                 :            :     ///
     123                 :            :     /// Marks the iterator as invalid; it can oly be set valid again by a call
     124                 :            :     /// to setLruIterator.
     125                 :         27 :     virtual void invalidateIterator() {
     126                 :         27 :         valid_ = false;
     127                 :         27 :     }
     128                 :            : 
     129                 :            : private:
     130                 :            :     typename isc::util::LruList<T>::iterator  iterator_;  ///< Handle into the LRU List
     131                 :            :     bool                           valid_;     ///< true if handle is valid
     132                 :            : };
     133                 :            : 
     134                 :            : } // namespace nsas
     135                 :            : } // namespace isc
     136                 :            : 
     137                 :            : 
     138                 :            : #endif // __NSAS_ENTRY_H

Generated by: LCOV version 1.9