Does a unit- rather than something it contains- have any sort of representation which is recognisably distinct from an object?

I've got a situation where if a library (.dll or .so) is opened under program control it is represented by an object, with entry points expressed as methods. Alternatively, if it's statically linked then it's represented by a unit, with entry points represented by procedures and functions. That allows me to write things like

caps := LibCapShim.cap_get_proc;  // LibCapShim is object or unit
if caps <> nil then
  try
    caplist := capability;
    r := LibCapShim.cap_set_flag(caps, flag, 1, @caplist, Ord(value));

which is OK at the application level irrespective of how the program's built.

What I can't do, without a conditional directive, is check whether the object representing the library is nil (because the dll/so isn't available). Instead I'm having to do

{$ifdef USE_DYNAMIC_LIBCAP }
  if LibCapShim = nil then      (* Initialisation failed        *)
    exit;
{$endif                    }
  caps := LibCapShim.cap_get_proc;

and so on. There's obviously ways around this, but they tend to rely on adding validity functions outside the object. Is there any way of doing something like

if LibCapShim is TObject then
  if LibCapShim = nil then      (* Initialisation failed        *)
    exit;

without the compiler objecting when it finds itself looking at a unit name in isolation?

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to