note
	description: "[
		To easily manage allocation and release of allocated C memory, and
		to perform insertion of basic elements. Byte order is by default
		platform specific.
		Although memory allocation routines do not accept a zero sized pointer
		MANAGED_POINTER does by allocating in fact a 1 byte sized pointer for
		this particular case.
	]"
	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 interface
	MANAGED_POINTER

create 
	make,
	make_from_array,
	make_from_pointer,
	share_from_pointer,
	own_from_pointer

feature -- Settings

	set_from_pointer (a_ptr: POINTER; n: INTEGER_32)
			-- Use directly a_ptr with count n to hold current data.
		require
			is_shared: is_shared
			a_ptr_not_null: a_ptr = default_pointer implies n = 0
			n_non_negative: n >= 0
		ensure
			item_set: item = a_ptr
			count_set: count = n
			is_shared_unchanged: is_shared
	
feature -- Access

	item: POINTER
		note
			option: transient

	count: INTEGER_32
			-- Number of elements that Current can hold.

	is_shared: BOOLEAN
			-- Is item shared with another memory area?
	
feature -- Comparison

	is_equal (other: like Current): BOOLEAN
			-- Is other attached to an object considered equal to current object?
	
feature -- Duplication

	copy (other: like Current)
			-- Update current object using fields of object attached
			-- to other, so as to yield equal objects. If is_shared
			-- and current is not large enough to hold other create
			-- a new pointer area and is_shared is set to False.
		ensure then
			sharing_status_not_preserved: (other /= Current) implies (old is_shared implies not is_shared)
			count_preserved: count = other.count
	
feature -- Access: Platform specific

	read_natural_8 (pos: INTEGER_32): NATURAL_8
			-- Read NATURAL_8 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_8_bytes) <= count

	read_natural_16 (pos: INTEGER_32): NATURAL_16
			-- Read NATURAL_16 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_16_bytes) <= count

	read_natural_32 (pos: INTEGER_32): NATURAL_32
			-- Read NATURAL_32 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_32_bytes) <= count

	read_natural_64 (pos: INTEGER_32): NATURAL_64
			-- Read NATURAL_64 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_64_bytes) <= count

	read_integer_8 (pos: INTEGER_32): INTEGER_8
			-- Read INTEGER_8 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_8_bytes) <= count

	read_integer_16 (pos: INTEGER_32): INTEGER_16
			-- Read INTEGER_16 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_16_bytes) <= count

	read_integer_32 (pos: INTEGER_32): INTEGER_32
			-- Read INTEGER at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_32_bytes) <= count

	read_integer_64 (pos: INTEGER_32): INTEGER_64
			-- Read INTEGER_64 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_64_bytes) <= count

	read_pointer (pos: INTEGER_32): POINTER
			-- Read POINTER at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + pointer_bytes) <= count

	read_boolean (pos: INTEGER_32): BOOLEAN
			-- Read BOOLEAN at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + boolean_bytes) <= count

	read_character (pos: INTEGER_32): CHARACTER_8
			-- Read CHARACTER at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Character_8_bytes) <= count

	read_real_32 (pos: INTEGER_32): REAL_32
			-- Read REAL_32 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Real_32_bytes) <= count

	read_real_64 (pos: INTEGER_32): REAL_64
			-- Read REAL_64 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Real_64_bytes) <= count

	read_array (pos, a_count: INTEGER_32): ARRAY [NATURAL_8]
			-- Read count bytes at position pos.
		require
			pos_nonnegative: pos >= 0
			count_positive: a_count > 0
			valid_position: (pos + a_count) <= count
		ensure
			read_array_not_void: Result /= Void
			read_array_valid_count: Result.count = a_count

	read_special_natural_8 (source_index, n: INTEGER_32): SPECIAL [NATURAL_8]
			-- Read n bytes of Current from position source_index.
		require
			source_index_non_negative: source_index >= 0
			n_non_negative: n >= 0
			n_is_small_enough_for_source: source_index + n <= count

	read_special_character_8 (source_index, n: INTEGER_32): SPECIAL [CHARACTER_8]
			-- Read n bytes of Current from position source_index.
		require
			source_index_non_negative: source_index >= 0
			n_non_negative: n >= 0
			n_is_small_enough_for_source: source_index + n <= count

	read_into_special_natural_8 (a_spec: SPECIAL [NATURAL_8]; source_index, destination_index, n: INTEGER_32)
			-- Read n bytes of Current from position source_index and store them in a_spec at destination_index.
		require
			a_spec_not_void: a_spec /= Void
			source_index_non_negative: source_index >= 0
			destination_index_non_negative: destination_index >= 0
			destination_index_in_bound: destination_index <= a_spec.count
			n_non_negative: n >= 0
			n_is_small_enough_for_source: source_index + n <= count
			n_is_small_enough_for_destination: destination_index + n <= a_spec.count

	read_into_special_character_8 (a_spec: SPECIAL [CHARACTER_8]; source_index, destination_index, n: INTEGER_32)
			-- Read n bytes of Current from position source_index and store them in a_spec at destination_index.
		require
			a_spec_not_void: a_spec /= Void
			source_index_non_negative: source_index >= 0
			destination_index_non_negative: destination_index >= 0
			destination_index_in_bound: destination_index <= a_spec.count
			n_non_negative: n >= 0
			n_is_small_enough_for_source: source_index + n <= count
			n_is_small_enough_for_destination: destination_index + n <= a_spec.count
	
