Loading...
Searching...
No Matches
OFvariant< Alternatives > Class Template Reference

A class template that represents a type-safe union. More...

## Public Member Functions

| OFvariant ()
| Constructs a variant holding a default constructed value of the first alternative.
| OFvariant (const OFvariant &rhs)
| Copy constructs a variant holding a copy of the value rhs holds.
| OFvariant (OFvariant &&rhs)
| Move constructs a variant by moving the value rhs holds.
template<typename T>
| OFvariant (T t)
| Constructs a variant holding the alternative that most closely matches the given argument.
| ~OFvariant ()
| Destroys the value that the variant currently holds.
OFvariant & | operator= (const OFvariant &rhs)
| Copy assigns the value rhs holds to *this.
OFvariant & | operator= (OFvariant &&rhs)
| Move assigns the value rhs holds to *this.
template<typename T>
OFvariant & | operator= (T t)
| Converts the given argument to one of the alternatives and assigns it to *this.
size_t | index () const
| Get the index of alternative that is currently being held.

## Related Definitions

Global types, methods and objects that are somehow related
template<typename Alternative, typename... Alternatives>
Alternative * | OFget (OFvariant< Alternatives... > *v)
| Try to get a pointer to the given alternative from an OFvariant object.
template<typename Alternative, typename... Alternatives>
const Alternative * | OFget (const OFvariant< Alternatives... > *v)
| Try to get a pointer to the given alternative from an OFvariant object.
template<typename Result, typename Visitor, typename... Alternatives>
Result | OFvisit (Visitor visitor, OFvariant< Alternatives... > &v)
| Applies the given visitor to the given OFvariant object.
template<typename Result, typename Visitor, typename... Alternatives>
Result | OFvisit (Visitor visitor, const OFvariant< Alternatives... > &v)
| Applies the given visitor to the given OFvariant object.

## Detailed Description

template<typename... Alternatives>
class OFvariant< Alternatives >

A class template that represents a type-safe union.

#include "dcmtk/ofstd/ofvriant.h" for using this class

Template Parameters

  Alternatives | a set of types that may be stored in this variant. All types must be (possibly cv-qualified) object types.

OFvariant is a custom implementation of a subset of C++17's std::variant, see http://en.cppreference.com/w/cpp/utility/variant for a description of std::variant. An instance of OFvariant at any given time holds a value of one of its alternative types. As with unions, if a variant holds a value of some object type T, the object representation of T is allocated directly within the object representation of the variant itself if possible.

Note
  If no suitable alignment specifiers were available for the target platform, OFvariant will use a fallback implementation that stores the alternative on the heap – as opposite to std::variant.

The preferred way to access an OFvariant object is visitation utilizing OFvisit. If a certain alternative is expected to be held by the variant, OFget may be used to access it directly.

See also
  OFvisit – Apply a visitor to an OFvariant object.
  OFget – Get a pointer to the value stored in an OFvariant holding the selected alternative.
  OFmonostate – A helper type for making OFvariant default constructible.
  OFin_place –

Tools for in-place construction of objects, e.g. certain OFvariant alternatives.

## Constructor & Destructor Documentation

## OFvariant() [1/4]

template<typename... Alternatives>

OFvariant< Alternatives >::OFvariant | ( |  | ) |

Constructs a variant holding a default constructed value of the first alternative.

Precondition
  The first alternative must be default constructible.

See also
  OFmonostate – A helper type for making OFvariant default constructible.

## OFvariant() [2/4]

template<typename... Alternatives>

OFvariant< Alternatives >::OFvariant | ( | const OFvariant< Alternatives > & | rhs | ) |

Copy constructs a variant holding a copy of the value rhs holds.

Parameters

  rhs | a const reference to another object of equal type.

Precondition
  All alternatives must be copy constructible.

## OFvariant() [3/4]

template<typename... Alternatives>

OFvariant< Alternatives >::OFvariant | ( | OFvariant< Alternatives > && | rhs | ) |

Move constructs a variant by moving the value rhs holds.

