Ada: packaging concepts

This is a continuation of my previous post here:

Ada: Understanding Private Types and Understanding Packaging

The implementation for the type Rectangularwas performed using one implementation, i.e. Rectangular_Method_1, and for this implementation, a specification file and a body file were needed.

If we want another implementation to Rectangular_Method_2be available to the user, then the main file rectangular_Form.adscan be changed to

-- with Rectangular_Method_1;
-- package Rectangular_Form renames Rectangular_Method_1;
with Rectangular_Method_2;
package Rectangular_Form renames Rectangular_Method_2;

Questions

  • Is this correct in software development to allow another implementation that the test file test_rectangular_form.adbremains unchanged for another implementation?

  • If we create a second implementation Rectangular_Method_2, is it necessary to create a separate specification file in addition to the required new body for this new implementation? However, there is a need to provide the same procedures / functions for Vector_Basis_r, Set_Horz, Get_Horzetc. In the new implementation so that we can call them in test_rectangular_form.adb.

Thank...

+2
source share
2 answers

If you use GNAT, you can use GPR files for the project. There you can change the file name for specific packages, for example:

for Specification (Rectangular_Form) use "Rectangular_Method_1.ads";
for Implementation (Rectangular_Form) use "Rectangular_Method_1.adb";

you can even set this depending on the environment variable.

If your spec files should all look the same, you can only use Rectangular_Form.adsand use the implementation line at the top.

An example of a GPR file might look like this:

project Example is

   type Methods is ("normal", "something_else");
   Method : Methods := external ("METHOD", "normal");

   package Naming is
      case Method is
         when "normal" =>
            for Implementation ("Example") use "example_normal.adb";
         when "something_else" =>
            for Implementation ("Example") use "example_something.adb";
      end case;
   end Naming;

end Example;

gnatmake -P example.gpr METHOD -XMETHOD=... gnatmake .

example_*.adb Example, Example_Normal ..

+4

- .

package Rectangular is
   type Instance is abstract tagged private;

   procedure Vector_Basis_r (A : in Long_Float; D : out Instance);
   procedure Set_Horz (R : in out Instance; H : Long_Float);
   function Get_Horz (R : Instance) return Long_Float;
private
   type instance is tagged null record;
end Rectangular;

with Rectangular;
package Rectangular_Method_1 is
    type Instance is new Rectangular.Instance with private;
    ...
private
    type Instance is new Rectangular.Instance with 
    record
        Horz, Vert: Long_Float;
    end record;
end Rectangular_Method_1;

( Rectangular_Method_2).

, , :

with Rectangular_Method_1;
with Rectangular_Method_2;
...
--  My_Rectangle : Rectangular_Method_1.Instance;
My_Rectangle : Rectangular_Method_2.Instance;

My_Rectangle.Set_Horiz(Whatever_Value);
...

, , , . , .

subtype Rectangle_Instance is Rectangular_Method_2.Instance;

/​​ (), , , , .

+3
source

All Articles