feature -- Element change: Platform specific

	put_natural_8 (i: NATURAL_8; pos: INTEGER_32)
			-- Insert i at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_8_bytes) <= count
		ensure
			inserted: i = read_natural_8 (pos)

	put_natural_16 (i: NATURAL_16; pos: INTEGER_32)
			-- Insert i at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_16_bytes) <= count
		ensure
			inserted: i = read_natural_16 (pos)

	put_natural_32 (i: NATURAL_32; pos: INTEGER_32)
			-- Insert i at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_32_bytes) <= count
		ensure
			inserted: i = read_natural_32 (pos)

	put_natural_64 (i: NATURAL_64; pos: INTEGER_32)
			-- Insert i at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_64_bytes) <= count
		ensure
			inserted: i = read_natural_64 (pos)

	put_integer_8 (i: INTEGER_8; pos: INTEGER_32)
			-- Insert i at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_8_bytes) <= count
		ensure
			inserted: i = read_integer_8 (pos)

	put_integer_16 (i: INTEGER_16; pos: INTEGER_32)
			-- Insert i at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_16_bytes) <= count
		ensure
			inserted: i = read_integer_16 (pos)

	put_integer_32 (i: INTEGER_32; pos: INTEGER_32)
			-- Insert i at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_32_bytes) <= count
		ensure
			inserted: i = read_integer_32 (pos)

	put_integer_64 (i: INTEGER_64; pos: INTEGER_32)
			-- Insert i at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_64_bytes) <= count
		ensure
			inserted: i = read_integer_64 (pos)

	put_pointer (p: POINTER; pos: INTEGER_32)
			-- Insert p at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + pointer_bytes) <= count
		ensure
			inserted: p = read_pointer (pos)

	put_boolean (b: BOOLEAN; pos: INTEGER_32)
			-- Insert b at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + boolean_bytes) <= count
		ensure
			inserted: b = read_boolean (pos)

	put_character (c: CHARACTER_8; pos: INTEGER_32)
			-- Insert c at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Character_8_bytes) <= count
		ensure
			inserted: c = read_character (pos)

	put_real_32 (r: REAL_32; pos: INTEGER_32)
			-- Insert r at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Real_32_bytes) <= count
		ensure
			inserted: r = read_real_32 (pos)

	put_real_64 (d: REAL_64; pos: INTEGER_32)
			-- Insert d at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Real_64_bytes) <= count
		ensure
			inserted: d = read_real_64 (pos)

	put_array (data: ARRAY [NATURAL_8]; pos: INTEGER_32)
			-- Copy content of data into item at position pos.
		require
			data_not_void: data /= Void
			pos_nonnegative: pos >= 0
			valid_position: (pos + data.count) <= count
		ensure
			inserted: read_array (pos, data.count) ~ data

	put_special_natural_8 (a_spec: SPECIAL [NATURAL_8]; source_index, destination_index, n: INTEGER_32)
			-- Write n bytes of a_spec from position source_index to Current at position destination_index.
		require
			a_spec_not_void: a_spec /= Void
			source_index_non_negative: source_index >= 0
			destination_index_non_negative: destination_index >= 0
			destination_index_in_bound: destination_index <= count
			n_non_negative: n >= 0
			n_is_small_enough_for_source: source_index + n <= a_spec.count
			n_is_small_enough_for_destination: destination_index + n <= count
		ensure
			inserted: a_spec.same_items (read_special_natural_8 (destination_index, n), 0, source_index, n)

	put_special_character_8 (a_spec: SPECIAL [CHARACTER_8]; source_index, destination_index, n: INTEGER_32)
			-- Write n bytes of a_spec from position source_index to Current at position destination_index.
		require
			a_spec_not_void: a_spec /= Void
			source_index_non_negative: source_index >= 0
			destination_index_non_negative: destination_index >= 0
			destination_index_in_bound: destination_index <= count
			n_non_negative: n >= 0
			n_is_small_enough_for_source: source_index + n <= a_spec.count
			n_is_small_enough_for_destination: destination_index + n <= count
		ensure
			inserted: a_spec.same_items (read_special_character_8 (destination_index, n), 0, source_index, n)
	
feature -- Access: Little-endian format

	read_natural_8_le (pos: INTEGER_32): NATURAL_8
			-- Read NATURAL_8 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_8_bytes) <= count

	read_natural_16_le (pos: INTEGER_32): NATURAL_16
			-- Read NATURAL_16 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_16_bytes) <= count

	read_natural_32_le (pos: INTEGER_32): NATURAL_32
			-- Read NATURAL_32 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_32_bytes) <= count

	read_natural_64_le (pos: INTEGER_32): NATURAL_64
			-- Read NATURAL_64 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_64_bytes) <= count

	read_integer_8_le (pos: INTEGER_32): INTEGER_8
			-- Read INTEGER_8 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_8_bytes) <= count

	read_integer_16_le (pos: INTEGER_32): INTEGER_16
			-- Read INTEGER_16 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_16_bytes) <= count

	read_integer_32_le (pos: INTEGER_32): INTEGER_32
			-- Read INTEGER at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_32_bytes) <= count

	read_integer_64_le (pos: INTEGER_32): INTEGER_64
			-- Read INTEGER_64 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_64_bytes) <= count

	read_real_32_le (pos: INTEGER_32): REAL_32
			-- Read REAL_32 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Real_32_bytes) <= count

	read_real_64_le (pos: INTEGER_32): REAL_64
			-- Read REAL_64 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Real_64_bytes) <= count
	
feature -- Element change: Little-endian format

	put_natural_8_le (i: NATURAL_8; pos: INTEGER_32)
			-- Insert i at position pos in little-endian format.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_8_bytes) <= count
		ensure
			inserted: i = read_natural_8_le (pos)

	put_natural_16_le (i: NATURAL_16; pos: INTEGER_32)
			-- Insert i at position pos in little-endian format.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_16_bytes) <= count
		ensure
			inserted: i = read_natural_16_le (pos)

	put_natural_32_le (i: NATURAL_32; pos: INTEGER_32)
			-- Insert i at position pos in little-endian format.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_32_bytes) <= count
		ensure
			inserted: i = read_natural_32_le (pos)

	put_natural_64_le (i: NATURAL_64; pos: INTEGER_32)
			-- Insert i at position pos in little-endian format.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_64_bytes) <= count
		ensure
			inserted: i = read_natural_64_le (pos)

	put_integer_8_le (i: INTEGER_8; pos: INTEGER_32)
			-- Insert i at position pos in little-endian format.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_8_bytes) <= count
		ensure
			inserted: i = read_integer_8_le (pos)

	put_integer_16_le (i: INTEGER_16; pos: INTEGER_32)
			-- Insert i at position pos in little-endian format.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_16_bytes) <= count
		ensure
			inserted: i = read_integer_16_le (pos)

	put_integer_32_le (i: INTEGER_32; pos: INTEGER_32)
			-- Insert i at position pos in little-endian format.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_32_bytes) <= count
		ensure
			inserted: i = read_integer_32_le (pos)

	put_integer_64_le (i: INTEGER_64; pos: INTEGER_32)
			-- Insert i at position pos in little-endian format.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_64_bytes) <= count
		ensure
			inserted: i = read_integer_64_le (pos)

	put_real_32_le (v: REAL_32; pos: INTEGER_32)
			-- Insert v at position pos in little-endian format.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Real_32_bytes) <= count
		ensure
			inserted: v = read_real_32_le (pos)

	put_real_64_le (v: REAL_64; pos: INTEGER_32)
			-- Insert v at position pos in little-endian format.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Real_64_bytes) <= count
		ensure
			inserted: v = read_real_64_le (pos)
	
