rtti

How to copy the properties of one class instance to another instance of the same class?

这一生的挚爱 提交于 2019-12-01 13:24:11
I want to duplicate a class. It is sufficient that I copy all properties of that class. Is it possible to: loop thru all properties of a class? assign each property to the other property, like a.prop := b.prop ? The getters and setters should take care of the underlying implementation details. EDIT: As Francois pointed out I did not word my question carefully enough. I hope the new wording of the question is better SOLUTION: Linas got the right solution. Find a small demo program below. Derived classes work as expected. I didn't know about the new RTTI possibilities until several people

How can I test if an unknown Delphi RTTI TValue reflects an object that is ANY type of generic TList<> (or at least TEnumerable<>)?

时间秒杀一切 提交于 2019-12-01 09:08:41
In Delphi, if I have a TValue instance reflecting an unknown object, how can I test if this object is an instance of ANY kind of generic TEnumerable<> (or even better, also which specific generic enumerable type it is an instance of, e.g. TList<> )? NOTE: I already know how to easily check its exact type, i.e. with the .BaseType property of the corresponding TRttiType of the TValue , resulting in for example TList<string> , but what I want to test is rather if it is a TList<> of any sub-item type . To exemplify how this hypothetical code "IsAnyKindOfGenericEnumerable()" would work, here is

How can I test if an unknown Delphi RTTI TValue reflects an object that is ANY type of generic TList<> (or at least TEnumerable<>)?

橙三吉。 提交于 2019-12-01 06:05:06
问题 In Delphi, if I have a TValue instance reflecting an unknown object, how can I test if this object is an instance of ANY kind of generic TEnumerable<> (or even better, also which specific generic enumerable type it is an instance of, e.g. TList<> )? NOTE: I already know how to easily check its exact type, i.e. with the .BaseType property of the corresponding TRttiType of the TValue , resulting in for example TList<string> , but what I want to test is rather if it is a TList<> of any sub-item

Can I ungarble GCC's RTTI names?

痞子三分冷 提交于 2019-12-01 04:42:18
Using gcc, when I ask for an object/variable's type using typeid, I get a different result from the type_info::name method from what I'd expect to get on Windows. I Googled around a bit, and found out that RTTI names are implementation-specific. Problem is, I want to get a type's name as it would be returned on Windows. Is there an easy way to do this? Gregory Pakosz If it's what you're asking, there is no compiler switch that would make gcc behave like msvc regarding the name returned by type_info::name() . However, in your code you can rely on the gcc specific __cxa_demangle function. There

C++ Class References

淺唱寂寞╮ 提交于 2019-12-01 04:23:04
问题 Coming from Delphi, I'm used to using class references (metaclasses) like this: type TClass = class of TForm; var x: TClass; f: TForm; begin x := TForm; f := x.Create(); f.ShowModal(); f.Free; end; Actually, every class X derived from TObject have a method called ClassType that returns a TClass that can be used to create instances of X. Is there anything like that in C++? 回答1: Apparently modern Delphi supports metaclasses in much the same way as original Smalltalk. There is nothing like that

Why is dynamic_cast evil or not ? Should I use dynamic_cast in this case?

泄露秘密 提交于 2019-12-01 03:50:33
Some say the use of dynamic_cast often means bad design and dynamic_cast can be replaced by virtual functions why is the use of dynamic_cast considered bad design? Suppose I have I function name func(Animal* animal, int animalType) , the implementation in func is like: bool func(Animal* animal, int animalType) { ... /* Animal is the base class of Bear, Panda, Fish .... dynamic_cast animal to real animals(Bear, Panda, Fish...) according to animalType. Do some processing with this specific type of animal, using its additional information beyond base class Animal. */ } Is this case a proper use

Get/Set sub properties ussing RTTI

时光毁灭记忆、已成空白 提交于 2019-11-30 23:58:57
Given the following code snippet below, using GetPropValue(MyComponent,'MySubComponent.Prop1') raises an EPropertyError exception. How can I retrieve or set the values of SubProperties using GetPropValue / SetPropValue? Type TMySubComponent = class(TInterfacedPersitent) private FProp1: Integer; published property Prop1: integer read FProp1 write FProp1; end; TMyComponent = class(TCompoent) private FMySubComponent : TMySubcomponent; published property MySubComponent: TMySubComponent read FMySubComponent write FMySubComponent ; end; The dot notation you used in your question is not supported.

Get/Set sub properties ussing RTTI

南笙酒味 提交于 2019-11-30 18:33:49
问题 Given the following code snippet below, using GetPropValue(MyComponent,'MySubComponent.Prop1') raises an EPropertyError exception. How can I retrieve or set the values of SubProperties using GetPropValue / SetPropValue? Type TMySubComponent = class(TInterfacedPersitent) private FProp1: Integer; published property Prop1: integer read FProp1 write FProp1; end; TMyComponent = class(TCompoent) private FMySubComponent : TMySubcomponent; published property MySubComponent: TMySubComponent read

How can I set the $RTTI directive for the entire project?

本秂侑毒 提交于 2019-11-30 15:55:13
问题 I'm working on migrating an old project from Delphi 2007 to Delphi 2010. One thing I've found is that the resulting executable has more than doubled in size, and the original was already quite big. (Over 50 MB.) I suspect that a lot of it has to do with extended RTTI. Since the project predates Delphi 2010, it doesn't use extended RTTI anywhere, and I'd like to be conservative about including it. Is there any way to use the Project Options dialog to globally set {$RTTI EXPLICIT METHODS([])

Accesing a strict private field using the RTTI

和自甴很熟 提交于 2019-11-30 15:32:47
问题 consider this simple code {$APPTYPE CONSOLE} uses Rtti, SysUtils; type {$M+} TFoo = class strict private class var Field1 : Integer; field2 : Integer; private field3 : Integer; class var Field4 : Integer; end; Var ctx : TRttiContext; f : TRttiField; begin try ctx:=TRttiContext.Create; for f in ctx.GetType(TFoo).GetFields do Writeln(f.Name); Writeln('Done'); readln; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end. When you run this, only the field3 is listed. it seems