comp.lang.ada
 help / color / mirror / Atom feed
From: Serge Robyns <serge.robyns@gmail.com>
Subject: Re: Bounded String question
Date: Wed, 11 Nov 2015 12:06:40 -0800 (PST)
Date: 2015-11-11T12:06:40-08:00	[thread overview]
Message-ID: <d3958370-a3c5-4aac-9abf-bc9ddc873494@googlegroups.com> (raw)
In-Reply-To: <n1vtm0$db7$1@dont-email.me>

I ended up using a per "type" bounded string instance.  This will anyway give me the most flexibility as each could have a different length in the final version.

package Client_Name_Strings is new Ada.Strings.Bounded.Generic_Bounded_Length (20);
subtype T_Client_Name is Client_Name_Strings.Bounded_String;
...
No_Client_Name : T_Client_Name renames Client_Name_Strings.Null_Bounded_String;
...
function To_Client_Name
  (Source : in String;
   Drop   : in Truncation := Error)
   return T_Client_Name
   renames Client_Name_Strings.To_Bounded_String;

I had to specify the full signature of To_Bounded_String, including default value to make it work with sub types.

With this construct the package also compiles and run with pragma preelaborate.
Moreover, "users" of the package will use types, constants and explicit functions and I can change the implementation at will.

On Wednesday, 11 November 2015 18:27:41 UTC+1, Jeffrey R. Carter  wrote:
> When you create a derived type, such as T_Client_Name, it inherits all the
> primitive operations of its parent. In this case, that includes To_String and
> To_Bounded_String. So you get functions
> 
> function To_String (S : T_Client_Name) return String;
> function To_Bounded_String (S : String) return T_Client_Name;
> 
> Basically, all the operations in Generic_Bounded_Length that have a parameter or
> return type of Bounded_String, with Bounded_String replaced by the name of the
> derived type.
> 
> If you want something named To_T_Client_Name then of course you have to define
> it yourself:
> 
> function To_T_Client_Name (S : String ) return T_Client_Name renames
> To_Bounded_String;
> 

I'm not sure this works, well not how I tried.

with Ada.Strings; use Ada.Strings;
with Ada.Strings.Bounded;

procedure BS_Test is
   package BS is new Ada.Strings.Bounded.Generic_Bounded_Length (20);

   type OS is new BS.Bounded_String;
   
   No_OS : OS renames BS.Null_Bounded_String;   
   function To_OS (Source : in String;
                  Drop : in Truncation := Error) return OS renames BS.To_Bounded_String;

   B : OS     := To_OS ("asdfghjkl");
   S : String := To_String (B);
begin -- BS_Test
   null;
end BS_Test;

gives

bs_test.adb:9:25: expected private type "OS" defined at line 7
bs_test.adb:9:25: found private type "Ada.Strings.Bounded.Bounded_String" from instance at line 5
bs_test.adb:10:04: no visible subprogram matches the specification for "To_OS"


  reply	other threads:[~2015-11-11 20:06 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-10 22:00 Bounded String question Serge Robyns
2015-11-11  0:48 ` Bob Duff
2015-11-11  2:01   ` Jeffrey R. Carter
2015-11-11 15:34     ` Bob Duff
2015-11-11 17:36       ` Jeffrey R. Carter
2015-11-11 19:22         ` Bob Duff
2016-03-06 18:59       ` Xavier Petit
2016-03-07 23:16         ` Randy Brukardt
2016-03-08  0:08           ` Jeffrey R. Carter
2016-03-09  1:18             ` Randy Brukardt
2015-11-11 10:52   ` Serge Robyns
2015-11-11 13:43     ` Serge Robyns
2015-11-11 14:32       ` brbarkstrom
2015-11-11 16:08         ` Serge Robyns
2015-11-11 17:27       ` Jeffrey R. Carter
2015-11-11 20:06         ` Serge Robyns [this message]
2015-11-11 20:23           ` AdaMagica
2015-11-11 20:27             ` AdaMagica
2015-11-11 20:32             ` Serge Robyns
2015-11-11 20:40               ` AdaMagica
2015-11-12 17:31                 ` Serge Robyns
2015-11-12 19:10                   ` AdaMagica
2015-11-12 21:29                     ` Randy Brukardt
2015-11-12 18:03                 ` G.B.
2015-11-12 18:13                   ` Serge Robyns
2015-11-12 18:14                     ` Serge Robyns
2015-11-12 19:37                   ` Randy Brukardt
2015-11-11 20:42           ` Jeffrey R. Carter
2015-11-12 16:59             ` Serge Robyns
2015-11-12 18:39               ` Jeffrey R. Carter
2015-11-12 21:19                 ` Randy Brukardt
2015-11-12 21:27               ` Randy Brukardt
2015-11-12 22:32                 ` Jeffrey R. Carter
2015-11-13  0:07                   ` Randy Brukardt
2015-11-13  1:01                     ` Jeffrey R. Carter
2015-11-11 15:41     ` Bob Duff
replies disabled

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