www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - derelict-sdl2 automatically stripping the SDL_ prefix from names

reply ProgramGamer <miguel.arseneault paulequipment.com> writes:
Hi,

I attempted to use a combination of traits and mixins to 
automatically create aliases of names in SDL2 without the SDL_ 
prefix for convenience. However, I was only able to achieve this 
for functions in the module, and not for types nor enum values. 
How could I go about achieving the desired result? Here is the 
code I'm using in an intermediate "sdl_stripped" module:

import std.regex;
import std.array;
public import derelict.sdl2.sdl;

string StripSDLPrefix() {
     auto members = [__traits(allMembers, derelict.sdl2.internal)];
     foreach(ref member; members) {
         if (member.length > 4 && member[0..4] == "SDL_") {
             member = "alias " ~ member[4..$] ~ " = " ~ member ~ 
";\n";
         }
         else {
             member = "";
         }
     }
     return members.join();
}

mixin(StripSDLPrefix());

// I added this to print the list of generated names for 
debugging purposes.
static if (false) {
     pragma(msg, StripSDLPrefix());
}
Nov 26 2018
parent unDEFER <undefer gmail.com> writes:
Hello, as I know allMembers returns members not recursively. If 
you want to get really all members you need to make recursive 
function. In my program I used the next routine to print all 
members of module:

static void allMembersOfModule(string module_name, bool 
root=false)()
{
     static if (module_name != "object" &&
         module_name != __MODULE__)
     {
         mixin("import "~module_name~";");

         pragma(msg, module_name~":");

         foreach(member; __traits(allMembers, mixin(module_name)))
         {
             static if (__traits(compiles, mixin(member)) &&
                 (!__traits(compiles, __traits(getProtection, 
mixin(member))) ||
                 __traits(getProtection, mixin(member)) != 
"private" &&
                 __traits(getProtection, mixin(member)) != 
"package"))
                 pragma(msg, member);
         }

         static if (root || module_name == "std.algorithm" || 
module_name == "std.range")
         {
             import std.algorithm.searching: startsWith;

             foreach(member; __traits(allMembers, 
mixin(module_name)))
             {
                 static if (__traits(compiles, mixin(member)) &&
                     __traits(compiles, 
mixin(member).stringof.startsWith("module ")) &&
                     mixin(member).stringof.startsWith("module "))
                 {
                     allMembersOfModule!(member);
                 }
             }
         }
     }
}

As I know it worked good. Maybe it helps to you also.
Nov 28 2018