simple_components_4.68.0_da9b0f3a/python-examples/class/py-class.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
--                                                                    --
--  package Py.Class                Copyright (c)  Dmitry A. Kazakov  --
--  Implementation                                 Luebeck            --
--                                                 Summer, 2022       --
--                                                                    --
--                                Last revision :  20:47 23 Jun 2022  --
--                                                                    --
--  This  library  is  free software; you can redistribute it and/or  --
--  modify it under the terms of the GNU General Public  License  as  --
--  published by the Free Software Foundation; either version  2  of  --
--  the License, 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 along with  --
--  this library; if not, write to  the  Free  Software  Foundation,  --
--  Inc., 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.       --
--____________________________________________________________________--

with System.Storage_Elements;  use System.Storage_Elements;

with Ada.Numerics.Elementary_Functions;
with Ada.Unchecked_Conversion;
with Ada.Unchecked_Deallocation;

package body Py.Class is

   function Get_X
            (  Self    : Object;
               Closure : System.Address
            )  return Object;
   pragma Convention (C, Get_X);

   function Get_Y
            (  Self    : Object;
               Closure : System.Address
            )  return Object;
   pragma Convention (C, Get_Y);

   function Length (Self : Object; Arg : Object) return Object;
   pragma Convention (C, Length);

   function New_Instance
            (  Class    : Object;
               Args     : Object;
               Keywords : Object
            )  return Object;
   pragma Convention (C, New_Instance);

   function Set_X
            (  Self  : Object;
               Value : Object;
               Closure : System.Address
            )  return int;
   pragma Convention (C, Set_X);

   function Set_Y
            (  Self  : Object;
               Value : Object;
               Closure : System.Address
            )  return int;
   pragma Convention (C, Set_Y);

   function Get (Object : Handle) return Point is
   begin
      Check_Handle (Object); -- Not checking the object type because we
      declare                -- do not keep it anywhere
         This : Point;
         pragma Import (Ada, This);
         for This'Address use System.Address (Object.Ptr)
                            + Storage_Offset (Object_HeadSize);
      begin
         return This;
      end;
   end Get;

   function Get_X
            (  Self    : Object;
               Closure : System.Address
            )  return Object is
      This : Point;
      pragma Import (Ada, This);
      for This'Address use System.Address (Self)
                         + Storage_Offset (Object_HeadSize);
   begin
      return Links.Float_FromDouble (double (This.X));
   exception
      when Python_Error =>
         return Null_Object;
      when others =>
         Throw_ValueError ("Invalid x");
         return Null_Object;
   end Get_X;

   function Get_Y
            (  Self    : Object;
               Closure : System.Address
            )  return Object is
      This : Point;
      pragma Import (Ada, This);
      for This'Address use System.Address (Self)
                         + Storage_Offset (Object_HeadSize);
   begin
      return Links.Float_FromDouble (double (This.Y));
   exception
      when Python_Error =>
         return Null_Object;
      when others =>
         Throw_ValueError ("Invalid y");
         return Null_Object;
   end Get_Y;

   function Length (Self : Object; Arg : Object) return Object is
      use Ada.Numerics.Elementary_Functions;
      This : Point;
      pragma Import (Ada, This);
      for This'Address use System.Address (Self)
                         + Storage_Offset (Object_HeadSize);
   begin
      return
         Links.Float_FromDouble (double (Sqrt (This.X**2 +This.Y**2 )));
   exception
      when Python_Error =>
         return Null_Object;
      when others =>
         Throw_ValueError ("Error in length");
         return Null_Object;
   end Length;

   New_List : constant Argument_List := - "x" - "y";

   function New_Instance
            (  Class    : Object;
               Args     : Object;
               Keywords : Object
            )  return Object is
      Result : Object;
   begin
      declare
         List : constant Object_Array :=
                         Parse (Args, Keywords, New_List);
         X, Y : Float := 0.0;
      begin
         if List (1) /= Null_Object then
            X := Float (Links.Float_AsDouble (List (1)));
            Check_Error;
         end if;
         if List (2) /= Null_Object then
            Y := Float (Links.Float_AsDouble (List (2)));
            Check_Error;
         end if;
         Result := -- Allocate the object
            Links.Type_GetSlot (Class, tp_alloc).tp_alloc (Class, 0);
         declare
            This : Point;
            pragma Import (Ada, This);
            for This'Address use System.Address (Result)
                               + Storage_Offset (Object_HeadSize);
         begin
            This.X := X;
            This.Y := Y;
         end;
      end;
      return Result;
   exception
      when Constraint_Error =>
         Throw_TypeError ("Argument is out of range");
         return Null_Object;
      when Python_Error =>
         return Null_Object;
   end New_Instance;

   procedure Set (Object : Handle; Value : Point) is
   begin
      Check_Handle (Object); -- Not checking the object type because we
      declare                -- do not keep it anywhere
         This : Point;
         pragma Import (Ada, This);
         for This'Address use System.Address (Object.Ptr)
                            + Storage_Offset (Object_HeadSize);
      begin
         This := Value;
      end;
   end Set;

   function Set_X
            (  Self  : Object;
               Value : Object;
               Closure : System.Address
            )  return int is
      This : Point;
      pragma Import (Ada, This);
      for This'Address use System.Address (Self)
                         + Storage_Offset (Object_HeadSize);
   begin
      This.X := Float (Links.Float_AsDouble (Value));
      return 0;
   exception
      when Python_Error =>
         return -1;
      when others =>
         Throw_TypeError ("Error setting x");
         return -1;
   end Set_X;

   function Set_Y
            (  Self  : Object;
               Value : Object;
               Closure : System.Address
            )  return int is
      This : Point;
      pragma Import (Ada, This);
      for This'Address use System.Address (Self)
                         + Storage_Offset (Object_HeadSize);
   begin
      This.Y := Float (Links.Float_AsDouble (Value));
      return 0;
   exception
      when Python_Error =>
         return -1;
      when others =>
         Throw_TypeError ("Error setting y");
         return -1;
   end Set_Y;

   GetSets : array (1..3) of aliased GetSetDef := -- Must be exist when
             (  (  Name    => New_String ("x"),   -- the type does
                   Get     => Get_X'Access,
                   Set     => Set_X'Access,
                   Doc     => New_String ("x-coordinate"),
                   Closure => System.Null_Address
                ),
                (  Name    => New_String ("y"),
                   Get     => Get_Y'Access,
                   Set     => Set_Y'Access,
                   Doc     => New_String ("y-coordinate"),
                   Closure => System.Null_Address
                ),
                End_GetSet
             );
   Methods : array (1..2) of aliased MethodDef :=
             (  (  Name  => New_String ("length"),
                   Meth  => (False, Length'Access),
                   Flags => METH_NOARGS,
                   Doc   => New_String ("vector length")
                ),
                End_Method
             );

   function Create_Point_Type return Handle is
      Result  : Handle;
      Module  : Handle;
      Doc     : aliased char_array := "point example" & NUL;
      Slots   : array (1..5) of aliased Type_Slot :=
         (  (tp_doc,     (tp_doc, To_Chars_Ptr (Doc'Unchecked_Access))),
            (tp_new,     (tp_new,     New_Instance'Access)),
            (tp_getset,  (tp_getset,  GetSets (1)'Unchecked_Access)),
            (tp_methods, (tp_methods, Methods (1)'Unchecked_Access)),
            End_Slot
         );
   begin
      Result.Ptr :=
         Links.Type_FromSpec
         (  (  Name       => New_String ("point"),
               Basic_Size =>
                  int (Object_HeadSize + (Point'Size + 7) / 8),
               Item_Size  => 0,
               Flags      => TPFLAGS_DEFAULT + TPFLAGS_HEAPTYPE,
               Slots      => Slots (Slots'First)'Unchecked_Access
         )  );
      if Result.Ptr = Null_Object then
         Check_Error;
      end if;
      Module := Import_AddModule ("point");
      Module_AddObject (Module, "point", Result);
      return Result;
   end Create_Point_Type;

end Py.Class;