Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class formulation concept from C++ and Java to Ada

Maybe the C++ and Java people can help me to define this problem I'm going to explain. I have a problem in Ada (you don't need to know it, I'm just interested in the concept) on how representing a Constructor of a class which implements three main branches of dynamic identifiers:

  • Pure number values (int, float, String, whatever)
  • List/stack item
  • Something what in C++ is likely a thread (in Ada we have a more wide concept of this, related to a task, but we can concept a simple task as a thread, so the concept applies too)

I'm gonna call this class Par_Class, and be any constructed object call Par_Obj. Thus, when an object Par_Obj is created (so, the number values are initialized, the lists/stacks have other lists/stacks allocated or null and the memory range for the thread execution is reserved), the OS automatically starts the execution of the new thread in parallel with my main application (and now they contend for system resources). But to simplify the example, let's suppose I'd have a class with an integer and a pointer to a string.

In C++, I could code, for example, (please correct me if I'm doing wrong)

class Par_Class {
public:
  Par_Class (int aValue, const std::string & aName);

private:
  int theValue;
  std::string theName;
};

the constructor could be implemented as

Par_Class::Par_Class (int aValue, const std::string & aName)
  : theValue(aValue)
  , theName(aName)
{
}

and finally we could instantiate this class with

Par_Class Par_Obj (23, "My object is this");

and sure this constructor method belongs to the class Par_Class and not to any other class.

Similarly, in Java, we could code

public class Par_Class {
  private int theValue;
  private String theName;

  public Par_Class (int aValue, String aName){
    theValue = aValue;
    theName = aName;
  }
};

and we could instantiate the object using

Par_Class Par_Obj = new Par_Class (23, "My object is this");

(again please correct me if I'm wrong). Again, Par_Class constructor is a method of the class Par_Class.

In Ada 2005, this class could be coded as

--par_pkg.ads
package Par_Pkg is
   type Par_Class is tagged private;
   type Par_Class_Ptr is access all Par_Class;
   type Integer_Ptr is access Integer;

   function Construct 
     (P : access Par_Class; aValue : Integer; aName : Integer_Ptr)
      return Par_Class_Ptr;

private
   type Par_Class is tagged
      record
         theValue : Integer;
         theName  : Integer_Ptr;
      end record;
end Par_Pkg;

-- par_pkg.adb
package body Par_Pkg is
   function Construct 
     (P : access Par_Class; aValue : Integer; aName : Integer_Ptr)
      return Par_Class_Ptr is
      pragma Unreferenced (P);
      P_Ptr : constant Par_Class_Ptr := new Par_Class;
   begin
      P_Ptr.theValue := aValue;
      P_Ptr.theName := aName;
      return P_Ptr;
   end Construct;

end Par_Pkg;

and the user could call

with Par_Pkg; use Par_Pkg;
procedure Par_Main is
   Par_Obj : Par_Class_Ptr;
   Int_Obj : Integer_Ptr;
begin
   Int_Obj := new Integer;
   Int_Obj.all := 12; -- don't worry about been string or integer
   Par_Obj := Par_Obj.Construct 
     (aValue => 23,
      aName => Int_Obj);
end Par_Main;

And that's where resides the problem. The compiler says me that I could not use the method Construct in Par_Obj := Par_Obj.Construct because yet my object is null. But it's so obvious, because just what I want to do is to initialize the object (so it would not be null anymore). There are other ways of constructing the object, for example, using a function from outside the class, but I don't want to use this approach because it runs away from architecture. Could you please help me to formulate the problem to my Ada friends so they can help me to implement it in Ada? I guess I'm having a bit difficult on explaining this in general concept terms. Thanks.

Answer

@paercebal gave me what I think could achieve my goal:

  • "Is there a way to have a "static" function declared inside Par_Class?" and "is there a way to have an non-member function declared friend of Par_Class?"

I could complete it with "is there a way to have a "static" function declared inside a tagged type? Also, could the package where the class is declared act as a friend or as a static function?"

Update

Got some more good reasons on why implementing it as suggested by @SimonWright and some people from comp.lang.ada forum:

function Construct (aValue: Integer; aName: Integer)
                    return Par_Class is
begin
  return (theValue => aValue,
          theName  => aName);
end Construct;

So I asked: In this case function Construct would behave as a C++ static function (or maybe a friend one?)?

And Dmitry Kazakov answered:

That depends on what you mean. In Ada:

  1. there is no hidden parameters

  2. an operation can be dispatching (virtual) in any combination of parameters and/or result. But an operation cannot be dispatching in more than one type (no multiple dispatch). All tags of dispatching parameters must be same (no multi-methods).

  3. there is no static or friend operations as the visibility rules are based on packages.

The function Construct above is a primitive operation, it is not a constructor.

Constructors in Ada are implicit, they consist of

  1. construction of the components (in an unspecified order, recursively);

  2. a call to Initialize if the type is a descendant of Ada.Finalization.[Limited_]Controlled. (Overridden bodies of Initialize are not called! I.e. Ada constructors do not traverse derivation path. In short, aggregation is safe, derivation is not;

  3. starting all task components. (Note, tasks are not running when Initialize is called!)

Destructors act in the reverse order: tasks - Finalize - components.

And I guess it responds. Thank you people.

like image 225
Rego Avatar asked Nov 10 '11 03:11

Rego


1 Answers

Par_Class (int aValue, const char *aName) is the very special C++ constructor syntax; when used the compiler generates a new blank area of memory (calling malloc() if there's a new, on the stack otherwise) and the constructor gets to fill it in.

This is not the same as

function Construct 
  (P : access Par_Class; aValue : Integer; aName : Integer_Ptr)
   return Par_Class_Ptr;

which requires there to be a previous Par_Class instance and is like having a C++ member function

Par_Class *a_Par_Class(int aValue, const char *aName);

which would fail in the same way as your current Ada code if called with an uninitialised Par_Class *.

The nearest (Ada 2012) equivalent to the constructor is I think

function Construct 
  (aValue : Integer; aName : Integer_Ptr)
   return Par_Class is
begin
   return Result : Par_Class do
      Result.theValue := aValue;
      Result.theName := aName;
   end return;
end Construct;

or, OK for Ada 95/2005 and nearest to your present scheme:

function Construct 
  (aValue : Integer; aName : Integer_Ptr)
   return Par_Class_Ptr is
   P_Ptr : constant Par_Class_Ptr := new Par_Class;
begin
   P_Ptr.theValue := aValue;
   P_Ptr.theName := aName;
   return P_Ptr;
end Construct;

@paercebal mentioned that you need a static member function, which is correct; the C++ constructor is I think syntactic sugar for this. In Ada, scoping is done at the package level, so function Construct - being declared in the public part of the same package and returning a Par_Class_Ptr - is unmistakably (architecturally) associated with Par_Class.

like image 151
Simon Wright Avatar answered Sep 20 '22 05:09

Simon Wright