rtti

Delphi - Invoke Record method per name

依然范特西╮ 提交于 2019-12-04 13:03:00
问题 I wrote a scriptlanguage for my applications and my goal is to make it possible to publish any type from delphi in the script. I use rtti to automatize this task. For any instance type like classes I use the following code to find and call a method from script. var Info : TRttiType; Meth : TRttiMethod; Param : TArray<TValue>; Result : TValue; AnyClass : TClass; begin ... Info := RttiContext.GetType(AnyClass); Meth := Info.GetMethod('AMethod'); Setlength(Param, 1); Param[0] := TValue.From

Getting the Unit Name which belongs to any type (TRttiType)

ε祈祈猫儿з 提交于 2019-12-04 12:07:51
问题 I need to get the name of the unit (namespace) of any TRttiType. so far, I have tried the following. 1) using the PTypeData.UnitName , this solution works, but only when the TTypeKind is tkClass. procedure ListAllUnits; var ctx : TRttiContext; lType: TRttiType; Units: TStrings; begin Units:=TStringList.Create; try ctx := TRttiContext.Create; for lType in ctx.GetTypes do if lType.IsInstance then //only works for classes if Units.IndexOf(UTF8ToString(GetTypeData(lType.Handle).UnitName))<0 then

How to link “parallel” class hierarchy?

霸气de小男生 提交于 2019-12-04 10:59:27
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 TMenuItem then Result := TMenuItemFrobber.Create(TMenuItem(AComponent)) else Result := TDefaultFrobber

How to get the class type reference by its name in Delphi XE?

点点圈 提交于 2019-12-04 10:11:50
I'm actually trying to use Rtti to implent a generic method invoker. It should work like this: I'll provide the class name, method name, and arguments the invoker will do its work by invoking the specified method of this class So I need the class reference in order to get its Rtti information and seek for the method I want to invoke. Is there any way to do that without implementing a class reference list of the classes I want to be working with? To get the class reference using his name you must use the TRttiContext.FindType function passing the Name of the class and the retrieve the instance

Run-time type information in C++

房东的猫 提交于 2019-12-04 09:02:40
What is runtime type control in C++? Naveen 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); } 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 the C++ Programming Language by Bjarne Stroustrup sharptooth It's dynamic_cast functionality - your code can

Do C++ POD types have RTTI?

二次信任 提交于 2019-12-04 08:24:37
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. Nicol Bolas There are two kinds of types (for the purposes of RTTI): polymorphic types and non-polymorphic types. A polymorphic type is a type that has a virtual function, in

Static cast vs. dymamic cast for traversing inheritance hierarchies

时光毁灭记忆、已成空白 提交于 2019-12-04 07:01:13
I saw one book on C++ mentioning that navigating inheritance hierarchies using static cast is more efficient than using dynamic cast. Example: #include <iostream> #include <typeinfo> using namespace std; class Shape { public: virtual ~Shape() {}; }; class Circle : public Shape {}; class Square : public Shape {}; class Other {}; int main() { Circle c; Shape* s = &c; // Upcast: normal and OK // More explicit but unnecessary: s = static_cast<Shape*>(&c); // (Since upcasting is such a safe and common // operation, the cast becomes cluttering) Circle* cp = 0; Square* sp = 0; // Static Navigation of

RTTI information for method pointer

浪子不回头ぞ 提交于 2019-12-04 04:12:56
问题 Is it possible to obtain RTTI information about a TMethod ? I can get the instance by Instance := TObject(Method.Data); so I can get the RTTI type of the instance, but how can I get the correct TRttiMethod ? I want to check for attributes on a method passed in using a method pointer. 回答1: This approach works in theory, and there's a good change it will work in practice, but there are a couple of things that could prevent you from getting hold of the TRttiMethod . The TMethod record says Data:

How to modify delphi property Getter/Setter with RTTI?

末鹿安然 提交于 2019-12-04 03:59:02
问题 I would like to replace the getter/setter for properties using RTTI. I know that you can access the getter setter with TPropInfo.SetProc/GetProc and I know that these fields points to different data depending if the property uses virtual methods, direct field access or static methods. I'm interesting on replacing propertiy setters/getters that point to virtual methods with custom virtual methods. TRttiInstanceProperty(RttiProperty).PropInfo^.SetProc := ? // SomeOtherInstance.Setter

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

喜欢而已 提交于 2019-12-04 01:01:31
问题 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