Frank-René Schäfer wrote:
The idea was to have something that
represents a list of arguments, i.e.
[snip example pseudocode]
First of all, thanks for focusing me on this problem! I certainly
have use for the result. Note that if you end up using the code
you'll probably want to add at least non-const access. Before I
explain the code, here's the achieved initialization syntax:
typedef mpl::vector<V, int, int, TempTest> Types;
X<Types> x(tltools::Argu ments<Types>(V( 3, 2.5))(7)()(1));
The code starts out with the "library" part in namespace tltools,
containing the Arguments and Instantiator facilities. Instantiator
corresponds to your linear_generato r; I used a different name to
avoid confusion in case you have already implemented the latter.
After that those facilities' use is demonstrated.
Both the Arguments and Instantiator class templates work in the same
basic way: they build an inheritance chain of template instantiations
from a typelist, with a node for each element type, using the primary
template except for the innermost base. This is similar to what I
posted before, but this time there is no sentinel class at the end.
Each node of an Arguments chain has a constructor taking the
corresponding type in the typelist. However, this ctor is only used
to pass the first argument with the intent to smooth out the point-
of-use syntax. Any further arguments are passed through the
operator() in the primary template that consequently takes the *base*
argument type and is not present in the template specialization. A
conceptually parameterless ctor is also required of the base, but it
cannot be the default ctor as that would disallow a default parameter
to the pass-in ctor -- hence the dummy parameter.
Argument storage and retrieval is delegated to the specialization
that type-safely encapsulates a void* container. It is also safe for
temporaries because those persist until the end of a statement like
the initialization of x in the code excerpt above. Finally, the
specialization has a conversion back to the outermost type, again for
convenient use. To ensure that this conversion is only used on a
completely filled Arguments structure, the primary template uses
protected inheritance.
Each node of an Instantiator inheritance chain has a field of the
corresponding type in the typelist and lets the user retrieve fields
of subsequent types specified by typelist iterators. It also gives
access to the next node, except that a virtual sentinel follows the
last node; this simplifies defining functions that operate on the
whole structure. Finally, there is the converting constructor from
Arguments for the same typelist.
After this "library" part comes a demonstration of the Arguments and
Instantiator facilities' use, including inductive definition of
functions over the Instantiator structure. The struct TempTest is
just to confirm that the compiler gets temporary lifetime right here.
Please let me know of any improvements you can think of!
Martin
#include <iostream>
#include <numeric>
#include <vector>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/begin_end.hpp>
#include <boost/mpl/next_prior.hpp>
#include <boost/mpl/deref.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/empty.hpp>
#include <boost/mpl/distance.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/static_assert.h pp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/is_convertible. hpp>
namespace mpl = boost::mpl;
namespace tltools {
template<typena me Types>
struct last {
typedef typename mpl::prior<type name mpl::end<Types> ::type>::type
type;
};
//-------
namespace detail {
struct Dummy;
typedef const Dummy* DummyPtr;
}
template<typena me Types, typename Position = typename
mpl::begin<Type s>::type>
struct Arguments
: protected Arguments<Types , typename mpl::next<Posit ion>::type>
{
BOOST_STATIC_AS SERT(!mpl::empt y<Types>::value );
typedef typename mpl::next<Posit ion>::type NextPosition;
typedef Arguments<Types , NextPosition> Base;
typedef typename mpl::deref<Posi tion>::type Value;
using Base::get;
Arguments(const Value& value = Value())
: Base(detail::Du mmyPtr(0))
{ this->template put<Position>(v alue); }
Base& operator()(cons t typename Base::Value& value = typename
Base::Value())
{
this->template put<NextPositio n>(value);
return *this;
}
protected:
Arguments(detai l::DummyPtr) : Base(detail::Du mmyPtr(0)) {}
};
template<typena me Types>
struct Arguments<Types , typename last<Types>::ty pe> {
BOOST_STATIC_AS SERT(!mpl::empt y<Types>::value );
typedef typename last<Types>::ty pe Position;
typedef typename mpl::deref<Posi tion>::type Value;
Arguments(const Value& value = Value())
: pointers_(mpl:: size<Types>::va lue)
{ put<Position>(v alue); }
template<typena me Iter>
void put(const typename mpl::deref<Iter >::type& value)
{
pointers_[mpl::distance<
typename mpl::begin<Type s>::type, Iter>::value] = &value;
}
template<typena me Iter>
const typename mpl::deref<Iter >::type& get() const
{
return *static_cast<co nst typename mpl::deref<Iter >::type*>
(pointers_[mpl::distance<t ypename mpl::begin<Type s>::type,
Iter>::value]);
}
operator const Arguments<Types >& () const
{ return static_cast<con st Arguments<Types >&>(*this); }
protected:
Arguments(detai l::DummyPtr) : pointers_(mpl:: size<Types>::va lue)
{}
private:
std::vector<con st void*> pointers_;
};
//-------
template<typena me Types, typename Position = typename
mpl::begin<Type s>::type>
struct Instantiator : Instantiator<Ty pes, typename
mpl::next<Posit ion>::type> {
BOOST_STATIC_AS SERT(!mpl::empt y<Types>::value );
typedef typename mpl::next<Posit ion>::type NextPosition;
typedef Instantiator<Ty pes, NextPosition> Base;
typedef typename mpl::deref<Posi tion>::type Value;
Value value;
Instantiator(co nst Arguments<Types >& arguments)
: Base(arguments) , value(arguments .get<Position>( ))
{}
template<typena me Iter>
const Instantiator<Ty pes, Iter>& get(typename boost::disable_ if<
boost::is_same< Iter, Position> >::type* dummy = 0)
{ return Base::template get<Iter>(); }
template<typena me Iter>
const Instantiator& get(typename boost::enable_i f<
boost::is_same< Iter, Position> >::type* dummy = 0)
{ return *this; }
const Base& next() const { return *this; }
};
typedef detail::DummyPt r EndOfInstances;
template<typena me Types>
struct Instantiator<Ty pes, typename last<Types>::ty pe>
{
BOOST_STATIC_AS SERT(!mpl::empt y<Types>::value );
typedef typename last<Types>::ty pe Position;
typedef typename mpl::deref<Posi tion>::type Value;
Value value;
Instantiator(co nst Arguments<Types >& arguments)
: value(arguments .get<Position>( ))
{}
template<typena me Iter>
const Instantiator& get()
{
BOOST_STATIC_AS SERT((boost::is _same<Iter, Position>::valu e));
return *this;
}
EndOfInstances next() const { return 0; }
};
} // namespace tltools
//-----------------------------------------------
double sum(double value)
{ return value; }
template<typena me T>
double sum(const std::vector<T>& v)
{ return std::accumulate (v.begin(), v.end(), 0.0,
std::plus<doubl e>()); }
double sum(tltools::En dOfInstances)
{ return 0.0; }
template<typena me Types, typename Position>
double sum(const tltools::Instan tiator<Types, Position>& instances)
{ return sum(instances.v alue) + sum(instances.n ext()); }
template<typena me Types>
struct X {
tltools::Instan tiator<Types> content;
X(const tltools::Argume nts<Types>& arguments) : content(argumen ts)
{}
virtual double process()
{
typedef typename mpl::begin<Type s>::type Begin;
typedef typename mpl::end<Types> ::type End;
typedef typename mpl::next<Begin >::type Second;
BOOST_STATIC_AS SERT((!boost::i s_same<Second, End>::value));
return sum(content) - sum(content.tem plate get<Second>().v alue);
}
};
struct TempTest {
int value;
operator int() const { return value; }
TempTest(int value) : value(value) {}
TempTest(const TempTest& other) : value(other.val ue) {}
~TempTest() { value = 0; }
};
int main() {
typedef std::vector<dou ble> V;
typedef mpl::vector<V, int, int, TempTest> Types;
X<Types> x(tltools::Argu ments<Types>(V( 3, 2.5))(7)()(1));
std::cout << "Expected 8.5, got " << x.process() << ".\n";
{ char c; std::cin >> c; }
return 0;
}
/* end of code */