One of point is to determine how to use compile-time and run-time opportunities. This topic is about one compile-time tool, which allows me to use a flexible layout of objects.
It is based on the possibility to manipulate C++ types at compile-time and use a partial specialization of templates for generating linear hierarchies of classes. This technique is used in the Loki and Boost libraries.
template < class, size_t >
struct slot;
template < size_t I >
struct slot< Loki::NullType, I >
{
///
typedef Loki::NullType
value_type;
///
value_type
value;
};
template < typename T, typename U, size_t I >
struct slot< Loki::Typelist< T, U >, I > : public slot< U, I + 1 >
{
///
typedef T
value_type;
///
value_type
value;
};
Template appointment - generate a classes linear hierarchy. Number of inherited classes equal to the number of types in the types list template parameter plus one.
Next design:
class С1 : public z3d::slot< LOKI_TYPELIST_4(int, char, float, long) >{};
will generate the following schematic nested structure:
class С1
{
{
{
{
{
{
Loki::NullType value;
}
long value;
};
float value;
};
char value;
};
int value;
};
};
Access to elements of the structure can be as an index, so by specifying the type:
С1 с1;
c1.set_value( boost::mpl::int_< 0 >(), 1 );
c1.set_value( boost::mpl::int_< 1 >(), 'A' );
std::cout << c1.get_value( boost::mpl::identity< float >() );
Class z3d::slot also provides a mechanism to initialize the values in the constructor:
С1 с1( INITLIST_1( 1, Loki::NullType(), 1.f, Loki::NullType() );
Template class z3d::scope openly inherited from z3d::slot and adds the ability to initialize the value using a class method:
class S1 : public z3d::scope< LOKI_TYPELIST_4(int, char, float, long) >
{};
S1 s1;
s1.assign( INITLIST_1( 1, Loki::NullType(), 1.f, Loki::NullType() );
z3d::scope
Комментариев нет:
Отправить комментарий