stephes_ada_library_3.7.3_08b48307/source/sal-memory_streams.ads

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
--  Abstract:
--
--  A memory stream type, for reading objects from some communication
--  channel.
--
--  Copyright (C) 2005, 2009, 2018 Stephen Leake.  All Rights Reserved.
--
--  This library is free software; you can redistribute it and/or
--  modify it under terms of the GNU General Public License as
--  published by the Free Software Foundation; either version 3, or (at
--  your option) any later version. This library is distributed in the
--  hope that it will be useful, but WITHOUT ANY WARRANTY; without even
--  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
--  PURPOSE. See the GNU General Public License for more details. You
--  should have received a copy of the GNU General Public License
--  distributed with this program; see file COPYING. If not, write to
--  the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
--  MA 02111-1307, USA.
--
--  As a special exception, if other files instantiate generics from
--  this unit, or you link this unit with other files to produce an
--  executable, this  unit  does not  by itself cause  the resulting
--  executable to be covered by the GNU General Public License. This
--  exception does not however invalidate any other reasons why the
--  executable file  might be covered by the  GNU Public License.

pragma License (Modified_GPL);

with Ada.Streams;
with System;
package SAL.Memory_Streams is

   type Stream_Type (Max_Length : Ada.Streams.Stream_Element_Count) is
     new Ada.Streams.Root_Stream_Type with private;

   procedure Create (Stream : in out Stream_Type);
   --  create an empty Stream with direction Out_Stream, for writing.

   procedure Create
     (Stream : in out Stream_Type;
      Data   : in     Ada.Streams.Stream_Element_Array);
   --  create a Stream with data, with direction In_Stream, for reading.
   --  raises Constraint_Error if Data overflows Stream

   procedure Create
     (Stream  : in out Stream_Type;
      Address : in     System.Address);
   --  create a Stream with data from Address, copying
   --  Stream.Max_Length bytes, with direction In_Stream, for reading.

   procedure Reset (Stream : in out Stream_Type);
   --  Reset to start of stream, for reading again.

   function Length (Stream : in Stream_Type) return Ada.Streams.Stream_Element_Count;
   --  for an In_Stream, the amount of data left to be read.
   --  for an Out_Stream, the amount of data written.

   function Address (Stream : in Stream_Type) return System.Address;
   --  for an In_Stream, raises Status_Error.
   --  for an Out_Stream, the address of the first element of the raw
   --  Stream, for passing to system routines.

   overriding procedure Read
     (Stream : in out Stream_Type;
      Item   :    out Ada.Streams.Stream_Element_Array;
      Last   :    out Ada.Streams.Stream_Element_Offset);
   --  for an In_Stream, reads elements from Stream, storing them in
   --  Item. Stops when Item'Last or end of Stream is reached, setting
   --  Last to last element of Item written.
   --
   --  for an Out_Stream, raises Status_Error.

   overriding procedure Write
     (Stream : in out Stream_Type;
      Item   : in     Ada.Streams.Stream_Element_Array);
   --  for an In_Stream, raises Status_Error.
   --
   --  for an Out_Stream, writes elements from Item to the Stream,
   --  stopping when Item'last is reached. Raises End_Error if attempt
   --  to write past end of Stream.

private

   type Direction_Type is (In_Stream, Out_Stream);

   type Stream_Type (Max_Length : Ada.Streams.Stream_Element_Count) is
     new Ada.Streams.Root_Stream_Type with
   record
      --  Direction is not a discriminant, because we anticipate
      --  changing direction on some streams.
      Direction : Direction_Type;
      Last      : Ada.Streams.Stream_Element_Offset := 0; -- last element of Raw that has been read/written
      Raw       : Ada.Streams.Stream_Element_Array (1 .. Max_Length);
   end record;

end SAL.Memory_Streams;