Talk:C++Builder Keyword Extensions
Contents
__delphirtti
Hello,
Can we add "__delphirtti(...)" to the list of keyword extensions?
It's similar to __classid except that it returns the TypeInfo* of the type [not the metaclass]. Earlier versions of C++Builder allowed __delphirtti(..) for Interface and DynamicArray<..> types. Now we allow __delphirtti(...) for any publishable types [i.e. types that can appear in the __published section of a class].
The following example illustrates simple usage of __delphirtti:
static bool validateTypeInfo(PTypeInfo pInfo, TTypeKind kind, const char* name) { VERIFY(pInfo != 0); if ((pInfo->Kind != kind) || (AnsiString(pInfo->Name) != name)) printf("ERROR: Kind(%d), Name(%s)\n", pInfo->Kind, AnsiString(pInfo->Name).c_str()); VERIFY(pInfo->Kind == kind); VERIFY(strcmp(AnsiString(pInfo->Name).c_str(), name) == 0); return true; } bool test() { validateTypeInfo(__delphirtti(ITest), tkInterface, "ITest"); validateTypeInfo(__delphirtti(DynamicArray<double>), tkDynArray, "TDoubleDynArray"); validateTypeInfo(__delphirtti(TSampleStruct), tkClass, "TSampleStruct"); validateTypeInfo(__delphirtti(int), tkInteger, "int"); validateTypeInfo(__delphirtti(AnsiString), tkLString, "AnsiString"); validateTypeInfo(__delphirtti(WideString), tkWString, "WideString"); validateTypeInfo(__delphirtti(UnicodeString), tkUString, "UnicodeString"); validateTypeInfo(__delphirtti(Variant), tkVariant, "Variant"); validateTypeInfo(__delphirtti(Set<SampleEnum, etNone, etALot>), tkSet, "Set"); validateTypeInfo(__delphirtti(OleVariant), tkVariant, "OleVariant"); validateTypeInfo(__delphirtti(Currency), tkFloat, "Currency"); validateTypeInfo(__delphirtti(TDateTime), tkFloat, "TDateTime"); return true; }
There are two things to keep in mind with __delphirtti:
- 1. C++ does not have strong aliases. So __dephirtti(xxx) of typedefs will always return the 'Name' of the first typedef the compiler encountered. You can see that with DynamicArray types.
- 2 Previously C++ code that wanted the typeinfo of a Delphi class would use "__classid(className)->ClassInfo();". This is less efficient as the TypeInfo is retrieve from the meta class at runtime. You can now just use "__delphirtti(className);".
__declspec
Maybe we should have a single entry for __declspec (not to list all the attributes).
Vadimb 06:23, 1 July 2011 (PDT)