AaronCameron.net
Wrecking things for people is important - it makes you a better person. -- Sabrina Constans
Not a Member? - Login or Create an Account...MC Offline
Saturday the 16th of December 2017 @ 07:28pm
Front Page Projects Your Profile About
[]

Filed Under: Journal - Development

mkEnumFlagsLookup

2010.10.18 08:35pm
Aaron

As mentioned here's the script to generate a ToString method for a C\C++ bitwise mask enumeration. Again this is simple stuff, but can be pretty useful for debugging.

This, again, reads from stdin. The idea is to copy the enum from the library source and paste it into the terminal, then copy the output and paste it in your code.

For example, copying SDL_RendererFlags enum in:

/**
 *  \brief Flags used when creating a rendering context
 */
typedef enum
{
    SDL_RENDERER_SINGLEBUFFER = 0x00000001,     /**< Render directly to the 
                                                     window, if possible */
 
    SDL_RENDERER_PRESENTCOPY = 0x00000002,      /**< Present uses a copy from 
                                                     back buffer to the front 
                                                     buffer */
 
    SDL_RENDERER_PRESENTFLIP2 = 0x00000004,     /**< Present uses a flip, 
                                                     swapping back buffer and 
                                                     front buffer */
 
    SDL_RENDERER_PRESENTFLIP3 = 0x00000008,     /**< Present uses a flip, 
                                                     rotating between two back 
                                                     buffers and a front buffer
                                                 */
 
    SDL_RENDERER_PRESENTDISCARD = 0x00000010,   /**< Present leaves the contents
                                                     of the backbuffer undefined
                                                 */
 
    SDL_RENDERER_PRESENTVSYNC = 0x00000020,     /**< Present is synchronized 
                                                     with the refresh rate */
 
    SDL_RENDERER_ACCELERATED = 0x00000040       /**< The renderer uses hardware 
                                                     acceleration */
 
    } SDL_RendererFlags;

Yields:

const std::string GetSDL_RendererFlagsAsString( const int iVal ) {
	string r, sep;
 
	if (iVal & SDL_RENDERER_SINGLEBUFFER) {
		r += sep+"SDL_RENDERER_SINGLEBUFFER";
		sep = ", ";
	}
 
	if (iVal & SDL_RENDERER_PRESENTCOPY) {
		r += sep+"SDL_RENDERER_PRESENTCOPY";
		sep = ", ";
	}
 
	if (iVal & SDL_RENDERER_PRESENTFLIP2) {
		r += sep+"SDL_RENDERER_PRESENTFLIP2";
		sep = ", ";
	}
 
	if (iVal & SDL_RENDERER_PRESENTFLIP3) {
		r += sep+"SDL_RENDERER_PRESENTFLIP3";
		sep = ", ";
	}
 
	if (iVal & SDL_RENDERER_PRESENTDISCARD) {
		r += sep+"SDL_RENDERER_PRESENTDISCARD";
		sep = ", ";
	}
 
	if (iVal & SDL_RENDERER_PRESENTVSYNC) {
		r += sep+"SDL_RENDERER_PRESENTVSYNC";
		sep = ", ";
	}
 
	if (iVal & SDL_RENDERER_ACCELERATED) {
		r += sep+"SDL_RENDERER_ACCELERATED";
		sep = ", ";
	}
 
	return r;
}

Braindead, but a great debugging tool. Here's the perl code:

mkEnumLookup.pl

#!/usr/bin/perl
use strict;
use warnings;
 
my $buf = join '', <>;
 
# Strip out useless stuff.
$buf =~ s/\/\.*$//g;
$buf =~ s/\n/ /g;
$buf =~ s/\/\*.*?\*\///g;
 
# Start processing.
my $name = '';
if ($buf =~ /\{(.*)$/) {
	$buf = $1;
}
 
if ($buf =~ /(.*)\}\s*(.*);+.*$/) {
	$buf = $1;
	$name = $2;
} elsif ($buf =~ /(.*)\}.*$/) {
	$buf = $1;
}
 
my @defs = split /\s*,\s*/, $buf;
my @names = ();
 
$name ||= 'MyEnum';
 
print "const std::string Get$name","AsString( const int iVal ) {\n",
	"\tstring r, sep;\n\n";
 
foreach (@defs) {
	$_ =~ /^\s*(.+?)(\s+|\s*=.+|)$/;
	print "\tif (iVal & $1) {\n\t\tr += sep+\"$1\";\n",
		"\t\tsep = \", \";\n\t}\n\n";
}
print "\treturn r;\n}\n";

More useful than you'd think.

Share:

Reader Comments

©2017 Aaron Cameron