ResourceLimitedContainerConfig.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2019 Proyectos y Sistemas de Mantenimiento SL (eProsima).
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @file ResourceLimitedContainerConfig.hpp
  16. *
  17. */
  18. #ifndef FASTRTPS_UTILS_COLLECTIONS_RESOURCELIMITEDCONTAINERCONFIG_HPP_
  19. #define FASTRTPS_UTILS_COLLECTIONS_RESOURCELIMITEDCONTAINERCONFIG_HPP_
  20. #include <cstddef>
  21. #include <limits>
  22. namespace eprosima {
  23. namespace fastrtps {
  24. /**
  25. * Specifies the configuration of a resource limited collection.
  26. * @ingroup UTILITIES_MODULE
  27. */
  28. struct ResourceLimitedContainerConfig
  29. {
  30. ResourceLimitedContainerConfig(
  31. size_t ini = 0,
  32. size_t max = std::numeric_limits<size_t>::max(),
  33. size_t inc = 1u)
  34. : initial(ini)
  35. , maximum(max)
  36. , increment(inc)
  37. {
  38. }
  39. //! Number of elements to be preallocated in the collection.
  40. size_t initial = 0;
  41. //! Maximum number of elements allowed in the collection.
  42. size_t maximum = std::numeric_limits<size_t>::max();
  43. //! Number of items to add when capacity limit is reached.
  44. size_t increment = 1u;
  45. /**
  46. * Return a resource limits configuration for a fixed size collection.
  47. * @param size Number of elements to allocate.
  48. * @return Resource limits configuration.
  49. */
  50. inline static ResourceLimitedContainerConfig fixed_size_configuration(size_t size)
  51. {
  52. return ResourceLimitedContainerConfig(size, size, 0u);
  53. }
  54. /**
  55. * Return a resource limits configuration for a linearly growing, dynamically allocated collection.
  56. * @param increment Number of new elements to allocate when increasing the capacity of the collection.
  57. * @return Resource limits configuration.
  58. */
  59. inline static ResourceLimitedContainerConfig dynamic_allocation_configuration(size_t increment = 1u)
  60. {
  61. return ResourceLimitedContainerConfig(0u, std::numeric_limits<size_t>::max(), increment ? increment : 1u);
  62. }
  63. };
  64. inline bool operator == (
  65. const ResourceLimitedContainerConfig& lhs,
  66. const ResourceLimitedContainerConfig& rhs)
  67. {
  68. return
  69. lhs.maximum == rhs.maximum &&
  70. lhs.initial == rhs.initial &&
  71. lhs.increment == rhs.increment;
  72. }
  73. } // namespace fastrtps
  74. } // namespace eprosima
  75. #endif /* FASTRTPS_UTILS_COLLECTIONS_RESOURCELIMITEDCONTAINERCONFIG_HPP_ */