This is the documentation of the Chooser API for C++. The major references for Chooser objects are the
choose(k) is a short form for
int A[] = {0,1,2,...,k-1};
choose(A, k);
The array expression
T A[] = {t1, t2, t3,...,tk};
choose(A, k);
will be translated into
vector<T> V;
for(int i=0; i<k; i++)
V.push_back(A[i]);
choose(V);
This is the most general form of choose().
The template parameter is a single-parametric STL-container, for example set<string> or vector<int>. The type parameter of the container becomes the return type. It will be determined by the compiler.
Mixin class used to provide a Chooser valued member variable to classes which derive from ChooserMixin
check is implemented as an unbound function. Check accepts functions as parameters having the following shape
void funcname(Chooser& chooser)
{
...
}
as well as “Functor” objects i.e. objects which overload the function call operator ()
class Foo {
...
public:
void operator()(Chooser& chooser);
...
}
The following example demonstrates how Chooser objects are used in practice
void test(Chooser& chooser)
{
int x = chooser.choose(2);
string y;
if(x)
{
string w[] = {"a", "b", "c"};
y = chooser.choose(w, 3);
}
else
{
set<string> w;
w.insert("x");
w.insert("y");
w.insert("z");
y = chooser.choose(w);
}
printf("x = %d, y = %s\n", x, y.c_str());
}
When we aooly check(test) the following result will be yielded
x = 0, y = x
x = 0, y = y
x = 0, y = z
x = 1, y = a
x = 1, y = b
x = 1, y = c