comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Experimenting with the OOP features in Ada
Date: Mon, 2 Jan 2017 17:41:16 +0100
Date: 2017-01-02T17:41:16+01:00	[thread overview]
Message-ID: <o4dvrc$gj4$1@gioia.aioe.org> (raw)
In-Reply-To: 29380aa7-0c3b-4908-94c6-aa91f3996b42@googlegroups.com

On 2017-01-02 14:21, Laurent wrote:

> Trying some OOP features in Ada. So not sure if everything is actually
> correct.
> Comments are welcome.
>
> So I have done a little test. Link at the end.
> I get the following error:
>
> raised CONSTRAINT_ERROR : my_strings-handle.adb:14 access check failed
>
> Which I don't understand. Sounds like the object no longer exists?
>
> test.adb:
>
> with Ada.Text_IO;
> with Ada.Exceptions;
>
> with Gnat.Traceback.Symbolic; use Gnat;
>
> with Base_Types.Antibiotics;
> with My_Strings;
> with My_Strings.Handle;
>
> procedure Test is
>
>    package SS renames My_Strings.Handle;
>
>    Test_Antibiotic : Base_Types.Antibiotics.Object
> :=Base_Types.Antibiotics.Create (Name     => "Test Antibiotic 1",
>    Code_SIL =>"123",
>    CMI      => "1",
>    SIR      => "S");
>
>    Test_String     : My_Strings.Handle.My_Safe_String := SS.Create ("Test
> single 1");
>
> begin
>
>    Ada.Text_IO.Put_Line (Test_String.Value);
>    Test_String := SS.Create ("Test single 2");
>    Ada.Text_IO.Put_Line (Test_String.Value);
>    Test_String := SS.Create ("Test single 3");
>    Ada.Text_IO.Put_Line (Test_String.Value);
>    Ada.Text_IO.Put_Line (Test_String.Value);
>    Ada.Text_IO.New_Line;
>
>    Ada.Text_IO.Put_Line ("Test record run 1:");
>    Ada.Text_IO.New_Line;
>
>    Ada.Text_IO.Put_Line ("Name: " & Test_Antibiotic.Name_Value);
>    Ada.Text_IO.Put_Line ("Code SIL: " & Test_Antibiotic.Code_SIL_Value);
>    Ada.Text_IO.Put_Line ("CMI: " & Test_Antibiotic.CMI_Value);
>    Ada.Text_IO.Put_Line ("SIR: " & Test_Antibiotic.SIR_Value);
>    Ada.Text_IO.New_Line;
>
>    Ada.Text_IO.Put_Line ("Test record run 2:");
>    Ada.Text_IO.New_Line;
>
>    Test_Antibiotic := Base_Types.Antibiotics.Create_Name (Name => "Test
> Antibiotic 2");
>    Ada.Text_IO.Put_Line ("Name: " & Test_Antibiotic.Name_Value);
>
>    Test_Antibiotic := Base_Types.Antibiotics.Create_Code_SIL (Code_SIL =>
> "amc");
>    Ada.Text_IO.Put_Line ("Code SIL: " & Test_Antibiotic.Code_SIL_Value);
>
>    Test_Antibiotic := Base_Types.Antibiotics.Create_CMI (CMI => "2");
>    Ada.Text_IO.Put_Line ("CMI: " & Test_Antibiotic.CMI_Value);
>
>    Test_Antibiotic := Base_Types.Antibiotics.Create_SIR (SIR => "R");
>    Ada.Text_IO.Put_Line ("SIR: " & Test_Antibiotic.SIR_Value);
>    Ada.Text_IO.New_Line;
>
>    Ada.Text_IO.Put_Line ("Test record run 3:");
>    Ada.Text_IO.New_Line;
>
>    Test_Antibiotic := Base_Types.Antibiotics.Create_Name (Name => "Test
> Antibiotic 3");
>    Test_Antibiotic := Base_Types.Antibiotics.Create_Code_SIL (Code_SIL =>
> "gen");
>    Test_Antibiotic := Base_Types.Antibiotics.Create_CMI (CMI => "8");
>    Test_Antibiotic := Base_Types.Antibiotics.Create_SIR (SIR => "I");
>
>    Ada.Text_IO.Put_Line ("Print run 3:");
>
>    Ada.Text_IO.Put_Line ("Name: " & Test_Antibiotic.Name_Value);
>    Ada.Text_IO.Put_Line ("Code SIL: " & Test_Antibiotic.Code_SIL_Value);
>    Ada.Text_IO.Put_Line ("CMI: " & Test_Antibiotic.CMI_Value);
>    Ada.Text_IO.Put_Line ("SIR: " & Test_Antibiotic.SIR_Value);
>
>    exception
>
>    when Err : others =>
>       Ada.Text_IO.Put_Line ("Problem: " &
>                               Ada.Exceptions.Exception_Information (Err));
>
>       Ada.Text_IO.Put_Line (Traceback.Symbolic.Symbolic_Traceback (Err));
>
> end Test;
>
> Terminal output:
>
> Test single 1
> Test single 2
> Test single 3
> Test single 3
>
> Test record run 1:
>
> Name: Test Antibiotic 1
> Code SIL: 123
> CMI: 1
> SIR: S
>
> Test record run 2:
>
> Name: Test Antibiotic 2
> Code SIL: amc
> CMI: 2
> SIR: R
>
> Test record run 3:
>
> Print run 3:
> Problem: raised CONSTRAINT_ERROR : my_strings-handle.adb:14 access check
> failed
>
>
> Why does it fail on the 3rd run? It works the 2 first ones?

You create a new object each time you make assignment. So at the line 42 
Create_Name creates an object with Name set. Then at the line 45 
Create_Code_SIL overwrites that object with another instance with no 
name set. When you try to get its name, the handle of the string is 
invalid. An attempt to get the value of gives Constraint_Error.

P.S. If you want to set components individually you must either expose 
them or else provide setters.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

  parent reply	other threads:[~2017-01-02 16:41 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-02 13:21 Experimenting with the OOP features in Ada Laurent
2017-01-02 14:11 ` Brian Drummond
2017-01-02 14:43   ` Brian Drummond
2017-01-02 16:27     ` Laurent
2017-01-03 13:01       ` Brian Drummond
2017-01-03 13:57         ` Laurent
2017-01-03 14:31           ` Dmitry A. Kazakov
2017-01-03 17:07             ` G.B.
2017-01-03 20:42               ` Dmitry A. Kazakov
2017-01-04 12:18             ` Laurent
2017-01-04 12:44               ` Dmitry A. Kazakov
2017-01-05 11:41                 ` Laurent
2017-01-05 13:02                   ` Dmitry A. Kazakov
2017-01-05 13:11                   ` AdaMagica
2017-01-04 15:09               ` Shark8
2017-01-05 11:54                 ` Laurent
     [not found]     ` <85ef59f3-bcbe-4630-9b4d-8285623ab456@googlegroups.com>
2017-01-03 12:48       ` Brian Drummond
2017-01-03 13:43         ` Laurent
2017-01-02 15:54   ` Laurent
2017-01-02 16:41 ` Dmitry A. Kazakov [this message]
2017-01-02 18:50   ` Laurent
2017-01-02 20:55     ` Dmitry A. Kazakov
2017-01-03 13:39       ` Laurent
2017-01-03 14:40         ` Dmitry A. Kazakov
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox