#include #include #include #include /** * @brief A Tuple of objects. * * A maximum of 9 objects is supported. * * Use the following construction to access the individual elements. \code Tuple my_tuple; std:string& s = Element<0>::get(my_tuple); float* p = Element<1>::get(my_tuple); // Access the third element in a generic way typedef ElementType<2, Tuple >::Type Type; Type& i = Element<2>::get(my_tuple); \endcode */ // structure for indicating the end of a tuple struct Nil {}; // Pair class template class Pair { public: /** * @brief The type of the first field. */ typedef T1 Type1; /** * @brief The type of the second field. */ typedef TT Type2; /** * @brief Constructor */ template Pair(const Type1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9); /** * @brief Constructor */ Pair(const Type1& t1, const TT& t2); /** * @brief Copy Constructor for implicit type conversion */ template Pair(const Pair& other); /** * @brief Assignment operator for implicit type conversion */ template Pair& operator=(const Pair& other); /** * @brief Get the first value */ Type1& first(); const Type1& first() const; /** * @brief Get the second value */ Type2& second(); const Type2& second() const; private: /** @brief The value of the first field. */ Type1 first_; /** @brief The value of the second field. */ Type2 second_; }; // implementation of the class Pair template template inline Pair::Pair(const Type1& first, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9) : first_(first), second_(t2,t3,t4,t5,t6,t7,t8,t9,Nil()) {} template inline Pair::Pair(const Type1& first, const TT& second) : first_(first), second_(second) {} template template inline Pair::Pair(const Pair& other) : first_(other.first_), second_(other.second_) {} template template inline Pair& Pair::operator=(const Pair& other) { first_=other.first_; second_=other.second_; return *this; } template inline T1& Pair::first() { return first_; } template inline const T1& Pair::first() const { return first_; } template inline T2& Pair::second() { return second_; } template inline const T2& Pair::second() const { return second_; } // specialization of class for Pair consisting of single element template class Pair { public: typedef T1 Type1; typedef Nil Type2; Pair(const Type1& first, const Nil&, const Nil&, const Nil&, const Nil&, const Nil&, const Nil&, const Nil&, const Nil&); Pair(const Type1& first, const Nil&); template Pair(const Pair& other); template Pair& operator=(const Pair& other); Type1& first(); const Type1& first() const; private: Type1 first_; }; // implementation of specialization of Pair consisting of a single element template inline Pair::Pair(const Type1& first, const Nil&, const Nil&, const Nil&, const Nil&, const Nil&, const Nil&, const Nil&, const Nil&) : first_(first) {} template inline Pair::Pair(const Type1& first, const Nil&) : first_(first) {} template template inline Pair::Pair(const Pair& other) : first_(other.first_) {} template template Pair& Pair::operator=(const Pair& other) { first_ = other.first_; return *this; } template inline T1& Pair::first() { return first_; } template inline const T1& Pair::first() const { return first_; } // structure converting a list of types into structure of nested Pairs template struct TupleToPairs { typedef Pair::Type > Type; }; // specialization for single type template struct TupleToPairs { typedef Pair Type; }; // the tuple class template class Tuple : public TupleToPairs::Type { public: //! Type of the first Pair defining the Tuple typedef typename TupleToPairs::Type FirstPair; Tuple(const T1& t1=T1(), const T2& t2=T2(), const T3& t3=T3(), const T4& t4=T4(), const T5& t5=T5(), const T6& t6=T6(), const T7& t7=T7(), const T8& t8=T8(), const T9& t9=T8()) : TupleToPairs::Type(t1, t2, t3, t4, t5, t6, t7, t8, t9) {} }; /** * @brief Print a pair or tuple. */ template inline std::ostream& operator<<(std::ostream& os, const Pair& pair) { os< inline std::ostream& operator<<(std::ostream& os, const Pair& pair) { os< void test(T& t) { std::cout << t << std::endl; std::cout << Length::value << std::endl; std::cout << Element<1>::get(t) << " " << Element<4>::get(t) << std::endl; } int main() { Tuple tup(0,sqrt(3.),0.5,-1,"hallo"); test(tup); }