Delphirtti (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows the usage of the __delphirtti operator functionality.

Code

#if defined(VERBOSE)
  #define VERIFY_PRINT(expr) printf("(%s:%3d): %s\n", __FILE__, __LINE__, #expr);
#else
  #define VERIFY_PRINT(expr)
#endif

#define  VERIFY(expr) do {     
  VERIFY_PRINT(expr)           
  if (!(expr)) {               
    printf("'%s' FAILED!  (%s:%d)\n",(#expr, __FILE__, __LINE__)); 
    assert(0);                 
  }                            
} while (0)

class TSampleStruct : public TObject {
private:
  WideString         FLastName;
  AnsiString         FFirstName;
  double             FSalary;
__published:
  __property WideString    LastName = { read=FLastName, write=FLastName };
  __property AnsiString    FirstName = { read=FFirstName, write=FFirstName };
  __property double        Salary = { read=FSalary, write=FSalary };
};

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;
}

int main(int argc, char *argv[])
{
   validateTypeInfo(__delphirtti(AnsiString), tkLString, "AnsiString"); // true
   validateTypeInfo(__delphirtti(WideString), tkWString, "WideString"); // true

   return 0;
}

See Also