no message

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@56 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
magmaikh 2002-10-06 06:25:22 +00:00
parent ef33406cb3
commit 41a466c1b2
3 changed files with 119 additions and 18 deletions

View file

@ -9,8 +9,8 @@
////////////////////////////////////////////////////////////////////////////////
#include "TypeList.h"
#include "StreamTypes.h"
//MSVC7 version
namespace Loki
{
////////////////////////////////////////////////////////////////////////////////
@ -26,7 +26,14 @@ namespace Loki
return typeid(T).name();
}
};
template<typename T>
struct sizeof_type
{
size_t operator()()
{
return sizeof(T);
}
};
template <class TList, template <typename> class UnitFunc>
struct IterateTypes;
@ -190,6 +197,17 @@ namespace Loki
this->tail.operator()(ii, p1);
}
};
//UnitFunc is really a template-template parameter, but MSVC7
// chokes on the correct defintion. Oddly enough, it works correctly
// with the 'undecorated' template parameter declaraion!
//template <typename> class UnitFunc
template<typename Types, class UnitFunc, typename II>
void iterate_types(II &ii)
{
Loki::TL::IterateTypes<Types, UnitFunc> it;
it(ii);
}
}//ns TL
}//ns Loki

View file

@ -10,6 +10,8 @@
#include "TypeList.h"
//Reference version
/************************************************************************************
// class template GenData
// Iteratates a TypeList, and invokes the functor GenFunc<T>
@ -29,9 +31,9 @@ struct ExtractDataType
}
};
Loki::GenData<parameter_tl, ExtractDataType> gen;
std::vector<user_defined_type> stuff;
gen(std::back_inserter(stuff));
Loki::IterateTypes<parameter_tl, ExtractDataType> gendata;
std::vector<some_type> stuff;
gendata(std::back_inserter(stuff));
*******************************************************************************/
namespace Loki
{
@ -45,22 +47,29 @@ namespace Loki
return typeid(T).name();
}
};
template<typename T>
struct sizeof_type
{
size_t operator()()
{
return sizeof(T);
}
};
template <class TList, template <class> class GenFunc>
struct IterateTypes;
template <class T1, class T2, template <class> class GenFunc>
struct IterateTypes<Typelist<T1, T2>, GenFunc>
: public IterateTypes<T1, GenFunc>
, public IterateTypes<T2, GenFunc>
{
typedef IterateTypes<T1, GenFunc> head_t;
head_t head;
typedef IterateTypes<T2, GenFunc> tail_t;
tail_t tail;
template<class II>
void operator()(II& ii)
{
typedef IterateTypes<T1, GenFunc> head_t;
typedef IterateTypes<T2, GenFunc> tail_t;
head_t::operator()(ii);
tail_t::operator()(ii);
head.operator()(ii);
tail.operator()(ii);
}
};
@ -70,8 +79,8 @@ namespace Loki
template<class II>
void operator()(II& ii)
{
typedef GenFunc<AtomicType> genfunc_t;
*ii = genfunc_t()();
GenFunc<AtomicType> genfunc;
*ii = genfunc();
++ii;
}
};
@ -83,6 +92,13 @@ namespace Loki
void operator()(II& ii)
{}
};
template<typename Types, template <typename> class UnitFunc, typename II>
void iterate_types(II &ii)
{
Loki::TL::IterateTypes<Types, UnitFunc> it;
it(ii);
}
}//ns TL
}//ns Loki

View file

@ -0,0 +1,67 @@
//DataGeneratorsTest.h
#ifndef DATAGENERATORSTEST_H
#define DATAGENERATORSTEST_H
#include <typeinfo>
#include <Loki/DataGenerators.h>
#include "UnitTest.h"
template<typename T>
T& remove_const(const T& t)
{
return const_cast<T&>(t);
}
struct DataGeneratorsTest : public Test
{
DataGeneratorsTest() : Test("DataGeneratorsTest.h")
{}
virtual void execute(TestResult& result)
{
this->printName(result);
bool b;
typedef Loki::TL::MakeTypelist<>::Result null_tl;
typedef Loki::TL::MakeTypelist<char,
unsigned char,
signed char,
wchar_t>::Result char_types;
int n = Loki::TL::Length<char_types>::value;
std::vector<const char*> names;
names.reserve(n);
Loki::TL::IterateTypes<char_types, Loki::TL::name_from_type> makenames;
//Some gcc fascist decided to make all temporaries /const/!
makenames(remove_const(std::back_inserter(names)));
b = names.size() == n;
testAssert("iterate_types - Check Length", b, result);
std::vector<size_t> sizes;
sizes.reserve(n);
typedef Loki::TL::MakeTypelist<char,
short,
int,
double>::Result some_types;
Loki::TL::iterate_types<some_types, Loki::TL::sizeof_type>(remove_const(std::back_inserter(sizes)));
size_t apriori_size[] = {sizeof(char), sizeof(short), sizeof(int), sizeof(double)};
b = true;
for(int i=0; i<n; ++i)
b &= sizes[i] == apriori_size[i];
testAssert("iterate_types - Check Elements", b, result);
sizes.resize(0);
Loki::TL::iterate_types<null_tl, Loki::TL::sizeof_type>(sizes);
b = sizes.size() == 0;
testAssert("iterate_types - Degenerate Case 1 - Null List", b, result);
sizes.resize(0);
Loki::TL::iterate_types<Loki::NullType, Loki::TL::sizeof_type>(sizes);
b = sizes.size() == 0;
testAssert("iterate_types - Degenerate Case 2 - NullType", b, result);
}
};
#endif //DATAGENERATORSTEST_H