rtti

Delphi How to get default value for property using RTTI

不羁岁月 提交于 2019-11-29 16:17:24
If I have a class like this: TServerSettings = class(TSettings) strict private FHTTPPort : Integer; published property HTTPPort : Integer read FHTTPPort write FHTTPPort default 80; end; How can I get the default attribute of the HTTPPort property using RTTI? Like this: {$APPTYPE CONSOLE} uses System.TypInfo; type TMyClass = class strict private FMyValue: Integer; published property MyValue: Integer read FMyValue default 42; end; var obj: TMyClass; PropInfo: PPropInfo; begin obj := TMyClass.Create; PropInfo := GetPropInfo(obj, 'MyValue'); Writeln(PropInfo.Default); end. Note that the class as

Delphi 2010 RTTI - RttiContext.FindType

你。 提交于 2019-11-29 15:36:46
With RttiContext.FindType('Classes.TStringList') I get RttiType of TStringList with no problem. But with RttiContext.FindType('MyUnit.TMyClass') I always get nil (of course MyUnit is in uses clause). Why, what is wrong? Example: unit MyUnit; interface uses Classes; type TMyClass = class(TStringList) end; implementation end. Main unit: ... uses MyUnit, ... var oCont: TRttiContext; oType: TRttiType; begin oCont := TRttiContext.Create; try oType := oCont.FindType('MyUnit.TMyClass'); <== oType = nil !! ... Daniele Teti Probably the class has not included by the delphi linker in the final

access all elements of a record using RTTI

元气小坏坏 提交于 2019-11-29 15:24:19
I want to dump a complex / long record into a memo for debugging purpose TmyRecord = aValue : String aNumber : Real; Morenumbers : Integer ; .... .... end; I think Delphi XE 2 RTTI should give me the chance to get the Fieldname , Fieldtype and value within a loop, to write this record to a memo or ..... As starting point - record with simple types. For complex fields (array, class etc) explore RTTI unit type TmyRecord = record aValue: String; aNumber: Real; Morenumbers: Integer; end; var m: TMyRecord; rtype: TRTTIType; fields: TArray<TRttiField>; i: Integer; begin m.aValue := 'OK'; m.aNumber :

Why do I need to #include <typeinfo> when using the typeid operator?

荒凉一梦 提交于 2019-11-29 13:14:52
The typeid represents a C++ RTTI operator being also a C++ keyword. It returns a std::type_info object that holds (dynamic) type specific information. From what I understood from various sources, one MUST include <typeinfo> when using typeid , otherwise the program is ill-formed. In fact, my gcc5.2 compiler doesn't even compile the program if I don't include the before-mentioned header. I don't understand why is a header inclusion mandated for the usage of a C++ keyword . I understand mandating a header for whenever we use some object declared/defined in that header, but typeid is not of a

How do I typecast with type_info?

泄露秘密 提交于 2019-11-29 12:54:09
问题 I've stored a pointer to a type_info object. int MyVariable = 123; const std::type_info* Datatype = &typeid(MyVariable); How might I use this to typecast another variable to that type? I tried this, but it doesn't work: std::cout << ((*Datatype)3.14) << std::endl; Using the function form of typecasting doesn't work, either: std::cout << (*Datatype(3.14)) << std::endl; 回答1: I don't think such casting can be done. Suppose you could do "dynamic" casting like this at runtime (not to mean dynamic

Is there a relation between RTTI and exceptions?

流过昼夜 提交于 2019-11-29 12:13:52
问题 I remember coding on platforms that had both RTTI and exceptions disabled, and on others that had them both enabled. However, I cannot remember coding on a platform that would enable one and disable the other one. Is there any kind of dependency between the two concepts? Said differently, do exceptions need RTTI to function? Or the contrary? 回答1: No, Exceptions do not need RTTI functionality neither vice versa both are separate features. Some of the implementations might allow you to disable

Getting type of record field with RTTI fails for static arrays

假装没事ソ 提交于 2019-11-29 10:36:59
I am trying to get types for record fields in order to create correct comparer (as general solution for any/almost any record type). I can't find type information for static arrays: TArrFieldTest = record a: string; b: array[0..3] of byte; end; procedure Test; var rttiContext: TRttiContext; rttiType: TRttiType; rttiFields: TArray<TRttiField>; begin rttiType := rttiContext.GetType(TypeInfo(TArrFieldTest)); rttiFields := rttiType.GetFields; Assert(rttiFields[0].FieldType<>nil); // it's ok Assert(rttiFields[1].FieldType<>nil); // fail here! end; FieldType is nil for static array of any type. Any

How can I get the sub-item type of a TObjectList<T> purely by RTTI information (i.e. without using any actual object instance) in Delphi?

只愿长相守 提交于 2019-11-29 08:58:10
I'm implementing generic code for streaming arbitrary Delphi objects using RTTI, and in order to get this to work (more specifically, in order to get the loading part to work), I need to somehow get the sub-item type of a TObjectList<T> field without making use of any actual object instance. The obvious reason for the requirement to not use any actual object instance is that in the case of loading an object from a stream (based solely on the knowledge of the class type of the object to be loaded), I won't have any instance at all available before the loading has completed - I will rather only

C++: using typeinfo to test class inheritance

痴心易碎 提交于 2019-11-29 07:46:01
I have a pointer to a polymorphic type, p . I also have a type_info for a class somewhere in the same hierarchy, ti . If I just compare the typeid(*p) == ti , then I can test at runtime whether the pointer is pointing to a direct instance of that class. Is there a similar way to use C++'s RTTI to test whether *p inherits from that class? There's no way to do this in standard C++ alone. If you're using an implementation that has the Itanium C++ ABI 1 you can do this though, for example: #include <iostream> #include <typeinfo> #include <cxxabi.h> #include <memory> class base { protected: base()

dynamic_cast with RTTI disabled

和自甴很熟 提交于 2019-11-29 05:29:48
I'm curious to know what happens when compiling code with a dynamic cast whith RTTI disabled (either with -fno-rtti on GCC or with /GR- on visual studio). Does the compiler "falls back" to static_cast ? Since (at least on VS) it does only issue a warning, what will the compiled code do ? More specifically, what bad things could happen if I compile without RTTI a code where I'm sure that there are no error possible with dynamic_cast (i.e. where dynamic_cast could be safely replaced by a static_cast ) like this one : class A{ /*...*/ } ; class B : public A { int foo() { return 42 ;} } ; //... A