1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-18 03:52:11 +01:00

OP-1456 fixed macro for uav object field to array casting and added compile time asserts to assure its working

This commit is contained in:
Corvus Corax 2014-08-24 16:14:47 +02:00
parent 6faf1a73ac
commit 551392d6bf

View File

@ -51,10 +51,42 @@ typedef void *UAVObjHandle;
#define MetaObjectId(id) ((id) + 1) #define MetaObjectId(id) ((id) + 1)
/**
* helper macro to access multi-element fields as array
*/
#define UAVObjectFieldToArray(type, var) \ #define UAVObjectFieldToArray(type, var) \
({ type *const dummy = &(var); \ (*({ type *const dummy = &(var); \
(((type##Array *)dummy)->array); } \ &(((type##Array *)dummy)->array); } \
) ))
// we have limited trust in our compiler
// make sure this macro actually works on all platforms
typedef struct __attribute__((__packed__)) {
uint16_t element1;
uint16_t element2;
uint16_t element3;
}
__DummyUAVObjectFieldData;
typedef struct __attribute__((__packed__)) {
uint16_t array[3];
}
__DummyUAVObjectFieldDataArray;
#define __DummyTA(var) UAVObjectFieldToArray(__DummyUAVObjectFieldData, var)
__attribute__((unused)) static void __DummyTAtest(void)
{
__DummyUAVObjectFieldData t;
PIOS_STATIC_ASSERT(sizeof(t) == sizeof(__DummyTA(t)));
PIOS_STATIC_ASSERT(sizeof(t.element1) == sizeof(__DummyTA(t)[0]));
PIOS_STATIC_ASSERT((void *)&t == (void *)&__DummyTA(t));
PIOS_STATIC_ASSERT((void *)&t.element1 == (void *)&__DummyTA(t)[0]);
PIOS_STATIC_ASSERT((void *)&t.element2 == (void *)&__DummyTA(t)[1]);
PIOS_STATIC_ASSERT((void *)&t.element3 == (void *)&__DummyTA(t)[2]);
}
/** /**
* Object update mode, used by multiple modules (e.g. telemetry and logger) * Object update mode, used by multiple modules (e.g. telemetry and logger)