note
	description: "Implementation of the STORABLE mechanism with streams."
	library: "Free implementation of ELKS library"
	status: "See notice at end of class."
	legal: "See notice at end of class."
	date: "$Date: 2020-05-19 14:29:33 +0000 (Tue, 19 May 2020) $"
	revision: "$Revision: 104259 $"

class 
	STREAM

inherit
	IO_MEDIUM

create 
	make,
	make_with_size

feature -- Initialization

	make
			-- Create stream object with a default_size of 100 bytes
		do
			make_with_size (200)
		end

	make_with_size (n: INTEGER_32)
			-- Create stream object with a default_size of n bytes
		do
			buffer_size := n
			create_c_buffer
			create last_string.make_empty
		end
	
feature -- Access

	item: POINTER
			-- Direct access to stored/retrieved data
		do
			Result := c_buffer (internal_buffer_access)
		end

	buffer: POINTER
		obsolete "Use `item' instead to directly access stored/retrieved data. [2017-05-31]"
			-- C buffer correspond to the Eiffel STREAM
		do
			Result := internal_buffer_access
		end

	buffer_size: INTEGER_32
			-- Buffer's size.

	object_stored_size: INTEGER_32
			-- Size of last stored object.

	create_c_buffer
			-- Create the C memory corresponding to the C buffer.
		do
			internal_buffer_access := c_malloc (buffer_size)
		end

	retrieved: ANY
			-- Retrieved object structure
			-- To access resulting object under correct type,
			-- use assignment attempt.
			-- Will raise an exception (code Retrieve_exception)
			-- if content is not a stored Eiffel structure.
		require else
				True
		local
			size: INTEGER_32
		do
			(create {MISMATCH_CORRECTOR}).Mismatch_information.do_nothing
			Result := c_retrieved (internal_buffer_access, buffer_size, 0, $size.to_pointer)
			object_stored_size := size
		end
	
feature -- Element change

	basic_store (object: ANY)
			-- Produce an external representation of the
			-- entire object structure reachable from object.
			-- Retrievable within current system only.
		local
			size: INTEGER_32
		do
			buffer_size := c_stream_basic_store (internal_buffer_access, buffer_size, $object.to_pointer, $size.to_pointer)
			object_stored_size := size
		end

	general_store (object: ANY)
			-- Produce an external representation of the
			-- entire object structure reachable from object.
			-- Retrievable from other systems for same platform
			-- (machine architecture).
		local
			size: INTEGER_32
		do
			buffer_size := c_stream_general_store (internal_buffer_access, buffer_size, $object.to_pointer, $size.to_pointer)
			object_stored_size := size
		end

	independent_store (object: ANY)
			-- Produce an external representation of the
			-- entire object structure reachable from object.
			-- Retrievable from other systems for the same or other
			-- platform (machine architecture).
		local
			size: INTEGER_32
		do
			buffer_size := c_stream_independent_store (internal_buffer_access, buffer_size, $object.to_pointer, $size.to_pointer)
			object_stored_size := size
		end

	set_additional_size (new_size: INTEGER_32)
			-- Set new_size to BUFFER_SIZE, internal value used to
			-- increment buffer_size during storable operations.
		external
			"C use %"eif_store.h%""
		alias
			"set_buffer_size"
		end
	
feature {NONE} -- Implementation

	internal_buffer_access: POINTER
			-- Access to C buffer pointed by item.

	c_buffer (a_buf: POINTER): POINTER
			-- Dereferenced pointer of a_buf
		require
			a_buf_not_null: a_buf /= default_pointer
		external
			"C inline"
		alias
			"return (*(EIF_POINTER *) $a_buf);"
		end

	c_stream_basic_store (stream_buffer: POINTER; stream_buffer_size: INTEGER_32; object: POINTER; c_real_size: POINTER): INTEGER_32
			-- Store object structure reachable form current object
			-- Return new size of internal_buffer_access.
		external
			"C signature (EIF_POINTER *, EIF_INTEGER, EIF_REFERENCE, EIF_INTEGER *): EIF_INTEGER use %"eif_store.h%""
		alias
			"stream_estore"
		end

	c_stream_general_store (stream_buffer: POINTER; stream_buffer_size: INTEGER_32; object: POINTER; c_real_size: POINTER): INTEGER_32
			-- Store object structure reachable form current object
			-- Return new size of internal_buffer_access.
		external
			"C signature (EIF_POINTER *, EIF_INTEGER, EIF_REFERENCE, EIF_INTEGER *): EIF_INTEGER use %"eif_store.h%""
		alias
			"stream_eestore"
		end

	c_stream_independent_store (stream_buffer: POINTER; stream_buffer_size: INTEGER_32; object: POINTER; c_real_size: POINTER): INTEGER_32
			-- Store object structure reachable form current object
			-- Return new size of internal_buffer_access.
		external
			"C signature (EIF_POINTER *, EIF_INTEGER, EIF_REFERENCE, EIF_INTEGER *): EIF_INTEGER use %"eif_store.h%""
		alias
			"stream_sstore"
		end

	c_retrieved (stream_buffer: POINTER; stream_buffer_size: INTEGER_32; stream_buffer_position: INTEGER_32; c_real_size: POINTER): ANY
			-- Object structured retrieved from stream of pointer
			-- stream_ptr
		external
			"C signature (EIF_POINTER *, EIF_INTEGER, EIF_INTEGER, EIF_INTEGER *): EIF_REFERENCE use %"eif_retrieve.h%""
		alias
			"stream_eretrieve"
		end

	c_malloc (size: INTEGER_32): POINTER
		external
			"C use %"eif_store.h%""
		alias
			"stream_malloc"
		end

	c_free (buf: POINTER)
		external
			"C signature (EIF_POINTER *) use %"eif_store.h%""
		alias
			"stream_free"
		end
	
