/usr/include/d2/4.6/std/bind.d is in libphobos2-4.6-dev 0.29.1-4.6.3-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 | // Written in the D programming language.
/**
* $(RED This module has been deprecated. Use delegates for binding arguments to specific values.)
*
* Bind function arguments to functions.
*
* References:
* $(LINK2 http://www.boost.org/libs/bind/bind.html, boost bind)
* Macros:
* WIKI = Phobos/StdBind
*
* Copyright: Copyright Tomasz Stachowiak 2006 - 2009.
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
* Authors: Tomasz Stachowiak
* Source: $(PHOBOSSRC std/_bind.d)
*/
/* Copyright Tomasz Stachowiak 2006 - 2009.
* 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)
*/
module std.bind;
deprecated:
import std.string : stdFormat = format;
import std.traits;
import std.typetuple;
struct DynArg(int i) {
static assert (i >= 0);
alias i argNr;
}
/**
When passed to the 'bind' function, they will mark dynamic params - ones that aren't statically bound
In boost, they're called __1, __2, __3, etc.. here __0, __1, __2, ...
*/
const DynArg!(0) _0;
const DynArg!(1) _1; /// ditto
const DynArg!(2) _2; /// ditto
const DynArg!(3) _3; /// ditto
const DynArg!(4) _4; /// ditto
const DynArg!(5) _5; /// ditto
const DynArg!(6) _6; /// ditto
const DynArg!(7) _7; /// ditto
const DynArg!(8) _8; /// ditto
const DynArg!(9) _9; /// ditto
/*
Detect if a given type is a DynArg of any index
*/
template isDynArg(T) {
static if (is(typeof(T.argNr))) { // must have the argNr field
static if(is(T : DynArg!(T.argNr))) { // now check the exact type
static enum bool isDynArg = true;
} else static enum bool isDynArg = false;
} else static enum bool isDynArg = false;
}
/*
Detect if a given type is a DynArg of the specified index
*/
template isDynArg(T, int i) {
static enum bool isDynArg = is(T : DynArg!(i));
}
/*
Converts a static array type to a dynamic array type
*/
template DynamicArrayType(T) {
alias typeof(T[0])[] DynamicArrayType;
}
/*
Assigns one entity to another. As static arrays don't like normal assignment, slice assignment is used for them.
Params:
a = destination
b = source
*/
template _assign(T) {
static if (isStaticArray!(T)) {
void _assign(DynamicArrayType!(T) a, DynamicArrayType!(T) b) {
a[] = b[];
}
} else {
void _assign(ref T a, ref T b) {
a = b;
}
}
}
/*
Assigns and potentially converts one entity to another
Normally, only implicit conversion is used, but when both operands are numeric types, an explicit cast is performed on them.
Params:
T = destination type
a = destination
Y = source type
b = source
copyStaticArrays = when a static array is assigned to a dynamic one, it sometimes has to be .dup'ed as the storage may exist in volatile locations
*/
template _assign(T, Y, bool copyStaticArrays = true) {
static if (isStaticArray!(T)) {
// if the destination is a static array, copy each element from the source to the destination by a foreach
void _assign(DynamicArrayType!(T) a, DynamicArrayType!(Y) b) {
foreach (i, x; b) {
_assign!(typeof(a[i]), typeof(x))(a[i], x);
}
}
} else static if (!isStaticArray!(T) && isStaticArray!(Y)) {
// the destination is a dynamic array and the source is a static array. this sometimes needs a .dup
void _assign(ref T a, DynamicArrayType!(Y) b) {
static if (copyStaticArrays) {
a = b.dup;
} else {
a = b;
}
}
} else {
// none of the items is a static array
void _assign(ref T a, ref Y b) {
static if (IndexOf!(T, NumericTypes.type) != -1 && IndexOf!(Y, NumericTypes.type) != -1) {
a = cast(T)b;
} else {
a = b;
}
}
}
}
/**
A simple tuple struct with some basic operations
*/
struct Tuple(T ...) {
alias Tuple meta;
enum bool expressionTuple = isExpressionTuple!(T);
static if (!expressionTuple) {
alias T type; // a built-in tuple
T value; // a built-in tuple instance
} else {
alias T value;
}
enum int length = value.length;
/**
Statically yields a tuple type with an extra element added at its end
*/
template appendT(X) {
alias .Tuple!(T, X) appendT;
}
/**
Yields a tuple with an extra element added at its end
*/
appendT!(X) append(X)(X x) {
appendT!(X) res;
foreach (i, y; value) {
_assign!(typeof(y))(res.value[i], y);
}
_assign!(typeof(x))(res.value[$-1], x);
return res;
}
/**
Statically yields a tuple type with an extra element added at its beginning
*/
template prependT(X) {
alias .Tuple!(X, T) prependT;
}
/**
Yields a tuple with an extra element added at its beginning
*/
prependT!(X) prepend(X)(X x) {
prependT!(X) res;
foreach (i, y; value) {
_assign!(typeof(y))(res.value[i+1], y);
}
_assign!(typeof(x))(res.value[0], x);
return res;
}
/**
Statically concatenates this tuple type with another tuple type
*/
template concatT(T ...) {
static if (expressionTuple) {
alias .Tuple!(value, T) concatT;
} else {
alias .Tuple!(type, T) concatT;
}
}
string toString() {
auto res = "(" ~ stdFormat(value[0]);
foreach (x; value[1..$]) {
res ~= stdFormat(", ", x);
}
return res ~ ")";
}
}
/**
An empty tuple struct
*/
struct Tuple() {
alias Tuple meta;
template EmptyTuple_(T ...) {
alias T EmptyTuple_;
}
alias EmptyTuple_!() type; /// an empty built-in tuple
alias EmptyTuple_!() value; /// an empty built-in tuple
enum bool expressionTuple = false;
enum int length = 0;
template appendT(X) {
alias .Tuple!(X) appendT;
}
alias appendT prependT;
appendT!(X) append(X)(X x) {
appendT!(X) res;
foreach (i, y; value) {
_assign!(typeof(y))(res.value[i], y);
}
return res;
}
alias append prepend;
// T - other tuple
template concatT(T ...) {
alias .Tuple!(T) concatT;
}
char[] toString() {
return "()";
}
}
/**
Dynamically create a tuple from the given items
*/
Tuple!(T) tuple(T ...)(T t) {
Tuple!(T) res;
foreach (i, x; t) {
_assign!(typeof(x))(res.value[i], x);
}
return res;
}
/**
Checks whether a given type is the Tuple struct of any length
*/
template isTypeTuple(T) {
static if (is(T.type)) {
static if (is(T == Tuple!(T.type))) {
enum bool isTypeTuple = true;
} else enum bool isTypeTuple = false;
} else enum bool isTypeTuple = false;
}
unittest
{
static assert(isTypeTuple!(Tuple!(int)));
static assert(isTypeTuple!(Tuple!(float, char)));
static assert(isTypeTuple!(Tuple!(double, float, int, char[])));
static assert(isTypeTuple!(Tuple!(Object, creal, long)));
static assert(!isTypeTuple!(Object));
static assert(!isTypeTuple!(int));
}
template minNumArgs_impl(alias fn, fnT) {
alias ParameterTypeTuple!(fnT) Params;
Params params = void;
template loop(int i = 0) {
static assert (i <= Params.length);
static if (is(typeof(fn(params[0..i])))) {
enum int res = i;
} else {
alias loop!(i+1).res res;
}
}
alias loop!().res res;
}
/**
Finds the minimal number of arguments a given function needs to be provided
*/
template minNumArgs(alias fn, fnT = typeof(&fn)) {
enum int minNumArgs = minNumArgs_impl!(fn, fnT).res;
}
// mixed into BoundFunc struct/class
template MBoundFunc() {
// meta
alias FAlias_ FAlias;
alias FT FuncType;
alias AllBoundArgs_ AllBoundArgs; // all arguments given to bind() or bindAlias()
static if (!is(typeof(FAlias) == EmptySlot)) {
alias Tuple!(ParameterTypeTuple!(FT)) RealFuncParams; // the parameters of the bound function
alias FuncReferenceParamsAsPointers!(FAlias) FuncParams; // references converted to pointers
} else {
alias Tuple!(ParameterTypeTuple!(FT)) FuncParams; // the parameters of the bound function
}
alias ReturnType!(FT) RetType; // the return type of the bound function
alias ExtractedBoundArgs!(AllBoundArgs.type) BoundArgs; // 'saved' arguments. this includes nested/composed functions
// if bindAlias was used, we can detect default arguments and only demand the non-default arguments to be specified
static if (!is(typeof(FAlias) == EmptySlot)) {
enum int minFuncArgs = minNumArgs!(FAlias);
alias ParamsPassMethodTuple!(FAlias) ParamPassingMethods; // find out whether the function expects parameters by value or reference
} else {
enum int minFuncArgs = FuncParams.length;
}
// the parameters that our wrapper function must get
alias getDynArgTypes!(FuncParams, AllBoundArgs, minFuncArgs).res.type DynParams;
// data
FuncType fp;
BoundArgs boundArgs;
// yields the number of bound-function parameters that are covered by the binding. takes tuple expansion into account
template numFuncArgsReallyBound(int argI = 0, int fargI = 0, int bargI = 0) {
// walk though all of AllBoundArgs
static if (argI < AllBoundArgs.length) {
// the argI-th arg is a composed/nested function
static if (isBoundFunc!(AllBoundArgs.type[argI])) {
alias DerefFunc!(AllBoundArgs.type[argI]).RetType FuncRetType;
enum int argLen = getArgLen!(FuncParams.type[fargI], FuncRetType);
enum int bargInc = 1;
}
// the argI-th arg is a dynamic argument whose value we will get in the call to func()
else static if (isDynArg!(AllBoundArgs.type[argI])) {
enum int argLen = getArgLen!(FuncParams.type[fargI], DynParams[AllBoundArgs.type[argI].argNr]);
enum int bargInc = 0;
}
// the argI-th arg is a statically bound argument
else {
enum int argLen = getArgLen!(FuncParams.type[fargI], BoundArgs.type[bargI]);
enum int bargInc = 1;
}
// iterate
enum int res = numFuncArgsReallyBound!(argI+1, fargI+argLen, bargI+bargInc).res;
} else {
// last iteration
// the number of bound args is the number of arguments we've detected in this template loop
enum int res = fargI;
// make sure we'll copy all args the function is going to need
static assert (res >= minFuncArgs);
}
}
enum int numSpecifiedParams = numFuncArgsReallyBound!().res;
// it's a tuple type whose instance will be applied to the bound function
alias Tuple!(FuncParams.type[0 .. numSpecifiedParams]) SpecifiedParams;
// argI = indexes AllBoundArgs
// fargI = indexes funcArgs
// bargI = indexes boundArgs
void copyArgs(int argI = 0, int fargI = 0, int bargI = 0)(ref SpecifiedParams funcArgs, DynParams dynArgs) {
static if (argI < AllBoundArgs.length) {
// the argI-th arg is a composed/nested function
static if (isBoundFunc!(AllBoundArgs.type[argI])) {
alias DerefFunc!(AllBoundArgs.type[argI]).RetType FuncRetType;
alias DerefFunc!(AllBoundArgs.type[argI]).DynParams FuncDynParams;
// if FuncDynParams contains an empty slot, e.g. as in the case bind(&f, bind(&g, _1), _0)
// then we cannot just apply the dynArgs tuple to the nested/composed function because it will have EmptySlot params
// while our dynArgs tuple will contain ordinary types
static if (ContainsEmptySlotType!(FuncDynParams)) {
FuncDynParams funcParams; // we'll fill it with values in a bit
foreach (i, dummy_; dynArgs) {
static if (!is(typeof(FuncDynParams[i] == EmptySlot))) {
// 3rd param is false because there is no need to .dup static arrays just for the function below this foreach
// the storage exists in the whole copyArgs function
// dynArgs[i] is used instead of dummy_ so that loop-local data isn't referenced in any dynamic arrays after the loop
_assign!(typeof(funcParams[i]), typeof(dummy_), false)(funcParams[i], dynArgs[i]);
}
}
FuncRetType funcRet = boundArgs.value[bargI].func(funcParams);
} else {
FuncRetType funcRet = boundArgs.value[bargI].func(dynArgs[0..FuncDynParams.length]); // only give it as many dynParams as it needs
}
// we'll take data from the returned value
auto srcItem = &funcRet;
enum int bargInc = 1; // nested/composed functions belong to the boundArgs tuple
enum bool dupStaticArrays = true; // because the function's return value is stored locally
}
// the argI-th arg is a dynamic argument whose value we will get in the call to func()
else static if (isDynArg!(AllBoundArgs.type[argI])) {
// we'll take data from dynArgs
auto srcItem = &dynArgs[AllBoundArgs.type[argI].argNr];
enum int bargInc = 0; // dynamic args don't belond to the boundArgs tuple
enum bool dupStaticArrays = true; // because we get dynArgs on stack
}
// the argI-th arg is a statically bound argument
else {
// we'll take data directly from boundArgs
auto srcItem = &boundArgs.value[bargI];
enum int bargInc = 1; // statically bound args belong to the boundArgs tuple
enum bool dupStaticArrays = false; // because the storage exists in boundArgs
}
// the number of bound-function parameters this argument will cover after tuple expansion
enum int argLen = getArgLen!(funcArgs.type[fargI], typeof(*srcItem));
static if (isTypeTuple!(typeof(*srcItem)) && !isTypeTuple!(funcArgs.type[fargI])) {
foreach (i, x; srcItem.value) {
_assign!(funcArgs.type[fargI + i], typeof(x), dupStaticArrays)(funcArgs.value[fargI + i], x);
}
} else {
static assert (1 == argLen);
_assign!(funcArgs.type[fargI], typeof(*srcItem), dupStaticArrays)(funcArgs.value[fargI], *srcItem);
}
// because we might've just expended a tuple, this may be larger than one
static assert (argLen >= 1);
// we could've just used a dynamic arg (0) or a statically bound arg(1)
static assert (bargInc == 0 || bargInc == 1);
return copyArgs!(argI+1, fargI+argLen, bargI+bargInc)(funcArgs, dynArgs);
} else {
// last iteration
// make sure we've copied all args the function will need
static assert (fargI >= minFuncArgs);
}
}
static if (SpecifiedParams.length > 0) {
/// The final wrapped function
RetType func(DynParams dynArgs) {
SpecifiedParams funcArgs;
copyArgs!()(funcArgs, dynArgs);
// if the function expects any parameters passed by reference, we'll have to use the ptrApply template
// and convert pointers back to references by hand
static if (!is(typeof(FAlias) == EmptySlot) && IndexOf!(PassByRef, ParamPassingMethods.type) != -1) {
// function parameter type pointers (int, float*, ref char) -> (int*, float*, char*)
PointerTuple!(Tuple!(RealFuncParams.type[0 .. SpecifiedParams.length])) ptrs;
// initialize the 'ptrs' tuple instance
foreach (i, dummy_; funcArgs.value) {
static if (is(ParamPassingMethods.type[i] == PassByRef)) {
version (BindNoNullCheck) {}
else {
assert (funcArgs.value[i], "references cannot be null");
}
ptrs.value[i] = funcArgs.value[i];
} else {
ptrs.value[i] = &funcArgs.value[i];
}
}
// and call the function :)
ptrApply!(RetType, FuncType, ptrs.type)(fp, ptrs.value);
} else {
// ordinary call-by-tuple
return fp(funcArgs.value);
}
}
} else {
/// The final wrapped function
RetType func() {
return fp();
}
}
/// The final wrapped function
alias func call;
/// The final wrapped function
alias func opCall;
/**
The type of the delegate that may be returned from this object
*/
template PtrType() {
alias typeof(&(new BoundFunc).call) PtrType;
}
/**
Get a delegate. Equivalent to getting it thru &foo.call
*/
PtrType!() ptr() {
return &this.func;
}
}
version (BindUseStruct) {
template DerefFunc(T) {
alias typeof(*T) DerefFunc;
}
/**
A context for bound/curried functions
*/
struct BoundFunc(FT, alias FAlias_, AllBoundArgs_) {
mixin MBoundFunc;
}
} else {
template DerefFunc(T) {
alias T DerefFunc;
}
/**
A context for bound/curried functions
*/
class BoundFunc(FT, alias FAlias_, AllBoundArgs_) {
mixin MBoundFunc;
}
}
/**
bind() can curry or "bind" arguments of a function, producing a different function which requires less parameters,
or a different order of parameters. It also allows function composition.
The syntax of a bind() call is:
bind(function or delegate pointer { , <b>argument</b> });
<b>argument</b> can be one of:
<ul>
<li> static/bound argument (an immediate value) </li>
<li> another bound function object </li>
<li> dynamic argument, of the form __[0-9], e.g. __0, __3 or __9 </li>
</ul>
The result is a function object, which can be called using call(), func() or opCall().
There also exists a convenience function, ptr() which returns a delegate to call/func/opCall
The resulting delegate accepts exactly as many parameters as many distinct dynamic arguments were used.
---
- bind(&foo, _0, _1) // will yield a delegate accepting two parameters
- bind(&foo, _1, _0) // will yield a delegate accepting two parameters
- bind(&bar, _0, _1, _2, _0) // will yield a delegate accepting three parameters
---
<br />
<br />
The types of dynamic parameters are extracted from the bound function itself and when necessary, type negotiation
is performed. For example, binding a function
---
void foo(int a, long b)
// with:
bind(&foo, _0, _0)
---
will result in a delegate accepting a single, optimal parameter type. The best type is computed
using std.typetuple.DerivedToFront, so in case of an int and a long, long will be selected. Generally, bind will try to find
a type that can be implicitly converted to all the other types a given dynamic parameter uses.
Note: in case of numeric types, an explicit, but transparent (to the user) cast will be performed
<br />
Function composition works intuitively:
---
bind(&f1, bind(&f2, _0))
---
which will yield a delegate, that takes the argument, calls f2, then uses the return value of f2 to call f1. Mathematically
speaking, it will yield a function composition:
---
f1(f2(_0))
---
When one function is composed multiple times, it will be called multiple times - Bind does no lazy evaluation, so
---
bind(&f3, bind(&f4, _0), bind(&f4, _0))
---
will produce a delegate, which, upon calling, will invoke f4 two times to evaluate the arguments for f3 and then call f3
One another feature that bind() supports is automatic tuple expansion. It means that having functions:
---
void foo(int a, int b)
Tuple!(int, int) bar()
---
Allows them to be bound by writing:
---
bind(&foo, bind(&bar))
// or
bind(&foo, tuple(23, 45))
---
*/
typeof(new BoundFunc!(FT, NullAlias, Tuple!(ArgList))) bind(FT, ArgList...)(FT fp, ArgList args) {
auto res = new DerefFunc!(ReturnType!(bind));
res.fp = fp;
extractBoundArgs!(0, 0, ArgList)(res.boundArgs, args);
return res;
}
/**
bindAlias() is similar to bind(), but it's more powerful. Use bindAlias() rather than bind() where possible. <br/>
The syntax is:
bindAlias!(Function)(argument, argument, argument, argument, ...);
bindAlias takes advantage of using aliases directly, thus being able to extract default values from functions and not forcing the user
to bind them. It doesn't, however mean that the resulting delegate can be called, omitting some of its parameters. It only means that these
arguments that have default values in the function provided to bindAlias don't have to be bound explicitly.
Additionally, bindAlias takes care of functions with out/ref parameters, by converting them to pointers internally. A function like:
---
void foo(ref a)
---
can be bound using:
---
int x;
bindAlias!(foo)(&x);
---
Note: there is no bind-time check for reference nullness, there is however a call-time check on all references which can be disabled
by using version=BindNoNullCheck or compiling in release mode.
*/
template bindAlias(alias FT) {
typeof(new BoundFunc!(typeof(&FT), FT, Tuple!(ArgList))) bindAlias(ArgList...)(ArgList args) {
auto res = new DerefFunc!(ReturnType!(bindAlias));
res.fp = &FT;
extractBoundArgs!(0, 0, ArgList)(res.boundArgs, args);
return res;
}
}
/*
Tells whether the specified type is a bound function
*/
template isBoundFunc(T) {
static if (is(DerefFunc!(T).FuncType)) {
static if (is(DerefFunc!(T).BoundArgs)) {
static if (is(typeof(DerefFunc!(T).FAlias))) {
static if (is(DerefFunc!(T) : BoundFunc!(DerefFunc!(T).FuncType, DerefFunc!(T).FAlias, DerefFunc!(T).AllBoundArgs))) {
static enum bool isBoundFunc = true;
} else static enum bool isBoundFunc = false;
} else static enum bool isBoundFunc = false;
} else static enum bool isBoundFunc = false;
} else static enum bool isBoundFunc = false;
}
// all numeric types as of dmd.175
alias Tuple!(byte, ubyte, short, ushort, int, uint, long, ulong, /+cent, ucent, +/float, double, real, ifloat, idouble, ireal, cfloat, cdouble, creal) NumericTypes;
/*
Gather all types that a given (i-th) dynamic arg uses.
The types will be inserted into a tuple
*/
template dynArgTypes(int i, FuncParams, BoundArgs, int minParamsLeft) {
// performs slicing on the tuple ... tuple[i .. $]
template sliceOffTuple(T, int i) {
alias Tuple!(T.type[i .. $]) res;
}
// prepends a T to the resulting tuple
// SkipType - the type in BoundArgs that we're just processing
template prependType(T, SkipType) {
static if (isTypeTuple!(SkipType) && !isTypeTuple!(FuncParams.type[0])) {
// perform tuple decomposition
// e.g. if a function being bound is accepting (int, int) and the current type is a Tuple!(int, int),
// then skip just one tuple in the bound args and the length of the tuple in func args
// - skips two ints and one tuple in the example
alias dynArgTypes!(
i,
sliceOffTuple!(FuncParams, SkipType.length).res,
Tuple!(BoundArgs.type[1..$]),
minParamsLeft - SkipType.length
).res tmp;
} else {
// just advance by one type
alias dynArgTypes!(
i,
sliceOffTuple!(FuncParams, 1).res,
Tuple!(BoundArgs.type[1..$]),
minParamsLeft-1
).res tmp;
}
static if (is(T == void)) { // void means that we aren't adding anything
alias tmp res;
} else {
alias tmp.meta.prependT!(T) res;
}
}
// iteration end detector
static if (is(BoundArgs == Tuple!())) {
static assert (minParamsLeft <= 0, "there are still unbound function parameters");
alias Tuple!() res;
}
else {
// w00t, detected a regular dynamic arg
static if (isDynArg!(BoundArgs.type[0], i)) {
alias prependType!(FuncParams.type[0], BoundArgs.type[0]).res res;
}
// the arg is a bound function, extract info from it. we will be evaluating it later
else static if (isBoundFunc!(BoundArgs.type[0])) {
alias DerefFunc!(BoundArgs.type[0]) BoundFunc; // the bound function is a struct pointer, we have to derefernce its type
// does that function even have any dynamic params ?
static if (BoundFunc.DynParams.length > i) {
alias prependType!(BoundFunc.DynParams[i], BoundFunc.RetType).res res;
}
// it doesn't
else {
alias prependType!(void, BoundFunc.RetType).res res;
}
}
// a static arg, just skip it since we want to find all types a given DynArg uses. static args <> dyn args
else alias prependType!(void, BoundArgs.type[0]).res res;
}
}
// just a simple util
private template maxInt(int a, int b) {
static if (a > b) static enum int maxInt = a;
else static enum int maxInt = b;
}
/*
Given a list of BoundArgs, it returns the nuber of args that should be specified dynamically
*/
template numDynArgs(BoundArgs) {
static if (BoundArgs.length == 0) {
// received an EmptyTuple
static enum int res = 0;
} else {
// ordinary dynamic arg
static if (isDynArg!(BoundArgs.type[0])) {
static enum int res = maxInt!(BoundArgs.type[0].argNr+1, numDynArgs!(Tuple!(BoundArgs.type[1..$])).res);
}
// count the args in nested / composed functions
else static if (isBoundFunc!(BoundArgs.type[0])) {
static enum int res = maxInt!(DerefFunc!(BoundArgs.type[0]).DynParams.length, numDynArgs!(Tuple!(BoundArgs.type[1..$])).res);
}
// statically bound arg, skip it
else {
static enum int res = numDynArgs!(Tuple!(BoundArgs.type[1..$])).res;
}
}
}
/*
Used internally to mark a parameter which is a dummy placeholder
E.g. when using bind(&f, bind(&g, _1), _0), then the inner bound function will use an EmptySlot for its 0-th parameter
*/
struct EmptySlot {
string toString( ) {
return "_";
}
}
/*
Get a tuple of all dynamic args a function binding will need
take nested/composed functions as well as tuple decomposition into account
*/
template getDynArgTypes(FuncParams, BoundArgs, int minFuncArgs) {
template loop(int i) {
static if (i < numDynArgs!(BoundArgs).res) {
alias dynArgTypes!(i, FuncParams, BoundArgs, minFuncArgs).res.type dirtyArgTypeList;
// 'clean' the type list, erasing all NoTypes from it that could've been added there from composed functions
// if the arg is not used, we'll mark it as NoType anyway, but for now, we only want 'real' types so the most derived one can be found
alias Tuple!(EraseAll!(EmptySlot, dirtyArgTypeList)) argTypeList;
// make sure the arg is used
static if(!is(argTypeList == Tuple!())) {
alias DerivedToFront!(argTypeList.type)[0] argType;
} else {
//static assert(false, i);
alias EmptySlot argType;
}
alias loop!(i+1).res.meta.prependT!(argType) res;
} else {
alias Tuple!() res;
}
}
alias loop!(0).res res;
}
/*
Given a tuple that bind() was called with, it will detect which types need to be stored in a BoundFunc object
*/
template ExtractedBoundArgs(BoundArgs ...) {
static if (BoundArgs.length == 0) {
alias Tuple!() ExtractedBoundArgs;
}
// we'll store all non-dynamic arguments...
else static if (!isDynArg!(BoundArgs[0])) {
alias ExtractedBoundArgs!(BoundArgs[1..$]).meta.prependT!(BoundArgs[0]) ExtractedBoundArgs;
}
// ... and we're going to leave the dynamic ones for later
else {
alias ExtractedBoundArgs!(BoundArgs[1..$]) ExtractedBoundArgs;
}
}
/*
Given a tuple that bind() was called with, it will copy all data that a BoundFunc object will store into an ExtractedBoundArgs tuple
*/
void extractBoundArgs(int dst, int src, BoundArgs ...)(ref ExtractedBoundArgs!(BoundArgs) result, BoundArgs boundArgs) {
static if (dst < result.length) {
// again, we only want non-dynamic arguments here
static if (!isDynArg!(BoundArgs[src])) {
_assign!(typeof(result.value[dst]), typeof(boundArgs[src]))(result.value[dst], boundArgs[src]);
return extractBoundArgs!(dst+1, src+1, BoundArgs)(result, boundArgs);
}
// the dynamic ones will be specified at the time BoundFunc.call() is invoked
else {
return extractBoundArgs!(dst, src+1, BoundArgs)(result, boundArgs);
}
}
}
/*
Number of args in the bound function that this Src arg will cover
*/
template getArgLen(Dst, Src) {
// if the arg is a tuple and the target isn't one, it will be expanded/decomposed to the tuple's length
static if (isTypeTuple!(Src) && !isTypeTuple!(Dst)) {
static enum int getArgLen = Src.length;
}
// plain arg - it will use 1:1 mapping of functioni params to bound params
else {
static enum int getArgLen = 1;
}
}
/*
Tell whether a parameter type tuple contains an EmptySlot struct
*/
template ContainsEmptySlotType(ParamList ...) {
enum bool ContainsEmptySlotType = -1 != IndexOf!(EmptySlot, ParamList);
}
// just something to be default in bind(). bindAlias() will use real aliases.
const EmptySlot NullAlias;
struct PassByCopy {}
struct PassByRef {}
template ParamsPassMethodTuple_impl(alias Func, int i = 0) {
alias Tuple!(ParameterTypeTuple!(typeof(&Func))) Params;
static if (Params.length == i) {
alias Tuple!() res;
} else {
Params params = void;
enum params.type[i] constParam;
// if the function expects references, it won't like our const.
static if (is(typeof(Func(params.value[0..i], constParam, params.value[i+1..$])))) {
alias ParamsPassMethodTuple_impl!(Func, i+1).res.meta.prependT!(PassByCopy) res;
} else {
alias ParamsPassMethodTuple_impl!(Func, i+1).res.meta.prependT!(PassByRef) res;
}
}
}
/*
Detect parameter passing methods: PassByCopy or PassByRef[erence]
*/
template ParamsPassMethodTuple(alias Func) {
alias ParamsPassMethodTuple_impl!(Func).res ParamsPassMethodTuple;
}
template FuncReferenceParamsAsPointers_impl(alias Func) {
alias Tuple!(ParameterTypeTuple!(typeof(&Func))) Params;
alias ParamsPassMethodTuple!(Func) PassMethods;
template loop(int i) {
static if (i == Params.length) {
alias Tuple!() res;
} else {
static if (is(PassMethods.type[i] == PassByRef)) {
alias Params.type[i]* type;
} else {
alias Params.type[i] type;
}
alias loop!(i+1).res.meta.prependT!(type) res;
}
}
alias loop!(0).res res;
}
/*
Takes a function/delegate alias and converts its refence parameters to pointers. E.g.
void function(int, ref char, float*) -> (int, char*, float*)
*/
template FuncReferenceParamsAsPointers(alias Func) {
alias FuncReferenceParamsAsPointers_impl!(Func).res FuncReferenceParamsAsPointers;
}
/*
Converts a tuple of types to a tuple containing pointer types of the original types
*/
template PointerTuple(T) {
static if (T.length > 0) {
alias PointerTuple!(Tuple!(T.type[1..$])).meta.prependT!(T.type[0]*) PointerTuple;
} else {
alias Tuple!() PointerTuple;
}
}
/*
Calls a function, dereferencing a pointer tuple for each argument
*/
RetType ptrApply(RetType, FN, T ...)(FN fn, T t) {
static if (1 == T.length) {
return fn(*t[0]);
}
else static if (2 == T.length) {
return fn(*t[0], *t[1]);
}
else static if (3 == T.length) {
return fn(*t[0], *t[1], *t[2]);
}
else static if (4 == T.length) {
return fn(*t[0], *t[1], *t[2], *t[3]);
}
else static if (5 == T.length) {
return fn(*t[0], *t[1], *t[2], *t[3], *t[4]);
}
else static if (6 == T.length) {
return fn(*t[0], *t[1], *t[2], *t[3], *t[4], *t[5]);
}
else static if (7 == T.length) {
return fn(*t[0], *t[1], *t[2], *t[3], *t[4], *t[5], *t[6]);
}
else static if (8 == T.length) {
return fn(*t[0], *t[1], *t[2], *t[3], *t[4], *t[5], *t[6], *t[7]);
}
else static if (9 == T.length) {
return fn(*t[0], *t[1], *t[2], *t[3], *t[4], *t[5], *t[6], *t[7], *t[8]);
}
else static if (10 == T.length) {
return fn(*t[0], *t[1], *t[2], *t[3], *t[4], *t[5], *t[6], *t[7], *t[8], *t[9]);
}
}
|