Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Delphi's conditional compilation allow the defined symbols to contain values?

In Delphi, you can define symbols, just like in C/C++.

Delphi:

{$DEFINE MY_SYMBOL}

C/C++:

#define MY_SYMBOL

This allows you to check whether the symbol is defined by using {$IFDEF MY_SYMBOL} in Delphi, or #ifdef MY_SYMBOL in C/C++.

This is a boolean value - either the symbol is defined during pre-processing or not. However, C/C++ also allows you to assign values to preprocessor symbols. E.g.:

#ifdef DEBUG
#define DB_HOST "127.0.0.1"
#else
#define DB_HOST "123.456.789.123"
#endif

Database *pDatabase = new Database(DB_HOST);

Is assigning a value to a preprocessor symbol possible in Delphi, or does Delphi only allow you to determine whether or not a symbol has been defined at all?

EDIT: As Delphi doesn't support symbol values in the preprocessor, I'm assuming it doesn't support preprocessor macros. Am I right in this assumption?

For example, in C you can define a preprocessor macro that gets copied and pasted with the appropriate parameters before compilation. This is useful when you want "inline function" capability for fast operations (e.g., binary logic, testing in an integer values, bit-shifting, etc) without the overhead of stack frames, parameter passing and the like.

#define OK_FLAG 0x00000001
#define OK(f) (f & OK_FLAG)

#define WEAK_XOR_CIPHER(v) (v ^ 0xff)
like image 299
magnus Avatar asked Mar 12 '14 00:03

magnus


People also ask

What statements are used for conditional compilation?

In this way, you can create multiple variants of the same program without the need to maintain separate source streams. The compiler directives that are used for conditional compilation are the DEFINE directive, the EVALUATE directive, and the IF directive.

What is conditional compilation explain with example?

#elif means "else if" and establishes an if else-if compilation chain. Amongst other things, #if provides an alternative method of "commenting out" code. For example, #if 0 printf("#d", total); #endif. Here, the compiler will ignore printf("#d", total); #ifdef and #ifndef.

What do you understand by conditional compilation directives?

A preprocessor conditional compilation directive causes the preprocessor to conditionally suppress the compilation of portions of source code.

What are #ifdef in C?

In the C Programming Language, the #ifdef directive allows for conditional compilation. The preprocessor determines if the provided macro exists before including the subsequent code in the compilation process.


1 Answers

You can simply use a constant, and define the value:

const
{$IFDEF DEBUG}
  DB_Host = '127.0.0.1';
{$ELSE}
  DB_Host = '123.45.67.89';
{$ENDIF}

You can also do the reverse (test for something not being defined):

const
{$IFNDEF DEBUG}
  DB_Host = '123.45.67.89';
{$ELSE}
  DB_Host = '127.0.0.1';
{$ENDIF}

This ensures that DB_Host is always available, and has the appropriate value when not debugging.

Recent versions of Delphi allow you to test the value of constants. For instance, the System unit has the constant RTLVersion defined, and you can test for a value using it:

{$IF RTLVersion <= 26}
  // Less than XE5
{$ELSE}
  // XE5
{$IFEND}

Delphi doesn't support macros of any sort, however. The typical solution to not having macros is to use an inline function. See, for instance the Windows unit's implementation of the WinAPI RGB macro:

function RGB(r, g, b: Byte): COLORREF; inline;

function RGB(r, g, b: Byte): COLORREF;
begin
  Result := (r or (g shl 8) or (b shl 16));
end;
like image 134
Ken White Avatar answered Nov 03 '22 00:11

Ken White