Tuple< T > Class Template Reference

#include <tuple.h>

Inheritance diagram for Tuple< T >:

Detailed Description

template<typename... T>
class maxon::Tuple< T >

Tuple provides in-place static storage for elements of arbitrary types. It is similar to a pair, but supports a variable number of elements. The memory layout is identical to a struct with the same member types:

Tuple<T1, T2, T3> t;
// ... same size and layout as ...
struct MyType { T1 a; T2 b; T3 c; };
MyType t;

Tuples allow to create generic classes which store a variable number of member-like types that can be initialized from a parameter pack. Example:

template <typename ... Ts> class MyGenericClass
{
Tuple<Ts ...> storedArgs;
// Similar to T1 m1; T2 m2; ... TN mN
MyGenericClass(Ts ... args) : storedArgs(args) { }
// Similar to m1(a1), m2(a2), ... mN(aN)
};

Tuples can also reduce the amount of boilerplate code when storing struct-like entries in containers like BaseArray, because they already come with the full range of constructors, assignment operators and comparison operators defined.

Another use case is simplifying multiple return values from a function by wrapping them in a tuple and using Tie to map them to variables:

A a; B b; C c;
Tie(a, b, c) = FuncThatReturnsTupleOfABC();

Since tuple elements are unnamed and can only be accessed by index or unique type, when using them in interfaces you should always consider whether using a struct with named elements would make things more obvious.

Template Parameters
TElement types.

Public Member Functions

 Tuple ()=default
 
template<typename... U, typename = typename std::enable_if<And<std::is_constructible<T, const U&>::value...>::value>::type>
 Tuple (const Tuple< U... > &src)
 
template<typename... U, typename = typename std::enable_if<And<std::is_constructible<T, U&&>::value...>::value>::type>
 Tuple (Tuple< U... > &&src)
 
MAXON_IMPLICIT Tuple (const typename std::conditional< COPYABLE, T, volatile DummyParamType >::type &... x)
 
template<typename... U, typename = typename std::enable_if<And<std::is_constructible<T, U&&>::value...>::value>::type>
MAXON_IMPLICIT Tuple (U &&... x)
 
template<typename... U>
Tupleoperator= (const Tuple< U... > &src)
 
template<typename... U>
Tupleoperator= (Tuple< U... > &&src)
 
Result< void > CopyFrom (const typename std::conditional< COPYABLE, DummyParamType, Tuple >::type &src)
 
SFINAEHelper< String, T... >::type ToString (const FormatStatement *format) const
 
UInt GetHashCode () const
 
template<Int I>
std::add_const< typename Pack::template At< I >::type >::type & Get () const
 
template<Int I>
Pack::template At< I >::type & Get ()
 
template<typename ELEMENTTYPE >
auto Get () const -> const ELEMENTTYPE &
 
template<typename ELEMENTTYPE >
auto Get () -> ELEMENTTYPE &
 
Bool operator== (const Tuple &other) const
 
Bool operator< (const Tuple &other) const
 
 MAXON_OPERATOR_COMPARISON (Tuple)
 
- Public Member Functions inherited from TupleStorage< T... >
 TupleStorage ()=default
 
 TupleStorage (std::index_sequence< I... > *, ARGS &&... args)
 
 MAXON_DECLARE_CONDITIONAL_COPY_CONSTRUCTOR (TupleStorage, COPY_CONSTRUCTIBLE)
 
 MAXON_DECLARE_CONDITIONAL_COPY_ASSIGNMENT (TupleStorage, COPY_ASSIGNABLE)
 
 MAXON_DECLARE_CONDITIONAL_MOVE_CONSTRUCTOR (TupleStorage, MOVE_CONSTRUCTIBLE)
 
 MAXON_DECLARE_CONDITIONAL_MOVE_ASSIGNMENT (TupleStorage, MOVE_ASSIGNABLE)
 
- Public Member Functions inherited from TupleStorageBase< And< std::is_default_constructible< T >::value... >::value, And< std::is_trivially_destructible< T >::value... >::value, T... >
 TupleStorageBase ()=delete
 
 ~TupleStorageBase ()
 

Private Types

using Super = maxon::details::TupleStorage< T... >
 
using IndexSequence = std::index_sequence_for< T... >
 
using Pack = ParameterPack< T... >
 

Private Member Functions

template<typename... U, size_t... I>
 Tuple (std::index_sequence< I... > *seq, const Tuple< U... > &src)
 
template<typename... U, size_t... I>
 Tuple (std::index_sequence< I... > *seq, Tuple< U... > &&src)
 
template<typename... U, size_t... I>
void CopyAssign (std::index_sequence< I... > *, const Tuple< U... > &src)
 
template<typename... U, size_t... I>
void MoveAssign (std::index_sequence< I... > *, Tuple< U... > &&src)
 
template<size_t... I>
Result< void > CopyFromImpl (std::index_sequence< I... > *, const Tuple &src)
 
template<size_t... I>
SFINAEHelper< String, T... >::type ToStringImpl (std::index_sequence< I... > *, const FormatStatement *format) const
 
template<size_t... I>
UInt GetHashCodeImpl (std::index_sequence< I... > *) const
 
template<size_t... I>
Bool EqualsImpl (std::index_sequence< I... > *, const Tuple &other) const
 
template<size_t... I>
Bool LessThanImpl (std::index_sequence< I... > *, const Tuple &other) const
 

Static Private Attributes

