PageRenderTime 24ms CodeModel.GetById 15ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

/src/lib/io/core/stream.e

http://github.com/tybor/Liberty
Specman e | 202 lines | 128 code | 21 blank | 53 comment | 4 complexity | f0f4ec4d36ed05e3e7a81b652896899f MD5 | raw file
  1-- This file is part of a Liberty Eiffel library.
  2-- See the full copyright at the end.
  3--
  4deferred class STREAM
  5   --
  6   -- A stream of characters.
  7   --
  8   -- There are two kinds of streams:
  9   -- + input streams (see INPUT_STREAM)
 10   -- + output_streams (see OUTPUT_STREAM)
 11   --
 12   -- Streams can:
 13   -- + be connected (e.g. to some system object)
 14   -- + be used to read or write characters, only if they are connected
 15   -- + be filtered (see FILTER)
 16   --
 17
 18insert
 19   RECYCLABLE
 20   DISPOSABLE
 21
 22feature {ANY}
 23   is_connected: BOOLEAN
 24         -- True if the stream is connected. Only in that case can data be transferred via this stream.
 25      deferred
 26      end
 27
 28   disconnect
 29         -- Try to disconnect the stream. Note that it *does not* ensure that the stream will effectively be
 30         -- disconnected (some terminal streams, for instance, are always connected) but the feature can be
 31         -- used to "shake off" filters.
 32      require
 33         is_connected
 34         can_disconnect
 35      deferred
 36      end
 37
 38   descriptor: INTEGER
 39         -- Some OS-dependent descriptor. Mainly used by the sequencer library (see READY_CONDITION).
 40      require
 41         is_connected
 42         has_descriptor
 43      do
 44         Result := filtered_descriptor
 45      end
 46
 47   has_descriptor: BOOLEAN
 48         -- True if that stream can be associated to some OS-meaningful descriptor.
 49      require
 50         is_connected
 51      do
 52         Result := filtered_has_descriptor
 53      end
 54
 55   can_disconnect: BOOLEAN
 56         -- True if the stream can be safely disconnected (without data loss, etc.)
 57      require
 58         is_connected
 59      deferred
 60      end
 61
 62   frozen url: URL
 63         -- The URL to this stream as resource
 64      do
 65         Result := url_memory
 66         if Result = Void then
 67            Result := new_url
 68            url_memory := Result
 69         end
 70      ensure
 71         not_void: Result /= Void
 72         always_the_same: Result = url
 73      end
 74
 75feature {}
 76   url_memory: URL
 77
 78   new_url: URL
 79      deferred
 80      ensure
 81         Result /= Void
 82      end
 83
 84feature {STREAM_HANDLER}
 85   stream_pointer: POINTER
 86         -- Some Back-end-dependent pointer (FILE* in C, InputStream or OutputStream in Java)
 87      require
 88         is_connected
 89         has_stream_pointer
 90      do
 91         Result := filtered_stream_pointer
 92      end
 93
 94   has_stream_pointer: BOOLEAN
 95         -- True if that stream can be associated to some Back-end-meaningful stream pointer.
 96      require
 97         is_connected
 98      do
 99         Result := filtered_has_stream_pointer
100      end
101
102feature {FILTER}
103   filtered_descriptor: INTEGER
104         -- Find the descriptor of the terminal stream... Filters do not have descriptors of their own
105      require
106         is_connected
107         filtered_has_descriptor
108      deferred
109      end
110
111   filtered_has_descriptor: BOOLEAN
112         -- True if the underlying terminal stream has a descriptor
113      require
114         is_connected
115      deferred
116      end
117
118   filtered_stream_pointer: POINTER
119         -- Find the pointer of the terminal stream... Filters do not have pointers of their own
120      require
121         is_connected
122         filtered_has_stream_pointer
123      deferred
124      end
125
126   filtered_has_stream_pointer: BOOLEAN
127         -- True if the underlying terminal stream has a pointer
128      require
129         is_connected
130      deferred
131      end
132
133feature {ANY}
134   event_exception: EVENT_DESCRIPTOR
135      do
136         Result := stream_exception
137         if Result = Void then
138            create stream_exception.make(Current)
139            Result := stream_exception
140         end
141      end
142
143feature {}
144   stream_exception: STREAM_EXCEPTION
145
146feature {RECYCLING_POOL}
147   recycle
148      do
149         if is_connected then
150            disconnect
151            check
152               -- Because the previous code is just here to catch
153               -- non-clean usage of STREAMs:
154               disconnect_file_after_use: False
155            end
156         end
157      end
158
159feature {}
160   dispose
161      do
162         if is_connected and then can_disconnect then
163            check
164               -- Because the previous code is just here to catch
165               -- non-clean usage of STREAMs:
166               disconnect_file_after_use: False
167            end
168            disconnect
169         end
170      end
171
172feature {}
173   sequencer_descriptor (file: POINTER): INTEGER
174      external "plug_in"
175      alias "{
176         location: "${sys}/plugins"
177         module_name: "sequencer"
178         feature_name: "sequencer_descriptor"
179         }"
180      end
181
182end -- class STREAM
183--
184-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
185--
186-- Permission is hereby granted, free of charge, to any person obtaining a copy
187-- of this software and associated documentation files (the "Software"), to deal
188-- in the Software without restriction, including without limitation the rights
189-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
190-- copies of the Software, and to permit persons to whom the Software is
191-- furnished to do so, subject to the following conditions:
192--
193-- The above copyright notice and this permission notice shall be included in
194-- all copies or substantial portions of the Software.
195--
196-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
197-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
198-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
199-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
200-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
201-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
202-- THE SOFTWARE.