3  * This file is a part of LEMON, a generic C++ optimization library
 
     5  * Copyright (C) 2003-2008
 
     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/error.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
 
    42   /// safe union. The C++ has strong limitations for using unions, by
 
    43   /// example we can not store type with non default constructor or
 
    44   /// destructor in an 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;
 
    56     struct WrongStateError : public lemon::LogicError {
 
    58       virtual const char* what() const throw() {
 
    59         return "lemon::BiVariant::WrongStateError";
 
    63     /// \brief Constructor
 
    65     /// This constructor initalizes to the default value of the \c First
 
    69       new(reinterpret_cast<First*>(data)) First();
 
    72     /// \brief Constructor
 
    74     /// This constructor initalizes to the given value of the \c First
 
    76     BiVariant(const First& f) {
 
    78       new(reinterpret_cast<First*>(data)) First(f);
 
    81     /// \brief Constructor
 
    83     /// This constructor initalizes to the given value of the \c
 
    85     BiVariant(const Second& s) {
 
    87       new(reinterpret_cast<Second*>(data)) Second(s);
 
    90     /// \brief Copy constructor
 
    93     BiVariant(const BiVariant& bivariant) {
 
    94       flag = bivariant.flag;
 
    96         new(reinterpret_cast<First*>(data)) First(bivariant.first());      
 
    98         new(reinterpret_cast<Second*>(data)) Second(bivariant.second());      
 
   102     /// \brief Destrcutor
 
   109     /// \brief Set to the default value of the \c First type.
 
   111     /// This function sets the variant to the default value of the \c
 
   113     BiVariant& setFirst() {
 
   116       new(reinterpret_cast<First*>(data)) First();   
 
   120     /// \brief Set to the given value of the \c First type.
 
   122     /// This function sets the variant to the given value of the \c
 
   124     BiVariant& setFirst(const First& f) {
 
   127       new(reinterpret_cast<First*>(data)) First(f);   
 
   131     /// \brief Set to the default value of the \c Second type.
 
   133     /// This function sets the variant to the default value of the \c
 
   135     BiVariant& setSecond() {
 
   138       new(reinterpret_cast<Second*>(data)) Second();   
 
   142     /// \brief Set to the given value of the \c Second type.
 
   144     /// This function sets the variant to the given value of the \c
 
   146     BiVariant& setSecond(const Second& s) {
 
   149       new(reinterpret_cast<Second*>(data)) Second(s);   
 
   153     /// \brief Operator form of the \c setFirst()
 
   154     BiVariant& operator=(const First& f) {
 
   158     /// \brief Operator form of the \c setSecond()
 
   159     BiVariant& operator=(const Second& s) {
 
   163     /// \brief Assign operator
 
   164     BiVariant& operator=(const BiVariant& bivariant) {
 
   165       if (this == &bivariant) return *this;
 
   167       flag = bivariant.flag;
 
   169         new(reinterpret_cast<First*>(data)) First(bivariant.first());      
 
   171         new(reinterpret_cast<Second*>(data)) Second(bivariant.second());      
 
   176     /// \brief Reference to the value
 
   178     /// Reference to the value of the \c First type.
 
   179     /// \pre The BiVariant should store value of \c First type.
 
   181       LEMON_ASSERT(flag, WrongStateError());
 
   182       return *reinterpret_cast<First*>(data); 
 
   185     /// \brief Const reference to the value
 
   187     /// Const reference to the value of the \c First type.
 
   188     /// \pre The BiVariant should store value of \c First type.
 
   189     const First& first() const { 
 
   190       LEMON_ASSERT(flag, WrongStateError());
 
   191       return *reinterpret_cast<const First*>(data); 
 
   194     /// \brief Operator form of the \c first()
 
   195     operator First&() { return first(); }
 
   196     /// \brief Operator form of the const \c first()
 
   197     operator const First&() const { return first(); }
 
   199     /// \brief Reference to the value
 
   201     /// Reference to the value of the \c Second type.
 
   202     /// \pre The BiVariant should store value of \c Second type.
 
   204       LEMON_ASSERT(!flag, WrongStateError());
 
   205       return *reinterpret_cast<Second*>(data); 
 
   208     /// \brief Const reference to the value
 
   210     /// Const reference to the value of the \c Second type.
 
   211     /// \pre The BiVariant should store value of \c Second type.
 
   212     const Second& second() const { 
 
   213       LEMON_ASSERT(!flag, WrongStateError());
 
   214       return *reinterpret_cast<const Second*>(data); 
 
   217     /// \brief Operator form of the \c second()
 
   218     operator Second&() { return second(); }
 
   219     /// \brief Operator form of the const \c second()
 
   220     operator const Second&() const { return second(); }
 
   222     /// \brief %True when the variant is in the first state
 
   224     /// %True when the variant stores value of the \c First type.
 
   225     bool firstState() const { return flag; }
 
   227     /// \brief %True when the variant is in the second state
 
   229     /// %True when the variant stores value of the \c Second type.
 
   230     bool secondState() const { return !flag; }
 
   236         reinterpret_cast<First*>(data)->~First();
 
   238         reinterpret_cast<Second*>(data)->~Second();
 
   242     char data[_variant_bits::CTMax<sizeof(First), sizeof(Second)>::value];
 
   246   namespace _variant_bits {
 
   248     template <int _idx, typename _TypeMap>
 
   251       typedef typename _TypeMap::template Map<_idx>::Type Current;
 
   253       static void destroy(int index, char* place) {
 
   255           reinterpret_cast<Current*>(place)->~Current();
 
   257           Memory<_idx - 1, _TypeMap>::destroy(index, place);
 
   261       static void copy(int index, char* to, const char* from) {
 
   263           new (reinterpret_cast<Current*>(to))
 
   264             Current(reinterpret_cast<const Current*>(from));
 
   266           Memory<_idx - 1, _TypeMap>::copy(index, to, from);
 
   272     template <typename _TypeMap>
 
   273     struct Memory<-1, _TypeMap> {
 
   275       static void destroy(int, char*) {
 
   276         LEMON_ASSERT(false, "Wrong Variant Index.");
 
   279       static void copy(int, char*, const char*) {
 
   280         LEMON_ASSERT(false, "Wrong Variant Index.");
 
   284     template <int _idx, typename _TypeMap>
 
   286       static const int value = 
 
   287       CTMax<sizeof(typename _TypeMap::template Map<_idx>::Type), 
 
   288             Size<_idx - 1, _TypeMap>::value>::value;
 
   291     template <typename _TypeMap>
 
   292     struct Size<0, _TypeMap> {
 
   293       static const int value = 
 
   294       sizeof(typename _TypeMap::template Map<0>::Type);
 
   299   /// \brief Variant type
 
   301   /// Simple Variant type. The Variant type is a type safe union. The
 
   302   /// C++ has strong limitations for using unions, by example we
 
   303   /// cannot store type with non default constructor or destructor in
 
   304   /// a union. This class always knowns the current state of the
 
   305   /// variant and it cares for the proper construction and
 
   308   /// \param _num The number of the types which can be stored in the
 
   310   /// \param _TypeMap This class describes the types of the Variant. The
 
   311   /// _TypeMap::Map<index>::Type should be a valid type for each index 
 
   312   /// in the range {0, 1, ..., _num - 1}. The \c VariantTypeMap is helper
 
   313   /// class to define such type mappings up to 10 types.
 
   315   /// And the usage of the class:
 
   317   /// typedef Variant<3, VariantTypeMap<int, std::string, double> > MyVariant;
 
   320   /// std::cout << var.get<0>() << std::endl;
 
   321   /// var.set<1>("alpha");
 
   322   /// std::cout << var.get<1>() << std::endl;
 
   323   /// var.set<2>(0.75);
 
   324   /// std::cout << var.get<2>() << std::endl;
 
   327   /// The result of course:
 
   333   template <int _num, typename _TypeMap>
 
   337     static const int num = _num;
 
   339     typedef _TypeMap TypeMap;
 
   341     struct WrongStateError : public lemon::LogicError {
 
   343       virtual const char* what() const throw() {
 
   344         return "lemon::Variant::WrongStateError";
 
   348     /// \brief Constructor
 
   350     /// This constructor initalizes to the default value of the \c type
 
   354       new(reinterpret_cast<typename TypeMap::template Map<0>::Type*>(data)) 
 
   355         typename TypeMap::template Map<0>::Type();
 
   359     /// \brief Copy constructor
 
   362     Variant(const Variant& variant) {
 
   364       _variant_bits::Memory<num - 1, TypeMap>::copy(flag, data, variant.data);
 
   367     /// \brief Assign operator
 
   370     Variant& operator=(const Variant& variant) {
 
   371       if (this == &variant) return *this;
 
   372       _variant_bits::Memory<num - 1, TypeMap>::
 
   375       _variant_bits::Memory<num - 1, TypeMap>::
 
   376         copy(flag, data, variant.data);
 
   380     /// \brief Destrcutor
 
   384       _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
 
   387     /// \brief Set to the default value of the type with \c _idx index.
 
   389     /// This function sets the variant to the default value of the
 
   390     /// type with \c _idx index.
 
   393       _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
 
   395       new(reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>(data)) 
 
   396         typename TypeMap::template Map<_idx>::Type();
 
   400     /// \brief Set to the given value of the type with \c _idx index.
 
   402     /// This function sets the variant to the given value of the type
 
   403     /// with \c _idx index.
 
   405     Variant& set(const typename _TypeMap::template Map<_idx>::Type& init) {
 
   406       _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
 
   408       new(reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>(data)) 
 
   409         typename TypeMap::template Map<_idx>::Type(init);
 
   413     /// \brief Gets the current value of the type with \c _idx index.
 
   415     /// Gets the current value of the type with \c _idx index.
 
   417     const typename TypeMap::template Map<_idx>::Type& get() const {
 
   418       LEMON_ASSERT(_idx == flag, "Wrong Variant Index.");
 
   419       return *reinterpret_cast<const typename TypeMap::
 
   420         template Map<_idx>::Type*>(data); 
 
   423     /// \brief Gets the current value of the type with \c _idx index.
 
   425     /// Gets the current value of the type with \c _idx index.
 
   427     typename _TypeMap::template Map<_idx>::Type& get() {
 
   428       LEMON_ASSERT(_idx == flag, "Wrong Variant Index.");
 
   429       return *reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>
 
   433     /// \brief Returns the current state of the variant.
 
   435     /// Returns the current state of the variant.
 
   442     char data[_variant_bits::Size<num - 1, TypeMap>::value];
 
   446   namespace _variant_bits {
 
   448     template <int _index, typename _List>
 
   450       typedef typename Get<_index - 1, typename _List::Next>::Type Type;
 
   453     template <typename _List>
 
   454     struct Get<0, _List> {
 
   455       typedef typename _List::Type Type;
 
   460     template <typename _Type, typename _List>
 
   466     template <int _idx, typename _T0, typename _T1, typename _T2, 
 
   467               typename _T3, typename _T5, typename _T4, typename _T6,
 
   468               typename _T7, typename _T8, typename _T9>
 
   471       typedef Insert<_T9, L10> L9;
 
   472       typedef Insert<_T8, L9> L8;
 
   473       typedef Insert<_T7, L8> L7;
 
   474       typedef Insert<_T6, L7> L6;
 
   475       typedef Insert<_T5, L6> L5;
 
   476       typedef Insert<_T4, L5> L4;
 
   477       typedef Insert<_T3, L4> L3;
 
   478       typedef Insert<_T2, L3> L2;
 
   479       typedef Insert<_T1, L2> L1;
 
   480       typedef Insert<_T0, L1> L0;
 
   481       typedef typename Get<_idx, L0>::Type Type;
 
   486   /// \brief Helper class for Variant
 
   488   /// Helper class to define type mappings for Variant. This class
 
   489   /// converts the template parameters to be mappable by integer.
 
   493     typename _T1 = void, typename _T2 = void, typename _T3 = void,
 
   494     typename _T5 = void, typename _T4 = void, typename _T6 = void,
 
   495     typename _T7 = void, typename _T8 = void, typename _T9 = void>
 
   496   struct VariantTypeMap {
 
   499       typedef typename _variant_bits::
 
   500       Mapper<_idx, _T0, _T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9>::Type