static const Bool COPYABLE
 

Additional Inherited Members

- Public Types inherited from TupleStorageBase< And< std::is_default_constructible< T >::value... >::value, And< std::is_trivially_destructible< T >::value... >::value, T... >
using Pack = ParameterPack< T... >
 
- Protected Member Functions inherited from TupleStorageBase< And< std::is_default_constructible< T >::value... >::value, And< std::is_trivially_destructible< T >::value... >::value, T... >
 TupleStorageBase (ENUM_DONT_INITIALIZE)
 

Member Typedef Documentation

◆ Super

using Super = maxon::details::TupleStorage<T...>
private

◆ IndexSequence

using IndexSequence = std::index_sequence_for<T...>
private

◆ Pack

using Pack = ParameterPack<T...>
private

Constructor & Destructor Documentation

◆ Tuple() [1/7]

Tuple ( )
default

◆ Tuple() [2/7]

Tuple ( const Tuple< U... > &  src)

Converting copy constructor.

Constructs Tuple<T...> from Tuple<U...> by doing an element-wise copy. Participates in overload resolution only if Ti is constructible from const Ui&.

◆ Tuple() [3/7]

Tuple ( Tuple< U... > &&  src)

Converting move constructor.

Constructs Tuple<T...> from Tuple<U...> by doing an element-wise forward. Participates in overload resolution only if Ti is constructible from Ui&&. This also covers the default move constructor.

◆ Tuple() [4/7]

MAXON_IMPLICIT Tuple ( const typename std::conditional< COPYABLE, T, volatile DummyParamType >::type &...  x)

Copy-from-value constructor.

Initializes elements with values.

Parameters
[in]xValues.

◆ Tuple() [5/7]

MAXON_IMPLICIT Tuple ( U &&...  x)

Forward-and-convert-from-value constructor.

Initializes elements with forwarded values. Participates in overload resolution only if Ti is constructible from Ui&&.

Parameters
[in]xValues.

◆ Tuple() [6/7]

Tuple ( std::index_sequence< I... > *  seq,
const Tuple< U... > &  src 
)
private

◆ Tuple() [7/7]

Tuple ( std::index_sequence< I... > *  seq,
Tuple< U... > &&  src 
)
private

Member Function Documentation

◆ operator=() [1/2]

Tuple& operator= ( const Tuple< U... > &  src)

Converting copy assignment. Does an element-wise copy assignment.

◆ operator=() [2/2]

Tuple& operator= ( Tuple< U... > &&  src)

Converting move assignment. Does an element-wise perfect-forwarded assignment.

◆ CopyFrom()

Result<void> CopyFrom ( const typename std::conditional< COPYABLE, DummyParamType, Tuple< T > >::type &  src)

Copies one tuple to another with an element-wise AssignCopy. Use this instead of regular assignment if copying an element may result in an error and is thus not supported with a regular copy assignment.

◆ ToString()

SFINAEHelper<String, T...>::type ToString ( const FormatStatement format) const

Returns a formated string representation for this tuple.

◆ GetHashCode()

UInt GetHashCode ( ) const

Returns the hash code generated from the elements of this tuple.

◆ Get() [1/4]

std::add_const<typename Pack::template At<I>::type>::type& Get ( void  ) const

Returns the element at index I. Equivalent to TupleGet<I>(*this).

◆ Get() [2/4]

Pack::template At<I>::type& Get ( void  )

◆ Get() [3/4]

auto Get ( void  ) const -> const ELEMENTTYPE&

Returns element with type ELEMENTTYPE. Equivalent to TupleGet<ELEMENTTYPE>(*this).

◆ Get() [4/4]

auto Get ( void  ) -> ELEMENTTYPE&

◆ operator==()

Bool operator== ( const Tuple< T > &  other) const

Element-wise comparison for equality.

Returns true, if myElem[i] == src.elem[i] for all i = 0 .. N.

◆ operator<()

Bool operator< ( const Tuple< T > &  other) const

Element-wise less-than from left to right.

Returns true, if myElem[i] < src.elem[i]. Returns false, if myElem[i] > src.elem[i]. Otherwise, if there exist elements at i+1, they are compared by the same rules. Otherwise, returns false.

◆ MAXON_OPERATOR_COMPARISON()

MAXON_OPERATOR_COMPARISON ( Tuple< T >  )

◆ CopyAssign()

void CopyAssign ( std::index_sequence< I... > *  ,
const Tuple< U... > &  src 
)
private

◆ MoveAssign()

void MoveAssign ( std::index_sequence< I... > *  ,
Tuple< U... > &&  src 
)
private

◆ CopyFromImpl()

Result<void> CopyFromImpl ( std::index_sequence< I... > *  ,
const Tuple< T > &  src 
)
private

◆ ToStringImpl()

SFINAEHelper<String, T...>::type ToStringImpl ( std::index_sequence< I... > *  ,
const FormatStatement format 
) const
private

◆ GetHashCodeImpl()

UInt GetHashCodeImpl ( std::index_sequence< I... > *  ) const
private

◆ EqualsImpl()

Bool EqualsImpl ( std::index_sequence< I... > *  ,
const Tuple< T > &  other 
) const
private

◆ LessThanImpl()

Bool LessThanImpl ( std::index_sequence< I... > *  ,
const Tuple< T > &  other 
) const
private

Member Data Documentation

◆ COPYABLE

const Bool COPYABLE
staticprivate