Parameters

  rhs | an rvalue reference to another object of equal type.

Precondition
  All alternatives must be move constructible.

## OFvariant() [4/4]

template<typename... Alternatives>
template<typename T>

OFvariant< Alternatives >::OFvariant | ( | T | t | ) |

Constructs a variant holding the alternative that most closely matches the given argument.

Template Parameters

  T | the type of the argument, will be deduced automatically.

Parameters

  t | an object of type T that will be converted to one of the alternatives.

Remarks
  There must be at least one alternative that can be constructed from the given parameter t and there must be exactly one such alternative that takes precedence over the others.

Attention
  t will be perfectly forwarded if C++11 support is available, i.e. the alternative may be move constructed from t if possible. Support for perfect forwarding is NOT available without C++11 support, therefore the alternative will be copy constructed in this case, this means: the selected alternative must be copy constructible if pre C++11 compilers shall be supported.

### Usage Example:

OFvariant<int,float,long>( 3 ); // OK
OFvariant<int,int>( 3 ); // ill formed, both alternatives take equal precedence
OFvariant<OFString,OFBool>( "abc" ); // OK, but chooses OFBool!
OFvariant::OFvariant
OFvariant()
Constructs a variant holding a default constructed value of the first alternative.

## Member Function Documentation

## index()

template<typename... Alternatives>

size_t OFvariant< Alternatives >::index | ( |  | ) | const

Get the index of alternative that is currently being held.

Returns
  the zero based index of that alternative that is currently being held by *this, i.e. 0 for the first alternative, 1 for the second, etc.

## operator=() [1/3]

template<typename... Alternatives>

OFvariant & OFvariant< Alternatives >::operator= | ( | const OFvariant< Alternatives > & | rhs | ) |

Copy assigns the value rhs holds to *this.

Parameters

  rhs | a const reference to another object of equal type.

Precondition
  all alternatives must be copy constructible and copy assignable.

Returns
  *this

Postcondition
  - if *this and rhs hold the same alternative, the value contained in rhs is copy assigned to the value contained in *this.
  - if *this and rhs hold different alternatives, the value contained in *this is destroyed and a new one is copy constructed from the value contained in rhs.

## operator=() [2/3]

template<typename... Alternatives>

OFvariant & OFvariant< Alternatives >::operator= | ( | OFvariant< Alternatives > && | rhs | ) |

Move assigns the value rhs holds to *this.

Parameters

  rhs | an rvalue reference to another object of equal type.

Precondition
  all alternatives must be move constructible and move assignable.

Returns
  *this

Postcondition
  - if *this and rhs hold the same alternative, the value contained in rhs is move assigned to the value contained in *this.
  - if *this and rhs hold different alternatives, the value contained in *this is destroyed and a new one is move constructed from the value contained in rhs.

## operator=() [3/3]

template<typename... Alternatives>
template<typename T>

OFvariant & OFvariant< Alternatives >::operator= | ( | T | t | ) |

Converts the given argument to one of the alternatives and assigns it to *this.

Template Parameters

  T | the type of the argument, will be deduced automatically.

Parameters

  t | an object of type T that will be converted to one of the alternatives for assignment.

Returns
  *this

Precondition
  There must be at least one alternative that can be constructed from the given parameter t and there must be exactly one such alternative that takes precedence over the others.

Attention
  t will be perfectly forwarded if C++11 support is available, i.e. the alternative may be move constructed from t if possible. Support for perfect forwarding is NOT available without C++11 support, therefore the alternative will be copy constructed in this case, this means: the selected alternative must be copy constructible if pre C++11 compilers shall be supported.

### Usage Example:

OFvariant<int,float,long> v1;
v1 = 3 // OK
OFvariant<int,int> v2;
v2 = 3 // ill formed, both alternatives take equal precedence
OFvariant<OFString,OFBool> v3;
v3 = "abc"; // OK, but chooses OFBool!

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

- ofstd/include/dcmtk/ofstd/ofvriant.h

Generated on for DCMTK Version 3.7.0 by Doxygen 1.15.0