rtti

Custom run-time type system/ library for C++

馋奶兔 提交于 2019-12-06 07:12:57
问题 In an application I'm making at the moment, I have an EventDispatcher class that works with a base Event class. The dispatcher is not templated, it works with the run-time types of each event; this is to allow scripts to inherit from the base Event class and make their own types of events. It'd like this event dispatcher to handle inheritance of events as well. For example, I have FooEvent that inherits from a FooBaseEvent ; whenever a FooEvent occurs, callbacks interested in FooBaseEvent are

Delphi RTTI SetValue for Enumerations

若如初见. 提交于 2019-12-06 05:47:36
问题 How do I use RTTI to set an enumerated field's value ? I.e. type TCPIFileStatus= (fsUnknown, fsProcessed); TTest = class FStatus: TCPIFileStatus; end; ... var Data: TTest; Ctx: TRttiContext; Status : TCPIFileStatus; begin Data := TTest.Create; Status := fsProcessed; Ctx.GetType(Data.ClassType).GetField('FStatus').SetValue(Data, Status); end; I get "Invalid class typecast." NB:I need to use RTTI because I will not always know the object type or field name at design time. 回答1: you must pass a

Delphi 2010 RTTI : Use TValue to store data

大憨熊 提交于 2019-12-06 05:18:12
I would like to be able to use TValue to store Data in a TList<>. Like in : type TXmlBuilder = class type TXmlAttribute = class Name: String; Value: TValue; // TValue comes from Rtti end; TXmlNode = class Name: String; Parent: TXmlNode; Value: TXmlNode; Attributes: TList<TXmlAttribute>; Nodes: TList<TXmlNode>; function AsString(Indent: Integer): String; end; ... public ... function N(const Name: String): TXmlBuilder; function V(const Value: String): TXmlBuilder; function A(const Name: String; Value: TValue): TXmlBuilder; overload; function A<T>(const Name: String; Value: T): TXmlBuilder;

How to link “parallel” class hierarchy?

我是研究僧i 提交于 2019-12-06 05:11:34
问题 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(AComponent: TComponent): IFrobber; begin if AComponent is TCustomAction then Result := TActionFrobber.Create(TCustomAction(AComponent)) else if AComponent is

Rtti data manipulation and consistency in Delphi 2010

这一生的挚爱 提交于 2019-12-06 03:33:22
Has anyone an idea, how I can make TValue using a reference to the original data? In my serialization project, I use (as suggested in XML-Serialization ) a generic serializer which stores TValues in an internal tree-structure (similar to the MemberMap in the example). This member-tree should also be used to create a dynamic setup form and manipulate the data. My idea was to define a property for the Data: TDataModel <T> = class {...} private FData : TValue; function GetData : T; procedure SetData (Value : T); public property Data : T read GetData write SetData; end; The implementation of the

Do C++ POD types have RTTI?

懵懂的女人 提交于 2019-12-06 03:15:56
问题 As I understand how RTTI is implemented in various C++ compilers (such as GCC), a pointer to the type_info data is stored in the vtable data of each class. And also as mentioned here, POD type may not have a vtable . But if POD types may not have a vtable then where is the pointer to the type_info stored? I know it is implementation-specific, but it would be better to be aware of a C++ compiler (such as GCC) internals. 回答1: There are two kinds of types (for the purposes of RTTI): polymorphic

Run-time type information in C++

和自甴很熟 提交于 2019-12-06 02:14:34
问题 What is runtime type control in C++? 回答1: It enables you to identify the dynamic type of a object at run time. For example: class A { virtual ~A(); }; class B : public A { } void f(A* p) { //b will be non-NULL only if dynamic_cast succeeds B* b = dynamic_cast<B*>(p); if(b) //Type of the object is B { } else //type is A { } } int main() { A a; B b; f(&a); f(&b); } 回答2: It is not just about dynamic_cast, but the entire RTTI is part of it. The best place to learn about RTTI is section 15.4 of

Getting the string name of an interface using Delphi RTTI

南楼画角 提交于 2019-12-06 02:07:48
问题 I have proved that I can get the name of an interface from its GUID using Delphi 2010 (eg IMyInterface converted to the string 'IMyInterface'. I'd like to achieve this in Delphi 7 (for compatibility). Is this possible? Or are there fundamental compiler limitations. 回答1: Yes you can get it, the following shows using the IExample type how you can get the name. The old Delphi 7 RTTI was done through the TypInfo Unit. program Project6; {$APPTYPE CONSOLE} uses SysUtils,TypInfo; type IExample =

Pointer to the start of an object (C++)

一笑奈何 提交于 2019-12-06 00:28:01
I need a way to get a pointer to the start of an object in C++. This object is used inside a template so it can be any type (polymorphic or not) and could potentially be an object that uses multiple inheritance. I found this article which describes a way to do it (see the section called "Dynamic Casts") using typeid and a dynamic_cast to void* in the case that T is a polymorphic type. This works perfectly well on MSVC, however on GCC (4.x) it seems to fall on its arse and spits out a compiler error when it is used with a non-polymorphic type. Does anyone know a way to: Make GCC behave itself,

isMemberOfClass vs comparing classes with ==

橙三吉。 提交于 2019-12-05 22:22:05
Is there any real difference between: id value; BOOL compare1 = [value isMemberOfClass:[SomeClass class]]; BOOL compare2 = [value class] == [SomeClass class]; to check if value is a SomeClass object? If value is an NSProxy , isMemberOfClass: will properly check the proxied object, the other construct, I believe, won't (I think it will clumsily duplicate isProxy: ). 来源: https://stackoverflow.com/questions/3126476/ismemberofclass-vs-comparing-classes-with