comp.lang.ada
 help / color / mirror / Atom feed
From: hanslad@gmail.com
Subject: Help with type definition
Date: Fri, 16 May 2014 00:37:25 -0700 (PDT)
Date: 2014-05-16T00:37:25-07:00	[thread overview]
Message-ID: <8beec1c1-9ca2-44fc-9d7e-0cd0aa772dcc@googlegroups.com> (raw)

Hello,
I need an advise on how to define a type in my project.
I want to implement a network protocol with the following definition on the type "string":

"All String values are encoded as a sequence of UTF8 characters without a null terminator and preceded by the length in bytes.
The length in bytes is encoded as Int32. A value of -1 is used to indicate a 'null' string."

The string is used in different datastructures send on network eg. like the one implemented in "Node" type below.
Am I on the right track here? 

Here is my code:
Ads:

with Interfaces;
with Ada.Finalization; use Ada.Finalization;
package Types is

   type String_Type is new Ada.Finalization.Controlled with private;
   
   type IdentifierType is
     (
      IdentifierType_Numeric,
      IdentifierType_String
     );
   for IdentifierType use
     (IdentifierType_Numeric => 1,
      IdentifierType_String  => 2);
   for IdentifierType'Size use 8;


   type Node (IdType  : IdentifierType := IdentifierType_Numeric )is record
      NamespaceIndex : Interfaces.Unsigned_16  := 0;
      case IdType is
      when IdentifierType_Numeric =>
         Numeric    : Interfaces.Unsigned_32 ;
      when IdentifierType_String =>
         String     : String_Type;
      end case;
   end record;

private
   overriding procedure Initialize (This: in out String_Type);
   overriding procedure Adjust (This: in out String_Type);
   overriding procedure Finalize (This : in out String_Type);

   type String_Type_Implementation(Count : Natural) is record
      Data : String(1 .. Count) := "";
   end record;

   type String_Type_Implementation_Ptr is access String_Type_Implementation;

   type String_Type is new Ada.Finalization.Controlled with record
      Length         : Integer;
      Implementation : String_Type_Implementation_Ptr;
   end record;
end Types;

Adb:
with Ada.Unchecked_Deallocation;
package body Types is 
   
    procedure Free is
     new Ada.Unchecked_Deallocation (String_Type_Implementation, String_Type_Implementation_Ptr);
   
   procedure Finalize (This : in out String_Type) is
   begin
      if(This.Implementation /= null) then
         Free (This.Implementation);
      end if;
   end Finalize;

   overriding procedure Adjust (This : in out String_Type) is
   begin
      This.Implementation :=
        new String_Type_Implementation'(This.Implementation.all);
   end Adjust;

   overriding procedure Initialize (This: in out String_Type) is
   begin
      This.Length := -1;
      This.Implementation := new String_Type_Implementation(0);
   end Initialize;
end Types;


Thanks,

Hans 


             reply	other threads:[~2014-05-16  7:37 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-16  7:37 hanslad [this message]
2014-05-16  7:54 ` Help with type definition Dmitry A. Kazakov
2014-05-16 10:03   ` G.B.
2014-05-16 11:26   ` Jacob Sparre Andersen
2014-05-16 15:38   ` Shark8
2014-05-16 15:50     ` Simon Wright
2014-05-16 16:30       ` Shark8
2014-05-16 11:29 ` Jacob Sparre Andersen
2014-05-16 12:29 ` G.B.
2014-05-16 17:57 ` Jeffrey Carter
2014-05-21 20:45 ` hanslad
2014-06-27 22:05 ` hanslad
2014-06-28  7:26   ` Shark8
replies disabled

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