Next: , Previous: Running the binding generator, Up: Generating Ada Bindings for C and C++ headers


3.11.4.2 Generating bindings for C++ headers

Generating bindings for C++ headers is done using the same options, always with the `g++' compiler. Note that generating Ada spec from C++ headers is a much more complex job and support for C++ headers is much more limited that support for C headers. As a result, you will need to modify the resulting bindings by hand more extensively when using C++ headers.

In this mode, C++ classes will be mapped to Ada tagged types, constructors will be mapped using the CPP_Constructor pragma, and when possible, multiple inheritance of abstract classes will be mapped to Ada interfaces (see the `Interfacing to C++' section in the GNAT Reference Manual for additional information on interfacing to C++).

For example, given the following C++ header file:

    class Carnivore {
    public:
       virtual int Number_Of_Teeth () = 0;
    };
    
    class Domestic {
    public:
       virtual void Set_Owner (char* Name) = 0;
    };
    
    class Animal {
    public:
      int Age_Count;
      virtual void Set_Age (int New_Age);
    };
    
    class Dog : Animal, Carnivore, Domestic {
     public:
      int  Tooth_Count;
      char *Owner;
    
      virtual int  Number_Of_Teeth ();
      virtual void Set_Owner (char* Name);
    
      Dog();
    };

The corresponding Ada code is generated:

    package Class_Carnivore is
      type Carnivore is limited interface;
      pragma Import (CPP, Carnivore);
    
      function Number_Of_Teeth (this : access Carnivore) return int is abstract;
    end;
    use Class_Carnivore;
    
    package Class_Domestic is
      type Domestic is limited interface;
      pragma Import (CPP, Domestic);
    
      procedure Set_Owner
        (this : access Domestic;
         Name : Interfaces.C.Strings.chars_ptr) is abstract;
    end;
    use Class_Domestic;
    
    package Class_Animal is
      type Animal is tagged limited record
        Age_Count : aliased int;
      end record;
      pragma Import (CPP, Animal);
    
      procedure Set_Age (this : access Animal; New_Age : int);
      pragma Import (CPP, Set_Age, "_ZN6Animal7Set_AgeEi");
    end;
    use Class_Animal;
    
    package Class_Dog is
      type Dog is new Animal and Carnivore and Domestic with record
        Tooth_Count : aliased int;
        Owner : Interfaces.C.Strings.chars_ptr;
      end record;
      pragma Import (CPP, Dog);
    
      function Number_Of_Teeth (this : access Dog) return int;
      pragma Import (CPP, Number_Of_Teeth, "_ZN3Dog15Number_Of_TeethEv");
    
      procedure Set_Owner
        (this : access Dog; Name : Interfaces.C.Strings.chars_ptr);
      pragma Import (CPP, Set_Owner, "_ZN3Dog9Set_OwnerEPc");
    
      function New_Dog return Dog;
      pragma CPP_Constructor (New_Dog);
      pragma Import (CPP, New_Dog, "_ZN3DogC1Ev");
    end;
    use Class_Dog;