feature -- Status report

	Support_storable: BOOLEAN = True
			-- Can medium be used to store an Eiffel structure?

	Exists: BOOLEAN = True
			-- Stream exists in any cases.

	Is_open_read: BOOLEAN = True
			-- Stream opens for input.

	Is_open_write: BOOLEAN = True
			-- Stream opens for output.

	Is_readable: BOOLEAN = True
			-- Is medium readable?

	is_executable: BOOLEAN
			-- Is stream executable?
		do
			Result := False
		end

	Is_writable: BOOLEAN = True
			-- Stream is writable.

	readable: BOOLEAN
			-- Is there a current item that may be read?
		do
		end

	extendible: BOOLEAN
			-- May new items be added?
		do
			Result := True
		end

	is_closed: BOOLEAN
			-- Is the I/O medium open
	
feature -- Status setting

	close
			-- Close medium.
		do
			is_closed := True
			c_free (internal_buffer_access)
			internal_buffer_access := default_pointer
		end
	
feature -- Output

	put_new_line
			-- Write a new line character to medium
			-- Was declared in STREAM as synonym of new_line.
		require else
			stream_exists: Exists
		do
			put_character ('%N')
		end

	new_line
			-- Write a new line character to medium
			-- Was declared in STREAM as synonym of put_new_line.
		require else
			stream_exists: Exists
		do
			put_character ('%N')
		end

	put_string (s: READABLE_STRING_8)
			-- Write s to medium.
			-- Was declared in STREAM as synonym of putstring.
		do
		end

	putstring (s: READABLE_STRING_8)
			-- Write s to medium.
			-- Was declared in STREAM as synonym of put_string.
		do
		end

	put_character (c: CHARACTER_8)
			-- Write c to medium.
			-- Was declared in STREAM as synonym of putchar.
		do
		end

	putchar (c: CHARACTER_8)
			-- Write c to medium.
			-- Was declared in STREAM as synonym of put_character.
		do
		end

	put_real (r: REAL_32)
			-- Write r to medium.
			-- Was declared in STREAM as synonym of putreal and put_real_32.
		do
		end

	putreal (r: REAL_32)
			-- Write r to medium.
			-- Was declared in STREAM as synonym of put_real and put_real_32.
		do
		end

	put_real_32 (r: REAL_32)
			-- Write r to medium.
			-- Was declared in STREAM as synonym of put_real and putreal.
		do
		end

	put_integer (i: INTEGER_32)
			-- Write i to medium.
			-- Was declared in STREAM as synonym of putint and put_integer_32.
		do
		end

	putint (i: INTEGER_32)
			-- Write i to medium.
			-- Was declared in STREAM as synonym of put_integer and put_integer_32.
		do
		end

	put_integer_32 (i: INTEGER_32)
			-- Write i to medium.
			-- Was declared in STREAM as synonym of put_integer and putint.
		do
		end

	put_integer_8 (i: INTEGER_8)
			-- Write i to medium.
		do
		end

	put_integer_16 (i: INTEGER_16)
			-- Write i to medium.
		do
		end

	put_integer_64 (i: INTEGER_64)
			-- Write i to medium.
		do
		end

	put_natural_8 (i: NATURAL_8)
			-- Write i to medium.
		do
		end

	put_natural_16 (i: NATURAL_16)
			-- Write i to medium.
		do
		end

	put_natural (i: NATURAL_32)
			-- Write i to medium.
			-- Was declared in STREAM as synonym of put_natural_32.
		do
		end

	put_natural_32 (i: NATURAL_32)
			-- Write i to medium.
			-- Was declared in STREAM as synonym of put_natural.
		do
		end

	put_natural_64 (i: NATURAL_64)
			-- Write i to medium.
		do
		end

	put_boolean (b: BOOLEAN)
			-- Write b to medium.
			-- Was declared in STREAM as synonym of putbool.
		do
		end

	putbool (b: BOOLEAN)
			-- Write b to medium.
			-- Was declared in STREAM as synonym of put_boolean.
		do
		end

	put_double (d: REAL_64)
			-- Write d to medium.
			-- Was declared in STREAM as synonym of putdouble and put_real_64.
		do
		end

	putdouble (d: REAL_64)
			-- Write d to medium.
			-- Was declared in STREAM as synonym of put_double and put_real_64.
		do
		end

	put_real_64 (d: REAL_64)
			-- Write d to medium.
			-- Was declared in STREAM as synonym of put_double and putdouble.
		do
		end

	put_managed_pointer (p: MANAGED_POINTER; start_pos, nb_bytes: INTEGER_32)
			-- Put data of length nb_bytes pointed by start_pos index in p at
			-- current position.
		do
		end
	
