Generate data from a TypeList and Unit Template

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@53 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
magmaikh 2002-10-01 03:24:27 +00:00
parent 1b82d40441
commit ef4a9b8276
2 changed files with 293 additions and 0 deletions

201
MSVC/1300/DataGenerators.h Normal file
View file

@ -0,0 +1,201 @@
////////////////////////////////////////////////////////////////////////////////
// The Loki Library
// Data Generator by Shannon Barber
// This code DOES NOT accompany the book:
// Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
// Patterns Applied". Copyright (c) 2001. Addison-Wesley.
//
// Code covered by the MIT License
////////////////////////////////////////////////////////////////////////////////
#include "TypeList.h"
#include "StreamTypes.h"
namespace Loki
{
////////////////////////////////////////////////////////////////////////////////
// class template IterateTypes
////////////////////////////////////////////////////////////////////////////////
namespace TL
{
template<typename T>
struct name_from_type
{
const char* operator()()
{
return typeid(T).name();
}
};
template <class TList, template <typename> class UnitFunc>
struct IterateTypes;
namespace Private
{
// for some reason VC7 needs the base definition altough not in use
template <typename TListTag>
struct IterateTypesHelper1
{
template <typename T, template <typename> class GenFunc>
struct In
{
typedef typename T::ERROR_THIS_INSTANCE_SELECTED Result;
};
};
template <typename TListTag>
struct IterateTypesHelper2
{
template <typename T, template <typename> class GenFunc>
struct In
{
typedef typename T::ERROR_THIS_INSTANCE_SELECTED Result;
};
};
template <>
struct IterateTypesHelper1<TL::Typelist_tag>
{
template <class TList, template <typename> class GenFunc>
struct In
{
typedef IterateTypes<typename TList::Head, GenFunc> Result;
};
};
template <>
struct IterateTypesHelper2<TL::Typelist_tag>
{
template <class TList, template <typename> class GenFunc>
struct In
{
typedef IterateTypes<typename TList::Tail, GenFunc> Result;
};
};
template <>
struct IterateTypesHelper1<TL::NoneList_tag>
{
template <typename AtomicType, template <typename> class GenFunc>
struct In
{
struct Result
{
typedef GenFunc<AtomicType> genfunc_t;
template<class II>
void operator()(II& ii)
{
genfunc_t gen;
*ii = gen();
++ii;
}
template<class II, class P1>
void operator()(II& ii, P1 p1)
{
genfunc_t gen;
*ii = gen(p1);
++ii;
}
};
};
};
template <>
struct IterateTypesHelper2<TL::NoneList_tag>
{
template <typename AtomicType, template <typename> class GenFunc>
struct In
{
struct Result
{
template<class II> void operator()(II&) {}
template<class II, class P1> void operator()(II&, P1) {}
};
};
};
template <>
struct IterateTypesHelper1<TL::NullType_tag>
{
template <class TList, template <typename> class GenFunc>
struct In
{
struct Result
{
template<class II> void operator()(II&) {}
template<class II, class P1> void operator()(II&, P1) {}
};
};
};
template <>
struct IterateTypesHelper2<TL::NullType_tag>
{
template <class TList, template <typename> class GenFunc>
struct In
{
struct Result
{
template<class II> void operator()(II&) {}
template<class II, class P1> void operator()(II&, P1) {}
};
};
};
} // namespace Private
////////////////////////////////////////////////////////////////////////////////
// class template IterateTypes
// Iteratates a TypeList, and invokes the ctor of GenFunc<T>
// for each type in the list, passing a functor along the way.
// The functor is designed to be an insertion iterator which GenFunc<T>
// can use to output information about the types in the list.
////////////////////////////////////////////////////////////////////////////////
template <typename T, template <typename> class GenFunc>
struct IterateTypes
{
public:
typedef typename Private::IterateTypesHelper1
<
typename TL::is_Typelist<T>::type_tag
>
::template In<T, GenFunc>::Result head_t;
typedef typename Private::IterateTypesHelper2
<
typename TL::is_Typelist<T>::type_tag
>
::template In<T, GenFunc>::Result tail_t;
head_t head;
tail_t tail;
template<class II>
void operator()(II& ii)
{
this->head.operator()(ii);
this->tail.operator()(ii);
}
template<class II, class P1>
void operator()(II& ii, P1 p1)
{
this->head.operator()(ii, p1);
this->tail.operator()(ii, p1);
}
};
}//ns TL
}//ns Loki
////////////////////////////////////////////////////////////////////////////////
// Change log:
// Aug 17, 2002: Ported to MSVC7 by Rani Sharoni
// Aug 18, 2002: Removed ctor(ii&), replaced with operator(ii&) Shannon Barber
////////////////////////////////////////////////////////////////////////////////

View file

@ -0,0 +1,92 @@
////////////////////////////////////////////////////////////////////////////////
// The Loki Library
// Data Generator by Shannon Barber
// This code DOES NOT accompany the book:
// Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
// Patterns Applied". Copyright (c) 2001. Addison-Wesley.
//
// Code covered by the MIT License
////////////////////////////////////////////////////////////////////////////////
#include "TypeList.h"
/************************************************************************************
// class template GenData
// Iteratates a TypeList, and invokes the functor GenFunc<T>
// for each type in the list, passing a functor along the way.
// The functor is designed to be an insertion iterator which GenFunc<T>
// can use to output information about the types in the list.
//
Example Use
template<typename T>
struct ExtractDataType
{
some_type operator()()
{
return create_value_from_type<T>;
}
};
Loki::GenData<parameter_tl, ExtractDataType> gen;
std::vector<user_defined_type> stuff;
gen(std::back_inserter(stuff));
*******************************************************************************/
namespace Loki
{
namespace TL
{
template<typename T>
struct name_from_type
{
const char* operator()()
{
return typeid(T).name();
}
};
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>
{
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);
}
};
template <class AtomicType, template <class> class GenFunc>
struct IterateTypes
{
template<class II>
void operator()(II& ii)
{
typedef GenFunc<AtomicType> genfunc_t;
*ii = genfunc_t()();
++ii;
}
};
template <template <class> class GenFunc>
struct IterateTypes<NullType, GenFunc>
{
template<class II>
void operator()(II& ii)
{}
};
}//ns TL
}//ns Loki
////////////////////////////////////////////////////////////////////////////////
// Change log:
// 9/20/02 Named changed from GenData to IterateTypes
////////////////////////////////////////////////////////////////////////////////