Reducing source code: weak+alias values in array

Jens Bauer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 1 17:25:47 PDT 2015


On Friday, 1 May 2015 at 21:36:29 UTC, Artur Skawina wrote:
> On 05/01/15 22:29, Jens Bauer via Digitalmars-d-learn wrote:
>> On Wednesday, 29 April 2015 at 13:58:14 UTC, Artur Skawina 
>> wrote:
> Use `@weakalias!"blah"` instead:
>
>    enum weakalias(string A) = gcc.attribute.attribute("alias", 
> A);
>
>    @weakalias!"defaultResetHandler" extern (C) void 
> Reset_Handler();

Here's my resulting code-snippet:

enum weak = gcc.attribute.attribute("weak");

alias Tuple(A...) = A;
alias weakalias(string A) = Tuple!(weak, 
gcc.attribute.attribute("alias", A));

...

     foreach (I, M; A.init.tupleof)
     {
         static if (is(typeof(M)==A.EXC))
             code ~= `@weakalias!"`~M.n~`" extern (C) void ` ~ 
__traits(identifier, A.tupleof[I]) ~ "();\n";
     }

... because the 'alias' attribute does not automcatically include 
the 'weak' attribute.
It seems to work, but did I write the code correctly ?

>> ... Is it possible to generate a static array without 
>> specifying a fixed array size ?
>
> No, but you can just do:
>
>    code ~= "\n at isr_vector VectorFunc[" ~ 
> A.tupleof.length.stringof ~ "] g_pfnVectors = [\n";

That works great. Thank you for your valuable help. :)

>> Apart from the above two mentioned problems, the code builds 
>> and produces the expected results. I even started to 
>> understand some parts of it, and I find it pretty awesome. ;)
>
> (Ab)using the compiler for the DSL parsing gets really awesome 
> {snip}

I remember the Atari 130XE (and thus the Atari 600XL/800XL) were 
able to auto-generate basic-code, saving a lot of typing. Though 
different, this really reminds me of those days. :)

I've never touched a C++ template, but I've been using #define in 
C.
Though #define is a neat feature, it does not beat this, and as 
I've never had enough reason to use C++ templates, I expect 
they're not as powerful as D's ability to generate code at 
compile-time.


More information about the Digitalmars-d-learn mailing list