daily pastebin goal
57%
SHARE
TWEET

Untitled

a guest Oct 21st, 2018 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. unittest
  2. {
  3.     alias Options = NamedArgsT!(NamedArg!(TypeT, "T"), NamedArg!(TypeT, "T2",
  4.             float), NamedArg!(int, "t1", 0), NamedArg!(string, "t2", null),
  5.             NamedArg!(AliasT, "t3", Alias!0));
  6.     static assert(!Options.Complete);
  7.     static assert(!__traits(hasMember, Options, "T"));
  8.     static assert(is(Options.T2 == float));
  9.     static assert(Options.t1 == 0);
  10.     static assert(Options.t2 == null);
  11.     static assert(Options.t3 == 0);
  12.     alias Opt1 = Options.set_T!int.set_t1!(4).set_t3!(Alias!20);
  13.     static assert(Opt1.Complete);
  14.     static assert(is(Opt1.T == int));
  15.     static assert(is(Opt1.T2 == float));
  16.     static assert(Opt1.t1 == 4);
  17.     static assert(Opt1.t2 == null);
  18.     static assert(Opt1.t3 == 20);
  19. }
  20.  
  21. import std.traits;
  22. import std.meta;
  23.  
  24. struct NamedArg(specs...)
  25. {
  26.     enum Instantiated = false;
  27.     static assert(specs.length == 2 || specs.length == 3);
  28.     static assert(is(specs[0]));
  29.     alias Type = specs[0];
  30.     static assert(is(typeof(specs[1]) == string));
  31.     enum Name = specs[1];
  32.     static if (specs.length == 3)
  33.     {
  34.         enum HasValue = true;
  35.         static if (is(Type == AliasT) || is(Type == TypeT))
  36.         {
  37.             alias Value = specs[2];
  38.         }
  39.         else
  40.         {
  41.             enum Value = specs[2];
  42.         }
  43.     }
  44.     else
  45.     {
  46.         enum HasValue = false;
  47.     }
  48. }
  49.  
  50. struct InstantiatedNamedArg(specs...)
  51. {
  52.     enum Instantiated = true;
  53.     enum HasValue = true;
  54.     static assert(specs.length == 3);
  55.     static assert(is(specs[0]));
  56.     alias Type = specs[0];
  57.     static assert(is(typeof(specs[1]) == string));
  58.     enum Name = specs[1];
  59.     static if (is(Type == AliasT) || is(Type == TypeT))
  60.     {
  61.         alias Value = specs[2];
  62.     }
  63.     else
  64.     {
  65.         enum Value = specs[2];
  66.     }
  67. }
  68.  
  69. struct AliasT;
  70. struct TypeT;
  71.  
  72. mixin template NamedArgMembers(Arg)
  73. {
  74.     static if (Arg.HasValue)
  75.     {
  76.         static if (is(Arg.Type == AliasT) || is(Arg.Type == TypeT))
  77.         {
  78.             mixin("alias " ~ Arg.Name ~ " = " ~ fullyQualifiedName!Arg ~ ".Value;");
  79.         }
  80.         else
  81.         {
  82.             mixin("enum " ~ Arg.Name ~ " = " ~ fullyQualifiedName!Arg ~ ".Value;");
  83.         }
  84.     }
  85. }
  86.  
  87. struct NamedArgsT(Args...)
  88. {
  89.     private template IsNotT(T)
  90.     {
  91.         private enum IsNotT(U) = !is(T == U);
  92.     }
  93.  
  94.     static foreach (i, Arg; Args)
  95.     {
  96.         mixin NamedArgMembers!Arg;
  97.         static if (!Arg.Instantiated)
  98.         {
  99.             static if (is(Arg.Type == TypeT))
  100.             {
  101.                 mixin("alias set_" ~ Arg.Name ~ "(__setNamedArgVal) = NamedArgsT!(InstantiatedNamedArg!("
  102.                         ~ fullyQualifiedName!(Arg) ~ ".Type, " ~ fullyQualifiedName!(
  103.                             Arg) ~ ".Name, __setNamedArgVal), Filter!(IsNotT!(" ~ fullyQualifiedName!(
  104.                             Arg) ~ "), Args));");
  105.             }
  106.             else static if (is(Arg.Type == AliasT))
  107.             {
  108.                 mixin("alias set_" ~ Arg.Name ~ "(alias __setNamedArgVal) = NamedArgsT!(InstantiatedNamedArg!("
  109.                         ~ fullyQualifiedName!(Arg) ~ ".Type, " ~ fullyQualifiedName!(
  110.                             Arg) ~ ".Name, __setNamedArgVal), Filter!(IsNotT!(" ~ fullyQualifiedName!(
  111.                             Arg) ~ "), Args));");
  112.             }
  113.             else
  114.             {
  115.                 mixin("alias set_" ~ Arg.Name ~ "(" ~ fullyQualifiedName!(
  116.                         Arg) ~ ".Type __setNamedArgVal) = NamedArgsT!(InstantiatedNamedArg!(" ~ fullyQualifiedName!(
  117.                         Arg) ~ ".Type, " ~ fullyQualifiedName!(
  118.                         Arg) ~ ".Name, __setNamedArgVal), Filter!(IsNotT!(" ~ fullyQualifiedName!(
  119.                         Arg) ~ "), Args));");
  120.             }
  121.         }
  122.     }
  123.  
  124.     private enum HasValue(T) = T.HasValue;
  125.     enum Complete = allSatisfy!(HasValue, Args);
  126. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top