LaKraven / LKSL

LaKraven Studios Standard Library
http://otapi.com
Other
51 stars 15 forks source link

Event streaming decouple #17

Closed LaKraven closed 9 years ago

LaKraven commented 9 years ago

TLKEvent is no longer a TLKStreamable object, thus there's far less to override and implement for basic Event types.

If you want to be able to Record or Transmit an Event, you'll need to create a companion type of TLKEvent from TLKEventStreamable.

I suggest using:

TMyEventTypeStreamable = TLKEventStreamable<TMyEventType>

This reduces further the amount of overrides to implement!

Here is a really simple example of defining a TLKEvent class:

type
  TTestEvent = class(TLKEvent)
  private
    FString: String;
  protected
    procedure Clone(const AFromEvent: TLKEvent); override;
  public
    constructor Create(const AString: String); reintroduce;
    property SomeString: String read FString;
  end;

And here's the implementation for it:

{ TTestEvent }

procedure TTestEvent.Clone(const AFromEvent: TLKEvent);
begin
  FString := TTestEvent(AFromEvent).FString;
end;

constructor TTestEvent.Create(const AString: String);
begin
  inherited Create;
  FString := AString;
end;

Easy, right?

Now, let's provide a Streamable Handler for it...

  TTestEventStreamable = class(TLKEventStreamable<TTestEvent>)
  protected
    procedure ReadEventFromStream(const AEvent: TTestEvent; const AStream: TStream); overload; override;
    procedure InsertEventIntoStream(const AEvent: TTestEvent; const AStream: TStream); overload; override;
    procedure WriteEventToStream(const AEvent: TTestEvent; const AStream: TStream); overload; override;
  public
    class function GetTypeGUID: TGUID; override;
  end;

And its implementation:


{ TTestEventStreamable }

class function TTestEventStreamable.GetTypeGUID: TGUID;
const
  TYPE_GUID: TGUID = '{2269CA30-923A-4167-9013-7E9EFB89E667}';
begin
  Result := TYPE_GUID;
end;

procedure TTestEventStreamable.InsertEventIntoStream(const AEvent: TTestEvent; const AStream: TStream);
begin
  inherited;
  StreamInsertString(AStream, AEvent.FString);
end;

procedure TTestEventStreamable.ReadEventFromStream(const AEvent: TTestEvent; const AStream: TStream);
begin
  inherited;
  AEvent.FString := StreamReadString(AStream);
end;

procedure TTestEventStreamable.WriteEventToStream(const AEvent: TTestEvent; const AStream: TStream);
begin
  inherited;
  StreamWriteString(AStream, AEvent.FString);
end;

initialization
  TTestEventStreamable.Register;
finalization
  TTestEventStreamable.Unregister;

Now we can Record (implemented and functional) or Transmit (temporarily removed for redesign and reimplementation) TTestEvent!