Delphi typcast tlist items C++ approach - c++

Hi I have one question regarding some type casting approaches. I am translating some Delphi files to C++. I have delphi declaration of class which is derived from TList and it's a base class for other derived classes.
type TBaseItem = class (TObject)
public
procedure SomeProcedure; virtual; abstract;
end;
Type TBaseClass = class(TList)
private
function GetItem(Index: Integer): TBaseItem;
procedure SetItem(Value: TBaseItem; Index: Integer);
public
property Items[Index: Integer]: TBaseItem read GetItem write SetItem;
end;
function TBaseClass.GetItem(Index: Integer): TBaseItem;
begin
Result := TBaseItem(inherited Items[Index]);
end;
procedure TBaseClass.SetItem(Value: TBaseItem; Index: Integer);
begin
inherited Items[Index] := Value;
end;
This are two base classe TBaseItem and TBaseClass. So here are declared new classes TchildItem which is derived from TBaseItem and TChildClass which is derived from TBaseClass. TChildItem is overriding method SomeMethod and what is more important is that TChildtClass is overriding property Items in a way that now we are returning TParentItem items insted of TBaseItem.
type TChildItem = class (TBaseItem)
public
procedure SomeProcedure; override;
end;
type TChildClass = class(TBaseClass)
private
function GetItem(Index: Integer): TChildItem;
procedure SetItem(Value: TChildItem; Index: Integer);
public
property Items[Index: Integer]: TChildItemread GetItem write SetItem;
end;
function TChildClass .GetItem(Index: Integer): TChildItem;
begin
Result := TChildItem(inherited Items[Index]);
end;
procedure TChildClass.SetItem(Value: TChildItem; Index: Integer);
begin
inherited Items[Index] := Value;
end;
With this example I wanted to show how easy can be done deriving classes and overriding properties. Getting proper type of item out of a list is simply done by calling parents (base) property Item and typecast it to proper type. This is delphi apporach.
I wonder how can I translate this part of code to C++. Currently I declared a new base class which is not derived from any class and it has public var Items which is
class TBaseItem{
virtual void SomeMethod();
}
class TBaseClass {
public:
vector<TBaseItem> Items;
};
class TChildItem : public TBaseItem{
}
class TChildClass : public TBaseClass {
};
and then use
return (TChildItem) Items[Idx]
That means I would like to access parent's (TBaseClass) public variables such as that vector Items and typecast it to proper type... My first impression is that I might be going into wrong direction with that Delphi approach.
What do you suggest? How should I go with that?
THANK YOU VERY MUCH!

The Delphi code is old and pre-dates generics, the Delphi analogue to C++ templates. In modern Delphi code those list classes would simply not exist. Instead one would use TList<TBaseItem> and TList<TChildItem>.
In C++ code you would simply use vector<TBaseItem*> and vector<TChildItem*>. There is simply no point in your C++ translation to implement TBaseClass and TChildClass.
I would also correct your terminology. Delphi properties cannot be overriden. The new property in TChildClass is just that, a new property.

Related

Delphi Mocks with class helper

I really like Delphi Mocks, but it seems to have a problem with class helpers. I have a class helper that extends TRESTResponse and want to test the functions I added. Unfortunately, this does not work. I have created the following simple reproduction:
MyClass = class
function MyFunc1: Integer; virtual;
end;
MyClassHelper = class helper for MyClass
function MyFunc2: Integer; virtual;
end;
var
LMock: TMock<MyClass>;
begin
LMock := TMock<MyClass>.Create;
LMock.Setup.WillReturn(2).When.MyFunc1;
LMock.Setup.WillReturn(2).When.MyFunc2;
Assert(LMock.Instance.MyFunc1 <> 1);
Assert(LMock.Instance.MyFunc2 <> 1);
end;
function MyClass.MyFunc1: Integer;
begin
Result := 1;
end;
function MyClassHelper.MyFunc2: Integer;
begin
Result := 1;
end;
Mocking MyFunc1 works as expected, but MyFunc2 cannot be mocked. The second LMock.Setup.WillReturn-line raises an exception [MyClass] already defines Will Return When for method [MyFunc1]. If you remove the mocking setup for MyFunc1 then no exception is raised, but MyFunc2 is not mocked.
Any suggestions how to solve this? Or is it not possible to mock class helpers with Delphi Mocks?

Adapter pattern : Why would we need to subclass the Adaptee?

