From ef4a9b8276217663d389d22bfa0227c97e18885a Mon Sep 17 00:00:00 2001 From: magmaikh Date: Tue, 1 Oct 2002 03:24:27 +0000 Subject: [PATCH] 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 --- MSVC/1300/DataGenerators.h | 201 +++++++++++++++++++++++++++++++++++++ Reference/DataGenerators.h | 92 +++++++++++++++++ 2 files changed, 293 insertions(+) create mode 100644 MSVC/1300/DataGenerators.h create mode 100644 Reference/DataGenerators.h diff --git a/MSVC/1300/DataGenerators.h b/MSVC/1300/DataGenerators.h new file mode 100644 index 0000000..384e35b --- /dev/null +++ b/MSVC/1300/DataGenerators.h @@ -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 + struct name_from_type + { + const char* operator()() + { + return typeid(T).name(); + } + }; + + template class UnitFunc> + struct IterateTypes; + + namespace Private + { + // for some reason VC7 needs the base definition altough not in use + template + struct IterateTypesHelper1 + { + template class GenFunc> + struct In + { + typedef typename T::ERROR_THIS_INSTANCE_SELECTED Result; + }; + }; + + template + struct IterateTypesHelper2 + { + template class GenFunc> + struct In + { + typedef typename T::ERROR_THIS_INSTANCE_SELECTED Result; + }; + }; + + + template <> + struct IterateTypesHelper1 + { + template class GenFunc> + struct In + { + typedef IterateTypes Result; + }; + }; + + template <> + struct IterateTypesHelper2 + { + template class GenFunc> + struct In + { + typedef IterateTypes Result; + }; + }; + + + template <> + struct IterateTypesHelper1 + { + template class GenFunc> + struct In + { + struct Result + { + typedef GenFunc genfunc_t; + template + void operator()(II& ii) + { + genfunc_t gen; + *ii = gen(); + ++ii; + } + template + void operator()(II& ii, P1 p1) + { + genfunc_t gen; + *ii = gen(p1); + ++ii; + } + }; + }; + }; + + template <> + struct IterateTypesHelper2 + { + template class GenFunc> + struct In + { + struct Result + { + template void operator()(II&) {} + template void operator()(II&, P1) {} + }; + }; + }; + + + template <> + struct IterateTypesHelper1 + { + template class GenFunc> + struct In + { + struct Result + { + template void operator()(II&) {} + template void operator()(II&, P1) {} + }; + }; + }; + + template <> + struct IterateTypesHelper2 + { + template class GenFunc> + struct In + { + struct Result + { + template void operator()(II&) {} + template void operator()(II&, P1) {} + }; + }; + }; + + } // namespace Private + + + +//////////////////////////////////////////////////////////////////////////////// +// class template IterateTypes +// Iteratates a TypeList, and invokes the ctor of GenFunc +// for each type in the list, passing a functor along the way. +// The functor is designed to be an insertion iterator which GenFunc +// can use to output information about the types in the list. +//////////////////////////////////////////////////////////////////////////////// + + + template class GenFunc> + struct IterateTypes + { + public: + + typedef typename Private::IterateTypesHelper1 + < + typename TL::is_Typelist::type_tag + > + ::template In::Result head_t; + + typedef typename Private::IterateTypesHelper2 + < + typename TL::is_Typelist::type_tag + > + ::template In::Result tail_t; + head_t head; + tail_t tail; + + template + void operator()(II& ii) + { + this->head.operator()(ii); + this->tail.operator()(ii); + } + template + 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 +//////////////////////////////////////////////////////////////////////////////// + diff --git a/Reference/DataGenerators.h b/Reference/DataGenerators.h new file mode 100644 index 0000000..02243ff --- /dev/null +++ b/Reference/DataGenerators.h @@ -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 +// for each type in the list, passing a functor along the way. +// The functor is designed to be an insertion iterator which GenFunc +// can use to output information about the types in the list. +// + +Example Use + +template +struct ExtractDataType + { + some_type operator()() + { + return create_value_from_type; + } + }; + +Loki::GenData gen; +std::vector stuff; +gen(std::back_inserter(stuff)); +*******************************************************************************/ +namespace Loki +{ + namespace TL + { + template + struct name_from_type + { + const char* operator()() + { + return typeid(T).name(); + } + }; + + template class GenFunc> + struct IterateTypes; + + template class GenFunc> + struct IterateTypes, GenFunc> + : public IterateTypes + , public IterateTypes + { + template + void operator()(II& ii) + { + typedef IterateTypes head_t; + typedef IterateTypes tail_t; + head_t::operator()(ii); + tail_t::operator()(ii); + } + }; + + template class GenFunc> + struct IterateTypes + { + template + void operator()(II& ii) + { + typedef GenFunc genfunc_t; + *ii = genfunc_t()(); + ++ii; + } + }; + + template