dranges.templates
This module defines some useful templates for the other modules and some 'meta-templates'. Templates
transforming other templates, currying them, flipping their arguments, etc.
License:
Boost License 1.0.
Authors:
Philippe Sigaud and Simen Kjærås
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- template
isInstanceOf
(T,alias templ)
- Alias itself to true if
T is an instance of templ . To obtain the template parameters,
see TemplateParametersTypeTuple.
Example:
auto cy = cycle([0,1,2,3]); // cy is a Cycle!(int[])
alias typeof(cy) Cy;
assert(isInstanceOf!(Cy, Cycle));
- template
staticSwitch
(alias F,T...) if (allSatisfy!(isAlias,T))
- Switches between template instantiations depending on the parameters passed.
Author:
Simen Kjærås.
Example:
alias staticSwitch( foo, 1, 2, 3 ).With callFoo;
callFoo( 2 ); // Actually calls foo!(2)( )
- template
BindT
(alias Template,alias Param0)
- Bind
Param0 as the first parameter of Template .
See Also:
CurryTemplate, for a more powerful and up-to-date version.
- template
BindF
(alias FunctionTemplate,alias Param0)
- Bind
Param0 as the first template parameter of a function template.
- template
CurryTemplate
(alias templ)
- Takes a n-args template and transforms it into n 1-arg templates inside each other.
Very useful for templates with many arguments, when you only have one arg right now, or want
to bind this arg.
CurryTemplate
also works with template alias parameters (and will
wait for an alias, not a type).
Example:
// StaticMap takes a template and typetuple as parameters.
alias CurryTemplate!(StaticMap) CStaticMap; // CStaticMap takes a template and then a T... (typetuple)
alias CStaticMap!Unqual SMU; // SMU is now an independant template taking a T... and mapping Unqual on it.
assert(is(SMU!(int, const int, immutable double, string) == TypeTuple!(int, int, double, string)));
- template
UnWrap
(string Wrapper,T)
- Takes a complex type (ie:Cycle!R, Retro!R) and get rid of the external type indicated
as a string. That is
UnWrap
!("Cycle", Cycle!U) aliases itself to U. It's useful
to get some return types, when you extract internal values in a range and need to get their types.
Example:
auto c = cycle([1,2,3][]);
assert(is(typeof(c) == Cycle!(int[]))); // A cycle around an array of int
alias UnWrap!("Cycle", typeof(c)) InternalType;
assert(is(InternalType == int[]));
Deprecated:
use TemplateParameterTypeTuple instead.
- template
Wrap
(string Wrapper,T)
- The converse of UnWrap.
- template
FlipTemplate
(alias templ)
- Takes a template and flips its arguments.
So, given:
template Foo(A,B) {}
Then
FlipTemplate
!Foo is equivalent to:
template FlippedFoo(B,A) { alias Foo!(A,B) FlippedFoo;}
Example:
template Pair(A,B)
{
alias Tuple!(A,B) Pair;
}
alias FlipTemplate!Pair FPair;
assert(is( Pair!(int,double) == Tuple!(int,double)));
assert(is(FPair!(int,double) == Tuple!(double,int)));
This works for any number of parameters.
Example:
alias FlipTemplate!Tuple FTuple;
assert(is(FTuple!(char,int,double,void delegate(string)) == Tuple!(void delegate(string),double,int,char)));
- template
TransformTemplate
(alias templ,alias transformTypes)
- A generalized version of FlipTemplate: takes a target template and
a transformation on types (another template). It will then apply
the transformation on the parameters before passing the result to the input
template.
Example:
template Foo(A) {}; // Foo takes one type as param.
// First is template defined in dranges.typetuple that takes a TypeTuple and alias itself to the first type.
alias TransformTemplate!(Foo, First) TFoo; // TFoo is a variadic template. Any T... passed to it is transformed
// by First!T into T[0], before serving as parameter for Foo.
// So, TFoo is now a variadic version of Foo.
assert(is(TFoo!(int, double, string) == Foo!(int)));
- template
T1
(string s)
- The template equivalent of unaryFun: takes a string as template argument and creates a simple template aliasing
the string. As for unaryFun, the resulting template argument is 'a'.
Example:
alias T1!"Tuple!(ReturnType!a, ParameterTypeTuple!a)" FunTypes;
assert(is(FunTypes!(int delegate(double, string)) == Tuple!(int, double, string)));
- template
T2
(string s)
- The binaryFun equivalent for templates: given a string with 'a' and 'b', generates a template from it.
It's useful to quickly make a template, for example for a StaticMap or template composition.
Example:
alias T2!"a delegate(b)" MkDelegate; // MkDelegate is a template taking two types a and b and becoming
// the type of delegates from b to a.
- template
TN
(string s)
- The n-types generalization of T1 and T2: given a string in 'a', 'b', ... (up to 'z'), creates
a template from it. It will automatically determine the corresponding number of parameters
by using the same heuristics than for naryFun in dranges.functional2: it looks for lone letters
and takes the higher one
Example:
alias TN!"a delegate(b,c,d)" MkDelegate3; // finds a 'd', arity = 4, MkDelegate3 is a four parameters template
assert(is(MkDelegate!(int,double,char,string) == int delegate(double,char,string)));
- template
Max
(alias a,alias b)
- Aliases itself to the max of a and b.
- template
Min
(alias a,alias b)
- Aliases itself to the min of a and b.
- template
isTemplate
(alias templ)
- Alias itself to true iff templ is a template (standard, function, class or struct template).
- template
TemplateArity
(alias templ) if (isTemplate!(templ))
- Alias itself to the number of parameters needed to instantiate template
templ .
To represent variadic template, returns a negative number.
Example:
template Foo0() {}
template Foo1(A) {}
template Foo2(A,B) {}
template FooVar(A,B,C...) {}
assert(TemplateArity!Foo0 == 0);
assert(TemplateArity!Foo1 == 1);
assert(TemplateArity!Foo2 == 2);
assert(TemplateArity!Foo3 == -3); // Three params, the last being variadic.
- template
TemplateParametersTypeTuple
(T)
- Takes a type instantiating a template (that is, T == A!(someTypes...) for some A)
and becomes the template's parameters typetuple: TypeTuple!(someTypes) in the previous example.
It won't work for alias parameters, because they're not imported by this module.
Example:
assert(is(TemplateParametersTypeTuple!(Cycle!(int[])) == TypeTuple!(int[])));
- template
TemplateName
(T)
- If T is a template instantiation, becomes the template name. For a non-templated type,
it just becomes this type name.
Example:
struct Foo(T...) {}
alias Foo!(int, double) Foo_id;
assert(TemplateName!(Foo_id) == "Foo");
assert(TemplateName!(int) == "int");
Note:
almost completly untested!
- template
TemplateFunctionPTT
(alias fun)
- Gives the parameters typetuple from a non-instantiated template function. It creates
a list of structs T0, T1, ..., Tn (n being TemplateArity!fun) and instantiates fun
to do that. So the resulting TypeTuple is defined for inexistant types!
Limitation:
Does not work if there are constraints on the types.
BUG:
Should be modified to work with variadic templates.
Example:
A foo(A,B)(A a, B b, Tuple!(B,A) c, B d) { return a;}
alias TemplateFunctionPTT!foo PTTfoo; // PTTfoo is TypeTuple!(T0, T1, Tuple!(T1,T0), T1)
- int
TemplateFunArity
(alias templ)();
- Gives the number of args of a non-instantiated template function. Not to be confused with
TemplateArity which gives the number of parameters for the template.
Example:
A foo(A,B)(A a, B b, Tuple!(B,A) c, B d) { return a;}
assert(TemplateFunArity!foo == 4);
assert(TemplateArity!foo == 2);
- template
CompareTypes
(T,U)
- A small template to sort types.
See Also:
dranges.typetuple.SortTypes
- string
TemplateConstraints
(alias templ)();
- Returns the constraints of a template (the guard if (...) after the template name), if any. If there is
no constraint, returns the empty string.
Example:
template Foo(alias A, B, C)
{
alias A!(B,C) Foo;
}
template Bar(A,B,C) if (isIntegral!B && is(C == CommonType!(A,B)))
{
alias Tuple!(A,B,C) Bar;
}
assert(TemplateConstraints!Bar == "isIntegral!(B) && is(C == CommonType!(A,B))");
assert(TemplateConstraints!Foo == "");
- template
isAlias
(alias a)
template
isAlias
(T)
- Is true if a is an alias (a symbol bound by an alias template parameter). It's useful when you have
a variadic type (T...) and some of its components may be aliases instead of types.
- template
isType
(T)
template
isType
(alias a)
- Is true if T is a type and not an alias. It's useful when you have
a variadic type (T...) and some of its components may be types or aliases..
- template
Constant
(T)
- The constant template (akin to the constant function).
Once instantiated with a type, its next instantiation will gives the original type.
It's sometimes useful while mapping templates, composing templates or acting on types in general.
Example:
alias Constant!int CInt;
assert(is(StaticMap!(CInt,double,int,string,char) == TypeTuple!(int,int,int,int)));
- template
Void
(T...)
- The void template: whatever types you give it, it's void.
- template
Null
()
- The null template: takes no type, is void (seems strange, but sometime useful)
- template
Id
(T...)
- The identity template: becomes the types you gives it as parameters. If you give
it one type, it alias itself to this type, not TypeTuple!type.
- template
Compose
(Templates...)
- Compose n templates together. This is one very powerful meta-template, if
I may say so...
Example:
alias Compose!(Cycle, ArrayType) MkCycle; // Takes a T, makes a Cycle!(T[]).
- template
ArrayType
(T)
- Alias itself to T[]. Useful for template composition.
- template
Apply
(T...)
- Apply types
T on successive templates (that is, instantiate them with T ) and makes a TypeTuple from it.
It's the complement of StaticMap , which takes a template an applies it on successive types.
Usage:
Apply!(SomeTypes).On!(Templates).
Example:
alias Apply!(int,double) Applier;
alias Applier.On!(MkDelegate, Tuple, Doubler, CommonType) Result;
assert(is(Result == TypeTuple!(int delegate(double), Tuple!(int,double), int, double, int, double, double)));
- template
On
(alias templ1,Rest...)
- ditto
- template
Instantiate
(alias templ1,Rest...)
- Like
Apply.On , but reversed: first, the templates, then the type.
Usage:
Instantiate!(SomeTemplates).With!(SomeTypes).
- template
MkDelegate
(ReturnType,ParameterTypes...)
- The type of a delegate with corresponding parameters.
- template
TransferParamsTo
(alias templ)
- Usage:
TransferParamsTo!(someTemplate).From!(someTemplatedType).
It will extract the template parameters from someTemplatedType and instantiate someTemplate with them.
Example:
alias TransferParamsTo!Repeat MkRepeat;
alias MkRepeat.From!(Cycle!(int[])) R; // takes a Cycle!(Range), extracts Range, makes a Repeat from it.
assert(is(R == Repeat!(int[]))); // R is a Repeat!(int[])
Note:
so, TransferParamsTo can be seen as a 'function' from a domain of types to another domain. It's a functor,
in a mathematical/Haskell sense (which has nothing to do with a C++ functor).
- template
TransferParamsFrom
(alias templ)
- The same, but with arguments inverted.
Usage:
TransferParamsFrom!(someTemplatedType).To!(someTemplate)
See Also:
TransferParamsTo
- class
Default
;
template
SwitchOnType
(alias value,Type,alias Action,Rest...) template
SwitchOnType
(alias value,Type : Default,alias Action)
- An small switch-like template to produce different code depending on the type of value. The
Default
class
is used to indicate (wait for it) a default value.
int i; double d; string s;
alias SwitchOnType!(i, int, "It's an int", double, "It's a double", Default, "It's something else") switchi;
alias SwitchOnType!(d, int, "It's an int", double, "It's a double", Default, "It's something else") switchd;
alias SwitchOnType!(s, int, "It's an int", double, "It's a double", Default, "It's something else") switchs;
assert(switchi == "It's an int");
assert(switchd == "It's a double");
assert(switchs == "It's something else");
BUG:
Well, not a bug really, but a severe limitation: the 'Action' alias are all evaluated at compile-time, there are not lazy.
I found out it drastically limits the interest of this template: it cannot be used as a way to mixin different pieces of code
based on a directing type.
Example:
// Different predicate strings based on value's type.
alias SwitchOnType!(value,
char, "a.field[1] == '" ~ value ~ "'", /+ Incorrect if value is an int +/
string, Format!("a.field[1] == \"%s\" ", value), /+ Incorrect if value is a char +/
Default, "a.field[1] == " ~ to!string(value)
) predicate;
|