AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

DLL und Threads

Ein Thema von haentschman · begonnen am 18. Apr 2014 · letzter Beitrag vom 19. Apr 2014
 
Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#8

AW: DLL und Threads

  Alt 18. Apr 2014, 12:51
Parser-Interface:
Delphi-Quellcode:
unit Parser;

interface

type
  IParser = interface
    ['{7082CCBB-2680-4BC4-8B17-7FFE1D602A0A}']
    function Parse( const AString : string ) : TArray<string>;
  end;

implementation

end.
und ein simpler nicht threadsafe Parser:
Delphi-Quellcode:
unit SimpleParser;

interface

uses
  Classes,
  Parser;

type
  TParserState = procedure( AChar : Char ) of object;

  TSimpleParser = class( TInterfacedObject, IParser )
  private
    FState : TParserState;
    FBuffer : string;
    FTokens : TStrings;
    procedure StartState( AChar : Char );
    procedure TokenState( AChar : Char );
  public
    constructor Create;
    destructor Destroy; override;

    function Parse( const AString : string ) : TArray<string>;
  end;

implementation

{ TSimpleParser }

constructor TSimpleParser.Create;
begin
  inherited;
  FTokens := TStringList.Create;
end;

destructor TSimpleParser.Destroy;
begin
  FTokens.Free;
  inherited;
end;

function TSimpleParser.Parse( const AString : string ) : TArray<string>;
var
  LIdx : Integer;
begin
  FTokens.Clear;
  FState := StartState;
  for LIdx := 1 to Length( AString ) do
  begin
    FState( AString[LIdx] );
  end;
  Result := FTokens.ToStringArray;
end;

procedure TSimpleParser.StartState( AChar : Char );
begin
  case AChar of
    ' ' :
      ;
    ',' :
      ;
  else
    FState := TokenState;
    FState(AChar);
  end;
end;

procedure TSimpleParser.TokenState( AChar : Char );
begin
  case AChar of
    ',' :
      begin
        FTokens.Add( FBuffer );
        FBuffer := '';
        FState := StartState;
      end;
  else
    FBuffer := FBuffer + AChar;
  end;
end;

end.
den ich einfach wrappe um mit dem doch threadsafe arbeiten zu können
Delphi-Quellcode:
unit ThreadSafeParser;

interface

uses
  Parser;

type
  TThreadSafeParser = class( TInterfacedObject, IParser )
  public
    function Parse( const AString : string ) : TArray<string>;
  end;

implementation

uses
  SimpleParser;

{ TThreadSafeParser }

function TThreadSafeParser.Parse( const AString : string ) : TArray<string>;
var
  LParser : IParser;
begin
  // TSimpleParser ist nicht threadsafe, aber
  // hier wird bei jedem Aufruf eine eigene Instanz erzeugt, auf die niemand sonst zugreift
  // dadurch wird das jetzt threadsafe
  LParser := TSimpleParser.Create;
  Result := LParser.Parse( AString );
end;

end.
Delphi-Quellcode:
procedure Test;
var
  LParser : IParser;
begin
  LParser := TSimpleParser.Create;
  OutputParseResult( LParser.Parse( '1,2,3,4' ) );

  LParser := TThreadSafeParser.Create;
  OutputParseResult( LParser.Parse( '1,2,3,4' ) );
end;
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)

Geändert von Sir Rufo (18. Apr 2014 um 12:54 Uhr)
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 03:59 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz