triplet.h 4.6 KB

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