patterncppMinor
Macro to build type declaration
Viewed 0 times
typedeclarationbuildmacro
Problem
These are some macros to help build a traits class (for the parser/printer classes I am building).
Traits.h
``
* To P1 (first argument)
* and a list of second arguments (placing a comma between each macro).
*
* Because NUM_ARGS is limited to 20, This expansion is also limited to 20
*/
#define REP_N(Act, P1, ...) REP_OF_N(Act, P1, NUM_ARGS(__VA_ARGS__), __VA_ARGS__)
#define REP_OF_N(Act, P1, Count, ...) REP_OF_N_(Act, P1, Count, __VA_ARGS__)
#define REP_OF_N_(Act, P1, Count, ...) REP_OF_ ## Count(Act, P1, __VA_ARGS__)
#define REP_OF_20(Act, P1, P2, ...) EXPAND(Act, P1 ,P2), REP_OF_19(Act, P1, __VA_ARGS__)
#define REP_OF_19(Act, P1, P2, ...) EXPAND(Act, P1 ,P2), REP_OF_18(Act, P1, __VA_ARGS__)
#define REP_OF_18(Act, P1, P2, ...) EXPAND(Act, P1 ,P2), REP_OF_17(Act, P1, __VA_ARGS__)
#define REP_OF_17(Act, P1, P2, ...) EXPAND(Act, P1 ,P2), REP_OF_16(Act, P1, __VA_ARGS__)
#define REP_OF_16(Act, P1, P2, ...) EXPAND(Act, P1 ,P2), REP_OF_15(Act, P1, __VA_ARGS__)
#define REP_OF_15(Act, P1, P2, ...) EXPAND(Act, P1 ,P2), REP_OF_14(Act, P1, __VA_ARGS__)
#defin
Traits.h
``
#ifndef THORS_ANVIL_SERIALIZE_TRAITS_H
#define THORS_ANVIL_SERIALIZE_TRAITS_H
#include
#include
/*
* Macros for counting the number of arguments
* Currently set up for a max of 20.
*/
#define NUM_ARGS(...) NUM_ARGS_(0, __VA_ARGS__, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 09, 08, 07, 06, 05, 04, 03, 02, 01)
#define NUM_ARGS_(Zero, I1, I2, I3, I4 ,I5, I6, I7, I8, I9, I10, I11, I12, I13, I14, I15, I16, I17, I18, I19, I20, A, ...) A
/*
* Macros to quote the parameter
* Used below by the actions.
*/
#define QUOTE_(A) A
#define QUOTE(A) QUOTE_(#A)
/*
* Macro to force expansion of a macro that takes two parameters.
Used by REP_
*/
#define EXPAND_(Result) Result
#define EXPAND(Act, P1, P2) EXPAND_(Act(P1, P2))
/*
* Macros that that applies the action Act` (a two parameter macro)* To P1 (first argument)
* and a list of second arguments (placing a comma between each macro).
*
* Because NUM_ARGS is limited to 20, This expansion is also limited to 20
*/
#define REP_N(Act, P1, ...) REP_OF_N(Act, P1, NUM_ARGS(__VA_ARGS__), __VA_ARGS__)
#define REP_OF_N(Act, P1, Count, ...) REP_OF_N_(Act, P1, Count, __VA_ARGS__)
#define REP_OF_N_(Act, P1, Count, ...) REP_OF_ ## Count(Act, P1, __VA_ARGS__)
#define REP_OF_20(Act, P1, P2, ...) EXPAND(Act, P1 ,P2), REP_OF_19(Act, P1, __VA_ARGS__)
#define REP_OF_19(Act, P1, P2, ...) EXPAND(Act, P1 ,P2), REP_OF_18(Act, P1, __VA_ARGS__)
#define REP_OF_18(Act, P1, P2, ...) EXPAND(Act, P1 ,P2), REP_OF_17(Act, P1, __VA_ARGS__)
#define REP_OF_17(Act, P1, P2, ...) EXPAND(Act, P1 ,P2), REP_OF_16(Act, P1, __VA_ARGS__)
#define REP_OF_16(Act, P1, P2, ...) EXPAND(Act, P1 ,P2), REP_OF_15(Act, P1, __VA_ARGS__)
#define REP_OF_15(Act, P1, P2, ...) EXPAND(Act, P1 ,P2), REP_OF_14(Act, P1, __VA_ARGS__)
#defin
Solution
First of all nice, consistent naming. I approve.
What I'm not too fond of is macros, I find the code hard to read because of the macros; however I appreciate that this kind of traits-system is hard (or impossible) to do without macros. The comments do help with understanding the code.
You seem to be missing
One thing that I wonder about the code is that how will you handle a type that evolves? Say that fields change name, get added or removed how would a parser handle this using this traits class? For example if a field changes name (or type).
Other than that I can't see anything wrong with the code. Good work.
What I'm not too fond of is macros, I find the code hard to read because of the macros; however I appreciate that this kind of traits-system is hard (or impossible) to do without macros. The comments do help with understanding the code.
You seem to be missing
Traits for char, wchar_t and std::wstring. You might want to consider adding the common types from ` as well ((u)intX_t` for example).One thing that I wonder about the code is that how will you handle a type that evolves? Say that fields change name, get added or removed how would a parser handle this using this traits class? For example if a field changes name (or type).
Other than that I can't see anything wrong with the code. Good work.
Context
StackExchange Code Review Q#81922, answer score: 2
Revisions (0)
No revisions yet.