Template shenannigans with multiple datatypes

vit vit at vit.vit
Fri May 13 13:06:32 UTC 2022


On Friday, 13 May 2022 at 11:58:15 UTC, zjh wrote:
> On Friday, 13 May 2022 at 08:28:56 UTC, vit wrote:
>
>>...
>
>
> ```d
> ...
> this(DataSources dataSources){
>     this.dataSources = dataSources;
> }
> ...
> return new MultiGraph!(staticMap!(PointerTarget, Ts))(ts);//ts
> ```
>
> How is `ts` convert to `DataSources`?

`ts` have same type like `DataSources`:

```d

import std.meta : staticMap, allSatisfy;
import std.traits : PointerTarget, isPointer;
import std.conv : to;

alias Pointer(T) = T*;

class MultiGraph(Ts...){	// Ts == (float, double, int)
     alias DataSources = staticMap!(Pointer, Ts);	// (float, 
double, int) -> (float*, double*, int*)

     DataSources dataSources;		// -> (float*, double*, int*)
     float[][DataSources.length] buffers;	// -> float[][3]

     this(DataSources dataSources){	// dataSources == (float*, 
double*, int*)
         this.dataSources = dataSources;
     }

     void onTick() {
         //grab datasource data and do something.
         foreach(enum i, alias d; dataSources)
             buffers[i] ~= to!float(*d); //or whatever
     }
}

auto multiGraph(Ts...)(Ts ts)	// Ts == (float*, double*, int*)
if(allSatisfy!(isPointer, Ts)){	//all types in Ts are pointers
     alias DataSources = staticMap!(PointerTarget, Ts);	// 
(float*, double*, int*) -> (float, double, int)
     return new MultiGraph!(DataSources)(ts);
}


void main(){
     float myFloat;
     double myDouble;
     int myInteger;
     auto g = multiGraph(&myFloat, &myDouble, &myInteger);

}

```


More information about the Digitalmars-d-learn mailing list