variant Class Template Reference

#include <variant.hpp>

List of all members.

Detailed Description

template<typename Types>
class boost::gil::variant< Types >

Represents a concrete instance of a run-time specified type from a set of types

A concept is typically modeled by a collection of different types. They may be instantiations of a templated type with different template parameters or even completely unrelated types.

We call the type with which the concept is instantiated in a given place in the code "the concrete type". The concrete type must be chosen at compile time, which sometimes is a severe limitation. Consider, for example, having an image concept modeled by an image class templated over the color space. It would be difficult to write a function that reads an image from file preserving its native color space, since the type of the return value is only available at run time. It would be difficult to store images of different color spaces in the same container or apply operations on them uniformly.

The variant class addresses this deficiency. It allows for run-time instantiation of a class from a given set of allowed classes specified at compile time. For example, the set of allowed classes may include 8-bit and 16-bit RGB and CMYK images. Such a variant can be constructed with rgb8_image_t and then assigned a cmyk16_image_t.

The variant has a templated constructor, which allows us to construct it with any concrete type instantiation. It can also perform a generic operation on the concrete type via a call to apply_operation. The operation must be provided as a function object whose application operator has a single parameter which can be instantiated with any of the allowed types of the variant.

variant breaks down the instantiated type into a non-templated underlying base type and a unique instantiation type identifier. In the most common implementation the concrete instantiation in stored 'in-place' - in 'bits_t'. bits_t contains sufficient space to fit the largest of the instantiated objects.

GIL's variant is similar to boost::variant in spirit (hence we borrow the name from there) but it differs in several ways from the current boost implementation. Most notably, it does not take a variable number of template parameters but a single parameter defining the type enumeration. As such it can be used more effectively in generic code.

The Types parameter specifies the set of allowable types. It models MPL Random Access Container

Public Types

typedef Types types_t

Public Member Functions

template<typename T>
 variant (const T &obj)
template<typename T>
 variant (T &obj, bool do_swap)
template<typename T>
variantoperator= (const T &obj)
variantoperator= (const variant &v)
 variant (const variant &v)
template<typename T>
void move_in (T &obj)
template<typename T>
const T & _dynamic_cast () const
template<typename T>
T & _dynamic_cast ()
template<typename T>
bool current_type_is () const

Static Public Member Functions

template<typename T>
static bool has_type ()


template<typename TS>
bool operator== (const variant< TS > &x, const variant< TS > &y)
template<typename TS>
bool operator!= (const variant< TS > &x, const variant< TS > &y)
template<typename Cs>
void swap (variant< Cs > &x, variant< Cs > &y)
template<typename Types2, typename UnaryOp>
UnaryOp::result_type apply_operation (variant< Types2 > &var, UnaryOp op)
template<typename Types2, typename UnaryOp>
UnaryOp::result_type apply_operation (const variant< Types2 > &var, UnaryOp op)
template<typename Types1, typename Types2, typename BinaryOp>
BinaryOp::result_type apply_operation (const variant< Types1 > &arg1, const variant< Types2 > &arg2, BinaryOp op)
 Invokes a generic constant operation (represented as a binary function object) on two variants.

The documentation for this class was generated from the following file:

Generated on Sat May 2 13:50:18 2009 for Generic Image Library by  doxygen 1.5.6