PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/src/lib/storage/repository/repository_transient.e

http://github.com/tybor/Liberty
Specman e | 136 lines | 86 code | 11 blank | 39 comment | 1 complexity | e3e8f43a62327048a2469e37ccc22d11 MD5 | raw file
  1-- This file is part of a Liberty Eiffel library.
  2-- See the full copyright at the end.
  3--
  4expanded class REPOSITORY_TRANSIENT
  5   --
  6   -- Allows one to register transient objects (ones not to be saved by a REPOSITORY). Those objects are not
  7   -- committed (they are replaced by a tag called "transient reference" in the saved stream); nor are they
  8   -- restored (if a "transient reference" is found it is replaced with the registered object).
  9   --
 10   -- Usual usage:
 11   --   (create {REPOSITORY_TRANSIENT}).register(to_internals)
 12   --
 13   -- '''Caveat''': if you have cycles involving both transient and normal objects, those cycles won't be
 14   -- detected (at commit) and won't be restored (at update). In a nutshell, ''don't do that''.
 15   --
 16
 17insert
 18   INTERNALS_HANDLER
 19
 20feature {ANY}
 21   register (transient_object: INTERNALS; transient_reference: STRING)
 22         -- Register a `transient_object' with the given `transient_reference'.
 23      require
 24         not transient_object.type_is_expanded
 25         transient_object.object_as_pointer /= default_pointer
 26         not transient_reference.is_empty
 27         not has_object(transient_reference)
 28      local
 29         ref: STRING; t: REPOSITORY_TRANSIENT_OBJECT
 30      do
 31         ref := strings.new_twin(transient_reference)
 32         if transient_objects_pool.is_empty then
 33            create t.set(transient_object, ref)
 34         else
 35            t := transient_objects_pool.item
 36            t.set(transient_object, ref)
 37         end
 38         transient.put(ref, t)
 39      ensure
 40         has_object(transient_reference)
 41      end
 42
 43   unregister (transient_reference: STRING)
 44         -- Unregister the `transient_reference'.
 45      require
 46         has_object(transient_reference)
 47      local
 48         t: REPOSITORY_TRANSIENT_OBJECT
 49      do
 50         t := transient.key_at(transient_reference)
 51         check
 52            t /= Void
 53         end
 54         transient.remove(t)
 55         strings.recycle(t.key)
 56         transient_objects_pool.recycle(t)
 57      ensure
 58         not has_object(transient_reference)
 59      end
 60
 61   has_object (a_transient_reference: STRING): BOOLEAN
 62         -- True if the `transient_reference'" is registered.
 63      do
 64         Result := transient.has_value(a_transient_reference)
 65      end
 66
 67feature {REPOSITORY_IMPL}
 68   reference (a_object: INTERNALS): STRING
 69      require
 70         not a_object.type_is_expanded
 71         a_object.object_as_pointer /= default_pointer
 72      local
 73         dummy: like dummy_transient_object
 74      do
 75         dummy := dummy_transient_object
 76         dummy.set(a_object, once "")
 77         Result := transient.reference_at(dummy)
 78         dummy.reset
 79      ensure
 80         Result /= Void implies not Result.is_empty
 81         Result /= Void implies a_object.object_as_pointer = object(Result).object_as_pointer
 82      end
 83
 84feature {REPOSITORY_LAYOUT}
 85   object (a_reference: STRING): INTERNALS
 86      require
 87         not a_reference.is_empty
 88         has_object(a_reference)
 89      do
 90         Result := transient.key_at(a_reference).internals
 91      end
 92
 93feature {}
 94   transient: HASHED_BIJECTIVE_DICTIONARY[STRING, REPOSITORY_TRANSIENT_OBJECT]
 95         -- The key is the transient object, to speed up the most frequent operations which are `reference'
 96         -- and `object'.
 97      once
 98         create Result.with_capacity(2)
 99      end
100
101   transient_objects_pool: RECYCLING_POOL[REPOSITORY_TRANSIENT_OBJECT]
102      once
103         create Result.make
104      end
105
106   dummy_transient_object: REPOSITORY_TRANSIENT_OBJECT
107      once
108         create Result.make
109      end
110
111   strings: STRING_RECYCLING_POOL
112      once
113         create Result.make
114      end
115
116end -- class REPOSITORY_TRANSIENT
117--
118-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
119--
120-- Permission is hereby granted, free of charge, to any person obtaining a copy
121-- of this software and associated documentation files (the "Software"), to deal
122-- in the Software without restriction, including without limitation the rights
123-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
124-- copies of the Software, and to permit persons to whom the Software is
125-- furnished to do so, subject to the following conditions:
126--
127-- The above copyright notice and this permission notice shall be included in
128-- all copies or substantial portions of the Software.
129--
130-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
131-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
132-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
133-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
134-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
135-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
136-- THE SOFTWARE.