Interface type checking

Top  Previous  Next

What is translated > Types > Records, Classes, Interfaces > Interfaces > Non-generic interfaces > Interface type checking

This section describes how Delphi2Cpp implements dynamic interface casting and type checking at runtime—analogous to Delphi’s built-in functions such as Supports, as and is.

 

 

Delphi2Cpp provides portable and compiler-specific implementations to support the following features:

 

- Safe type checking (Supports)

- Safe or throwing casting (as<T>)

- Interface-to-object back-conversion (FromInterface)

- Class type inspection (ObjectIs)

 

Supports

 

The Supports function checks whether an object or interface instance implements a specific interface and optionally retrieves a typed smart reference (DelphiInterface<T>).

If the call succeeds, AddRef is called and lifetime is managed automatically.

 

 

as_di<T> – Dynamic Interface Cast

 

The as_di<T> function attempts to cast an interface or object to another interface type using QueryInterface.

If the conversion fails, it throws std::bad_cast, similar to Delphi’s as.

 

Internally uses QueryInterface or GetInterface.
Returns a DelphiInterface<T> smart pointer for RAII.

 

 

ObjectIs and FromInterface

 

These utilities support runtime inspection and reconstruction of original object instances from interface references.

 

ObjectIs<T>(iface, ClassPtrType)

Checks whether an interface belongs to a given class type.

 

Internally uses dynamic_cast

 

FromInterface:

 

Reconstructs the underlying TObject* from an IInterface*, assuming the object implements ID2CObject.

 

ID2CObject must expose a method GetSelfPointer() returning the TObject*.

Internally uses QueryInterface on ID2CObject.

 

 

Delphi

 

if obj is TMyClass then ...

if Supports(obj, IMyInterface) then ...

 

 

C++Builder

 

 

DelphiInterface<IMyInterface> intf;

if (Supports(obj, intf)) { ... }

 

 

Portable C++ (non-C++Builder)

 

 

if (dynamic_cast<TMyClass*>(obj)) { ... }

if (obj->QueryInterface(IID_IMyInterface, (void**)&intf) == S_OK) { ... }

 

Delphi2Cpp provides optional helper functions like SupportsInterface<T>() to simplify portable C++ interface queries.

 

 



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content