tuples.cpp

This example shows how to declare and use tuples.

#include "acedia/tuple.hpp"

#include <iostream>
#include <boost/cstdint.hpp>

using std::cout;
using std::endl;

typedef boost::int32_t i32;

// declare a tuple with 3 integer fields named val1, val2 and val3
ACEDIA_DECLARE_CASE_TUPLE3((Tuple3i), val1, i32, val2, i32, val3, i32)

// declare a tuple with 2 string fields named hello and world
// note: you should always use acedia::String or another implicit shared
//       string class like QString in tuples to avoid deep copys.
ACEDIA_DECLARE_CASE_TUPLE2((StrTuple), hello, acedia::String, world, acedia::String)

void echoTuple(const acedia::Tuple &t, bool verboseOutput)
{
    // print all elements of the tuple
    for (boost::uint32_t i = 0; i < t.length(); ++i)
    {
        cout << t.at(i).toString(verboseOutput).const_str();
        if (i < t.length() - 1) cout << ", ";
    }
    cout << endl;
    // use the toString method of Any
    acedia::Any a = t;
    cout << a.toString(verboseOutput).const_str() << endl;
}

int main(int, char**)
{
    Tuple3i tuple1(1,2,3);

    // access elements through get()
    // -> output: 1, 2, 3
    cout << tuple1.get<0>() << ", " << tuple1.get<1>() << ", "
         << tuple1.get<2>() << endl;

    // access elements through element names
    // -> output: 1, 2, 3
    cout << tuple1.val1() << ", " << tuple1.val2() << ", "
         << tuple1.val3() << endl;

    // use at() and toString()
    // -> output (line 1): 1, 2, 3
    //           (line 2): Tuple3i(val1 = 1, val2 = 2, val3 = 3)
    echoTuple(tuple1, false);

    // verbose
    // -> output (line 1): boost::int32_t(1), boost::int32_t(2), boost::int32_t(3)
    //           (line 2): Tuple3i(val1 = boost::int32_t(1), val2 = boost::int32_t(2), val3 = boost::int32_t(3))
    echoTuple(tuple1, true);

    StrTuple tuple2("hello", "world");

    // use at() and toString()
    // -> output (line 1): "hello", "world"
    //           (line 2): StrTuple(hello = "hello", world = "world")
    // note: echoTuple(tuple2, true) would print the same result
    echoTuple(tuple2, false);

    StrTuple tuple3("hello", "world");

    // output: true
    cout << (tuple2 == tuple3 ? "true" : "false") << endl;

    // assign a new created tuple to tuple3
    tuple3 = StrTuple("Hello", "world");

    // output: false
    cout << (tuple2 == tuple3 ? "true" : "false") << endl;

    // initializes all fields with the default constructor
    StrTuple tuple4;

    // -> output (line 1): "", ""
    //           (line 2): StrTuple(hello = "", world = "")
    echoTuple(tuple4, false);

    // initializes all fields with the default constructor
    Tuple3i tuple5;

    // -> output (line 1): 0, 0, 0
    //           (line 2): Tuple3i(val1 = 0, val2 = 0, val3 = 0)
    echoTuple(tuple5, false);

    // -> output (line 1): boost::int32_t(0), boost::int32_t(0), boost::int32_t(0)
    //           (line 2): Tuple3i(val1 = boost::int32_t(0), val2 = boost::int32_t(0), val3 = boost::int32_t(0))
    echoTuple(tuple5, true);

    return 0;
}

Generated by  doxygen 1.6.2