wiki:League/Settings

League - Settings

The League.Settings package provides persistent platform-independed application settings.

Examples

Basic usage

Here is example of typical use of Settings. Settings object is initialize automatically and provide transparent access to set of settings with fallbacks mechanism. Type of settings storage (INI files, Windows Registry) and number of settings storages used by fallbacks mechanism depends from operating system and its configuration.

with Ada.Wide_Wide_Text_IO;

with League.Application;
with League.Settings;
with League.Strings;
with League.Holders;

procedure Main is

   function "+"
    (Item : Wide_Wide_String) return League.Strings.Universal_String
       renames League.Strings.To_Universal_String;

begin
   --  Set organization name and domain, application name.

   League.Application.Set_Organization_Name (+"My Software");
   League.Application.Set_Organization_Domain (+"example.com");
   League.Application.Set_Application_Name (+"Settings Example");

   declare
      S : League.Settings.Settings;
      V : League.Holders.Holder;

   begin
      --  Setting value

      V := League.Holders.To_Holder (+"value");
      S.Set_Value (+"section/key", V);

      --  Getting value

      Ada.Wide_Wide_Text_IO.Put_Line
       (League.Holders.Element
         (S.Value (+"section/key")).To_Wide_Wide_String);
   end;
end Main;

Application and Organization settings and Fallbacks

This example highlights creation of organization settings and shows how they are used by fallbacks mechanism. Type of settings storage (INI files, Windows Registry) depends from operating system.

with Ada.Wide_Wide_Text_IO;

with League.Application;
with League.Settings;
with League.Strings;
with League.Values.Strings;

procedure Main is

   function "+"
    (Item : Wide_Wide_String) return League.Strings.Universal_String
       renames League.Strings.To_Universal_String;

begin
   --  Set organization name and domain, application name.

   League.Application.Set_Organization_Name (+"My Software");
   League.Application.Set_Organization_Domain (+"example.com");
   League.Application.Set_Application_Name (+"Settings Example");

   --  Create organization settings

   declare
      S : League.Settings.Settings
        := League.Settings.Create (+"My Software", +"example.com");
      V : League.Values.Value;

   begin
      --  Setting value

      League.Values.Strings.Set (V, +"organization_value");
      S.Set_Value (+"section/override_key", V);

      League.Values.Strings.Set (V, +"organization_value");
      S.Set_Value (+"section/derived_key", V);
   end;

   --  Create application settings

   declare
      S : League.Settings.Settings
        := League.Settings.Create
            (+"My Software", +"example.com", +"Settings Example");
      V : League.Values.Value;

   begin
      --  Setting value

      League.Values.Strings.Set (V, +"application_value");
      S.Set_Value (+"section/override_key", V);
   end;

   --  Show work of fallbacks mechanism

   declare
      S : League.Settings.Settings;
      V : League.Values.Value;

   begin
      --  Getting value

      Ada.Wide_Wide_Text_IO.Put_Line
       (League.Values.Strings.Get
         (S.Value (+"section/override_key")).To_Wide_Wide_String);

      --  Getting value

      Ada.Wide_Wide_Text_IO.Put_Line
       (League.Values.Strings.Get
         (S.Value (+"section/derived_key")).To_Wide_Wide_String);
   end;
end Main;
Last modified 4 years ago Last modified on Dec 7, 2016, 2:17:30 AM
Note: See TracWiki for help on using the wiki.