nsas_entry.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // $Id$
  15. #ifndef __NSAS_ENTRY_H
  16. #define __NSAS_ENTRY_H
  17. #include <iostream>
  18. #include "exceptions/exceptions.h"
  19. #include "hash_key.h"
  20. #include "hash_table.h"
  21. #include "lru_list.h"
  22. namespace isc {
  23. namespace nsas {
  24. /// \brief Invalid Iterator
  25. ///
  26. /// Thrown if an attempt was made to access the iterator - the pointer into
  27. /// the LRU list where this element is located - when it is marked as invalid.
  28. class InvalidLruIterator : public isc::Exception {
  29. public:
  30. InvalidLruIterator(const char* file, size_t line, const char* what) :
  31. Exception(file, line, what)
  32. {}
  33. };
  34. /// \brief Element of NSAS Internal Lists
  35. ///
  36. /// This defines an element of the NSAS lists. All elements stored in these
  37. /// lists *MUST* be derived from this object.
  38. ///
  39. /// The class provides two properties:
  40. ///
  41. /// # The method hashKey(), which returns a hash key associated with the
  42. /// object.
  43. /// # Storage for a pointer into the LRU list, used to quickly locate the
  44. /// element when it is being "touched".
  45. ///
  46. /// Although it would be possible to require classes stored in the list
  47. /// to have particular methods (and so eliminate the inheritance), this
  48. /// would require the implementor to know something about the list and to
  49. /// provide the appropriate logic.
  50. ///
  51. /// Unfortunately, using a base class does not simplify the definition of
  52. /// the list classes (by allowing the list to be defined as a list
  53. /// of base class objects), as the lists are a list of shared pointers to
  54. /// objects, not a list of pointers to object. Arguments are shared
  55. /// pointers, but a shared pointer to a base class is not a subclass of a
  56. /// shared pointer to a derived class. For this reason, the type of element
  57. /// being stored is a template parameter.
  58. template <typename T>
  59. class NsasEntry {
  60. public:
  61. /// \brief Default Constructor
  62. ///
  63. /// Ensures that the handle into the LRU list is invalid.
  64. NsasEntry() : valid_(false)
  65. {}
  66. /// \brief Virtual Destructor
  67. virtual ~NsasEntry()
  68. {}
  69. /// Copy constructor and assignment operator OK for this class
  70. /// \brief Hash Key
  71. ///
  72. /// Returns the hash key for this element.
  73. ///
  74. /// TODO: Consider returning a reference to an internal object, for speed
  75. virtual HashKey hashKey() const = 0;
  76. /// \brief Sets the iterator of the object
  77. ///
  78. /// Sets the iterator of an object and, as a side effect, marks it as valid.
  79. ///
  80. /// \param iterator Iterator of this element in the list
  81. virtual void setLruIterator(typename LruList<T>::iterator iterator) {
  82. iterator_ = iterator;
  83. valid_ = true;
  84. }
  85. /// \brief Return Iterator
  86. ///
  87. /// \return iterator Iterator of this element in the list.
  88. ///
  89. /// \exception InvalidLruIterator Thrown if the iterator is not valid.
  90. virtual typename LruList<T>::iterator getLruIterator() const {
  91. if (! valid_) {
  92. isc_throw(InvalidLruIterator,
  93. "pointer of element into LRU list was not valid");
  94. }
  95. return iterator_;
  96. }
  97. /// \brief Iterator Valid
  98. ///
  99. /// \return true if the stored iterator is valid.
  100. virtual bool iteratorValid() const {
  101. return valid_;
  102. }
  103. /// \brief Invalidate Iterator
  104. ///
  105. /// Marks the iterator as invalid; it can oly be set valid again by a call
  106. /// to setLruIterator.
  107. virtual void invalidateIterator() {
  108. valid_ = false;
  109. }
  110. private:
  111. typename LruList<T>::iterator iterator_; ///< Handle into the LRU List
  112. bool valid_; ///< true if handle is valid
  113. };
  114. } // namespace nsas
  115. } // namespace isc
  116. #endif // __NSAS_ENTRY_H