Thread overview
Need help with .h conversion
Sep 16, 2005
Traveler Hauptman
Sep 16, 2005
Walter Bright
Sep 16, 2005
Georg Wrede
September 16, 2005
I have been doing pretty well converting .h files to .d. But I have some tricky ones with heavy Macro use that I'd like some best practices help with.

Can some of the experienced guys monitoring this group suggest ways to convert the snippet below? It's a survey question, the more ways to do this the better.



#define GT_DEPTH_SHIFT		(0)
#define GT_SIZE_SHIFT		(8)
#define GT_SCHEME_SHIFT		(24)
#define GT_DEPTH_MASK		(0xffU << GT_DEPTH_SHIFT)

/* Macros to extract info from a ggi_graphtype. */
#define GT_DEPTH(x)		(((x) & GT_DEPTH_MASK) >> GT_DEPTH_SHIFT)

/* Macros to set info in a ggi_graphtype. */
#define GT_SETDEPTH(gt,x) \
do { (gt) = ((gt) & ~GT_DEPTH_MASK) | ((x)<<GT_DEPTH_SHIFT); } while (0)

#define GT_TEXT			((0x01) << GT_SCHEME_SHIFT)

/* Macro that constructs a graphtype */
#define GT_CONSTRUCT(depth,scheme,size) \
((depth) | (scheme) | ((size) << GT_SIZE_SHIFT))

/* Common graphtypes */
#define GT_TEXT16	GT_CONSTRUCT(4, GT_TEXT, 16)




--Traveler Hauptman
September 16, 2005
"Traveler Hauptman" <Traveler_member@pathlink.com> wrote in message news:dgefv3$nfd$1@digitaldaemon.com...
>
> I have been doing pretty well converting .h files to .d. But I have some tricky ones with heavy Macro use that I'd like some best practices help with.
>
> Can some of the experienced guys monitoring this group suggest ways to
> convert
> the snippet below? It's a survey question, the more ways to do this the
> better.


This can be accomplished by taking advantage of one of the stranger features of templates.  Observe:


const uint GT_DEPTH_SHIFT = 0;
const uint GT_SIZE_SHIFT = 8;
const uint GT_SCHEME_SHIFT = 24;
const uint GT_DEPTH_MASK  = 0xffU << GT_DEPTH_SHIFT;

const uint GT_TEXT = 0x01 << GT_SCHEME_SHIFT;

// Macro that constructs a graphtype
template GT_CONSTRUCT(uint depth, uint scheme, uint size)
{
 // notice the name of the const is the same as that of the template
 const uint GT_CONSTRUCT = (depth | scheme | (size << GT_SIZE_SHIFT));
}

// Common graphtypes
const uint GT_TEXT16 = GT_CONSTRUCT!(4, GT_TEXT, 16);


If you define a constant in a template that has the same name as the template, it will kind of function as a "return value."  It's very odd.

Note that you only need to do this with function-style macros that must be called at module level.  If it is a macro that is only ever called in functions, you can just translate it to a small function.


September 16, 2005
"Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:dgeroq$1549$1@digitaldaemon.com...
> This can be accomplished by taking advantage of one of the stranger
features
> of templates.

That's a nice tip. In fact, I added it to www.digitalmars.com/d/htomodule.html

Thanks!


September 16, 2005
This ought to get to the web sites that have D examples!


Jarrett Billingsley wrote:
> "Traveler Hauptman" <Traveler_member@pathlink.com> wrote in message news:dgefv3$nfd$1@digitaldaemon.com...
> 
>>I have been doing pretty well converting .h files to .d. But I have some
>>tricky ones with heavy Macro use that I'd like some best practices help with.
>>
>>Can some of the experienced guys monitoring this group suggest ways to convert
>>the snippet below? It's a survey question, the more ways to do this the better.
> 
> 
> 
> This can be accomplished by taking advantage of one of the stranger features of templates.  Observe:
> 
> 
> const uint GT_DEPTH_SHIFT = 0;
> const uint GT_SIZE_SHIFT = 8;
> const uint GT_SCHEME_SHIFT = 24;
> const uint GT_DEPTH_MASK  = 0xffU << GT_DEPTH_SHIFT;
> 
> const uint GT_TEXT = 0x01 << GT_SCHEME_SHIFT;
> 
> // Macro that constructs a graphtype
> template GT_CONSTRUCT(uint depth, uint scheme, uint size)
> {
>  // notice the name of the const is the same as that of the template
>  const uint GT_CONSTRUCT = (depth | scheme | (size << GT_SIZE_SHIFT));
> }
> 
> // Common graphtypes
> const uint GT_TEXT16 = GT_CONSTRUCT!(4, GT_TEXT, 16);
> 
> 
> If you define a constant in a template that has the same name as the template, it will kind of function as a "return value."  It's very odd.
> 
> Note that you only need to do this with function-style macros that must be called at module level.  If it is a macro that is only ever called in functions, you can just translate it to a small function. 
> 
>