feature -- Access: Big-endian format

	read_natural_8_be (pos: INTEGER_32): NATURAL_8
			-- Read NATURAL_8 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_8_bytes) <= count

	read_natural_16_be (pos: INTEGER_32): NATURAL_16
			-- Read NATURAL_16 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_16_bytes) <= count

	read_natural_32_be (pos: INTEGER_32): NATURAL_32
			-- Read NATURAL_32 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_32_bytes) <= count

	read_natural_64_be (pos: INTEGER_32): NATURAL_64
			-- Read NATURAL_64 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_64_bytes) <= count

	read_integer_8_be (pos: INTEGER_32): INTEGER_8
			-- Read INTEGER_8 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_8_bytes) <= count

	read_integer_16_be (pos: INTEGER_32): INTEGER_16
			-- Read INTEGER_16 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_16_bytes) <= count

	read_integer_32_be (pos: INTEGER_32): INTEGER_32
			-- Read INTEGER at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_32_bytes) <= count

	read_integer_64_be (pos: INTEGER_32): INTEGER_64
			-- Read INTEGER_64 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_64_bytes) <= count

	read_real_32_be (pos: INTEGER_32): REAL_32
			-- Read REAL_32 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Real_32_bytes) <= count

	read_real_64_be (pos: INTEGER_32): REAL_64
			-- Read REAL_64 at position pos.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Real_64_bytes) <= count
	
feature -- Element change: Big-endian format

	put_natural_8_be (i: NATURAL_8; pos: INTEGER_32)
			-- Insert i at position pos in big-endian format.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_8_bytes) <= count
		ensure
			inserted: i = read_natural_8_be (pos)

	put_natural_16_be (i: NATURAL_16; pos: INTEGER_32)
			-- Insert i at position pos in big-endian format.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_16_bytes) <= count
		ensure
			inserted: i = read_natural_16_be (pos)

	put_natural_32_be (i: NATURAL_32; pos: INTEGER_32)
			-- Insert i at position pos in big-endian format.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_32_bytes) <= count
		ensure
			inserted: i = read_natural_32_be (pos)

	put_natural_64_be (i: NATURAL_64; pos: INTEGER_32)
			-- Insert i at position pos in big-endian format.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Natural_64_bytes) <= count
		ensure
			inserted: i = read_natural_64_be (pos)

	put_integer_8_be (i: INTEGER_8; pos: INTEGER_32)
			-- Insert i at position pos in big-endian format.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_8_bytes) <= count
		ensure
			inserted: i = read_integer_8_be (pos)

	put_integer_16_be (i: INTEGER_16; pos: INTEGER_32)
			-- Insert i at position pos in big-endian format.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_16_bytes) <= count
		ensure
			inserted: i = read_integer_16_be (pos)

	put_integer_32_be (i: INTEGER_32; pos: INTEGER_32)
			-- Insert i at position pos in big-endian format.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_32_bytes) <= count
		ensure
			inserted: i = read_integer_32_be (pos)

	put_integer_64_be (i: INTEGER_64; pos: INTEGER_32)
			-- Insert i at position pos in big-endian format.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Integer_64_bytes) <= count
		ensure
			inserted: i = read_integer_64_be (pos)

	put_real_32_be (v: REAL_32; pos: INTEGER_32)
			-- Insert v at position pos in big-endian format.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Real_32_bytes) <= count
		ensure
			inserted: v = read_real_32_be (pos)

	put_real_64_be (v: REAL_64; pos: INTEGER_32)
			-- Insert v at position pos in big-endian format.
		require
			pos_nonnegative: pos >= 0
			valid_position: (pos + Real_64_bytes) <= count
		ensure
			inserted: v = read_real_64_be (pos)
	
feature -- Concatenation

	append (other: like Current)
			-- Append other at the end of Current.
		require
			not_shared: not is_shared
			other_not_void: other /= Void
	
feature -- Resizing

	resize (n: INTEGER_32)
			-- Reallocate item to hold n bytes.
		require
			n_non_negative: n >= 0
			not_shared: not is_shared
	
feature -- Disposal

	dispose
			-- Release memory pointed by item.
		ensure then
			shared_reset: not is_shared
	
invariant
	item_not_null: item = default_pointer implies (count = 0 and is_shared)
	valid_count: count >= 0

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 MANAGED_POINTER

Generated by ISE EiffelStudio