Skip to content

Instantly share code, notes, and snippets.

@jarroddavis68
Created November 5, 2021 23:41
Show Gist options
  • Save jarroddavis68/8795eea0d69f797adef2ed747cfdc14a to your computer and use it in GitHub Desktop.
Save jarroddavis68/8795eea0d69f797adef2ed747cfdc14a to your computer and use it in GitHub Desktop.
InterfaceFactory - Manage a list is generic NoRef counted interfaces.
(*******************************************************************************
InterfaceFactory - Manage a list is generic NoRef counted interfaces.
Copyright © 2021 tinyBigGAMES™ LLC
All Rights Reserved.
Website: https://tinybiggames.com
Email : support@tinybiggames.com
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in
a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
3. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
4. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
5. All video, audio, graphics and other content accessed through the
software in this distro is the property of the applicable content owner
and may be protected by applicable copyright law. This License gives
Customer no rights to such content, and Company disclaims any liability
for misuse of content.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
-------------------------------------------------------------------------------
Release 1.0.stable:
Initial Release
-------------------------------------------------------------------------------
Usage:
type
IMyInterface = interface(IBaseInterface)
['{2A52D6A4-F436-40E7-B12E-F3DE36D2D0A0}']
procedure Test;
end;
TMyInterface = class(TBaseInterface, IMyInterface)
public
procedure Test;
end;
var
F: TInterfaceFactory;
I: IMyInterface;
begin
F := TInterfaceFactory.Create;
F.RegisterInterface(IMyInterface, TMyInterface);
F.GetInterface(IMyInterface, I);
I.Test;
F.ReleaseInterface(I);
FreeAndNil(F);
end.
*******************************************************************************)
unit InterfaceFactory;
interface
uses
System.SysUtils,
System.Generics.Collections;
type
{ IBaseInterface }
IBaseInterface = interface
['{29D01AA3-E370-4EB0-B258-FB9D17BB090A}']
end;
{ TBaseInterface }
TBaseInterface = class(TNoRefCountObject, IBaseInterface)
public
constructor Create; virtual;
destructor Destroy; override;
end;
{ TBaseInterfaceClass }
TBaseInterfaceClass = class of TBaseInterface;
{ TInterfaceFactory }
TInterfaceFactory = class(TObject)
protected
FList: TDictionary<TGUID, TBaseInterfaceClass>;
public
constructor Create;
destructor Destroy; override;
function RegisterInterface(const aGUID: TGUID; const aClass: TBaseInterfaceClass): Boolean;
function GetInterface(const aGUID: TGUID; const [ref] aInterface: IBaseInterface): Boolean;
function ReleaseInterface(const [ref] aInterface: IBaseInterface): Boolean;
end;
implementation
{ TBaseInterface }
constructor TBaseInterface.Create;
begin
inherited;
end;
destructor TBaseInterface.Destroy;
begin
inherited;
end;
{ TInterfaceFactory }
constructor TInterfaceFactory.Create;
begin
FList := TDictionary<TGUID, TBaseInterfaceClass>.Create;
end;
destructor TInterfaceFactory.Destroy;
begin
FreeAndNil(FList);
inherited;
end;
function TInterfaceFactory.RegisterInterface(const aGUID: TGUID; const aClass: TBaseInterfaceClass): Boolean;
begin
if Supports(aClass, aGUID) then
Result := FList.TryAdd(aGUID, aClass)
else
Result := False;
end;
function TInterfaceFactory.GetInterface(const aGUID: TGUID; const [ref] aInterface: IBaseInterface): Boolean;
var
LClass: TBaseInterfaceClass;
begin
Result := False;
if FList.TryGetValue(aGUID, LClass) then
Result := Supports(LClass.Create, aGUID, IBaseInterface(Pointer(@aInterface)^));
if not Result then
IBaseInterface(Pointer(@aInterface)^) := nil;
end;
function TInterfaceFactory.ReleaseInterface(const [ref] aInterface: IBaseInterface): Boolean;
var
Temp: TBaseInterface;
begin
Result := False;
if aInterface = nil then Exit;
Temp := TBaseInterface(aInterface);
IBaseInterface(Pointer(@aInterface)^) := nil;
Temp.Free;
Result := True;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment