Tuples and Objects

#include "tuple.tcc"

Tuples

Tuples can be used to group objects of different types. A Tuple is recursively defined as being either:

  • Empty.
  • A pair (head, tail), where head is of an arbitrary type and tail is an other Tuple.

Initialisation of a Tuple can be done with a brace-initializer-list as follows.

Tuple<int, char> t = {10, 'c'};

Element retrieval and assignment is described below in the Helper functions section.

Note that a Tuple, like any higher order data structure, should be passed by reference.

Class definitions

template <class… Membs>
struct

Empty Tuple.

template <class H, class… Tail>
struct class Tuple<H, Tail...>

Non-empty Tuple.

Public Members

H Tuple::head

First element.

Tuple<Tail...> Tuple::tail

Remaining elements.

Objects

An Object is functionally equivalent to a Tuple, except that its internal structure is preserved after serialisation. More on serialisation of Objects can be found in the Method discovery section.

Initialisation of an Object can be done with a brace-initializer-list as follows.

Object<int, char> o(10, 'c');

Element retrieval and assignment is described below in the Helper functions section.

Note that an Object, like any higher order data structure, should be passed by reference.

Class definitions

template <class… Membs>
struct

Object.

Inherits from Tuple< Membs… >

Helper functions

Elements of a Tuple or Object can be retrieved in two ways, either via the head and tail member variables, or using with the get<>() helper function.

int i = t.head;
char c = t.tail.head;

int j = get<0>(t);
char d = get<1>(t)';

Likewise, assignment of an element can be done via its member variables or with the get<>() helper function.

t.head = 11;
t.tail.head = 'd';

get<0>(t) = 11;
get<1>(t) = 'd';

There are additional helper functions available for the creation of Tuples.

The function pack() can be used to create a temporary Tuple to be used in a function call.

function(pack('a', 'b', 10));

The castStruct() function can be used to convert a C struct to a Tuple.

struct S {
  int i;
  char c;
};

S s;
function(castStruct<int, char>(s));

Functions

template <size_t k, class… Membs>
get(Tuple<Membs...> &t)

Get the k-th element of a Tuple or Object.

This can be used for both retrieval as well as assignment.

Return
Reference to the k-th element in t.
Parameters

template <class… Args>
Tuple<Args...> pack(Args... args)

Make a Tuple from a parameter pack.

Return
Tuple containing args.
Parameters
  • args -

    Values to store in a Tuple.

template <class… Membs, class T>
Tuple<Membs...> castStruct(T &s)

Cast a struct to a Tuple.

Return
Tuple representation of s.
Parameters
  • s -

    Struct.