Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed meta-programming magic to define a mother lode of bit fields in an error-free way
    text
    copied!<p>The goal is to control which types of users are allowed to perform which operations at the UI level. This code has been in place for a while; I just want to improve it a bit. The file which I am trying to improve should probably be auto-generated, but that would be too big of a change, so I seek a simpler solution.</p> <p>A file which we shall call <code>PermissionBits.h</code> has a bunch of these:</p> <pre><code>// Here names are mangled; for example XYZ_OP_A is: // permission to operation A in category/context XYZ // SCU64 = static const unsigned __int64 // Some namespaces utilize all 64 bits // The actual values (as long as they are proper bit fields) // do not matter - they are always used by name namespace XYZPermissionBits { SCU64 XYZ_OP_A = 1UI64 &lt;&lt; 0; // 1 = 0x0000000000000001 SCU64 XYZ_OP_B = 1UI64 &lt;&lt; 1; // 2 = 0x0000000000000002 SCU64 XYZ_OP_C = 1UI64 &lt;&lt; 2; // 4 = 0x0000000000000004 SCU64 XYZ_OP_C = 1UI64 &lt;&lt; 3; // 8 = 0x0000000000000008 SCU64 XYZ_OP_D = 1UI64 &lt;&lt; 4; // 16 = 0x0000000000000010 SCU64 XYZ_OP_E = 1UI64 &lt;&lt; 5; // 32 = 0x0000000000000020 SCU64 XYZ_OP_F = 1UI64 &lt;&lt; 6; // 64 = 0x0000000000000040 SCU64 XYZ_OP_G = 1UI64 &lt;&lt; 7; // 128 = 0x0000000000000080 SCU64 XYZ_OP_H = 1UI64 &lt;&lt; 8; // 256 = 0x0000000000000100 SCU64 XYZ_OP_I = 1UI64 &lt;&lt; 9; // 512 = 0x0000000000000200 SCU64 XYZ_OP_J = 1UI64 &lt;&lt; 10; // 1024 = 0x0000000000000400 SCU64 XYZ_OP_K = 1UI64 &lt;&lt; 11; // 2048 = 0x0000000000000800 SCU64 XYZ_OP_L = 1UI64 &lt;&lt; 12; // 4096 = 0x0000000000001000 } </code></pre> <p>Even with the help of <code>1UI64 &lt;&lt; &lt;numBits&gt;;</code> shortcut there are still problems, as coders create flags with duplicate values, make typos, etc.</p> <p>Ideally I would like a macro which can be nicely formatted and look like:</p> <pre><code>BITFIELDS_FOR_NAMESPACE( //*************** &lt;&lt;== I want to make the namespace name more vivid XYZPermissionBits, //*************** &lt;&lt;== somehow if that is possible. It is not a must-have. XYZ_OP_A, // Being able to add a comment here would be nice, but not critical XYZ_OP_B, XYZ_OP_C, XYZ_OP_D, XYZ_OP_E, XYZ_OP_F, XYZ_OP_G, XYZ_OP_H, XYZ_OP_I, XYZ_OP_J, XYZ_OP_K, XYZ_OP_L ) </code></pre> <p>I would like this macro be flexible and prevent me from entering less than 2 or more than 65 arguments - namespace name + 64 flags. Is it possible to do what I want or close to it, or should I resort to generated code? What other advice do you have?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload