/src/lib/storage/low_level/string_recycling_item.e

http://github.com/tybor/Liberty · Specman e · 100 lines · 57 code · 10 blank · 33 comment · 5 complexity · e5877d6fe24729f23f1f4b66e33072c7 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. expanded class STRING_RECYCLING_ITEM
  5. --
  6. -- Used by STRING_RECYCLING_POOL to track STRING capacities and to allow the use of a sorted array with
  7. -- "holes" (since STRING's are WEAK_REFERENCE'ed)
  8. --
  9. insert
  10. ANY
  11. redefine
  12. is_equal, copy
  13. end
  14. feature {ANY}
  15. is_equal (other: like Current): BOOLEAN
  16. do
  17. Result := capacity = other.capacity
  18. end
  19. copy (other: like Current)
  20. do
  21. set_item(Void)
  22. set_capacity(other.capacity)
  23. end
  24. feature {STRING_RECYCLING_POOL, STRING_RECYCLING_ITEM, STRING_RECYCLING_ITEM_SORTER}
  25. capacity: INTEGER
  26. item: STRING
  27. -- The STRING item. May become Void if the GC decides so.
  28. do
  29. if item_memory /= Void then
  30. Result := item_memory.item
  31. end
  32. end
  33. set_item (a_item: like item)
  34. -- Stores the STRING as being reusable. The GC may remove it afterwards.
  35. require
  36. a_item = Void or else a_item.is_empty
  37. do
  38. if a_item = Void then
  39. if item_memory /= Void then
  40. item_memory.set_item(Void)
  41. end
  42. else
  43. capacity := a_item.capacity
  44. if item_memory = Void then
  45. create item_memory.set_item(a_item)
  46. else
  47. item_memory.set_item(a_item)
  48. end
  49. end
  50. ensure
  51. item = a_item
  52. end
  53. set_capacity (a_capacity: like capacity)
  54. -- Sets the capacity. Useful for array sorting and element comparison. Should not be used when a
  55. -- STRING is stored.
  56. require
  57. a_capacity >= 0
  58. item = Void
  59. do
  60. capacity := a_capacity
  61. ensure
  62. capacity = a_capacity
  63. end
  64. feature {}
  65. item_memory: WEAK_REFERENCE[STRING]
  66. -- Holds a recyclable STRING
  67. invariant
  68. --| **** item /= Void implies item.capacity = capacity
  69. capacity >= 0
  70. end -- class STRING_RECYCLING_ITEM
  71. --
  72. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  73. --
  74. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  75. -- of this software and associated documentation files (the "Software"), to deal
  76. -- in the Software without restriction, including without limitation the rights
  77. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  78. -- copies of the Software, and to permit persons to whom the Software is
  79. -- furnished to do so, subject to the following conditions:
  80. --
  81. -- The above copyright notice and this permission notice shall be included in
  82. -- all copies or substantial portions of the Software.
  83. --
  84. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  85. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  86. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  87. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  88. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  89. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  90. -- THE SOFTWARE.