For an Object Adapter design, GoF states :
makes it harder to override Adaptee behavior. It will require subclassing Adaptee and making Adapter refer to the subclass rather than the Adaptee itself
My question is that why is this subclassing required when we are creating the clases as follows :
class Target {
public :
virtual void op() = 0 ;
} ;
class Adaptee {
public :
void adapteeOp() {cout<<"adaptee op\n" ;}
} ;
class Adapter : public Target {
Adaptee *adaptee ;
public :
Adapter(Adaptee *a) : adaptee(a) {}
void op() {
// added behavior
cout<<"added behavior\n" ;
adaptee->adapteeOp() ;
// more added behavior
cout<<"more added behavior\n" ;
}
} ;
main() { //client
Adapter adapter(new Adaptee) ;
adapter.op() ;
}
I have not been able to appreciate the requirement for subclassing as mentioned by GoF when I am able to override the behavior here also.
Please explain what is the point that I am missing out.
I have not been able to appreciate the requirement for subclassing as mentioned by GoF when I am able to override the behavior here also.
I see your confusion. Your example is too simple as it only contains cout statements. I wouldn't qualify adding cout statements before and after a call to one of Adaptees methods as adding any significant behavior. You need to consider more complex scenarios.
Imagine that you want to add newFunctionality to the Adaptee that uses the protected data from Adaptee. You can't modify the Adaptee so the only option you have is to subclass it.
class NewAdaptee : public Adaptee {
public :
void adapteeOp() {
cout<<"adaptee op\n" ; //step 3
}
void newFunctionality() { //use protected members from Adaptee }
} ;
The above code demonstrates a more complex use case of adding functionality to the Adaptee where subclassing is the only way to achieve this. So you now want to start using this new Adaptee in your Adapter. If you go with the object adapter option, you will need to start using a NewAdaptee reference in the Adaptor
class Adapter : public Target {
NewAdaptee *adaptee ;
//more code follows
}
This has the immediate issue that your Adapter can no longer be passed any direct subclasses of Adaptee. This is what they mean when they say It will require subclassing Adaptee and making Adapter refer to the subclass rather than the Adaptee itself. This would take away the advantage of the object adapter approach which was to allow a single adapter to work with all the subclasses of the Adaptee.
Note : In the class adapter approach, NewAdaptee would actually be your adapter and would also inherit Target.

Check if a Field is a TList with Reflection

Using Delphi how can I check if a field of an object is a TList ?
I tryed using this
var
c : TRttiContext;
t : TRttiType;
f : TRttiField;
begin
c := TRttiContext.Create;
t := c.GetType(Self.ClassType);
for f in t.GetFields do begin
//check if the field is TList<T>
//check also the Generic type T
end;
end;
You have to use the IsType<T> method if you want to check for TList (the one from System.Classes).
If you want to check if it is a TList<T> you have to do some string parsing of the class name. Even more so if you want to check the specific type of T. This is because Delphi has no special RTTI about generic types and it does not support open generic types.
You can look at the Spring.Helpers unit from Spring4D how this can be solved.
Some example code using this:
if f.FieldType.IsGenericType then
if f.FieldType.GetGenericTypeDefinition = 'TList<>' then
if f.FieldType.GetGenericArguments[0].Handle = TypeInfo(TMyClass) then
Writeln('found');
You can check if object contains ToArray() method:
var
method: TRttiMethod;
begin
...
method := f.FieldType.GetMethod('ToArray');
if Assigned(method) then
begin
//...
end;
Note, however, that this will return True for all TList<> descendants too.

Need help converting Graphics32 Delphi sample to C++

USING BDS2006:
I'm trying to convert the Graphics32 Resampler_ex example in C++, but i can't even understand what happens in some codes, or how to rewrite that code in C++.
In that sample there's a combobox to choose what resampler to use:
This is the Deplhi code in his OnChange event:
procedure TfmResamplersExample.KernelClassNamesListClick(Sender: TObject);
var
Index: Integer;
begin
Index := KernelClassNamesList.ItemIndex;
if Src.Resampler is TKernelResampler then
with TKernelResampler(Src.Resampler) do
begin
Kernel := TCustomKernelClass(KernelList[Index]).Create;
LbParameter.Visible := (Kernel is TAlbrechtKernel) or
{$IFDEF Ex}
(Kernel is TGaussianKernel) or
(Kernel is TKaiserBesselKernel) or
(Kernel is TNutallKernel) or
(Kernel is TBurgessKernel) or
(Kernel is TBlackmanHarrisKernel) or
(Kernel is TLawreyKernel) or
{$ENDIF}
(Kernel is TSinshKernel);
gbParameter.Visible := LbParameter.Visible;
SetKernelParameter(Kernel);
CurveImage.Repaint;
end;
end;
where:
{ TClassList }
{ This is a class that maintains a list of classes. }
TClassList = class(TList)
protected
function GetItems(Index: Integer): TClass;
procedure SetItems(Index: Integer; AClass: TClass);
public
function Add(AClass: TClass): Integer;
function Extract(Item: TClass): TClass;
function Remove(AClass: TClass): Integer;
function IndexOf(AClass: TClass): Integer;
function First: TClass;
function Last: TClass;
function Find(AClassName: string): TClass;
procedure GetClassNames(Strings: TStrings);
procedure Insert(Index: Integer; AClass: TClass);
property Items[Index: Integer]: TClass read GetItems write SetItems; default;
end;
ResamplerList: TClassList;
My problems are on this line
Kernel := TCustomKernelClass(KernelList[Index]).Create;
How can i convert this line in C++?
EDIT AFTER THE COMMENTS AND THE ANWERS:
Ok, seems beyond my undertanding. For my purposes, it will suffice to be able to replicate what this code do without too much hassle.
Could it be possible to instantiate the right class just using a switch based on the itemindex?
These are the 4 classes i should instantiate:
class DELPHICLASS TNearestResampler;
class PASCALIMPLEMENTATION TNearestResampler : public Gr32::TCustomResampler
{
typedef Gr32::TCustomResampler inherited;
[...]
}
class DELPHICLASS TLinearResampler;
class PASCALIMPLEMENTATION TLinearResampler : public Gr32::TCustomResampler
{
typedef Gr32::TCustomResampler inherited;
[...]
};
class DELPHICLASS TDraftResampler;
class PASCALIMPLEMENTATION TDraftResampler : public TLinearResampler
{
typedef TLinearResampler inherited;
[...]
};
class DELPHICLASS TKernelResampler;
class PASCALIMPLEMENTATION TKernelResampler : public Gr32::TCustomResampler
{
typedef Gr32::TCustomResampler inherited;
[...]
};
I don't ever get how could i assign one of them to "Kernel"....
The Delphi code relies on Delphi virtual constructors. This functionality does not exist in C++.
If you wanted to translate the code literally then you'd need to find a way to instantiate the class by calling the virtual Delphi constructor. You cannot do that in C++ so you'd need some Delphi code to glue it all together. Remy's answer here shows how to use __classid() to obtain a Delphi metaclass. You'd then need to pass that metaclass to a Delphi function to perform the instantiation.
Frankly I would view that as being a rather convoluted solution to the problem. Instead I think I'd replace the class list with a function list. The functions in the list would have the task of instantiating a new instance of a kernel. So instead of adding a class to the list, you add a function that creates a new instance of the class. In fact you might want a map between name and function, it all depends on your needs.
From what I remember from Delphi programming, this will actually instantiate the same type of class, which currently is kept in KernelList[index] and then cast it back to TCustomKernelClass. AFAIK there is no such mechanism in C++, but you can solve it by introducing virtual CreateInstance method:
class TCustomKernelClass
{
public:
virtual TCustomKernelClass * CreateInstance() = 0;
}
class TDerivedKernelClass
{
public:
TCustomKernelClass * CreateInstance()
{
return new TDerivedKernelClass();
}
}
You'll have to introduce some changes in the classes though. I doubt it can be solved directly in C++.
Edit: In response to comments
Ok, I see now, there are class definitions kept in that list. Since RTTI in C++ is not as extensive as in Delphi, I'd change the code to (written from memory, may not compile):
std::vector<std::function<TBaseKernelClass(void)>> KernelList;
KernelList.push_back([]() { return new TDerivedKernelClass(); });
// (...)
Kernel = KernelList[index]();

Lazarus/Pascal: Problems with dynamic Toggleboxes

We have two classes which are important:
uSitz, uGUI.
unit uSitz;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, stdctrls;
type txyTogglebox=class(TTogglebox)
private
x:integer;
y:integer;
public
constructor create(xi,yi:integer;TheOwner:tobject);
end;
implementation
constructor txytogglebox.create(xi,yi:integer;TheOwner:tobject);
begin
x:=xi;
y:=yi;
end;
end.
It inherits the class TToggleBox!
So then we have the uGUI:
unit uGUI;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, usaal, usitz, stdctrls;
type
{ TForm1 }
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
i,j:integer;
saal:tsaal;
xytoggles: array of array of txytogglebox;
implementation
{$R *.lfm}
{ TForm1 }
procedure toggleboxclick(Sender:TObject);
begin
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
setlength(xytoggles, 10, 10);
saal.create(10,10);
for i:=0 to 10 do
begin
for j:=0 to 10 do
begin
xytoggles[i,j].create(i,j,form1);
xytoggles[i,j].parent:=form1;
xytoggles[i,j].Visible:=true;
xytoggles[i,j].top:=i*10;
xytoggles[i,j].left:=j*10;
xytoggles[i,j].width:=10;
xytoggles[i,j].height:=10;
xytoggles[i,j].onclick:=#toggleboxclick;
end;
end;
end;
end.
So the compiler says that we have an error #: ugui.pas(64,29) Error: Incompatible types: got "<address of procedure(TObject);Register>" expected "<procedure variable type of procedure(TObject) of object;Register> ".
If i change the procedure "toggleboxclick" to TForm1.toggleboxclick i will get a new error: nullpointer-exception (sigserv or something like this) # "xytoggles[i,j].parent:=form1;". How i can solve this problem.
At last: Sry for my english ;)
If i change the procedure "toggleboxclick" to TForm1.toggleboxclick
That is the correct way to do it
i will get a new error: nullpointer-exception (sigserv or something like this) # "xytoggles[i,j].parent:=form1;". How i can solve this problem.
You cannot use object.create. You need to use object := TClass.create. I.e.: In your case:
saal := tsaal.create(10, 10);
xytoggles[i,j] := txyTogglebox.create(i,j,form1);
It is also bad style to have global variables like i,j,saal, xytoggles. Better make i,j local variables; and saal, xytoggles class fields