Board index » cppbuilder » BDS2006: RTTI - determing a Boolean type

BDS2006: RTTI - determing a Boolean type


2007-07-15 02:13:38 AM
cppbuilder106
Hello,
for Booleans an Enum is used. The only way I found so far determining
whether that Enum represents an Boolean Type is to check whether the
Name part of the TTypeInfo-record is "Boolean". Is there any other
possibility, don't want to compare a string every time?
Thanks and greetings
Udo
 
 

Re:BDS2006: RTTI - determing a Boolean type

"Udo Weik" < XXXX@XXXXX.COM >wrote in message
Quote
Is there any other possibility, don't want to compare a string every time?
You can check if your target data contains a pointer to the bool's TTypeInfo
structure itself, rather than just compare its Name member by itself.
Unfortunately, there is no way to get a TTypeInfo structure for POD types in
C++. You need Delphi code for that. Fortunately, you can include Delphi
units in C++ projects. For example:
--- GetPODTypeInfo.pas ---
unit GetPODTypeInfo;
interface
uses TypInfo;
function GetBooleanTypeInfo: PTypeInfo;
{ other types here if desired... }
implementation
function GetBooleanTypeInfo: PTypeInfo;
begin
Result := TypeInfo(Boolean);
end;
end.
--- CppUnit.cpp ---
#include "GetPODTypeInfo.hpp"
void __fastcall CheckBoolType(PTypeData TypeData)
{
static PTypeInfo BooleanType = GetBooleanTypeInfo();
if( *(TypeData->BaseType) == BooleanType )
ShowMessage("Is a bool!");
else
ShowMessage("Is not a bool!");
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
PPropInfo PropInfo;
PTypeData TypeData;
TTypeKinds EnumKind = TTypeKinds() << tkEnumeration;
PropInfo = GetPropInfo(CheckBox1, "Checked", EnumKind);
TypeData = GetTypeData(*(PropInfo->PropType));
CheckBoolType(TypeData);
PropInfo = GetPropInfo(CheckBox1, "Alignment", EnumKind);
TypeData = GetTypeData(*(PropInfo->PropType));
CheckBoolType(TypeData);
}
Simple add GetPODTypeInfo.pas to your project and then compile it to
generate the .hpp file. You can then compile the rest of your project
normally.
Gambit