rtti

Accesing a strict private field using the RTTI

五迷三道 提交于 2019-11-30 14:50:54
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 which the RTTI does not support fields which are strict private or class var , So the questions are Is

Delphi Rtti for interfaces in a generic context

隐身守侯 提交于 2019-11-30 14:33:40
for a framework I wrote a wrapper which takes any object, interface or record type to explore its properties or fields. The class declaration is as follows: TWrapper<T> = class private FType : TRttiType; FInstance : Pointer; {...} public constructor Create (var Data : T); end; In the constructor I try to get the type information for further processing steps. constructor TWrapper<T>.Create (var Data : T); begin FType := RttiCtx.GetType (TypeInfo (T)); if FType.TypeKind = tkClass then FInstance := TObject (Data) else if FType.TypeKind = tkRecord then FInstance := @Data else if FType.TypeKind =

Problems throwing and catching exceptions on OS X with -fno-rtti

天涯浪子 提交于 2019-11-30 13:45:55
问题 The issue is somewhat similar to this question but the accepted answer does not really propose a solution or workaround. In our project, we have a dylib and the main executalble. The dylib is compiled with -fno-rtti , while the executable does use RTTI. The problem happens when an exception (e.g. std::bad_alloc ) is thrown from the dylib and is caught in the exe. (Before you yell "exceptions need RTTI so you must have it on!", please note that the RTTI necessary for exceptions is always

TRTTIContext multi-thread issue

倾然丶 夕夏残阳落幕 提交于 2019-11-30 12:42:08
Everything I've read indicates that TRTTIContext is thread-safe. However, TRTTIContext.FindType seems to fail (returns nil) occasionally when multithreading. Using a TCriticalSection around it fixes the issue. Note that I'm using XE6, and the issue doesn't seem to exist in XE. Edit: Seems to exist in all Delphi editions that have the new RTTI units. I've worked up a test project you can use to see for yourself. Create a new VCL project, drop a TMemo and a TButton, replace unit1 with below, and assign the Form1.OnCreate, Form1.OnDestroy and Button1.OnClick events. The key CS is the GRTTIBlock

Rtti accessing fields and properties in complex data structures

随声附和 提交于 2019-11-30 12:20:10
问题 As already discussed in Rtti data manipulation and consistency in Delphi 2010 a consistency between the original data and rtti values can be reached by accessing members by using a pair of TRttiField and an instance pointer. This would be very easy in case of a simple class with only basic member types (like e.g. integers or strings). But what if we have structured field types? Here is an example: TIntArray = array [0..1] of Integer; TPointArray = array [0..1] of Point; TExampleClass = class

How to create an instance of object with RTTI in Delphi 2010?

。_饼干妹妹 提交于 2019-11-30 12:11:38
问题 As we all known, when we call a constructor of a class like this: instance := TSomeClass.Create; The Delphi compiler actually do the following things: Call the static NewInstance method to allocate memory and initialize the memory layout. Call the constructor method to perform the initialization of the class Call the AfterConstruction method It's simple and easy to understand. but I'm not very sure how the compiler handle exceptions in the second and the third step. It seems there are no

What is `type_info::before` useful for?

只谈情不闲聊 提交于 2019-11-30 11:40:03
According to cplusplus.com, the std::type_info::before() function... Returns true if the type precedes the type of rhs in the collation order. The collation order is just an internal order kept by a particular implementation and is not necessarily related to inheritance relations or declaring order. So what is it useful for? Consider you want to put your type_info objects as keys into a map<type_info*, value> . The type_info doesn't have an operator < defined, so you must provide your own comparator. The only thing that is guaranteed to work from the type_info interface is the before()

How to use Delphi RTTI to get and set Record Values

家住魔仙堡 提交于 2019-11-30 11:37:33
问题 I'm attempting to use the enhanced RTTI features in Delphi XE or later, to read and write objects to XML. So far I've been successful with integers, floats, strings, enumerated types, sets and classes but can't output or read records correctly. The problem seems to be getting an instance (pointer) to the record property. //Outputs Properties To XML procedure TMyBase.SaveToXML(node: TJclSimpleXMLElem); var child , subchild : TjclSimpleXMLElem ; FContext : TRttiContext ; FType : TRttiType ;

What's a good way to serialize Delphi object tree to XML--using RTTI and not custom code?

牧云@^-^@ 提交于 2019-11-30 10:42:39
问题 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? Have you built your own? Am I missing something obvious, in Delphi? 回答1: You can use the JVCL TJvAppXMLFileStorage component to serialize TPersistent derived

How do I typecast with type_info?

时光怂恿深爱的人放手 提交于 2019-11-30 08:57:30
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; Mark B I don't think such casting can be done. Suppose you could do "dynamic" casting like this at runtime (not to mean dynamic_cast ). Then if you used the result of the cast to call a function the compiler could no longer do