Eina_Magic provides run-time type-checking. More...
Macros | |
#define | EINA_MAGIC_NONE 0x1234fedc |
Definition of a random value for specifying that a structure using the magic feature has already been freed. More... | |
#define | EINA_MAGIC Eina_Magic __magic; |
Definition of of a variable of type Eina_Magic. More... | |
#define | EINA_MAGIC_SET(d, m) (d)->__magic = (m) |
Definition to set the magic number of d to m . More... | |
#define | EINA_MAGIC_CHECK(d, m) (EINA_LIKELY((d) && ((d)->__magic == (m)))) |
Definition to test if d is NULL or not, and if not NULL , if d->__eina_magic is equal to m . More... | |
#define | EINA_MAGIC_FAIL(d, m) |
Definition to call eina_magic_fail() with the parameters d , d->__magic , m , FILE, func, and LINE. More... | |
Typedefs | |
typedef unsigned int | Eina_Magic |
An abstract type for a magic number. | |
Functions | |
EINA_API const char * | eina_magic_string_get (Eina_Magic magic) |
Gets the string associated with the given magic identifier. More... | |
EINA_API Eina_Bool | eina_magic_string_set (Eina_Magic magic, const char *magic_name) |
Sets the string associated with the given magic identifier. More... | |
EINA_API Eina_Bool | eina_magic_string_static_set (Eina_Magic magic, const char *magic_name) |
Sets the string associated with the given magic identifier. More... | |
EINA_API void | eina_magic_fail (void *d, Eina_Magic m, Eina_Magic req_m, const char *file, const char *fnc, int line) |
Displays a message or aborts if a magic check failed. More... | |
Variables | |
EINA_API Eina_Error | EINA_ERROR_MAGIC_FAILED |
The error identifier corresponding to the magic check failure. | |
EINA_API Eina_Error | EINA_ERROR_MAGIC_FAILED = 0 |
The error identifier corresponding to the magic check failure. | |
Eina_Magic provides run-time type-checking.
C is a weak statically typed language, in other words, it just checks for types during compile time and any cast that makes the compiler believe the type is correct.
In the real world code, we often need to deal with casts, either explicit or implicit, by means of void*
. We also need to resort to casts when doing inheritance in C.
Eina_Magic gives us a way to do casts and still be certain of the type we are operating on.
An example should elucidate matters.
#define EINA_MAGIC_NONE 0x1234fedc |
Definition of a random value for specifying that a structure using the magic feature has already been freed.
It is used by eina_magic_fail().
0
. #define EINA_MAGIC Eina_Magic __magic; |
Definition of of a variable of type Eina_Magic.
To put in a structure when one wants to use the magic feature of Eina with the functions of that structure, like this:
#define EINA_MAGIC_SET | ( | d, | |
m | |||
) | (d)->__magic = (m) |
Definition to set the magic number of d
to m
.
d
must be a valid pointer to a structure holding an Eina magic number declaration. Use EINA_MAGIC to add such a declaration.
0
. #define EINA_MAGIC_CHECK | ( | d, | |
m | |||
) | (EINA_LIKELY((d) && ((d)->__magic == (m)))) |
Definition to test if d
is NULL
or not, and if not NULL
, if d->__eina_magic
is equal to m
.
d
must be a structure that holds an Eina magic number declaration. Use EINA_MAGIC to add such a declaration.
1
. #define EINA_MAGIC_FAIL | ( | d, | |
m | |||
) |
Definition to call eina_magic_fail() with the parameters d
, d->__magic
, m
, FILE, func, and LINE.
d
must be a structure that holds an Eina magic number declaration. Use EINA_MAGIC to add such a declaration.
EINA_API const char * eina_magic_string_get | ( | Eina_Magic | magic | ) |
Gets the string associated with the given magic identifier.
This function returns the string associated to magic
. Even if none are found this function still returns non NULL
, in this case an identifier such as "(none)", "(undefined)", or "(unknown)".
[in] | magic | The magic identifier |
Referenced by eina_magic_fail().
EINA_API Eina_Bool eina_magic_string_set | ( | Eina_Magic | magic, |
const char * | magic_name | ||
) |
Sets the string associated with the given magic identifier.
This function sets the string magic_name
to magic
. It is not checked if number or string are already set, in which case you end with duplicates. Internally, eina makes a copy of magic_name
.
[in] | magic | The magic identifier |
[in] | magic_name | The string associated with the identifier, must not be NULL |
References EINA_FALSE, EINA_SAFETY_ON_NULL_RETURN_VAL, EINA_TRUE, and ERR.
Referenced by eldbus_init().
EINA_API Eina_Bool eina_magic_string_static_set | ( | Eina_Magic | magic, |
const char * | magic_name | ||
) |
Sets the string associated with the given magic identifier.
This function sets the string magic_name
to magic
. It is not checked if number or string are already set, in which case you might end with duplicates. Eina does not make a copy of magic_name
, this means that magic_name
has to be a valid pointer for as long as magic
is used.
[in] | magic | The magic identifier |
[in] | magic_name | The string associated with the identifier, must not be NULL |
References EINA_FALSE, EINA_SAFETY_ON_NULL_RETURN_VAL, and EINA_TRUE.
EINA_API void eina_magic_fail | ( | void * | d, |
Eina_Magic | m, | ||
Eina_Magic | req_m, | ||
const char * | file, | ||
const char * | fnc, | ||
int | line | ||
) |
Displays a message or aborts if a magic check failed.
This function displays an error message if a magic check has failed, using the following logic in the following order:
d
is NULL
, a message warns about a NULL
pointer. m
is equal to EINA_MAGIC_NONE, a message warns about a handle that is already freed. m
is equal to req_m
, a message warns about a handle that is of the wrong type. [in] | d | The checked data pointer |
[in] | m | The magic identifier to check |
[in] | req_m | The requested magic identifier to check |
[in] | file | The file in which the magic check failed |
[in] | fnc | The function in which the magic check failed |
[in] | line | The line at which the magic check failed |
References EINA_LOG_DOMAIN_GLOBAL, EINA_LOG_LEVEL_CRITICAL, EINA_LOG_LEVEL_ERR, eina_log_print(), EINA_MAGIC_NONE, and eina_magic_string_get().