Delphi Compatibility Macros

From RAD Studio
Jump to: navigation, search

Go Up to The Preprocessor Index


The five macros described here are used for compatibility between Delphi and C++ code, typically when generating .hpp files for code written in Delphi. These macros can be found in include\windows\rtl\sysopen.h.

Consider the following example of a Delphi procedure:

procedure myProc(a: array of Integer);

In this case a is called an open array because you can pass any type of integer dynamic arrays to it. Any type of integer static arrays are passable. This can happen because the compiler considers the procedure to be as follows:

procedure myProc(a: ^Integer; a_size: Integer);

where a is a pointer to the first element of the passed array and a_size is the number of elements.

This technique is used when generating the C++ .hpp files for a Delphi source:

void myProc(int* a, int a_size)

The code snippet above is the definition of the C++ proc in this case.

OPENARRAY

#define OPENARRAY(type, values) \
   OpenArray<type>values, OpenArrayCount<type>values.GetHigh()

The OPENARRAY macro can be used as in the following example:

myProc(OPENARRAY(TVarRec,(1, 2, 3, 4, 5)))
      //in which case the expanded code looks like:
myProc(OpenArray<TVarRec>(1, 2, 3, 4, 5), OpenArrayCount<TVarRec>(1, 2, 3, 4, 5).GetHigh())
     //which corresponds to what Delphi methods expect.

The same technique is used for the rest of the macros.

ARRAYOFCONST

#define ARRAYOFCONST(values) \
   OpenArray<TVarRec>values, OpenArrayCount<TVarRec>values.GetHigh()

The ARRAYOFCONST macro can be used as in the following example:

myProc(ARRAYOFCONST((1, 2, 3, 4, 5)))
     //in which case the expanded code looks like:
myProc(OpenArray<TVarRec>(1, 2, 3, 4, 5), OpenArrayCount<TVarRec>(1, 2, 3, 4, 5).GetHigh())
     //which corresponds to what Delphi methods expect.

ARRAYSIZE

#define ARRAYSIZE(a) (sizeof(a)/sizeof(a[0]))

EXISTINGARRAY

For EXISTINGARRAY, the a parameter is an existing array where an open array is expected.

#define EXISTINGARRAY(a) (a), ((sizeof(a)/sizeof(a[0]))-1)

SLICE

For SLICE, a is a part of an existing array where an open array is expected.

#define SLICE(a, n) (a), (n - 1)

VCL_MESSAGE_HANDLER

#define VCL_MESSAGE_HANDLER(msg,type,meth)          \
         case    msg:                              \
           meth(*((type *)Message));               \
           break;

__interface

#define __interface struct