Table Of Contents

This Page

C++ Chooser API

Introduction

This is the documentation of the Chooser API for C++. The major references for Chooser objects are the

  • eXplode paper where Choosers originated.
  • the article How to do model checking of Python code by Mark Seaborn who gave a simple implementation and whose blog article introduced me to the method.
  • finally there is my own blog article Choosers in Python and C++ which contains yet another introduction along with an even more simplified Python algorithm. It covers also some aspects of interest for C++ programmers.

Download

chooser.h

Classes

class Chooser
int choose(int choices)

choose(k) is a short form for

int A[] = {0,1,2,...,k-1};
choose(A, k);
template<typename T> T choose(T choices[], int cnt)

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);
template <typename Container> typename Container::value_type choose(Container& C)

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.

class ChooserMixin
Mixin class used to provide a Chooser valued member variable to classes which derive from ChooserMixin
void check()
Function being analog to the unbound check function. It takes no argument but calls the test method instead which is responsible for invoking an arbitrary method that can access the Chooser member of the mixin.
void test() = 0
Abstract test method which has to be overridden in classes which inherit from ChooserMixin.

Functions

template <typename Functor> void check(Functor& func)

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);
   ...
}

Usage

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