PageRenderTime 26ms CodeModel.GetById 18ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lib/sequencer/loop_stack.e

http://github.com/tybor/Liberty
Specman e | 132 lines | 86 code | 12 blank | 34 comment | 6 complexity | dbccecb4d29bb771ac9d4ff4e29402e5 MD5 | raw file
  1-- This file is part of a Liberty Eiffel library.
  2-- See the full copyright at the end.
  3--
  4class LOOP_STACK
  5   -- Manage `LOOP_ITEM'. When a new loop is pushed, all jobs in the currently
  6   -- running loop are suspended until the new loop ends (all jobs end or break).
  7   --
  8
  9create {ANY}
 10   make
 11
 12feature {}
 13   loop_stack: FAST_ARRAY[LOOP_ITEM]
 14
 15   make
 16      do
 17         create loop_stack.make(0)
 18         loop_stack.add_last(create {LOOP_ITEM}.make)
 19      ensure
 20         current_loop /= Void
 21      end
 22
 23feature {ANY}
 24   stop: BOOLEAN
 25
 26   new_loop
 27         -- create new empty loop (ie without job) and push it on the stack
 28      local
 29         loop_item: LOOP_ITEM
 30      do
 31         if not loop_stack.is_empty then
 32            current_loop.pause_loop
 33         end
 34         create loop_item.make
 35         loop_stack.add_last(loop_item)
 36      end
 37
 38   push_loop (l: like current_loop)
 39         -- `l' is restarted and pushed on the stack
 40      require
 41         l /= Void
 42      do
 43         if not loop_stack.is_empty then
 44            current_loop.pause_loop
 45         end
 46         l.restart
 47         loop_stack.add_last(l)
 48      ensure
 49         current_loop = l
 50      end
 51
 52   run
 53         -- run `current_loop' (ie execute it's jobs)
 54      require
 55         current_loop /= Void
 56      do
 57         from
 58            stop := False
 59         until
 60            stop or else loop_stack.is_empty
 61         loop
 62            current_loop.run
 63            if current_loop /= Void then
 64               --may be Void after `break'
 65               if not current_loop.pause then
 66                  loop_stack.remove_last
 67               end
 68            end
 69         end
 70      ensure
 71         loop_stack.is_empty or stop
 72      end
 73
 74   add_job (j: JOB)
 75         -- Add a job to the current loop
 76      require
 77         j /= Void
 78      do
 79         current_loop.add_job(j)
 80      end
 81
 82   break
 83         -- Exit current loop
 84      require
 85         current_loop /= Void
 86      do
 87         current_loop.break_loop
 88         loop_stack.remove_last
 89      ensure
 90         current_loop /= old current_loop
 91      end
 92
 93   exit_all
 94         --TODO: Really needed feature ?
 95      require
 96         stop = False
 97      do
 98         stop := True
 99         current_loop.pause_loop
100      ensure
101         stop = True
102      end
103
104   current_loop: LOOP_ITEM
105         --TODO: change this function into an attribute to be more efficient
106      do
107         if not loop_stack.is_empty then
108            Result := loop_stack.last
109         end
110      end
111
112end -- class LOOP_STACK
113--
114-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
115--
116-- Permission is hereby granted, free of charge, to any person obtaining a copy
117-- of this software and associated documentation files (the "Software"), to deal
118-- in the Software without restriction, including without limitation the rights
119-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
120-- copies of the Software, and to permit persons to whom the Software is
121-- furnished to do so, subject to the following conditions:
122--
123-- The above copyright notice and this permission notice shall be included in
124-- all copies or substantial portions of the Software.
125--
126-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
127-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
128-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
129-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
130-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
131-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
132-- THE SOFTWARE.