libgpr_24.0.0_d9c96bda/examples/ada_cpp/src2/animals.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
with Interfaces.C.Strings; use Interfaces.C.Strings;
with Animals_Interfaces; use Animals_Interfaces;
package Animals is

   type Animal is tagged limited record
      Age : Natural;
   end record;
   pragma Import (CPP, Animal);
   --  Note that we are not allowed to initialize the record components
   --  since this is reponsibility of the constructor and it is imported
   --  from C++

   procedure Set_Age (X : in out Animal; Age : Natural);
   pragma Import (CPP, Set_Age);

   function Age (X : Animal) return Natural;
   pragma Import (CPP, Age);

   function New_Animal return Animal;
   pragma CPP_Constructor (New_Animal);
   pragma Import (CPP, New_Animal, "_ZN6AnimalC2Ev");
   --  We must import the constructor from C++ since all the primitives
   --  are defined in C++ (and hence the C++ constructor is responsible
   --  of building the dispatch tables).

   --  -----------------------------------------------------------------------
   type Dog is new Animal and Carnivore and Domestic with record
      Tooth_Count : Natural;
      Owner       : String (1 .. 30);
   end record;
   pragma Import (CPP, Dog);

   function Number_Of_Teeth (A : Dog) return Natural;
   pragma Import (CPP, Number_Of_Teeth);

   procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
   pragma Import (CPP, Set_Owner);

   function New_Dog return Dog'Class;
   pragma CPP_Constructor (New_Dog);
   pragma Import (CPP, New_Dog, "_ZN3DogC2Ev");

   --  -----------------------------------------------------------------------
   --  Example of a type derivation defined in the Ada side that inherites
   --  all the dispatching primitives of the ancestor from the C++ side.

   type Vaccinated_Dog is new Dog with null record;
   function Vaccination_Expired (A : Vaccinated_Dog) return Boolean;
   pragma Convention (CPP, Vaccination_Expired);
end Animals;