PageRenderTime 26ms CodeModel.GetById 21ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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