Re: heterogenous container class
"Paul" <pchristor@yahoo.co.uk> wrote in message
news:X2fXo.74859$Zo4.50520@newsfe14.ams2...
Please find the incomplete and buggy code I mentioned in my previous post.
The intention of this class was that a container class would be created to
contain an array of bData pointers.
I'm basically posting this to show the different template design I found
necessary for an indefinite list of type parameters and compatability with
the STL.
#include <iostream>
template<class T1=int, class T2=char, class T3=double, class T4=bool>
class traits{
public:
struct Empty{};
template<class H,class T>struct LN{
typedef H head;
typedef T tail;
};
template<class t1=Empty,class t2=Empty,class t3=Empty,class t4=Empty>struct
List{
typedef LN<t1,LN<t2,LN<t3,LN<t4,Empty> > > > type;
};
typedef typename List<T1,T2,T3,T4>::type type_list;
template<class T>
struct size_of{
enum{value = sizeof(T)};
};
template<int I>struct get_type{
typedef Empty type;
};
template<>struct get_type<1>{
typedef T1 type;
};
template<>struct get_type<2>{
typedef T2 type;
};
template<>struct get_type<3>{
typedef T3 type;
};
template<>struct get_type<4>{
typedef T4 type;
};
template<class T, class U>struct is_same{
enum{value = 0};
};
template<class T>struct is_same<T,T>{
enum{value = 1};
};
template<typename T, typename L>struct is_member{
enum{value= traits::is_same<T, typename L::head>::value ||
traits::is_member<T, typename L::tail>::value};
};
template<typename T>struct is_member<T, typename traits::Empty>{
enum{value= false};
};
template<class T, class L>struct index{
enum{ value = is_member<T,L>::value + traits::index<T, typename
L::tail>::value };
};
template<class T>struct index<T, typename traits::Empty>{
enum{value = 0};
};
template<class L>struct index<typename L::head, L>{
enum{value = 1};
};
template<class L>
struct length{
enum{ value = not_empty<L::head>::value + length<typename L::tail>::value};
};
template<>
struct length<typename traits::Empty>{
enum{value=0};
};
template<typename L>struct not_empty{
enum{value = 1};
};
template<>struct not_empty<typename traits::Empty>{
enum{value = 0};
};
typedef typename List<int,char,unsigned,long>::type int_types;
};
//template<class T1>class bData;
//template<class T1,class T2>class Data;
template<class _traits=traits<> >
class bData{
public:
bData(){std::cout<<"bData()\n";}
bData(const bData& rhs){std::cout<<"bData(const bData&)\n";}
virtual ~bData(){std::cout<<"~bData()\n";}
virtual bData& operator=(const bData&
rhs){std::cout<<"bData::operator=(const bData&)\n";return *this;}
typedef typename _traits::type_list::head t1;
typedef typename _traits::type_list::tail node2;
typedef typename node2::tail node3;
typedef typename node3::tail node4;
typedef typename node2::head t2;
typedef typename node3::head t3;
typedef typename node4::head t4;
virtual bData& operator=(const t1&){std::cout<<"bData::operator=(const
t1&)\n"; return *this;}
virtual bData& operator=(const t2&){std::cout<<"bData::operator=(const
t2&)\n"; return *this;}
virtual bData& operator=(const t3&){std::cout<<"bData::operator=(const
t3&)\n"; return *this;}
virtual bData& operator=(const t4&){std::cout<<"bData::operator=(const
t4&)\n"; return *this;}
virtual operator t1()const{std::cout<<"bData::operator t1\n"; return t1();}
virtual operator t2()const{std::cout<<"bData::operator t2\n"; return t2();}
virtual operator t3()const{std::cout<<"bData::operator t3\n"; return t3();}
virtual operator t4()const{std::cout<<"bData::operator t4\n"; return t4();}
virtual int size_of(){return 0;}
};
template<class T,class _traits=traits<> >
class Data:public bData<_traits>{
public:
Data(){std::cout<<"Data()\n";}
Data(const T& d){itsData = d; std::cout<<"Data(T&)\n";}
~Data(){std::cout<<"~Data()\n";}
Data(const Data& rhs){itsData = rhs.itsData; std::cout<<"Data(cost
Data&)\n";}
Data& operator=(const T& rhs){itsData=rhs;
std::cout<<"Data::operator=(const T&)\n"; return *this;}
Data& operator=(const Data& rhs){itsData=rhs.itsData;
std::cout<<"Data::operator=(const Data&)\n"; return *this;}
Data& operator=(const bData<_traits>& rhs){itsData = rhs;
std::cout<<"Data::operator=(const bData&)\n"; return *this;}
operator T()const{std::cout<<"Data::operator T\n"; return itsData;}
int size_of(){return _traits::size_of<T>::value;}
private:
T itsData;
};