feature -- Input

	read_real
			-- Read a new real.
			-- Make result available in last_real.
			-- Was declared in STREAM as synonym of readreal and read_real_32.
		do
		end

	readreal
			-- Read a new real.
			-- Make result available in last_real.
			-- Was declared in STREAM as synonym of read_real and read_real_32.
		do
		end

	read_real_32
			-- Read a new real.
			-- Make result available in last_real.
			-- Was declared in STREAM as synonym of read_real and readreal.
		do
		end

	read_double
			-- Read a new double.
			-- Make result available in last_double.
			-- Was declared in STREAM as synonym of readdouble and read_real_64.
		do
		end

	readdouble
			-- Read a new double.
			-- Make result available in last_double.
			-- Was declared in STREAM as synonym of read_double and read_real_64.
		do
		end

	read_real_64
			-- Read a new double.
			-- Make result available in last_double.
			-- Was declared in STREAM as synonym of read_double and readdouble.
		do
		end

	read_character
			-- Read a new character.
			-- Make result available in last_character.
			-- Was declared in STREAM as synonym of readchar.
		do
		end

	readchar
			-- Read a new character.
			-- Make result available in last_character.
			-- Was declared in STREAM as synonym of read_character.
		do
		end

	read_integer
			-- Read a new integer.
			-- Make result available in last_integer.
			-- Was declared in STREAM as synonym of readint and read_integer_32.
		do
		end

	readint
			-- Read a new integer.
			-- Make result available in last_integer.
			-- Was declared in STREAM as synonym of read_integer and read_integer_32.
		do
		end

	read_integer_32
			-- Read a new integer.
			-- Make result available in last_integer.
			-- Was declared in STREAM as synonym of read_integer and readint.
		do
		end

	read_integer_8
			-- Read a new integer.
			-- Make result available in last_integer_8.
		do
		end

	read_integer_16
			-- Read a new integer.
			-- Make result available in last_integer_16.
		do
		end

	read_integer_64
			-- Read a new integer.
			-- Make result available in last_integer_64.
		do
		end

	read_natural_8
			-- Read a new natural.
			-- Make result available in last_natural_8.
		do
		end

	read_natural_16
			-- Read a new natural.
			-- Make result available in last_natural_16.
		do
		end

	read_natural
			-- Read a new natural.
			-- Make result available in last_natural.
			-- Was declared in STREAM as synonym of read_natural_32.
		do
		end

	read_natural_32
			-- Read a new natural.
			-- Make result available in last_natural.
			-- Was declared in STREAM as synonym of read_natural.
		do
		end

	read_natural_64
			-- Read a new natural.
			-- Make result available in last_natural_64.
		do
		end

	read_stream (nb_char: INTEGER_32)
			-- Read a string of at most nb_char bound characters
			-- or until end of medium is encountered.
			-- Make result available in last_string.
			-- Was declared in STREAM as synonym of readstream.
		do
		end

	readstream (nb_char: INTEGER_32)
			-- Read a string of at most nb_char bound characters
			-- or until end of medium is encountered.
			-- Make result available in last_string.
			-- Was declared in STREAM as synonym of read_stream.
		do
		end

	read_line
			-- Read characters until a new line or
			-- end of medium.
			-- Make result available in last_string.
			-- Was declared in STREAM as synonym of readline.
		do
		end

	readline
			-- Read characters until a new line or
			-- end of medium.
			-- Make result available in last_string.
			-- Was declared in STREAM as synonym of read_line.
		do
		end

	read_to_managed_pointer (p: MANAGED_POINTER; start_pos, nb_bytes: INTEGER_32)
			-- Read at most nb_bytes bound bytes and make result
			-- available in p at position start_pos.
		do
		end
	
feature -- Not exported

	name: detachable STRING_8
			-- Not meaningful
		do
		end

	handle: INTEGER_32
			-- Handle to medium
		do
		end

	handle_available: BOOLEAN
			-- Is the handle available after class has been
			-- created?
		do
		end
	
note
	copyright: "Copyright (c) 1984-2020, Eiffel Software and others"
	license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
	source: "[
		Eiffel Software
		5949 Hollister Ave., Goleta, CA 93117 USA
		Telephone 805-685-1006, Fax 805-685-6869
		Website http://www.eiffel.com
		Customer support http://support.eiffel.com
	]"

end -- class STREAM

Generated by ISE EiffelStudio