comp.lang.ada
 help / color / mirror / Atom feed
* Novice help with types and de-allocation.
@ 2002-11-18  6:40 Stapler
  2002-11-18 11:05 ` Colin Paul Gloster
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Stapler @ 2002-11-18  6:40 UTC (permalink / raw)


Alright, I have a package which more or less works but I'm having trouble
in a couple spots. I've placed comments where I'm having trouble.


with Ada.Unchecked_Deallocation;
generic
	Size : Positive;
	
package int_buff is
	
	type Int_Buffer is limited private;
	type Buff_Ptr is limited private;
	type Buff_Out is array(Positive range <>) of Integer;
	
	procedure Insert( X : in Integer; Y : in out Int_Buffer);
	procedure Clear_Buff( Y : in out Int_Buffer; Z : in out Buff_Ptr);
	procedure Ditch_Buff( Y : in out Int_Buffer; Z : in out Buff_Ptr);
	
	function Read_Buff( Y : in Int_Buffer; Read_length : in Natural; Loc :in Natural) return Buff_Out;  
	function Sum_Buff( Y : in Int_Buffer) return Integer;
	
private

	Buff_Indexer : Positive range 1..Size;
	
	type Int_Buffer is array(1..Size) of Integer;
	pragma Pack( Int_Buffer );
	type Buff_Ptr is access Int_Buffer;
	procedure Free_Buff is new Ada.Unchecked_Deallocation(Int_Buffer, Buff_Ptr);
	
end int_buff;
	
package body int_buff is

	procedure Insert(X : in Integer; Y : in out Int_Buffer) is
	
	begin
		Buff_Indexer := X;
		Y(Buff_Indexer) := X;
		
	end Insert;
	
	procedure Clear_Buff( Y : in out Int_Buffer; Z : in out Buff_Ptr) is
		X : Int_Buffer;
	-- This procedure obviously needs more work.
	-- It's suppose to de-allocate the old buffer and create a new one.
	begin
		
		Free_Buff(Z);
		Y := X;
		
	end Clear_Buff;
	
	function Read_Buff(Y : in Int_Buffer; Read_Length : in Natural; Loc : in Natural) return Buff_Out is
	
		Ret_Buff : Buff_Out(1..Read_Length);
	begin
		-- For some reason, the compiler expects Y to be of type Buff_Out.
		-- Do I need to use Unchecked_Conversion here?
		Ret_Buff := Y(Loc .. Read_Length - 1);
	
		return Ret_Buff;
		
	end Read_Buff;
	
	function Sum_Buff( Y : in Int_Buffer) return Integer is
	
		Sum : Integer := 0;
		
	begin
	
		for I in Y'range loop
		
			Sum := Sum + Y(I);
			
		end loop;
		
		return Sum;
		
	end Sum_Buff;
	
	procedure Ditch_Buff(Y : in out Int_Buffer; Z : in out Buff_Ptr) is
	
	begin
	
		Free_Buff(Z);
		
	end Ditch_Buff;
	
end int_buff;
	
	
Try not to laugh to hard. I'm still a newb. Any pointers would be
appreciated.
		

Stapler
"Still tilting at WindMills."



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2002-11-18 23:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-18  6:40 Novice help with types and de-allocation Stapler
2002-11-18 11:05 ` Colin Paul Gloster
2002-11-18 15:29 ` Ted Dennison
2002-11-18 21:33   ` Stapler
2002-11-18 23:34   ` Stapler
2002-11-18 19:06 ` Jeffrey Carter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox