1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
 
     3  * This file is a part of LEMON, a generic C++ optimization library.
 
     5  * Copyright (C) 2003-2009
 
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
 
     9  * Permission to use, modify and distribute this software is granted
 
    10  * provided that this copyright notice appears in all copies. For
 
    11  * precise terms see the accompanying LICENSE file.
 
    13  * This software is provided "AS IS" with no warranty of any kind,
 
    14  * express or implied, and with no claim as to its suitability for any
 
    19 #ifndef LEMON_BITS_VARIANT_H
 
    20 #define LEMON_BITS_VARIANT_H
 
    22 #include <lemon/assert.h>
 
    25 // \brief Variant types
 
    29   namespace _variant_bits {
 
    31     template <int left, int right>
 
    33       static const int value = left < right ? right : left;
 
    39   // \brief Simple Variant type for two types
 
    41   // Simple Variant type for two types. The Variant type is a type-safe
 
    42   // union. C++ has strong limitations for using unions, for
 
    43   // example you cannot store a type with non-default constructor or
 
    44   // destructor in a union. This class always knowns the current
 
    45   // state of the variant and it cares for the proper construction
 
    47   template <typename _First, typename _Second>
 
    51     // \brief The \c First type.
 
    53     // \brief The \c Second type.
 
    54     typedef _Second Second;
 
    58     // This constructor initalizes to the default value of the \c First
 
    62       new(reinterpret_cast<First*>(data)) First();
 
    67     // This constructor initalizes to the given value of the \c First
 
    69     BiVariant(const First& f) {
 
    71       new(reinterpret_cast<First*>(data)) First(f);
 
    76     // This constructor initalizes to the given value of the \c
 
    78     BiVariant(const Second& s) {
 
    80       new(reinterpret_cast<Second*>(data)) Second(s);
 
    83     // \brief Copy constructor
 
    86     BiVariant(const BiVariant& bivariant) {
 
    87       flag = bivariant.flag;
 
    89         new(reinterpret_cast<First*>(data)) First(bivariant.first());
 
    91         new(reinterpret_cast<Second*>(data)) Second(bivariant.second());
 
   102     // \brief Set to the default value of the \c First type.
 
   104     // This function sets the variant to the default value of the \c
 
   106     BiVariant& setFirst() {
 
   109       new(reinterpret_cast<First*>(data)) First();
 
   113     // \brief Set to the given value of the \c First type.
 
   115     // This function sets the variant to the given value of the \c
 
   117     BiVariant& setFirst(const First& f) {
 
   120       new(reinterpret_cast<First*>(data)) First(f);
 
   124     // \brief Set to the default value of the \c Second type.
 
   126     // This function sets the variant to the default value of the \c
 
   128     BiVariant& setSecond() {
 
   131       new(reinterpret_cast<Second*>(data)) Second();
 
   135     // \brief Set to the given value of the \c Second type.
 
   137     // This function sets the variant to the given value of the \c
 
   139     BiVariant& setSecond(const Second& s) {
 
   142       new(reinterpret_cast<Second*>(data)) Second(s);
 
   146     // \brief Operator form of the \c setFirst()
 
   147     BiVariant& operator=(const First& f) {
 
   151     // \brief Operator form of the \c setSecond()
 
   152     BiVariant& operator=(const Second& s) {
 
   156     // \brief Assign operator
 
   157     BiVariant& operator=(const BiVariant& bivariant) {
 
   158       if (this == &bivariant) return *this;
 
   160       flag = bivariant.flag;
 
   162         new(reinterpret_cast<First*>(data)) First(bivariant.first());
 
   164         new(reinterpret_cast<Second*>(data)) Second(bivariant.second());
 
   169     // \brief Reference to the value
 
   171     // Reference to the value of the \c First type.
 
   172     // \pre The BiVariant should store value of \c First type.
 
   174       LEMON_DEBUG(flag, "Variant wrong state");
 
   175       return *reinterpret_cast<First*>(data);
 
   178     // \brief Const reference to the value
 
   180     // Const reference to the value of the \c First type.
 
   181     // \pre The BiVariant should store value of \c First type.
 
   182     const First& first() const {
 
   183       LEMON_DEBUG(flag, "Variant wrong state");
 
   184       return *reinterpret_cast<const First*>(data);
 
   187     // \brief Operator form of the \c first()
 
   188     operator First&() { return first(); }
 
   189     // \brief Operator form of the const \c first()
 
   190     operator const First&() const { return first(); }
 
   192     // \brief Reference to the value
 
   194     // Reference to the value of the \c Second type.
 
   195     // \pre The BiVariant should store value of \c Second type.
 
   197       LEMON_DEBUG(!flag, "Variant wrong state");
 
   198       return *reinterpret_cast<Second*>(data);
 
   201     // \brief Const reference to the value
 
   203     // Const reference to the value of the \c Second type.
 
   204     // \pre The BiVariant should store value of \c Second type.
 
   205     const Second& second() const {
 
   206       LEMON_DEBUG(!flag, "Variant wrong state");
 
   207       return *reinterpret_cast<const Second*>(data);
 
   210     // \brief Operator form of the \c second()
 
   211     operator Second&() { return second(); }
 
   212     // \brief Operator form of the const \c second()
 
   213     operator const Second&() const { return second(); }
 
   215     // \brief %True when the variant is in the first state
 
   217     // %True when the variant stores value of the \c First type.
 
   218     bool firstState() const { return flag; }
 
   220     // \brief %True when the variant is in the second state
 
   222     // %True when the variant stores value of the \c Second type.
 
   223     bool secondState() const { return !flag; }
 
   229         reinterpret_cast<First*>(data)->~First();
 
   231         reinterpret_cast<Second*>(data)->~Second();
 
   235     char data[_variant_bits::CTMax<sizeof(First), sizeof(Second)>::value];
 
   239   namespace _variant_bits {
 
   241     template <int _idx, typename _TypeMap>
 
   244       typedef typename _TypeMap::template Map<_idx>::Type Current;
 
   246       static void destroy(int index, char* place) {
 
   248           reinterpret_cast<Current*>(place)->~Current();
 
   250           Memory<_idx - 1, _TypeMap>::destroy(index, place);
 
   254       static void copy(int index, char* to, const char* from) {
 
   256           new (reinterpret_cast<Current*>(to))
 
   257             Current(reinterpret_cast<const Current*>(from));
 
   259           Memory<_idx - 1, _TypeMap>::copy(index, to, from);
 
   265     template <typename _TypeMap>
 
   266     struct Memory<-1, _TypeMap> {
 
   268       static void destroy(int, char*) {
 
   269         LEMON_DEBUG(false, "Variant wrong index.");
 
   272       static void copy(int, char*, const char*) {
 
   273         LEMON_DEBUG(false, "Variant wrong index.");
 
   277     template <int _idx, typename _TypeMap>
 
   279       static const int value =
 
   280       CTMax<sizeof(typename _TypeMap::template Map<_idx>::Type),
 
   281             Size<_idx - 1, _TypeMap>::value>::value;
 
   284     template <typename _TypeMap>
 
   285     struct Size<0, _TypeMap> {
 
   286       static const int value =
 
   287       sizeof(typename _TypeMap::template Map<0>::Type);
 
   292   // \brief Variant type
 
   294   // Simple Variant type. The Variant type is a type-safe union.
 
   295   // C++ has strong limitations for using unions, for example you
 
   296   // cannot store type with non-default constructor or destructor in
 
   297   // a union. This class always knowns the current state of the
 
   298   // variant and it cares for the proper construction and
 
   301   // \param _num The number of the types which can be stored in the
 
   303   // \param _TypeMap This class describes the types of the Variant. The
 
   304   // _TypeMap::Map<index>::Type should be a valid type for each index
 
   305   // in the range {0, 1, ..., _num - 1}. The \c VariantTypeMap is helper
 
   306   // class to define such type mappings up to 10 types.
 
   308   // And the usage of the class:
 
   310   // typedef Variant<3, VariantTypeMap<int, std::string, double> > MyVariant;
 
   313   // std::cout << var.get<0>() << std::endl;
 
   314   // var.set<1>("alpha");
 
   315   // std::cout << var.get<1>() << std::endl;
 
   317   // std::cout << var.get<2>() << std::endl;
 
   320   // The result of course:
 
   326   template <int _num, typename _TypeMap>
 
   330     static const int num = _num;
 
   332     typedef _TypeMap TypeMap;
 
   334     // \brief Constructor
 
   336     // This constructor initalizes to the default value of the \c type
 
   340       new(reinterpret_cast<typename TypeMap::template Map<0>::Type*>(data))
 
   341         typename TypeMap::template Map<0>::Type();
 
   345     // \brief Copy constructor
 
   348     Variant(const Variant& variant) {
 
   350       _variant_bits::Memory<num - 1, TypeMap>::copy(flag, data, variant.data);
 
   353     // \brief Assign operator
 
   356     Variant& operator=(const Variant& variant) {
 
   357       if (this == &variant) return *this;
 
   358       _variant_bits::Memory<num - 1, TypeMap>::
 
   361       _variant_bits::Memory<num - 1, TypeMap>::
 
   362         copy(flag, data, variant.data);
 
   370       _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
 
   373     // \brief Set to the default value of the type with \c _idx index.
 
   375     // This function sets the variant to the default value of the
 
   376     // type with \c _idx index.
 
   379       _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
 
   381       new(reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>(data))
 
   382         typename TypeMap::template Map<_idx>::Type();
 
   386     // \brief Set to the given value of the type with \c _idx index.
 
   388     // This function sets the variant to the given value of the type
 
   389     // with \c _idx index.
 
   391     Variant& set(const typename _TypeMap::template Map<_idx>::Type& init) {
 
   392       _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
 
   394       new(reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>(data))
 
   395         typename TypeMap::template Map<_idx>::Type(init);
 
   399     // \brief Gets the current value of the type with \c _idx index.
 
   401     // Gets the current value of the type with \c _idx index.
 
   403     const typename TypeMap::template Map<_idx>::Type& get() const {
 
   404       LEMON_DEBUG(_idx == flag, "Variant wrong index");
 
   405       return *reinterpret_cast<const typename TypeMap::
 
   406         template Map<_idx>::Type*>(data);
 
   409     // \brief Gets the current value of the type with \c _idx index.
 
   411     // Gets the current value of the type with \c _idx index.
 
   413     typename _TypeMap::template Map<_idx>::Type& get() {
 
   414       LEMON_DEBUG(_idx == flag, "Variant wrong index");
 
   415       return *reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>
 
   419     // \brief Returns the current state of the variant.
 
   421     // Returns the current state of the variant.
 
   428     char data[_variant_bits::Size<num - 1, TypeMap>::value];
 
   432   namespace _variant_bits {
 
   434     template <int _index, typename _List>
 
   436       typedef typename Get<_index - 1, typename _List::Next>::Type Type;
 
   439     template <typename _List>
 
   440     struct Get<0, _List> {
 
   441       typedef typename _List::Type Type;
 
   446     template <typename _Type, typename _List>
 
   452     template <int _idx, typename _T0, typename _T1, typename _T2,
 
   453               typename _T3, typename _T4, typename _T5, typename _T6,
 
   454               typename _T7, typename _T8, typename _T9>
 
   457       typedef Insert<_T9, L10> L9;
 
   458       typedef Insert<_T8, L9> L8;
 
   459       typedef Insert<_T7, L8> L7;
 
   460       typedef Insert<_T6, L7> L6;
 
   461       typedef Insert<_T5, L6> L5;
 
   462       typedef Insert<_T4, L5> L4;
 
   463       typedef Insert<_T3, L4> L3;
 
   464       typedef Insert<_T2, L3> L2;
 
   465       typedef Insert<_T1, L2> L1;
 
   466       typedef Insert<_T0, L1> L0;
 
   467       typedef typename Get<_idx, L0>::Type Type;
 
   472   // \brief Helper class for Variant
 
   474   // Helper class to define type mappings for Variant. This class
 
   475   // converts the template parameters to be mappable by integer.
 
   479     typename _T1 = void, typename _T2 = void, typename _T3 = void,
 
   480     typename _T4 = void, typename _T5 = void, typename _T6 = void,
 
   481     typename _T7 = void, typename _T8 = void, typename _T9 = void>
 
   482   struct VariantTypeMap {
 
   485       typedef typename _variant_bits::
 
   486       Mapper<_idx, _T0, _T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9>::Type