library_handle.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (C) 2013 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. #ifndef LIBRARY_HANDLE_H
  15. #define LIBRARY_HANDLE_H
  16. #include <string>
  17. namespace isc {
  18. namespace hooks {
  19. // Forward declarations
  20. class CalloutHandle;
  21. class CalloutManager;
  22. /// Typedef for a callout pointer. (Callouts must have "C" linkage.)
  23. extern "C" {
  24. typedef int (*CalloutPtr)(CalloutHandle&);
  25. };
  26. /// @brief Library handle
  27. ///
  28. /// This class is accessed by the user library when registering callouts,
  29. /// either by the library's load() function, or by one of the callouts
  30. /// themselves.
  31. ///
  32. /// It is really little more than a shell around the CalloutManager. By
  33. /// presenting this object to the user-library callouts, callouts can manage
  34. /// the callout list for their own library, but cannot affect the callouts
  35. /// registered by other libraries.
  36. ///
  37. /// (This restriction is achieved by the CalloutManager maintaining the concept
  38. /// of the "current library". When a callout is registered - either by the
  39. /// library's load() function, or by a callout in the library - the registration
  40. /// information includes the library active at the time. When that callout is
  41. /// called, the CalloutManager uses that information to set the "current
  42. /// library": the registration functions only operator on data whose
  43. /// associated library is equal to the "current library".)
  44. class LibraryHandle {
  45. public:
  46. /// @brief Constructor
  47. ///
  48. /// @param manager Back pointer to the containing CalloutManager.
  49. /// This pointer is used to access appropriate methods in that
  50. /// object. Note that the "raw" pointer is safe - the only
  51. /// instance of the LibraryHandle in the system is as a member of
  52. /// the CalloutManager to which it points.
  53. ///
  54. /// @param index Index of the library to which the LibraryHandle applies.
  55. /// If negative, the library index as set in the CalloutManager is
  56. /// used. Otherwise the current library index is saved, this value
  57. /// is set as the current index, the registration call is made, and
  58. /// the CalloutManager's library index is reset. Note: although -1
  59. /// is a valid argument value for
  60. /// isc::hooks::CalloutManager::setLibraryIndex(), in this class is
  61. /// is used as a sentinel to indicate that the library index in
  62. /// isc::util::CalloutManager should not be set or reset.
  63. LibraryHandle(CalloutManager* manager, int index = -1)
  64. : callout_manager_(manager), index_(index)
  65. {}
  66. /// @brief Register a callout on a hook
  67. ///
  68. /// Registers a callout function with a given hook. The callout is added
  69. /// to the end of the callouts for the current library that are associated
  70. /// with that hook.
  71. ///
  72. /// @param name Name of the hook to which the callout is added.
  73. /// @param callout Pointer to the callout function to be registered.
  74. ///
  75. /// @throw NoSuchHook The hook name is unrecognised.
  76. /// @throw Unexpected The hook name is valid but an internal data structure
  77. /// is of the wrong size.
  78. void registerCallout(const std::string& name, CalloutPtr callout);
  79. /// @brief De-Register a callout on a hook
  80. ///
  81. /// Searches through the functions registered by the current library with
  82. /// the named hook and removes all entries matching the callout. It does
  83. /// not affect callouts registered by other libraries.
  84. ///
  85. /// @param name Name of the hook from which the callout is removed.
  86. /// @param callout Pointer to the callout function to be removed.
  87. ///
  88. /// @return true if a one or more callouts were deregistered.
  89. ///
  90. /// @throw NoSuchHook The hook name is unrecognised.
  91. /// @throw Unexpected The hook name is valid but an internal data structure
  92. /// is of the wrong size.
  93. bool deregisterCallout(const std::string& name, CalloutPtr callout);
  94. /// @brief Removes all callouts on a hook
  95. ///
  96. /// Removes all callouts associated with a given hook that were registered.
  97. /// by the current library. It does not affect callouts that were
  98. /// registered by other libraries.
  99. ///
  100. /// @param name Name of the hook from which the callouts are removed.
  101. ///
  102. /// @return true if one or more callouts were deregistered.
  103. ///
  104. /// @throw NoSuchHook Thrown if the hook name is unrecognised.
  105. bool deregisterAllCallouts(const std::string& name);
  106. private:
  107. /// @brief Copy constructor
  108. ///
  109. /// Private (with no implementation) as it makes no sense to copy an object
  110. /// of this type. All code receives a reference to an existing handle which
  111. /// is tied to a particular CalloutManager. Creating a copy of that handle
  112. /// runs the risk of a "dangling pointer" to the original handle's callout
  113. /// manager.
  114. ///
  115. /// @param Unused - should be the object to copy.
  116. LibraryHandle(const LibraryHandle&);
  117. /// @brief Assignment operator
  118. ///
  119. /// Declared private like the copy constructor for the same reasons. It too
  120. /// has no implementation.
  121. ///
  122. /// @param Unused - should be the object to copy.
  123. LibraryHandle& operator=(const LibraryHandle&);
  124. /// Back pointer to the collection object for the library
  125. CalloutManager* callout_manager_;
  126. /// Library index to which this handle applies. -1 indicates that it
  127. /// applies to whatever index is current in the CalloutManager.
  128. int index_;
  129. };
  130. } // namespace util
  131. } // namespace isc
  132. #endif // LIBRARY_HANDLE_H