triplet.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright (C) 2012, 2014 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 TRIPLET_H
  15. #define TRIPLET_H
  16. #include <exceptions/exceptions.h>
  17. namespace isc {
  18. namespace dhcp {
  19. /// @brief This template specifies a parameter value
  20. ///
  21. /// This template class is used to store configuration parameters, like lifetime
  22. /// or T1. It defines 3 parameters: min, default, and max value. If the
  23. /// particular configuration parameter is not mandatory, it is possible to
  24. /// mark the parameter described by a @c Triplet "unspecified". For example, the
  25. /// T1 and T2 values in DHCPv4 server are optional and may be not specified
  26. /// in the configuration. The @c Triplets describing these parameters will be
  27. /// marked "unspecified". If the server finds that the particular parameter
  28. /// is unspecified it will not include it (e.g. option 58 or 59) in the message
  29. /// to a client.
  30. ///
  31. /// There are 3 constructors:
  32. /// - without parameters - marks the parameter "unspecified"
  33. /// - simple (just one value that sets all parameters)
  34. /// - extended (that sets default value and two thresholds)
  35. ///
  36. /// It will be used with integer types. It provides necessary operators, so
  37. /// it can be assigned to a plain integer or integer assigned to a Triplet.
  38. /// See TripletTest.operator test for details on an easy Triplet usage.
  39. template <class T>
  40. class Triplet {
  41. public:
  42. /// @brief Base type to Triplet conversion.
  43. ///
  44. /// Typically: uint32_t to Triplet assignment. It is very convenient
  45. /// to be able to simply write Triplet<uint32_t> x = 7;
  46. ///
  47. /// @param other A number to be assigned as min, max and default value.
  48. Triplet<T>& operator=(T other) {
  49. min_ = other;
  50. default_ = other;
  51. max_ = other;
  52. // The value is now specified because we just assigned one.
  53. unspecified_ = false;
  54. return (*this);
  55. }
  56. /// @brief Triplet to base type conversion
  57. ///
  58. /// Typically: Triplet to uint32_t assignment. It is very convenient
  59. /// to be able to simply write uint32_t z = x; (where x is a Triplet)
  60. operator T() const {
  61. return (default_);
  62. }
  63. /// @brief Constructor without parameters.
  64. ///
  65. /// Marks value in @c Triplet unspecified.
  66. Triplet()
  67. : min_(0), default_(0), max_(0),
  68. unspecified_(true) {
  69. }
  70. /// @brief Sets a fixed value.
  71. ///
  72. /// This constructor assigns a fixed (i.e. no range, just a single value)
  73. /// value.
  74. ///
  75. /// @param value A number to be assigned as min, max and default value.
  76. Triplet(T value)
  77. : min_(value), default_(value), max_(value),
  78. unspecified_(false) {
  79. }
  80. /// @brief Sets the default value and thresholds
  81. ///
  82. /// @throw BadValue if min <= def <= max rule is violated
  83. Triplet(T min, T def, T max)
  84. : min_(min), default_(def), max_(max),
  85. unspecified_(false) {
  86. if ( (min_ > def) || (def > max_) ) {
  87. isc_throw(BadValue, "Invalid triplet values.");
  88. }
  89. }
  90. /// @brief Returns a minimum allowed value
  91. T getMin() const { return (min_);}
  92. /// @brief Returns the default value
  93. T get() const { return (default_); }
  94. /// @brief Returns value with a hint
  95. ///
  96. /// DHCP protocol treats any values sent by a client as hints.
  97. /// This is a method that implements that. We can assign any value
  98. /// from configured range that client asks.
  99. ///
  100. /// @param hint A value being returned when if it is within the range
  101. /// between min and max value of @c Triplet. If the hint value is lower
  102. /// than min value, the min value is returned. if the hint is greater
  103. /// than max value, the max value is returned.
  104. ///
  105. /// @return A value adjusted to the hint.
  106. T get(T hint) const {
  107. if (hint <= min_) {
  108. return (min_);
  109. }
  110. if (hint >= max_) {
  111. return (max_);
  112. }
  113. return (hint);
  114. }
  115. /// @brief Returns a maximum allowed value
  116. T getMax() const { return (max_); }
  117. /// @brief Check if the value has been specified.
  118. ///
  119. /// @return true if the value hasn't been specified, or false otherwise.
  120. bool unspecified() const {
  121. return (unspecified_);
  122. }
  123. private:
  124. /// @brief the minimum value
  125. T min_;
  126. /// @brief the default value
  127. T default_;
  128. /// @brief the maximum value
  129. T max_;
  130. /// @brief Indicates whether the value is unspecified.
  131. bool unspecified_;
  132. };
  133. } // namespace isc::dhcp
  134. } // namespace isc
  135. #endif // TRIPLET_H