12

I have a unit I wrote in Delphi 7 some time ago, and have just had the fun (pain) of converting to Delphi XE (Unicode).

The Unit works fine after some trouble, I am now trying to make this unit compatible with different Delphi Versions should I ever need to switch IDE back to Delphi 7 whilst updating some other code.

I only have Delphi 7 and Delphi XE, but from what I gather code written in Delphi 1 to Delphi 2007 will compile, but code from Delphi 2009 and above will be Unicode.

...Anyway, in the Unit I am separating the non-unicode and unicode like so:

{$IFDEF VER150} //Delphi 7
// code
{$ELSE IFDEF VER220} //Delphi XE
// code
{$ENDIF}

How do I modify the compiler directive so the rules apply to multi versions? For example something like:

{$IFDEF VER80,  //D1
        VER90,  //D2
        VER100, //D3
        VER120, //D4
        VER130, //D5
        VER140, //D6
        VER150, //D7}

This would then cover all Delphi versions should I distribute the source or .dcu unit.

Thanks.

2 Answers2

18

I wonder if the simplest approach in this instance is to switch behaviour on the UNICODE conditional. This conditional is defined if and only if you are using a Unicode version of Delphi, i.e. in Delphi 2009 and later. The big advantage of this is that it is future proof—you don't need to update your code every time a new Delphi is released. What's more, the conditional switch will be far more readable since it will clearly express the intent.

The Delphi documentation has an excellent topic listing all the pre-defined conditionals. The full list of version conditionals is also linked from there.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • +1, you were faster. If OP wants only to separate the code by Unicode version of Delphi then `UNICODE` directive is the exact one. There are also CompilerVersion and RTLVersion for determining the current Delphi version. – TLama Nov 25 '11 at 14:31
  • ... unless someone has been using `UNICODE` in the past. For example to accommodate the lack of Unicode support within Delphi itself ;) ... still, +1 ... your solution seems cleaner if it can be guaranteed that `UNICODE` is not already used. – 0xC0000022L Nov 25 '11 at 14:33
  • 1
    @STATUS_ACCESS_DENIED In that case the code that defines and uses a custom conditional named `UNICODE` should be changed. – David Heffernan Nov 25 '11 at 14:36
  • @David: I'm not sure I agree. In C/C++ it has been customary for certain entities (defines and functions) to start with two leading underscores if they are compiler-specific. Borland (et. al.) should have made such rules long ago. They're changing the rules and breaking the code all the time. Particularly the Unicode support is/was a mess in that regard. I still don't understand how committed Delphians can remain so calm ;) ... I've kind of left all of that behind me some time ago, although I still use Delphi at times. – 0xC0000022L Nov 25 '11 at 14:53
  • @STATUS Be that as it may, it is what it is and if Emba define `UNICODE` then you need not to, whether you like it or not. – David Heffernan Nov 25 '11 at 14:54
  • @David thanks for the information, using UNICODE directive is much more better :) –  Nov 25 '11 at 15:09
6

Your best bet is actually to look at one of the many JEDI projects, http://sourceforge.net/projects/jedi-apilib/ for example and look at how they do it. They have common include files that contain exactly the details you are interested in. JVCL is another good choice ...

0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
  • Yes the jvcl inc is perfect. There 's just every compiler directive to code some version-specific sections. – az01 Nov 25 '11 at 14:18