libpsynth 0.2.1
Classes | Public Types | Public Member Functions | Static Public Member Functions | Friends
psynth::sound::variant< Types > Class Template Reference

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

#include <variant.hpp>

Collaboration diagram for psynth::sound::variant< Types >:
Collaboration graph
[legend]

List of all members.

Classes

struct  base_t

Public Types

typedef Types types_t

Public Member Functions

 variant ()
virtual ~variant ()
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 & do_dynamic_cast () const
template<typename T >
T & do_dynamic_cast ()
template<typename T >
bool current_type_is () const
base_t bits () const
std::size_t index () const

Static Public Member Functions

template<typename T >
static bool has_type ()

Friends

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.

Detailed Description

template<typename Types>
class psynth::sound::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


Member Typedef Documentation

template<typename Types>
typedef Types psynth::sound::variant< Types >::types_t

Constructor & Destructor Documentation

template<typename Types>
psynth::sound::variant< Types >::variant ( ) [inline]
template<typename Types>
virtual psynth::sound::variant< Types >::~variant ( ) [inline, virtual]
template<typename Types>
template<typename T >
psynth::sound::variant< Types >::variant ( const T &  obj) [inline, explicit]
template<typename Types >
template<typename T >
psynth::sound::variant< Types >::variant ( T &  obj,
bool  do_swap 
) [explicit]
template<typename Types>
psynth::sound::variant< Types >::variant ( const variant< Types > &  v) [inline]

Member Function Documentation

template<typename Types>
base_t psynth::sound::variant< Types >::bits ( ) const [inline]
template<typename Types>
template<typename T >
bool psynth::sound::variant< Types >::current_type_is ( ) const [inline]
template<typename Types>
template<typename T >
const T& psynth::sound::variant< Types >::do_dynamic_cast ( ) const [inline]
template<typename Types>
template<typename T >
T& psynth::sound::variant< Types >::do_dynamic_cast ( ) [inline]
template<typename Types>
template<typename T >
static bool psynth::sound::variant< Types >::has_type ( ) [inline, static]
template<typename Types>
std::size_t psynth::sound::variant< Types >::index ( ) const [inline]
template<typename Types>
template<typename T >
void psynth::sound::variant< Types >::move_in ( T &  obj) [inline]
template<typename Types>
variant& psynth::sound::variant< Types >::operator= ( const variant< Types > &  v) [inline]
template<typename Types>
template<typename T >
variant& psynth::sound::variant< Types >::operator= ( const T &  obj) [inline]

Friends And Related Function Documentation

template<typename Types>
template<typename Types2 , typename UnaryOp >
UnaryOp::result_type apply_operation ( variant< Types2 > &  var,
UnaryOp  op 
) [friend]
template<typename Types>
template<typename Types2 , typename UnaryOp >
UnaryOp::result_type apply_operation ( const variant< Types2 > &  var,
UnaryOp  op 
) [friend]
template<typename Types>
template<typename Types1 , typename Types2 , typename BinaryOp >
BinaryOp::result_type apply_operation ( const variant< Types1 > &  arg1,
const variant< Types2 > &  arg2,
BinaryOp  op 
) [friend]

Invokes a generic constant operation (represented as a binary function object) on two variants.

template<typename Types>
template<typename TS >
bool operator!= ( const variant< TS > &  x,
const variant< TS > &  y 
) [friend]
template<typename Types>
template<typename TS >
bool operator== ( const variant< TS > &  x,
const variant< TS > &  y 
) [friend]
template<typename Types>
template<typename Cs >
void swap ( variant< Cs > &  x,
variant< Cs > &  y 
) [friend]

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