option_type.ads 1.08 KB
Newer Older
Maxime Perrotin's avatar
Maxime Perrotin committed
1
2
3
4
5
6
7
--  *************************** buildsupport ****************************  --
--  (c) 2008-2017 European Space Agency - maxime.perrotin@esa.int
--  LGPL license, see LICENSE file
--  Define a generic Option type
generic
   type T is private;
package Option_Type is
8
   type Option is tagged private;
Maxime Perrotin's avatar
Maxime Perrotin committed
9
10
   function Just (I : T) return Option;
   function Nothing return Option;
11
   function Just (O : Option) return T;
Maxime Perrotin's avatar
Maxime Perrotin committed
12
13
   function Value (O : Option) return T renames Just;
   function Value_Or (O : Option; Default : T) return T;
14
   function Has_Value (O : Option) return Boolean;
Maxime Perrotin's avatar
Maxime Perrotin committed
15
16
17

private

18
   type Option is tagged
Maxime Perrotin's avatar
Maxime Perrotin committed
19
      record
20
21
          Present : Boolean := False;
          Value   : T;
Maxime Perrotin's avatar
Maxime Perrotin committed
22
23
24
25
26
27
      end record;

   function Just (I : T) return Option is
       (Present => True, Value => I);

   function Nothing return Option is
28
       (Present => False, others => <>);
Maxime Perrotin's avatar
Maxime Perrotin committed
29
30
31
32
33
34

   function Just (O : Option) return T is (O.Value);

   function Value_Or (O : Option; Default : T) return T is
       (if O.Present then O.Value else Default);

35
   function Has_Value (O : Option) return Boolean is (O.Present);
Maxime Perrotin's avatar
Maxime Perrotin committed
36
end Option_Type;