Collection to variable assigment

From time to time programmer needs to assign from collections to arbitrary variables. Quite a tedious and error prone pattern.

int arr[128];

...

int a = arr[0];
long b = arr[1];
double c = arr[2];

Templatious library allows to simply distribute collection over variables with StaticManipulator::distribute:

int arr[128];

...

int a;
long b;
double c;

// a = arr[0], b = arr[1], c = arr[2]
SM::distribute(arr,a,b,c);

Also, there's StaticManipulator::disributeR function to distribute in reverse (from variables to collection):

// arr[0] = a, arr[1] = b, arr[2] = c
SM::distributeR(arr,a,b,c);

For convenience, variables can be packed together into one variable for more convenient and reusable distribution.

auto p = SF::pack(a,b,c);

// a = arr[0], b = arr[1], c = arr[2]
SM::distribute(arr,p);

// arr[0] = a, arr[1] = b, arr[2] = c
SM::distributeR(arr,p);

Distribute function returns how many elements were distributed in case distribution ranges don't match up in size.

std::vector<int> v;

SA::add(v,7,17);

int a; long b; double c;

// ditribute function returns count of
// successfull distrubutions
// a = v[0] = 7, b = v[1] = 17, c = ?
assert( SM::distribute(v,a,b,c) == 2 );

Distribute function can distribute collections over collections also.

std::vector<int> v1(3);
std::vector<double> v2(4);

// v1[0] = 0, v1[1] = 1, v1[2] = 2
// v2[0] = 3, v2[1] = 4, v2[2] = 5, v2[3] = 6
SM::distribute(SF::seqL(7),v1,v2);

There's a special overload StaticManipulator::distributeSpecial for distributing using a special function.

int a,b,c;

// a = 0*2 = 0, b = 1*2 = 2, c = 2*2 = 4
SM::distributeSpecial([](int x,int& y) { y = 2*x; },
    SF::seqL(10),a,b,c);

Distribute Special overload also allows to stop distribution
at any point. To stop distribution return false from function.

int a,b,c;

int count = 0;
// a = 0, b = 1, c = ?
SM::distributeSpecial([&](int x,int& y) {
        y = x;
        ++count;
        return count < 2;
    },
    SF::seqL(10),a,b,c);
// count = 2

Variables don't have to be assigned in a row. Arbitrary amount of assigments can be skipped using dummy variable function. Dummy variable overloads = operator for any class which does nothing, so, essentially it just skips iterators:

int a,b;

// skips one variable
// a = 0, b = 2
SM::distribute(SF::seqL(10),
    a,SF::dummyVar(),b);

// skips seven variables
// a = 0, b = 8
SM::distribute(SF::seqL(10),
    a,SF::dummyVar<7>(),b);