ADDED src/natools-s_expressions-templates-generic_discrete_render.adb Index: src/natools-s_expressions-templates-generic_discrete_render.adb ================================================================== --- src/natools-s_expressions-templates-generic_discrete_render.adb +++ src/natools-s_expressions-templates-generic_discrete_render.adb @@ -0,0 +1,54 @@ +------------------------------------------------------------------------------ +-- Copyright (c) 2014, Natacha Porté -- +-- -- +-- Permission to use, copy, modify, and distribute this software for any -- +-- purpose with or without fee is hereby granted, provided that the above -- +-- copyright notice and this permission notice appear in all copies. -- +-- -- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -- +-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -- +-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -- +-- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -- +-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -- +-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -- +-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -- +------------------------------------------------------------------------------ + +procedure Natools.S_Expressions.Templates.Generic_Discrete_Render + (Output : in out Ada.Streams.Root_Stream_Type'Class; + Template : in out Lockable.Descriptor'Class; + Value : in T) +is + Current_Value : T := T'First; + Event : Events.Event := Template.Current_Event; +begin + loop + case Event is + when Events.Add_Atom => + if Current_Value = Value then + Output.Write (Template.Current_Atom); + return; + end if; + + when Events.Open_List => + loop + Template.Next (Event); + + case Event is + when Events.Add_Atom => + Output.Write (Template.Current_Atom); + when others => + return; + end case; + end loop; + + when Events.Close_List | Events.End_Of_Input | Events.Error => + exit; + end case; + + Template.Next (Event); + Current_Value := T'Succ (Current_Value); + end loop; + + Output.Write (To_Atom (Default_Image (Value))); +end Natools.S_Expressions.Templates.Generic_Discrete_Render; ADDED src/natools-s_expressions-templates-generic_discrete_render.ads Index: src/natools-s_expressions-templates-generic_discrete_render.ads ================================================================== --- src/natools-s_expressions-templates-generic_discrete_render.ads +++ src/natools-s_expressions-templates-generic_discrete_render.ads @@ -0,0 +1,37 @@ +------------------------------------------------------------------------------ +-- Copyright (c) 2014, Natacha Porté -- +-- -- +-- Permission to use, copy, modify, and distribute this software for any -- +-- purpose with or without fee is hereby granted, provided that the above -- +-- copyright notice and this permission notice appear in all copies. -- +-- -- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -- +-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -- +-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -- +-- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -- +-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -- +-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -- +-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -- +------------------------------------------------------------------------------ + +------------------------------------------------------------------------------ +-- Natools.S_Expressions.Templates.Generic_Discrete_Render provides a -- +-- S-expression template renderer for discrete types. -- +-- The template is a list of atoms, representing images in the order of the -- +-- formal discrete type, optionally followed by a list whose atoms are -- +-- concatenated to form a fallback image for all others values. When -- +-- fallback image is not provided, formal Default_Image function is used. -- +------------------------------------------------------------------------------ + +with Ada.Streams; +with Natools.S_Expressions.Lockable; + +generic + type T is (<>); + with function Default_Image (Object : T) return String is T'Image; + with function "=" (Left, Right : T) return Boolean is <>; +procedure Natools.S_Expressions.Templates.Generic_Discrete_Render + (Output : in out Ada.Streams.Root_Stream_Type'Class; + Template : in out Lockable.Descriptor'Class; + Value : in T); +pragma Preelaborate (Natools.S_Expressions.Templates.Generic_Discrete_Render);