We often hear/read that one should avoid dynamic casting. I was wondering what would be 'good use' examples of it, according to you?
Edit:
@Sam: yes, I'm aware of that other thread: it is indeed when reading one of the first answers there that I asked my question!
...
I'm writing a Delphi expert. I need to be able to write a value to a property on a property which is an object. E.g. I have a GroupBox on the form and I want to edit the Margins.Left property. I'm using the following procedure to do it but if gives an AV on the marked line.
The procedure takes a component from the (property editor) the ...
Which of these pieces of code is faster?
if (obj is ClassA) {}
if (obj.GetType() == typeof(ClassA)) {}
Edit:
I'm aware that they don't do the same thing.
...
I am familiar with C++ RTTI, and find the concept interesting.
Still there exist a lot of more ways to abuse it than to use it correctly (the RTTI-switch dread comes to mind). As a developer, I found (and used) only two viable uses for it (more exactly, one and a half).
Could you share some of the ways RTTI is a viable solution to a pr...
Although I'm doubtful, I'm curious as to whether it's possible to extract primitive-type template parameters from an existing type, perhaps using RTTI.
For example:
typedef std::bitset<16> WordSet;
Would it be possible to extract the number 16 in the above code without hard-coding it elsewhere? Compiler specific implementations are w...
Is there any possibility that GetPropInfo returns nil even if the given class is declared with correct {$METHODINFO} directives.
type
...
...
{$METHODINFO ON}
TMyClass = class
private
fField: integer;
published
property Field: integer read fField write fField;
end;
{$METHODINFO OFF}
...
.....
There are many ways to check programs for memory leaks. You end up with that list of pointers to leaked memory blocks, but is there a good way to find out more information for each block? For example: if I know that the object was a string, the actual string value could make finding the leak a lot easier.
Is there a backdoor into RTTI t...
What's a good way to serialize a Delphi object tree to XML--using RTTI and not custom code?
I would have loved to find that this feature is already built into Delphi, but it doesn't seem to be.
I've found a few components (posted, below) that seem like they might perform this function. Have you used any of them or some other offering?...
I've got a little class hierarchy where each class corresponds to a certain TComponent descendent (say base class TDefaultFrobber with descendents TActionFrobber and TMenuItemFrobber, corresponding to TComponent, TCustomAction and TMenuItem, respectively). Now I want a factory (?) function something like this:
function CreateFrobber(ACo...
I understand that there is a resource hit from using RTTI, but how big is it? Everywhere I've looked just says that "RTTI is expensive," but none of them actually give any benchmarks or quantitative data reguarding memory, processor time, or speed.
So, just how expensive is RTTI? I might use it on an embedded system where I have only ...
Remember the old class hierarchy posters that used to come w/Delphi?
I'm wanting a full class hierarchy browser for ALL my Delphi classes, including the custom ones I've built and installed on the palette, plus third-party components. Ideally easily searchable by class name (including "whole word only" searches, so partial matches don'...
i have a list of class instances of various kinds. i need to be able to create a new instance of a class without knowing for sure what to create. all the objects involved have the same ancestor. the actual copying of the object's member variables is easy...it's the creation of the new object where i have a problem.
admittedly i could...
In Delphi 2007, I added a new string type to my project:
type
String40 = string;
This property is used in a class:
type
TPerson = class
private
FFirstName = String40;
published
FirstName: string40 read FFirstName write FFirstName;
end;
During runtime, I want to get the name of the property FirstName by using RTTI....
For example, is there a way to find out that this class has a virtual constructor (at runtime)?
TMyClass = class(TObject)
MyStrings: TStrings;
constructor Create; virtual;
end;
For example, in this code I would like to test if the class referenced by Clazz has a virtual constructor:
procedure Test;
var
Clazz: TCl...
In C++ the problem is simple.
I have 2 classes one contains the other as part of its implementation.
struct A
{
void do_something()
{
};
};
struct B
{
A obj_A;
void hello_world()
{
};
};
Now the problem is that structure B is one byte larger if A is part of B when I do a sizeof(B) and object of type B. A ...
Is it possible in Delphi to use RTTI (or something else) to check if a class is declared as abstract?
Something like:
TMyAbstractClass = class abstract(TObject)
// ...
end;
...
if IsAbstract(TMyAbstractClass.ClassInfo) then
ShowMessage('Yeah')
else
ShowMessage('Computer says no...');
...
I'm using Visual Studio and performing a valid dynamic cast. RTTI is enabled.
Edit : Updated the code to be more realistic
struct base
{
virtual base* Clone()
{
base* ptr = new base;
CopyValuesTo( ptr );
return ptr;
}
virtual void CopyValuesTo( base* ptr )
{
...
}
virtual ~ba...
Hi, first apologies for my english, I hope it make sense what I`ve wrote here. Now to my problem.
How can I get the string representation of the content type of a Variant using TypInfo.GetEnumName(). I have tried the following, without luck, i get a numeric representation.
myString := GetEnumName( TypeInfo(TVarType), TVarData(myVar).V...
I'm trying to implement exception handling for an embedded OS and I'm stuck at how to detect the type of the thrown "exception" (to select the appropriate handler).
The saving and restoring context parts of the exception handling are already done, but I can't have specific handles since I can't detect the type of the thrown 'exception'....
I'm familiar with using Delphi RTTI to access "simple" properties (ints/enums/strings, etc) but I cannot grasp how to work with Array properties.
I'm starting by looking for array equivalents for the GetPropValue/SetPropValue calls. I'd expect to see similar ones to these, but taking an extra "index" parameter, but can't seem to find a...