Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit EnumSettingsU;
- interface
- uses
- Generics.Collections;
- {$M+}
- type
- TEnumSettings<T: record > = class(TEnumerable<T>)
- private const
- KeyDelimiter: Char = '=';
- ElementDelimiter: Char = ';';
- var
- FDictionary: TDictionary<T, string>;
- function GetItem(const Key: T): string;
- procedure SetItem(const Key: T; const Value: string);
- function GetAsString: string;
- procedure SetAsString(const Value: string);
- protected
- function DoGetEnumerator: TEnumerator<T>; override;
- published
- property AsString: string read GetAsString write SetAsString;
- public
- constructor Create(const DataValue: string); reintroduce;
- destructor Destroy; override;
- class function StringToEnum(const Value: string): T; inline;
- property Items[const Key: T]: string read GetItem write SetItem; default;
- end;
- implementation
- uses
- System.SysUtils, System.TypInfo, System.Classes, System.Rtti;
- { TEnumSettings<TKey> }
- constructor TEnumSettings<T>.Create(const DataValue: string);
- begin
- if PTypeInfo(System.TypeInfo(T)).Kind <> tkEnumeration then
- raise Exception.Create(string(PTypeInfo(System.TypeInfo(T)).Name) + ' is not an Enumeration');
- inherited Create;
- FDictionary := TDictionary<T, string>.Create;
- AsString := DataValue;
- end;
- destructor TEnumSettings<T>.Destroy;
- begin
- FreeAndNil(FDictionary);
- inherited;
- end;
- function TEnumSettings<T>.DoGetEnumerator: TEnumerator<T>;
- begin
- Result := TDictionary<T, string>.TKeyEnumerator.Create(FDictionary);
- end;
- function TEnumSettings<T>.GetAsString: string;
- var
- Buffer: TStringList;
- Element: TPair<T, string>;
- begin
- Buffer := TStringList.Create;
- try
- Buffer.Delimiter := ElementDelimiter;
- for Element in FDictionary do
- Buffer.Add(TValue.From<T>(Element.Key).ToString + KeyDelimiter + Element.Value);
- Result := Buffer.DelimitedText;
- finally
- FreeAndNil(Buffer);
- end;
- end;
- function TEnumSettings<T>.GetItem(const Key: T): string;
- begin
- if not FDictionary.TryGetValue(Key, Result) then
- Result := '';
- end;
- procedure TEnumSettings<T>.SetAsString(const Value: string);
- var
- Element: string;
- KeyValue: TArray<string>;
- begin
- for Element in Value.Split(ElementDelimiter) do
- begin
- KeyValue := Element.Split(KeyDelimiter);
- if Length(KeyValue) <> 2 then
- continue;
- FDictionary.Add(StringToEnum(KeyValue[0]), KeyValue[1]);
- end;
- end;
- procedure TEnumSettings<T>.SetItem(const Key: T; const Value: string);
- begin
- FDictionary.AddOrSetValue(Key, Value);
- end;
- class function TEnumSettings<T>.StringToEnum(const Value: string): T;
- var
- EnumValue: Integer;
- begin
- EnumValue := GetEnumValue(TypeInfo(T), Value);
- Move(EnumValue, Result, SizeOf(Result));
- end;
- end.
- ************************
- And a small example of how to use:
- Uses
- EnumSettingsU;
- Type
- TMyEnum = (One, Two, Three);
- procedure TForm8.FormCreate(Sender: TObject);
- var
- EnumSettings: TEnumSettings<TMyEnum>;
- MyEnum: TMyEnum;
- begin
- EnumSettings := TEnumSettings<TMyEnum>.Create('One=1;Two=2;Three=3');
- for MyEnum in EnumSettings do
- ShowMessage(EnumSettings[MyEnum]);
- EnumSettings.Free;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement