| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#ifndef MACRO_UTILS_H |
| 5 |
#define MACRO_UTILS_H |
| 6 |
|
| 7 |
#include "azure_macro_utils/macro_utils_generated.h" |
| 8 |
|
| 9 |
#ifdef __cplusplus |
| 10 |
#include <cstring> |
| 11 |
#include <cstddef> |
| 12 |
extern "C" { |
| 13 |
#else |
| 14 |
#include <string.h> |
| 15 |
#include <stddef.h> |
| 16 |
#endif |
| 17 |
|
| 18 |
#if (defined OPTIMIZE_RETURN_CODES) |
| 19 |
#define MU_FAILURE 1 |
| 20 |
#else |
| 21 |
#define MU_FAILURE __LINE__ |
| 22 |
#endif |
| 23 |
|
| 24 |
/*"pointer or NULL" macro - because when printf-ing arguments NULL is not valid for %s (section 7.1.4 of C11 standard) */ |
| 25 |
#define MU_P_OR_NULL(p) (((p)!=NULL)?(p):"NULL") |
| 26 |
#define MU_WP_OR_NULL(p) (((p)!=NULL)?(p):L"NULL") |
| 27 |
|
| 28 |
#define MU_TOSTRING_(x) #x |
| 29 |
#define MU_TOSTRING(x) MU_TOSTRING_(x) |
| 30 |
|
| 31 |
#define MU_TRIGGER_PARENTHESIS(...) , |
| 32 |
|
| 33 |
#define MU_LPAREN ( |
| 34 |
|
| 35 |
#define MU_C2_(x,y) x##y |
| 36 |
|
| 37 |
#define MU_C2(x,y) MU_C2_(x,y) |
| 38 |
|
| 39 |
#define MU_C3(x,y,z) MU_C2(x, MU_C2(y,z)) |
| 40 |
|
| 41 |
#define MU_C4(x,y,z, u) MU_C2(MU_C2(x,y), MU_C2(z,u)) |
| 42 |
|
| 43 |
#define MU_C5(x,y,z,u, v) MU_C2(MU_C4(x,y, z, u), v) |
| 44 |
|
| 45 |
#define MU_C1_(x) x |
| 46 |
|
| 47 |
#define MU_C1(x) MU_C1_(x) |
| 48 |
|
| 49 |
#define MU_C2STRING(x,y) x y |
| 50 |
|
| 51 |
#define MU_C3STRING(x,y,z) x y z |
| 52 |
|
| 53 |
#define MU_C4STRING(x,y,z,u) x y z u |
| 54 |
|
| 55 |
#define MU_C5STRING(x,y,z,u,v) x y z u v |
| 56 |
|
| 57 |
/* we need some sort of macro that does: |
| 58 |
MU_IF(0, "true", "false") => "false" |
| 59 |
MU_IF(1, "true", "false") => "true" |
| 60 |
MU_IF(X, "true", "false") => "true" |
| 61 |
*/ |
| 62 |
|
| 63 |
#define MU_INTERNALIF(x) MU_INTERNALIF##x |
| 64 |
#define MU_INTERNALIF0 |
| 65 |
|
| 66 |
#define MU_ISZERO(x) MU_COUNT_ARG(MU_INTERNALIF(x)) |
| 67 |
|
| 68 |
#define MU_IF(condition, trueBranch, falseBranch) MU_C2(MU_IF,MU_ISZERO(condition))(trueBranch, falseBranch) |
| 69 |
#define MU_IF0(trueBranch, falseBranch) falseBranch |
| 70 |
#define MU_IF1(trueBranch, falseBranch) trueBranch |
| 71 |
|
| 72 |
|
| 73 |
/*the following macro want to eat empty arguments from a list */ |
| 74 |
/*examples: */ |
| 75 |
/*MU_EAT_EMPTY_ARGS(, , X) expands to X */ |
| 76 |
|
| 77 |
#define MU_EXPAND_TO_NOTHING(arg) |
| 78 |
#define MU_EAT_EMPTY_ARG(arg_count, arg) MU_IF(MU_ISEMPTY(arg),,arg) MU_IF(MU_ISEMPTY(arg),MU_EXPAND_TO_NOTHING,MU_IFCOMMALOGIC)(MU_DEC(arg_count)) |
| 79 |
#define MU_EAT_EMPTY_ARGS(...) MU_FOR_EACH_1_COUNTED(MU_EAT_EMPTY_ARG, __VA_ARGS__) |
| 80 |
|
| 81 |
#define MU_DEFINE_ENUMERATION_CONSTANT(x) x, |
| 82 |
/*MU_DEFINE_ENUM_WITHOUT_INVALID goes to header*/ |
| 83 |
#define MU_DEFINE_ENUM_WITHOUT_INVALID(enumName, ...) typedef enum MU_C2(enumName, _TAG) { MU_FOR_EACH_1(MU_DEFINE_ENUMERATION_CONSTANT, __VA_ARGS__)} enumName; \ |
| 84 |
extern const char* MU_C2(enumName,Strings)(enumName value); \ |
| 85 |
extern int MU_C2(enumName, _FromString)(const char* enumAsString, enumName* destination); |
| 86 |
|
| 87 |
// this macro is a wrapper on top of MU_DEFINE_ENUM_WITHOUT_INVALID, adding an _INVALID value as the first enum value in the enum |
| 88 |
#define MU_DEFINE_ENUM(enumName, ...) \ |
| 89 |
MU_DEFINE_ENUM_WITHOUT_INVALID(enumName, MU_C2(enumName, _INVALID), __VA_ARGS__) |
| 90 |
|
| 91 |
#define MU_DEFINE_ENUMERATION_CONSTANT_AS_WIDESTRING(x) MU_C2(L, MU_TOSTRING(x)) , |
| 92 |
#define MU_DEFINE_ENUMERATION_CONSTANT_AS_STRING(x) MU_TOSTRING(x) , |
| 93 |
/*MU_DEFINE_ENUM_STRINGS_WITHOUT_INVALID goes to .c*/ |
| 94 |
#define MU_DEFINE_ENUM_STRINGS_WITHOUT_INVALID(enumName, ...) const char* MU_C2(enumName, StringStorage)[MU_COUNT_ARG(__VA_ARGS__)] = {MU_FOR_EACH_1(MU_DEFINE_ENUMERATION_CONSTANT_AS_STRING, __VA_ARGS__)}; \ |
| 95 |
const char* MU_C2(enumName,Strings)(enumName value) \ |
| 96 |
{ \ |
| 97 |
if((int)value<0 || (int)value>=MU_COUNT_ARG(__VA_ARGS__)) \ |
| 98 |
{ \ |
| 99 |
/*this is an error case*/ \ |
| 100 |
return "NULL"; \ |
| 101 |
} \ |
| 102 |
else \ |
| 103 |
{ \ |
| 104 |
return MU_C2(enumName, StringStorage)[value]; \ |
| 105 |
} \ |
| 106 |
} \ |
| 107 |
int MU_C2(enumName, _FromString)(const char* enumAsString, enumName* destination) \ |
| 108 |
{ \ |
| 109 |
if( \ |
| 110 |
(enumAsString==NULL) || (destination==NULL) \ |
| 111 |
) \ |
| 112 |
{ \ |
| 113 |
return MU_FAILURE; \ |
| 114 |
} \ |
| 115 |
else \ |
| 116 |
{ \ |
| 117 |
size_t i; \ |
| 118 |
for(i=0;i<MU_COUNT_ARG(__VA_ARGS__);i++) \ |
| 119 |
{ \ |
| 120 |
if(strcmp(enumAsString, MU_C2(enumName, StringStorage)[i])==0) \ |
| 121 |
{ \ |
| 122 |
*destination = (enumName)i; \ |
| 123 |
return 0; \ |
| 124 |
} \ |
| 125 |
} \ |
| 126 |
return MU_FAILURE; \ |
| 127 |
} \ |
| 128 |
} \ |
| 129 |
|
| 130 |
// this macro is a wrapper on top of MU_DEFINE_ENUM_STRINGS_WITHOUT_INVALID, adding an _INVALID value as the first enum value in the enum |
| 131 |
#define MU_DEFINE_ENUM_STRINGS(enumName, ...) \ |
| 132 |
MU_DEFINE_ENUM_STRINGS_WITHOUT_INVALID(enumName, MU_C2(enumName, _INVALID), __VA_ARGS__) |
| 133 |
|
| 134 |
#define MU_DEFINE_LOCAL_ENUM_WITHOUT_INVALID(enumName, ...) typedef enum MU_C2(enumName, _TAG) { MU_FOR_EACH_1(MU_DEFINE_ENUMERATION_CONSTANT, __VA_ARGS__)} enumName; \ |
| 135 |
static const char* MU_C2(enumName, StringStorage)[MU_COUNT_ARG(__VA_ARGS__)] = {MU_FOR_EACH_1(MU_DEFINE_ENUMERATION_CONSTANT_AS_STRING, __VA_ARGS__)}; \ |
| 136 |
static const char* MU_C2(enumName,Strings)(enumName value) \ |
| 137 |
{ \ |
| 138 |
if((int)value<0 || (int)value>=MU_COUNT_ARG(__VA_ARGS__)) \ |
| 139 |
{ \ |
| 140 |
/*this is an error case*/ \ |
| 141 |
return "NULL"; \ |
| 142 |
} \ |
| 143 |
else \ |
| 144 |
{ \ |
| 145 |
return MU_C2(enumName, StringStorage)[value]; \ |
| 146 |
} \ |
| 147 |
} |
| 148 |
|
| 149 |
// this macro is a wrapper on top of MU_DEFINE_LOCAL_ENUM_WITHOUT_INVALID, adding an _INVALID value as the first enum value in the enum |
| 150 |
#define MU_DEFINE_LOCAL_ENUM(enumName, ...) \ |
| 151 |
MU_DEFINE_LOCAL_ENUM_WITHOUT_INVALID(enumName, MU_C2(enumName, _INVALID), __VA_ARGS__) |
| 152 |
|
| 153 |
// this macro returns the number of enum values (taking into account that an invalid value is generated) |
| 154 |
#define MU_ENUM_VALUE_COUNT(...) (MU_COUNT_ARG(__VA_ARGS__) + 1) |
| 155 |
|
| 156 |
// this macro returns the number of enum 2 values (taking into account that an invalid value is generated) |
| 157 |
#define MU_ENUM_2_VALUE_COUNT(...) ((MU_COUNT_ARG(__VA_ARGS__) / 2) + 1) |
| 158 |
|
| 159 |
#define MU_ENUM_TO_STRING(enumName, enumValue) MU_C2(enumName, Strings)(enumValue) |
| 160 |
#define MU_STRING_TO_ENUM(stringValue, enumName, addressOfEnumVariable) MU_C2(enumName, _FromString)(stringValue, addressOfEnumVariable) |
| 161 |
|
| 162 |
#define MU_EMPTY() |
| 163 |
#define MACRO_UTILS_DELAY(id) id MU_EMPTY MU_LPAREN ) |
| 164 |
|
| 165 |
#define MU_DEFINE_ENUMERATION_CONSTANT_2(enumerationConstant, constantExpression) enumerationConstant = constantExpression, |
| 166 |
|
| 167 |
#define MU_DECLARE_ENUM_STRINGS_2(enumIdentifier, ...) extern const char* MU_C3(MU_, enumIdentifier,_ToString)(enumIdentifier enumerationConstant); |
| 168 |
|
| 169 |
#define MU_DEFINE_ENUM_2_WITHOUT_INVALID(enumIdentifier, ... ) typedef enum MU_C2(enumIdentifier, _TAG) {MU_FOR_EACH_2(MU_DEFINE_ENUMERATION_CONSTANT_2, __VA_ARGS__)} enumIdentifier; \ |
| 170 |
MU_DECLARE_ENUM_STRINGS_2(enumIdentifier, __VA_ARGS__) |
| 171 |
|
| 172 |
// this macro is a wrapper on top of MU_DEFINE_ENUM, adding an _INVALID value as the first enum value in the enum |
| 173 |
#define MU_DEFINE_ENUM_2(enumName, ...) \ |
| 174 |
MU_DEFINE_ENUM_2_WITHOUT_INVALID(enumName, MU_C2(enumName, _INVALID), (int)0xDDDDDDDD, __VA_ARGS__) |
| 175 |
|
| 176 |
typedef struct ENUM_VALUE_AND_STRING_TAG |
| 177 |
{ |
| 178 |
int value; |
| 179 |
const char* valueAsString; |
| 180 |
}ENUM_VALUE_AND_STRING; |
| 181 |
|
| 182 |
#define MU_DEFINE_ENUM_VALUE_AND_STRING(enumerationConstant, constantExpression) {enumerationConstant, MU_TOSTRING(enumerationConstant)}, |
| 183 |
#define MU_DEFINE_ENUM_STRINGS_2(enumIdentifier, ... ) static const ENUM_VALUE_AND_STRING MU_C2(enumIdentifier, _ValuesAndStrings)[MU_DIV2(MU_COUNT_ARG(__VA_ARGS__))] ={MU_FOR_EACH_2(MU_DEFINE_ENUM_VALUE_AND_STRING, __VA_ARGS__)}; \ |
| 184 |
const char* MU_C3(MU_, enumIdentifier,_ToString)(enumIdentifier value) \ |
| 185 |
{ \ |
| 186 |
for(size_t i=0;i<sizeof(MU_C2(enumIdentifier, _ValuesAndStrings))/sizeof(MU_C2(enumIdentifier, _ValuesAndStrings)[0]);i++) \ |
| 187 |
{ \ |
| 188 |
if(MU_C2(enumIdentifier, _ValuesAndStrings)[i].value == (int)value) \ |
| 189 |
{ \ |
| 190 |
return MU_C2(enumIdentifier, _ValuesAndStrings)[i].valueAsString; \ |
| 191 |
} \ |
| 192 |
} \ |
| 193 |
return "NULL"; \ |
| 194 |
} \ |
| 195 |
|
| 196 |
#define MU_ENUM_TO_STRING_2(enumIdentifier, value) MU_C3(MU_, enumIdentifier,_ToString)(value) |
| 197 |
|
| 198 |
/*PRI_MU_ENUM and MU_ENUM_VALUE/MU_ENUM_VALUE_2 work together as printf format specifier/argument. e.g: printf("enumValue was=%" PRI_MU_ENUM "\n", MU_ENUM_TO_STRING(enumIdentifier, enumValue));*/ |
| 199 |
#define PRI_MU_ENUM "s%s (%d)" |
| 200 |
|
| 201 |
#define MU_ENUM_VALUE(enumIdentifier, value) "", MU_ENUM_TO_STRING(enumIdentifier, (value)), (int)(value) |
| 202 |
|
| 203 |
#define MU_ENUM_VALUE_2(enumIdentifier, value) "", MU_ENUM_TO_STRING_2(enumIdentifier, (value)), (int)(value) |
| 204 |
|
| 205 |
#define MU_DEFINE_STRUCT_FIELD(fieldType, fieldName) fieldType fieldName; |
| 206 |
|
| 207 |
/*MU_DEFINE_STRUCT allows creating a struct typedef based on a list of fields*/ |
| 208 |
#define MU_DEFINE_STRUCT(structName, ...) typedef struct MU_C2(structName, _TAG) { MU_FOR_EACH_2(MU_DEFINE_STRUCT_FIELD, __VA_ARGS__)} structName; |
| 209 |
|
| 210 |
// this macro allows counting of elements in an array |
| 211 |
#define MU_COUNT_ARRAY_ITEMS(A) (sizeof(A)/sizeof((A)[0])) |
| 212 |
|
| 213 |
#ifdef __cplusplus |
| 214 |
} |
| 215 |
#endif |
| 216 |
|
| 217 |
#endif /*MACRO_UTILS_H*/ |