Last active
August 29, 2015 13:57
-
-
Save MrSmith33/9374266 to your computer and use it in GitHub Desktop.
multiple binding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IProperty firstName, lastName, fullName; | |
// fetch properties from widgets. | |
auto converter = (Variant firstName, Variant lastName){ | |
return Variant(firstName ~ " " ~ lastName); | |
}; | |
fullName.pipeFrom(cast(Variant delegate(Variant[]...))converter, firstName, lastName); | |
template isVariant(T) | |
{ | |
enum isVariant = is(T == Variant); | |
} | |
import std.conv; | |
alias ExtractValues(Properties...) = ExtractValuesImpl!(0, Properties); | |
template ExtractValuesImpl(uint index, Properties...) | |
{ | |
pragma(msg, (Properties).length); | |
static if(Properties.length > 1) | |
enum ExtractValuesImpl = "sources[" ~ to!string(index) ~ "].value, " ~ ExtractValuesImpl!(index + 1, Properties[1..$]); | |
else | |
enum ExtractValuesImpl = "sources[" ~ to!string(index) ~ "].value"; | |
} | |
import std.algorithm; | |
import std.range; | |
import std.traits; | |
import std.typetuple; | |
/// creates one-way binding from multiple to one property | |
/// destination.pipeFrom((Variant a, Variant b)=> a+b, num1, num2); | |
void pipeFrom(ConverterType, P ...)(IProperty dest, ConverterType converter, P sources) | |
if (is(ReturnType!ConverterType == Variant) && | |
(ParameterTypeTuple!ConverterType).length == P.length && | |
is(NoDuplicates!(ParameterTypeTuple!ConverterType) == TypeTuple!Variant) && | |
is(NoDuplicates!P == TypeTuple!IProperty)) | |
{ | |
auto handler = (FlexibleObject obj, Variant value){ | |
dest.value = mixin("converter("~ExtractValues!sources~")"); | |
}; | |
foreach(index, IProperty source; sources) | |
{ | |
source.valueChanged.connect(handler); | |
} | |
dest.value = mixin("converter("~ExtractValues!sources~")"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment