Advertisement
Guest User

approach-3 -- modified

a guest
Dec 20th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 5.20 KB | None | 0 0
  1. import std.traits    : isIntegral, isFloatingPoint, isCallable,
  2.                        PIT=ParameterIdentifierTuple, PTT=ParameterTypeTuple;
  3. import std.conv      : to ;
  4. import std.algorithm : startsWith ;
  5. import std.array     : split ;
  6.  
  7. //import std.stdio     : writeln ;
  8.  
  9. template GenStructs(uint count, U...)
  10. {
  11.     static if (U.length == 0)
  12.     {
  13.         enum GenStructs = "\n";    
  14.     }
  15.     else
  16.     {
  17.         enum GenStructs = "\n" ~ (U[0]).stringof ~ " _s" ~    to!string(count) ~ ";" ~
  18.                           GenStructs!(1+count, U[1..$]);
  19.     }
  20.    
  21.     //pragma(msg, GenStructs);
  22. }
  23.  
  24. /*
  25. struct test0    {      }
  26.  
  27. struct test1(T)
  28. {
  29.     T    A;  
  30.          this(T A_) { A = A_; }
  31.     void setA(T A_) { A = A_; }
  32.     T    getA()     { return A; }
  33.     void empty()    { writeln("empty() called"); }
  34.          ~this()    { }  
  35. }
  36. */
  37.  
  38. //pragma(msg, GenStructs!(0, test0, test1!(int)) );
  39.  
  40.  
  41. //pragma(msg, __traits(allMembers, test1!(int)));
  42.  
  43. template Filter(U...)
  44. {
  45.     static if (U.length == 0)
  46.     {
  47.         enum Filter = "";
  48.     }
  49.     else
  50.     {
  51.         static if ( (U[0]).startsWith("__") || (U[0]).startsWith("op") )
  52.         {
  53.             enum Filter = Filter!(U[1..$]); //skip U[0]
  54.         }
  55.         else
  56.         {
  57.             enum Filter = U[0] ~ "," ~ Filter!(U[1..$]);
  58.         }
  59.     }
  60. }
  61.  
  62. //pragma(msg, Filter!(__traits(allMembers, test1!(int)))[0..$-1].split(",") );
  63.  
  64. template StringOf(TS...)
  65. {
  66.     static if(TS.length == 0)
  67.     {
  68.         enum StringOf = "";
  69.     }
  70.     else static if(TS.length == 1)
  71.     {
  72.         enum StringOf = (TS[0]).stringof;
  73.     }
  74.     else
  75.     {
  76.         enum StringOf = (TS[0]).stringof ~ "," ~ StringOf!(TS[1..$]) ;
  77.     }
  78. }
  79.  
  80. template ArgStringOf(TS...)
  81. {
  82.     static if(TS.length == 0)
  83.     {
  84.         enum ArgStringOf = "";
  85.     }
  86.     else static if(TS.length == 1)
  87.     {
  88.         enum ArgStringOf = TS[0];
  89.     }
  90.     else
  91.     {
  92.         enum ArgStringOf = TS[0] ~ "," ~ ArgStringOf!(TS[1..$]);
  93.     }
  94. }
  95.  
  96. string combine(string[] types, string[] members)
  97. {
  98.     assert(types.length == members.length);
  99.    
  100.     string combined = "";
  101.    
  102.     for(int i=0; i < (types.length) ; ++i)
  103.     {
  104.         combined ~= types[i] ~ " " ~ members[i] ~ ", ";
  105.     }
  106.    
  107.     if(combined != "") combined = combined[0..$-2] ; //trim end ", "
  108.    
  109.     return combined;
  110. }
  111.  
  112. template GenProperty(string M, string N, alias SN)
  113. {
  114.     enum GenProperty = `
  115.                        @property auto ref ` ~ N ~ `(` ~
  116.                         combine( StringOf!(PTT!(SN)).split(","),
  117.                                  ArgStringOf!(PIT!(SN)).split(",") )~ `)
  118.                        { return ` ~ M ~ `(` ~ ArgStringOf!(PIT!(SN)) ~ `); }`;
  119.  
  120.     //pragma(msg, GenProperty);
  121. }
  122.  
  123. string genProperty(string mem, string name, string s_name)
  124. {
  125.     return `static if ( !__traits(compiles, ` ~ name ~ `) )
  126.            {    static if (isCallable!(` ~ s_name ~ `.` ~ name ~ `))
  127.                {
  128.                    mixin (GenProperty!( "`~ mem ~`", "`~ name ~`", `~ s_name ~`.`~ name ~`));
  129.                }
  130.                else
  131.                {
  132.                    @property auto ref ` ~ name ~ `() { return ` ~ mem ~ `; };
  133.                    @property ` ~ name ~ `(typeof(` ~ s_name ~ `.` ~ name ~ `) _` ~ name ~ `)
  134.                    { ` ~ mem ~ ` = _` ~ name ~ `; }
  135.                }
  136.            }
  137.            else
  138.            {
  139.            }
  140.            `;
  141. }
  142.  
  143. string getAlias(uint id, string[] members, string s_name)
  144. {
  145.     string output;
  146.  
  147.     foreach(m ; members)
  148.     {    
  149.         output ~= genProperty("this._s" ~ to!string(id) ~ "." ~ m, m, s_name);
  150.     }
  151.    
  152.     return output;
  153. }
  154.  
  155. template GenAliases(uint count, U...)
  156. {
  157.     static if (U.length == 0)
  158.     {
  159.         enum GenAliases = "";    
  160.     }
  161.     else
  162.     {
  163.         enum GenAliases = getAlias(count,
  164.                                    Filter!(__traits(allMembers, U[0]))[0..$-1].split(","),
  165.                                    U[0].stringof) ~ GenAliases!(1+count, U[1..$]);
  166.     }
  167.     //pragma(msg, GenAliases);
  168. }
  169.                                                      
  170. /*
  171. struct test2
  172. {
  173.     int X;
  174.     int sumX(int val) { return (cast(int)(X + val)); }
  175. }
  176. */                                                    
  177.                                                      
  178. //pragma(msg, GenAliases!(0, test1!(int), test2) );
  179.  
  180. template Gen(string name, U...)
  181. {
  182.     static assert(U.length != 0); //otherwise no use for this template
  183.    
  184.     enum Gen = `struct ` ~ name ~ ` { ` ~  GenStructs!(0, U) ~
  185.                                            GenAliases!(0, U) ~ ` }`;
  186.    
  187.     //pragma(msg, Gen);
  188. }
  189.  
  190. //pragma(msg, Gen!("T1", test1!(int)));
  191.  
  192. //mixin (Gen!("T1", test1!(int)));
  193.  
  194.  
  195. ////////////////////////////////////////////////////////////
  196.  
  197. struct S_A { int x; int y; void func() { x = 2*x; } ; void funcA() { } ; }
  198. struct S_B { int x; int z; void func() { x = 3*x; } ; void funcB() { } ; }
  199.                                                      
  200. void main()
  201. {    
  202.     import std.stdio: writeln;
  203.    
  204.     mixin (Gen!("S_AB", S_A, S_B));
  205.     pragma(msg, __traits(allMembers, S_AB));
  206.      
  207.     S_AB s_ab;
  208.     s_ab.x = 10;
  209.     s_ab.func();
  210.     assert(s_ab.x == 20);
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement