Add M_TYPE_FROM_FIELD

This commit is contained in:
Patrick Pelissier committed 2017-03-15 22:06:10 +01:00
1 parent de7d0bd682
commit 5022c1edb7
1 file changed
+19 -11
+19 -11
View File
@@ -562,6 +562,25 @@
(y) = tmp; \ (y) = tmp; \
} while (0) } while (0)
/* Check if 'n' is assignable to an object of type 'type'
Return the object.
Two definitions: one with compound-literals for C, the other with static_cast for C++.
NOTE: C definition is safer than the C++ one.
*/
#ifndef __cplusplus
# define M_ASSIGN_CAST(type, n) ((type) { 0 } = (n))
#else
# define M_ASSIGN_CAST(type, n) static_cast<type>(n)
#endif
/*
* From a pointer to a 'field_type' 'field' of a 'type'structure,
* return pointer to the structure.
* NOTE: Cast Inside!
*/
#define M_TYPE_FROM_FIELD(type, ptr, field_type, field) \
((type *)(void*)( (char *)M_ASSIGN_CAST(field_type*, (ptr)) - offsetof(type, field) ))
/************************************************************/ /************************************************************/
/********************* HASH selection ***********************/ /********************* HASH selection ***********************/
/************************************************************/ /************************************************************/
@@ -828,17 +847,6 @@ m_core_hash(const void *ptr, size_t s)
cont && (M_GET_INIT oplist (name), true); \ cont && (M_GET_INIT oplist (name), true); \
(M_GET_CLEAR oplist (name), cont = false)) (M_GET_CLEAR oplist (name), cont = false))
/* Check if 'n' is assignable to an object of type 'type'
Return the object.
Two definitions: one with compound-literals for C, the other with static_cast for C++.
Note: C definition is safer than the C++ one.
*/
#ifndef __cplusplus
# define M_ASSIGN_CAST(type, n) ((type) { 0 } = (n))
#else
# define M_ASSIGN_CAST(type, n) static_cast<type>(n)
#endif
/* By putting this after a method, we transform the argument list /* By putting this after a method, we transform the argument list
so that the first argument becomes a pointer to the destination. */ so that the first argument becomes a pointer to the destination. */
#define M_IPTR(...) ( & __VA_ARGS__ ) #define M_IPTR(...) ( & __VA_ARGS__ )