PetscDefined#

Determine whether a boolean macro is defined

Synopsis#

#include <petscmacros.h>
int PetscDefined(def)

No Fortran Support

Input Parameter#

  • def - PETSc-style preprocessor variable (without PETSC_ prepended!)

Output Parameter#

  • <return- value> - Either integer literal 0 or 1

Notes#

PetscDefined() returns 1 if and only if “PETSC_ ## def” is defined (but empty) or defined to integer literal 1. In all other cases, PetscDefined() returns integer literal 0. Therefore this macro should not be used if its argument may be defined to a non-empty value other than 1.

The prefix “PETSC_” is automatically prepended to def. To avoid prepending “PETSC_”, say to add custom checks in user code, one should use PetscDefined_().

  #define FooDefined(d) PetscDefined_(PetscConcat(FOO_, d))

Developer Notes#

Getting something that works in C and CPP for an arg that may or may not be defined is tricky. Here, if we have “#define PETSC_HAVE_BOOGER 1” we match on the placeholder define, insert the “0,” for arg1 and generate the triplet (0, 1, 0). Then the last step cherry picks the 2nd arg (a one). When PETSC_HAVE_BOOGER is not defined, we generate a (… 1, 0) pair, and when the last step cherry picks the 2nd arg, we get a zero.

Our extra expansion via PetscDefined__take_second_expand() is needed with MSVC, which has a nonconforming implementation of variadic macros.

Example Usage#

Suppose you would like to call either “foo()” or “bar()” depending on whether PETSC_USE_DEBUG is defined then

  #if PetscDefined(USE_DEBUG)
    foo();
  #else
    bar();
  #endif

  // or alternatively within normal code
  if (PetscDefined(USE_DEBUG)) {
    foo();
  } else {
    bar();
  }

is equivalent to

  #if defined(PETSC_USE_DEBUG)
  #  if MY_DETECT_EMPTY_MACRO(PETSC_USE_DEBUG) // assuming you have such a macro
       foo();
  #   elif PETSC_USE_DEBUG == 1
       foo();
  #   else
       bar();
  #  endif
  #else
  bar();
  #endif

See Also#

PetscHasAttribute(), PetscUnlikely(), PetscLikely(), PetscConcat(), PetscExpandToNothing(), PetscCompl()

Level#

intermediate

Location#

include/petscmacros.h

Examples#

src/snes/tutorials/ex5.c
src/snes/tutorials/ex55.c
src/dm/impls/stag/tutorials/ex4.c


Index of all Sys routines
Table of Contents for all manual pages
Index of all manual pages