zeromq_ada_4.1.5_b2a857f7/src/zmq-messages.adb

  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
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
-------------------------------------------------------------------------------
--                                                                           --
--                             0MQ Ada-binding                               --
--                                                                           --
--                          Z M Q . M E S S A G E S                          --
--                                                                           --
--                                  B o d y                                  --
--                                                                           --
--            Copyright (C) 2020-2030, per.s.sandberg@bahnhof.se             --
--                                                                           --
--  Permission is hereby granted, free of charge, to any person obtaining a  --
--  copy of this software and associated documentation files                 --
--  (the "Software"), to deal in the Software without restriction, including --
--  without limitation the rights to use, copy, modify, merge, publish,      --
--  distribute, sublicense, and / or sell copies of the Software, and to     --
--  permit persons to whom the Software is furnished to do so, subject to    --
--  the following conditions :                                               --
--                                                                           --
--  The above copyright notice and this permission notice shall be included  --
--  in all copies or substantial portions of the Software.                   --
--                                                                           --
--  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  --
--  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               --
--  MERCHANTABILITY,                                                         --
--  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL  --
--  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR     --
--  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,    --
--  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR    --
--  OTHER DEALINGS IN THE SOFTWARE.                                          --
-------------------------------------------------------------------------------


with Interfaces.C;
with GNAT.OS_Lib;
with GNAT.Source_Info;
with Ada.Unchecked_Conversion;
with System.Address_To_Access_Conversions;
package body ZMQ.Messages is
   use Interfaces.C;


   --  ========================================================================
   --  Initialize with size
   --  ========================================================================

   procedure Initialize (Self : in out Message; Size : Natural) is
      Ret : int;
   begin
      if Size > 0 then
         Ret := Low_Level.zmq_msg_init_size (Self.Msg'Access, size_t (Size));
      else
         Ret := Low_Level.zmq_msg_init (Self.Msg'Access);
      end if;
      if Ret /= 0 then
         raise ZMQ_Error with Error_Message (GNAT.OS_Lib.Errno) & " in " &
           GNAT.Source_Info.Enclosing_Entity;
      end if;
   end Initialize;

   --  ========================================================================
   --  Initialize with data
   --  ========================================================================
   procedure Initialize (Self : in out Message;
                         Data : System.Address;
                         Size : Natural) is
      type Data_Type is new String (1 .. Size);
      package Conv is new System.Address_To_Access_Conversions (Data_Type);
   begin
      Self.Initialize (Size);
      Conv.To_Pointer ((Self.GetData)).all := Conv.To_Pointer (Data).all;
   end Initialize;

   procedure Initialize (Self : in out Message; Data : String) is
   begin
      Self.Initialize (Data'Address, Data'Length);
   end Initialize;

   procedure Initialize
     (Self : in out Message;
      Data : Ada.Streams.Stream_Element_Array) is
   begin
      Self.Initialize (Data'Address, Data'Length);
   end Initialize;

   --  ========================================================================
   --  Initialize with data ref and free
   --  ========================================================================
   procedure Initialize
     (Self    : in out Message;
      Message : System.Address;
      Size    : Natural;
      Free    : Free_Proc;
      Hint    : System.Address := System.Null_Address)
   is
      Ret : int;
   begin
      Ret := Low_Level.zmq_msg_init_data (Self.Msg'Access,
                                          Message,
                                          size_t (Size),
                                          Free,
                                          Hint);
      if Ret /= 0 then
         raise ZMQ_Error with Error_Message (GNAT.OS_Lib.Errno) & " in " &
           GNAT.Source_Info.Enclosing_Entity;
      end if;
   end Initialize;


   ------------------------
   -- Initialize_Generic --
   ------------------------

   procedure Initialize_Generic
     (Self   : in out Message;
      Data   : Element_Access)
   is
      function Conv is new
        Ada.Unchecked_Conversion (Source => System.Address,
                                  Target => Element_Access);
      procedure Internal_Free (Data : System.Address; Hint : System.Address);
      procedure Internal_Free (Data : System.Address; Hint : System.Address) is
         pragma Unreferenced (Hint);
         Temp  : Element_Access := Conv (Data);
      begin
         Free (Temp);
      end Internal_Free;
   begin
      Self.Initialize (Data.all'Address,
                       Data.all'Size / 8,
                       Internal_Free'Unrestricted_Access);
   end Initialize_Generic;

   ----------------------------------
   -- Initialize_Generic_With_Hint --
   ----------------------------------

   procedure Initialize_Generic_With_Hint
     (Self  : in out Message;
      Data  : Element_Access;
      Hint  : Hint_Access)
   is
      function ConvHint is new
        Ada.Unchecked_Conversion (Source => System.Address,
                                  Target => Hint_Access);
      function Conv is new
        Ada.Unchecked_Conversion (Source => System.Address,
                                  Target => Element_Access);

      procedure Internal_Free (Data : System.Address; Hint : System.Address);
      procedure Internal_Free (Data : System.Address; Hint : System.Address) is
         Temp_Data  : Element_Access := Conv (Data);
         Temp_Hint  : constant Hint_Access := ConvHint (Hint);
      begin
         Free (Temp_Data, Temp_Hint);
      end Internal_Free;
   begin
      Self.Initialize (Data.all'Address,
                       Data.all'Size / 8,
                       Internal_Free'Unrestricted_Access,
                       Hint.all'Address);
   end Initialize_Generic_With_Hint;


   --  ========================================================================
   --  GetData / GetSize
   --  ========================================================================

   ---------------
   --  getData  --
   ---------------
   function GetData (Self : Message) return System.Address is
   begin
      return Low_Level.zmq_msg_data (Self.Msg'Unrestricted_Access);
   end GetData;


   ---------------
   --  getData  --
   ---------------
   function GetData (Self : Message) return String is
      type Data_Type is new String (1 .. Self.GetSize);
      package Conv is new System.Address_To_Access_Conversions (Data_Type);
   begin
      return String (Conv.To_Pointer (Self.GetData).all);
   end GetData;

   ---------------
   --  getData  --
   ---------------
   function  GetData  (Self : Message)
                       return Ada.Streams.Stream_Element_Array is
      type Data_Type is new Ada.Streams.Stream_Element_Array
        (1 .. Ada.Streams.Stream_Element_Offset (Self.GetSize));
      package Conv is new System.Address_To_Access_Conversions (Data_Type);
   begin
      return Ada.Streams.Stream_Element_Array
        (Conv.To_Pointer (Self.GetData).all);
   end GetData;

   ---------------
   --  getData  --
   ---------------
   function  GetData  (Self : Message)
                       return Ada.Strings.Unbounded.Unbounded_String is
      type Data_Type is new String (1 .. Self.GetSize);
      package Conv is new System.Address_To_Access_Conversions (Data_Type);
   begin
      return Ada.Strings.Unbounded.To_Unbounded_String
        (String (Conv.To_Pointer (Self.GetData).all));
   end GetData;

   -----------------------
   --  getData_Generic  --
   -----------------------
   function  GetData_Generic  (Self : Message)
                               return Element is
      package Conv is new System.Address_To_Access_Conversions (Element);
   begin
      return Conv.To_Pointer (Self.GetData).all;
   end GetData_Generic;


   ---------------
   --  getSize  --
   ---------------
   function GetSize (Self : Message) return Natural is
   begin
      return Natural (Low_Level.zmq_msg_size (Self.Msg'Unrestricted_Access));
   end GetSize;


   --------------
   -- Finalize --
   --------------

   procedure Finalize (Self : in out Message) is
      Ret : int;
   begin
      Ret := Low_Level.zmq_msg_close (Self.Msg'Access);
      if Ret /= 0 then
         raise ZMQ_Error with Error_Message (GNAT.OS_Lib.Errno) & " in " &
           GNAT.Source_Info.Enclosing_Entity;
      end if;
   end Finalize;

   -------------
   -- getImpl --
   -------------

   function GetImpl (Self : Message) return not null Zmq_Msg_T_Access is
   begin
      return Self.Msg'Unrestricted_Access;
   end GetImpl;

   procedure Process_Data_Generic
     (Self   : Message;
      Handle : access procedure (Item : Element)) is
      package Conv is new System.Address_To_Access_Conversions (Element);
   begin
      Handle (Conv.To_Pointer (Self.GetData).all);
   end Process_Data_Generic;

end ZMQ.Messages;