PebbleOS v4.47.0-512-g5285d4236
Loading...
Searching...
No Matches

Array and member size helpers. More...

Macros

#define PBL_IS_ARRAY(array)
 Fail the build if the argument is not an array.
 
#define PBL_ARRAY_SIZE(array)   ((size_t)(PBL_IS_ARRAY(array) + sizeof(array) / sizeof((array)[0])))
 Get the number of elements of an array.
 
#define PBL_STATIC_ARRAY_SIZE(type, array)   (sizeof((type[])array) / sizeof(type))
 Get the number of elements of an array literal, as a compile-time constant.
 
#define PBL_SIZEOF_FIELD(type, member)   sizeof(((type *)0)->member)
 Get the size of a structure member without an instance.
 

Detailed Description

Array and member size helpers.

Macro Definition Documentation

◆ PBL_ARRAY_SIZE

#define PBL_ARRAY_SIZE (   array)    ((size_t)(PBL_IS_ARRAY(array) + sizeof(array) / sizeof((array)[0])))

Get the number of elements of an array.

Fails the build if array is a pointer.

Parameters
arrayArray.
Returns
Number of elements, as a constant expression.

◆ PBL_IS_ARRAY

#define PBL_IS_ARRAY (   array)
Value:
((int)sizeof(struct { \
_Static_assert(!PBL_TYPES_COMPATIBLE(__typeof__(array), __typeof__(&(array)[0])), \
"argument must be an array"); \
}))
#define PBL_TYPES_COMPATIBLE(a, b)
Constant expression: 1 if the two types are compatible, else 0.
Definition compiler.h:156

Fail the build if the argument is not an array.

A pointer, such as an array parameter, has the type of its first element's address; an array does not.

Parameters
arrayExpression to check.
Returns
0, as a constant expression.

◆ PBL_SIZEOF_FIELD

#define PBL_SIZEOF_FIELD (   type,
  member 
)    sizeof(((type *)0)->member)

Get the size of a structure member without an instance.

Parameters
typeStructure type.
memberMember name.
Returns
Size of the member in bytes.

◆ PBL_STATIC_ARRAY_SIZE

#define PBL_STATIC_ARRAY_SIZE (   type,
  array 
)    (sizeof((type[])array) / sizeof(type))

Get the number of elements of an array literal, as a compile-time constant.

#define RATES {10, 25, 50}
static const uint16_t s_rates[] = RATES;
static_assert(PBL_STATIC_ARRAY_SIZE(uint16_t, RATES) == 3, "");
#define PBL_STATIC_ARRAY_SIZE(type, array)
Get the number of elements of an array literal, as a compile-time constant.
Definition size.h:55
Parameters
typeType of the elements.
arrayBrace-enclosed array literal.
Returns
Number of elements.