Advertisement
Borrisholt

EnumSettings

Feb 9th, 2015
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.19 KB | None | 0 0
  1. unit EnumSettingsU;
  2.  
  3. interface
  4.  
  5. uses
  6.   Generics.Collections;
  7. {$M+}
  8.  
  9. type
  10.   TEnumSettings<T: record > = class(TEnumerable<T>)
  11.   private const
  12.     KeyDelimiter: Char = '=';
  13.     ElementDelimiter: Char = ';';
  14.   var
  15.     FDictionary: TDictionary<T, string>;
  16.     function GetItem(const Key: T): string;
  17.     procedure SetItem(const Key: T; const Value: string);
  18.     function GetAsString: string;
  19.     procedure SetAsString(const Value: string);
  20.   protected
  21.     function DoGetEnumerator: TEnumerator<T>; override;
  22.   published
  23.     property AsString: string read GetAsString write SetAsString;
  24.   public
  25.     constructor Create(const DataValue: string); reintroduce;
  26.     destructor Destroy; override;
  27.     class function StringToEnum(const Value: string): T; inline;
  28.     property Items[const Key: T]: string read GetItem write SetItem; default;
  29.   end;
  30.  
  31. implementation
  32.  
  33. uses
  34.   System.SysUtils, System.TypInfo, System.Classes, System.Rtti;
  35.  
  36. { TEnumSettings<TKey> }
  37.  
  38. constructor TEnumSettings<T>.Create(const DataValue: string);
  39. begin
  40.   if PTypeInfo(System.TypeInfo(T)).Kind <> tkEnumeration then
  41.     raise Exception.Create(string(PTypeInfo(System.TypeInfo(T)).Name) + ' is not an Enumeration');
  42.  
  43.   inherited Create;
  44.   FDictionary := TDictionary<T, string>.Create;
  45.  
  46.   AsString := DataValue;
  47. end;
  48.  
  49. destructor TEnumSettings<T>.Destroy;
  50. begin
  51.   FreeAndNil(FDictionary);
  52.   inherited;
  53. end;
  54.  
  55. function TEnumSettings<T>.DoGetEnumerator: TEnumerator<T>;
  56. begin
  57.   Result :=  TDictionary<T, string>.TKeyEnumerator.Create(FDictionary);
  58. end;
  59.  
  60. function TEnumSettings<T>.GetAsString: string;
  61. var
  62.   Buffer: TStringList;
  63.   Element: TPair<T, string>;
  64. begin
  65.   Buffer := TStringList.Create;
  66.   try
  67.     Buffer.Delimiter := ElementDelimiter;
  68.  
  69.     for Element in FDictionary do
  70.       Buffer.Add(TValue.From<T>(Element.Key).ToString + KeyDelimiter + Element.Value);
  71.  
  72.     Result := Buffer.DelimitedText;
  73.   finally
  74.     FreeAndNil(Buffer);
  75.   end;
  76. end;
  77.  
  78. function TEnumSettings<T>.GetItem(const Key: T): string;
  79. begin
  80.   if not FDictionary.TryGetValue(Key, Result) then
  81.     Result := '';
  82. end;
  83.  
  84. procedure TEnumSettings<T>.SetAsString(const Value: string);
  85. var
  86.   Element: string;
  87.   KeyValue: TArray<string>;
  88. begin
  89.   for Element in Value.Split(ElementDelimiter) do
  90.   begin
  91.     KeyValue := Element.Split(KeyDelimiter);
  92.  
  93.     if Length(KeyValue) <> 2 then
  94.       continue;
  95.  
  96.     FDictionary.Add(StringToEnum(KeyValue[0]), KeyValue[1]);
  97.   end;
  98. end;
  99.  
  100. procedure TEnumSettings<T>.SetItem(const Key: T; const Value: string);
  101. begin
  102.   FDictionary.AddOrSetValue(Key, Value);
  103. end;
  104.  
  105. class function TEnumSettings<T>.StringToEnum(const Value: string): T;
  106. var
  107.   EnumValue: Integer;
  108. begin
  109.   EnumValue := GetEnumValue(TypeInfo(T), Value);
  110.   Move(EnumValue, Result, SizeOf(Result));
  111. end;
  112.  
  113. end.
  114.  
  115. ************************
  116.  
  117. And a small example of how to use:
  118.  
  119.  
  120. Uses
  121.    EnumSettingsU;
  122.  
  123.  
  124. Type
  125.   TMyEnum = (One, Two, Three);
  126.  
  127. procedure TForm8.FormCreate(Sender: TObject);
  128. var
  129.   EnumSettings: TEnumSettings<TMyEnum>;
  130.   MyEnum: TMyEnum;
  131. begin
  132.   EnumSettings := TEnumSettings<TMyEnum>.Create('One=1;Two=2;Three=3');
  133.   for MyEnum in EnumSettings do
  134.     ShowMessage(EnumSettings[MyEnum]);
  135.   EnumSettings.Free;
  136. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement