unit Resources.Types;
interface
uses
System.Classes,
System.SysUtils,
System.Types;
type
TResource =
class abstract( TInterfacedPersistent, IStreamPersist )
protected
function GetDataStream: TStream;
virtual;
abstract;
protected
procedure AssignTo( Dest: TPersistent );
override;
public
procedure LoadFromStream( Stream: TStream );
procedure SaveToStream( Stream: TStream );
end;
TEmbeddedResource =
class( TResource )
private
FResName:
string;
protected
function GetDataStream: TStream;
override;
public
constructor Create(
const ResName:
string );
property ResName:
string read FResName;
end;
implementation
{ TResource }
procedure TResource.AssignTo( Dest: TPersistent );
var
other : IStreamPersist;
source: TStream;
begin
if Supports( Dest, IStreamPersist, other )
then
begin
source := GetDataStream( );
try
other.LoadFromStream( source );
finally
source.Free;
end;
end
else
inherited;
end;
procedure TResource.LoadFromStream( Stream: TStream );
begin
raise EInvalidOperation.Create( '
Resources are read only' );
end;
procedure TResource.SaveToStream( Stream: TStream );
var
source: TStream;
begin
source := GetDataStream( );
try
Stream.CopyFrom( source, -1 );
finally
source.Free;
end;
end;
{ TEmbeddedResource }
constructor TEmbeddedResource.Create(
const ResName:
string );
begin
inherited Create;
FResName := ResName;
end;
function TEmbeddedResource.GetDataStream: TStream;
begin
Result := TResourceStream.Create( HInstance, FResName, RT_RCDATA );
end;
end.