Skip to content

Commit

Permalink
vibe.utils.meta.funcattr : add IsAttributedParameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Dicebot committed Oct 18, 2013
1 parent 65b7d82 commit 971a0b7
Showing 1 changed file with 58 additions and 2 deletions.
60 changes: 58 additions & 2 deletions source/vibe/utils/meta/funcattr.d
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,62 @@ unittest
@after!filter()
int foo() { return 42; }
}
/**
Checks if parameter is calculated by one of attached
functions.
Params:
Function = function symbol to query for attributes
name = parameter name to check
Returns:
`true` if it is calculated
*/
template IsAttributedParameter(alias Function, string name)
{
import std.traits : FunctionTypeOf;

static assert (is(FunctionTypeOf!Function));

private {
alias Data = AttributedParameterMetadata!Function;

template Impl(T...)
{
static if (T.length == 0) {
enum Impl = false;
}
else {
static if (T[0].name == name) {
enum Impl = true;
}
else {
enum Impl = Impl!(T[1..$]);
}
}
}
}

enum IsAttributedParameter = Impl!Data;
}

///
unittest
{
int foo()
{
return 42;
}

@before!foo("name1")
void bar(int name1, double name2)
{
}

static assert (IsAttributedParameter!(bar, "name1"));
static assert (!IsAttributedParameter!(bar, "name2"));
static assert (!IsAttributedParameter!(bar, "oops"));
}

// internal attribute definitions
private {
Expand Down Expand Up @@ -390,11 +446,11 @@ private {
*/
struct AttributedFunction(alias Function, alias StoredArgTypes)
{
import std.traits : isSomeFunction, ReturnType;
import std.traits : isSomeFunction, ReturnType, FunctionTypeOf;
import vibe.utils.meta.typetuple : Group, isGroup;

static assert (isGroup!StoredArgTypes);
static assert (isSomeFunction!(typeof(Function)));
static assert (is(FunctionTypeOf!Function));

/**
Stores argument tuple for attached function calls
Expand Down

0 comments on commit 971a0b7

Please sign in to comment.