Advertisement
Guest User

Untitled

a guest
Jun 14th, 2010
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.57 KB | None | 0 0
  1. /**
  2.  * Returns the name of the given function
  3.  *
  4.  * Params:
  5.  *     func = the function alias to get the name of
  6.  *    
  7.  * Returns: the name of the function
  8.  */
  9. template functionNameOf (alias func)
  10. {
  11.     version(LDC)
  12.         const functionNameOf = (&func).stringof[1 .. $];
  13.    
  14.     else
  15.         const functionNameOf = (&func).stringof[2 .. $];
  16. }
  17.  
  18. /**
  19.  * Returns the parameter names of the given function
  20.  *
  21.  * Params:
  22.  *     func = the function alias to get the parameter names of
  23.  *    
  24.  * Returns: an array of strings containing the parameter names
  25.  */
  26. template parameterNamesOf (alias func)
  27. {
  28.     const parameterNamesOf = parameterNamesOfImpl!(func);
  29. }
  30.  
  31. /**
  32.  * Returns the parameter names of the given function
  33.  *  
  34.  * Params:
  35.  *     func = the function alias to get the parameter names of
  36.  *    
  37.  * Returns: an array of strings containing the parameter names
  38.  */
  39. private string[] parameterNamesOfImpl (alias func) ()
  40. {
  41.     string funcStr = typeof(&func).stringof;
  42.  
  43.     auto start = funcStr.indexOf('(');
  44.     auto end = funcStr.indexOf(')');
  45.    
  46.     const firstPattern = ' ';
  47.     const secondPattern = ',';
  48.    
  49.     funcStr = funcStr[start + 1 .. end];
  50.    
  51.     if (funcStr == "")
  52.         return null;
  53.        
  54.     funcStr ~= secondPattern;
  55.    
  56.     string token;
  57.     string[] arr;
  58.    
  59.     foreach (c ; funcStr)
  60.     {      
  61.         if (c != firstPattern && c != secondPattern)
  62.             token ~= c;
  63.        
  64.         else
  65.         {          
  66.             if (token)
  67.                 arr ~= token;
  68.            
  69.             token = null;
  70.         }          
  71.     }
  72.    
  73.     if (arr.length == 1)
  74.         return arr;
  75.    
  76.     string[] result;
  77.     bool skip = false;
  78.    
  79.     foreach (str ; arr)
  80.     {
  81.         skip = !skip;
  82.        
  83.         if (skip)
  84.             continue;
  85.        
  86.         result ~= str;
  87.     }
  88.    
  89.     return result;
  90. }
  91.  
  92. /**
  93.  * Helper function for callWithNamedArguments
  94.  *
  95.  * Returns:
  96.  */
  97. private string buildFunction (alias func, string args) ()
  98. {
  99.     const str = split(args);
  100.     string[] params;
  101.     string[] values;
  102.     auto mixinString = functionNameOf!(func) ~ "(";
  103.    
  104.     foreach (s ; str)
  105.     {
  106.         auto index = s.indexOf('=');
  107.         params ~= s[0 .. index];
  108.         values ~= s[index + 1 .. $];
  109.     }      
  110.  
  111.     const parameterNames = parameterNamesOf!(func);
  112.  
  113.     foreach (i, s ; parameterNames)
  114.     {
  115.         auto index = params.indexOf(s);
  116.        
  117.         if (index != params.length)
  118.             mixinString ~= values[index] ~ ",";
  119.     }
  120.    
  121.     return mixinString[0 .. $ - 1] ~ ");";
  122. }
  123.  
  124. /**
  125.  * Calls the given function with named arguments
  126.  *
  127.  * Params:
  128.  *     func = an alias to the function to call
  129.  *     args = a string containing the arguments to call using this syntax: `arg2=value,arg1="value"`
  130.  */
  131. void callWithNamedArguments (alias func, string args) ()
  132. {
  133.     mixin(buildFunction!(func, args));
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement