Standard Macros

Standard Macros — Commonly-used macros.

Stability Level

Stable, unless otherwise indicated

Synopsis

#include <mlib.h>

#define             M_BEGIN_DECLS
#define             M_END_DECLS
#define             NULL
#define             MAX                                 (a,
                                                         b)
#define             MIN                                 (a,
                                                         b)
#define             ABS                                 (a)
#define             CLAMP                               (x,
                                                         low,
                                                         high)
#define             M_N_ELEMENTS                        (arr)
#define             M_STMT_START
#define             M_STMT_END

Description

These macros provide a few commonly-used features.

Details

M_BEGIN_DECLS

#define             M_BEGIN_DECLS

Used (along with M_END_DECLS) to bracket header files. If the compiler in use is a C++ compiler, adds extern "C" around the header.


M_END_DECLS

#define             M_END_DECLS

Used (along with M_BEGIN_DECLS) to bracket header files. If the compiler in use is a C++ compiler, adds extern "C" around the header.


NULL

#define             NULL

Defines the standard NULL pointer.


MAX()

    #define MAX(a, b)  (((a) > (b)) ? (a) : (b))

Calculates the maximum of a and b.

a :

a numeric value.

b :

a numeric value.

Returns :

the maximum of a and b.

MIN()

    #define MIN(a, b)  (((a) < (b)) ? (a) : (b))

Calculates the minimum of a and b.

a :

a numeric value.

b :

a numeric value.

Returns :

the minimum of a and b.

ABS()

    #define ABS(a)	   (((a) < 0) ? -(a) : (a))

For example,

ABS(-10) is 10.

ABS(10) is also 10.

Calculates the absolute value of a. The absolute value is simply the number with any negative sign taken away.

a :

a numeric value.

Returns :

the absolute value of a.

CLAMP()

    #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))

For example,

CLAMP(5, 10, 15) is 10.

CLAMP(15, 5, 10) is 10.

CLAMP(20, 15, 25) is 20.

Ensures that x is between the limits set by low and high. If low is greater than high the result is undefined.

x :

the value to clamp.

low :

the minimum value allowed.

high :

the maximum value allowed.

Returns :

the value of x clamped to the range between low and high.

M_N_ELEMENTS()

    #define M_N_ELEMENTS(arr)		(sizeof (arr) / sizeof ((arr)[0]))

Determines the number of elements in an array.

The array must be declared so the compiler knows its size at compile-time; this macro will not work on an array allocated on the heap, only static arrays or arrays on the stack.

arr :

the array

M_STMT_START

    #  define M_STMT_START  do

Used within multi-statement macros so that they can be used in places where only one statement is expected by the compiler.


M_STMT_END

    #  define M_STMT_END    while (0)

Used within multi-statement macros so that they can be used in places where only one statement is expected by the compiler.

See Also

GLib Standard Macros