deba@416: /* -*- mode: C++; indent-tabs-mode: nil; -*- deba@414: * deba@416: * This file is a part of LEMON, a generic C++ optimization library. deba@414: * deba@414: * Copyright (C) 2003-2008 deba@414: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@414: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@414: * deba@414: * Permission to use, modify and distribute this software is granted deba@414: * provided that this copyright notice appears in all copies. For deba@414: * precise terms see the accompanying LICENSE file. deba@414: * deba@414: * This software is provided "AS IS" with no warranty of any kind, deba@414: * express or implied, and with no claim as to its suitability for any deba@414: * purpose. deba@414: * deba@414: */ deba@414: deba@414: #ifndef LEMON_BITS_VARIANT_H deba@414: #define LEMON_BITS_VARIANT_H deba@414: deba@414: #include deba@414: kpeter@431: // \file kpeter@431: // \brief Variant types deba@414: deba@414: namespace lemon { deba@414: deba@414: namespace _variant_bits { deba@416: deba@414: template deba@414: struct CTMax { deba@414: static const int value = left < right ? right : left; deba@414: }; deba@414: deba@414: } deba@414: deba@414: kpeter@431: // \brief Simple Variant type for two types kpeter@431: // kpeter@431: // Simple Variant type for two types. The Variant type is a type-safe kpeter@431: // union. C++ has strong limitations for using unions, for kpeter@431: // example you cannot store a type with non-default constructor or kpeter@431: // destructor in a union. This class always knowns the current kpeter@431: // state of the variant and it cares for the proper construction kpeter@431: // and destruction. deba@414: template deba@414: class BiVariant { deba@414: public: deba@414: kpeter@431: // \brief The \c First type. deba@414: typedef _First First; kpeter@431: // \brief The \c Second type. deba@414: typedef _Second Second; deba@414: kpeter@431: // \brief Constructor kpeter@431: // kpeter@431: // This constructor initalizes to the default value of the \c First kpeter@431: // type. deba@414: BiVariant() { deba@414: flag = true; deba@414: new(reinterpret_cast(data)) First(); deba@414: } deba@414: kpeter@431: // \brief Constructor kpeter@431: // kpeter@431: // This constructor initalizes to the given value of the \c First kpeter@431: // type. deba@414: BiVariant(const First& f) { deba@414: flag = true; deba@414: new(reinterpret_cast(data)) First(f); deba@414: } deba@414: kpeter@431: // \brief Constructor kpeter@431: // kpeter@431: // This constructor initalizes to the given value of the \c kpeter@431: // Second type. deba@414: BiVariant(const Second& s) { deba@414: flag = false; deba@414: new(reinterpret_cast(data)) Second(s); deba@414: } deba@414: kpeter@431: // \brief Copy constructor kpeter@431: // kpeter@431: // Copy constructor deba@414: BiVariant(const BiVariant& bivariant) { deba@414: flag = bivariant.flag; deba@414: if (flag) { deba@416: new(reinterpret_cast(data)) First(bivariant.first()); deba@414: } else { deba@416: new(reinterpret_cast(data)) Second(bivariant.second()); deba@414: } deba@414: } deba@414: kpeter@431: // \brief Destrcutor kpeter@431: // kpeter@431: // Destructor deba@414: ~BiVariant() { deba@414: destroy(); deba@414: } deba@414: kpeter@431: // \brief Set to the default value of the \c First type. kpeter@431: // kpeter@431: // This function sets the variant to the default value of the \c kpeter@431: // First type. deba@414: BiVariant& setFirst() { deba@414: destroy(); deba@414: flag = true; deba@416: new(reinterpret_cast(data)) First(); deba@414: return *this; deba@414: } deba@414: kpeter@431: // \brief Set to the given value of the \c First type. kpeter@431: // kpeter@431: // This function sets the variant to the given value of the \c kpeter@431: // First type. deba@414: BiVariant& setFirst(const First& f) { deba@414: destroy(); deba@414: flag = true; deba@416: new(reinterpret_cast(data)) First(f); deba@414: return *this; deba@414: } deba@414: kpeter@431: // \brief Set to the default value of the \c Second type. kpeter@431: // kpeter@431: // This function sets the variant to the default value of the \c kpeter@431: // Second type. deba@414: BiVariant& setSecond() { deba@414: destroy(); deba@414: flag = false; deba@416: new(reinterpret_cast(data)) Second(); deba@414: return *this; deba@414: } deba@414: kpeter@431: // \brief Set to the given value of the \c Second type. kpeter@431: // kpeter@431: // This function sets the variant to the given value of the \c kpeter@431: // Second type. deba@414: BiVariant& setSecond(const Second& s) { deba@414: destroy(); deba@414: flag = false; deba@416: new(reinterpret_cast(data)) Second(s); deba@414: return *this; deba@414: } deba@414: kpeter@431: // \brief Operator form of the \c setFirst() deba@414: BiVariant& operator=(const First& f) { deba@414: return setFirst(f); deba@414: } deba@414: kpeter@431: // \brief Operator form of the \c setSecond() deba@414: BiVariant& operator=(const Second& s) { deba@414: return setSecond(s); deba@414: } deba@414: kpeter@431: // \brief Assign operator deba@414: BiVariant& operator=(const BiVariant& bivariant) { deba@414: if (this == &bivariant) return *this; deba@414: destroy(); deba@414: flag = bivariant.flag; deba@414: if (flag) { deba@416: new(reinterpret_cast(data)) First(bivariant.first()); deba@414: } else { deba@416: new(reinterpret_cast(data)) Second(bivariant.second()); deba@414: } deba@414: return *this; deba@414: } deba@414: kpeter@431: // \brief Reference to the value kpeter@431: // kpeter@431: // Reference to the value of the \c First type. kpeter@431: // \pre The BiVariant should store value of \c First type. deba@414: First& first() { deba@414: LEMON_DEBUG(flag, "Variant wrong state"); kpeter@431: return *reinterpret_cast(data); deba@414: } deba@414: kpeter@431: // \brief Const reference to the value kpeter@431: // kpeter@431: // Const reference to the value of the \c First type. kpeter@431: // \pre The BiVariant should store value of \c First type. kpeter@431: const First& first() const { deba@414: LEMON_DEBUG(flag, "Variant wrong state"); kpeter@431: return *reinterpret_cast(data); deba@414: } deba@414: kpeter@431: // \brief Operator form of the \c first() deba@414: operator First&() { return first(); } kpeter@431: // \brief Operator form of the const \c first() deba@414: operator const First&() const { return first(); } deba@414: kpeter@431: // \brief Reference to the value kpeter@431: // kpeter@431: // Reference to the value of the \c Second type. kpeter@431: // \pre The BiVariant should store value of \c Second type. kpeter@431: Second& second() { deba@414: LEMON_DEBUG(!flag, "Variant wrong state"); kpeter@431: return *reinterpret_cast(data); deba@414: } deba@414: kpeter@431: // \brief Const reference to the value kpeter@431: // kpeter@431: // Const reference to the value of the \c Second type. kpeter@431: // \pre The BiVariant should store value of \c Second type. kpeter@431: const Second& second() const { deba@414: LEMON_DEBUG(!flag, "Variant wrong state"); kpeter@431: return *reinterpret_cast(data); deba@414: } deba@414: kpeter@431: // \brief Operator form of the \c second() deba@414: operator Second&() { return second(); } kpeter@431: // \brief Operator form of the const \c second() deba@414: operator const Second&() const { return second(); } deba@414: kpeter@431: // \brief %True when the variant is in the first state kpeter@431: // kpeter@431: // %True when the variant stores value of the \c First type. deba@414: bool firstState() const { return flag; } deba@414: kpeter@431: // \brief %True when the variant is in the second state kpeter@431: // kpeter@431: // %True when the variant stores value of the \c Second type. deba@414: bool secondState() const { return !flag; } deba@414: deba@414: private: deba@414: deba@414: void destroy() { deba@414: if (flag) { deba@414: reinterpret_cast(data)->~First(); deba@414: } else { deba@414: reinterpret_cast(data)->~Second(); deba@414: } deba@414: } deba@416: deba@414: char data[_variant_bits::CTMax::value]; deba@414: bool flag; deba@414: }; deba@414: deba@414: namespace _variant_bits { deba@416: deba@414: template deba@414: struct Memory { deba@414: deba@414: typedef typename _TypeMap::template Map<_idx>::Type Current; deba@414: deba@414: static void destroy(int index, char* place) { deba@414: if (index == _idx) { deba@414: reinterpret_cast(place)->~Current(); deba@414: } else { deba@414: Memory<_idx - 1, _TypeMap>::destroy(index, place); deba@414: } deba@414: } deba@414: deba@414: static void copy(int index, char* to, const char* from) { deba@414: if (index == _idx) { deba@414: new (reinterpret_cast(to)) deba@414: Current(reinterpret_cast(from)); deba@414: } else { deba@414: Memory<_idx - 1, _TypeMap>::copy(index, to, from); deba@414: } deba@414: } deba@414: deba@414: }; deba@414: deba@414: template deba@414: struct Memory<-1, _TypeMap> { deba@414: deba@414: static void destroy(int, char*) { deba@414: LEMON_DEBUG(false, "Variant wrong index."); deba@414: } deba@414: deba@414: static void copy(int, char*, const char*) { deba@414: LEMON_DEBUG(false, "Variant wrong index."); deba@414: } deba@414: }; deba@414: deba@414: template deba@414: struct Size { deba@416: static const int value = deba@416: CTMax::Type), deba@414: Size<_idx - 1, _TypeMap>::value>::value; deba@414: }; deba@414: deba@414: template deba@414: struct Size<0, _TypeMap> { deba@416: static const int value = deba@414: sizeof(typename _TypeMap::template Map<0>::Type); deba@414: }; deba@414: deba@414: } deba@414: kpeter@431: // \brief Variant type kpeter@431: // kpeter@431: // Simple Variant type. The Variant type is a type-safe union. kpeter@431: // C++ has strong limitations for using unions, for example you kpeter@431: // cannot store type with non-default constructor or destructor in kpeter@431: // a union. This class always knowns the current state of the kpeter@431: // variant and it cares for the proper construction and kpeter@431: // destruction. kpeter@431: // kpeter@431: // \param _num The number of the types which can be stored in the kpeter@431: // variant type. kpeter@431: // \param _TypeMap This class describes the types of the Variant. The kpeter@431: // _TypeMap::Map::Type should be a valid type for each index kpeter@431: // in the range {0, 1, ..., _num - 1}. The \c VariantTypeMap is helper kpeter@431: // class to define such type mappings up to 10 types. kpeter@431: // kpeter@431: // And the usage of the class: kpeter@431: //\code kpeter@431: // typedef Variant<3, VariantTypeMap > MyVariant; kpeter@431: // MyVariant var; kpeter@431: // var.set<0>(12); kpeter@431: // std::cout << var.get<0>() << std::endl; kpeter@431: // var.set<1>("alpha"); kpeter@431: // std::cout << var.get<1>() << std::endl; kpeter@431: // var.set<2>(0.75); kpeter@431: // std::cout << var.get<2>() << std::endl; kpeter@431: //\endcode kpeter@431: // kpeter@431: // The result of course: kpeter@431: //\code kpeter@431: // 12 kpeter@431: // alpha kpeter@431: // 0.75 kpeter@431: //\endcode deba@414: template deba@414: class Variant { deba@414: public: deba@414: deba@414: static const int num = _num; deba@414: deba@414: typedef _TypeMap TypeMap; deba@414: kpeter@431: // \brief Constructor kpeter@431: // kpeter@431: // This constructor initalizes to the default value of the \c type kpeter@431: // with 0 index. deba@414: Variant() { deba@414: flag = 0; deba@416: new(reinterpret_cast::Type*>(data)) deba@414: typename TypeMap::template Map<0>::Type(); deba@414: } deba@414: deba@414: kpeter@431: // \brief Copy constructor kpeter@431: // kpeter@431: // Copy constructor deba@414: Variant(const Variant& variant) { deba@414: flag = variant.flag; deba@414: _variant_bits::Memory::copy(flag, data, variant.data); deba@414: } deba@414: kpeter@431: // \brief Assign operator kpeter@431: // kpeter@431: // Assign operator deba@414: Variant& operator=(const Variant& variant) { deba@414: if (this == &variant) return *this; deba@414: _variant_bits::Memory:: deba@414: destroy(flag, data); deba@414: flag = variant.flag; deba@414: _variant_bits::Memory:: deba@414: copy(flag, data, variant.data); deba@414: return *this; deba@414: } deba@414: kpeter@431: // \brief Destrcutor kpeter@431: // kpeter@431: // Destructor deba@414: ~Variant() { deba@414: _variant_bits::Memory::destroy(flag, data); deba@414: } deba@414: kpeter@431: // \brief Set to the default value of the type with \c _idx index. kpeter@431: // kpeter@431: // This function sets the variant to the default value of the kpeter@431: // type with \c _idx index. deba@414: template deba@414: Variant& set() { deba@414: _variant_bits::Memory::destroy(flag, data); deba@414: flag = _idx; deba@416: new(reinterpret_cast::Type*>(data)) deba@414: typename TypeMap::template Map<_idx>::Type(); deba@414: return *this; deba@414: } deba@414: kpeter@431: // \brief Set to the given value of the type with \c _idx index. kpeter@431: // kpeter@431: // This function sets the variant to the given value of the type kpeter@431: // with \c _idx index. deba@414: template deba@414: Variant& set(const typename _TypeMap::template Map<_idx>::Type& init) { deba@414: _variant_bits::Memory::destroy(flag, data); deba@414: flag = _idx; deba@416: new(reinterpret_cast::Type*>(data)) deba@414: typename TypeMap::template Map<_idx>::Type(init); deba@414: return *this; deba@414: } deba@414: kpeter@431: // \brief Gets the current value of the type with \c _idx index. kpeter@431: // kpeter@431: // Gets the current value of the type with \c _idx index. deba@414: template deba@414: const typename TypeMap::template Map<_idx>::Type& get() const { deba@414: LEMON_DEBUG(_idx == flag, "Variant wrong index"); deba@414: return *reinterpret_cast::Type*>(data); deba@414: } deba@414: kpeter@431: // \brief Gets the current value of the type with \c _idx index. kpeter@431: // kpeter@431: // Gets the current value of the type with \c _idx index. deba@414: template deba@414: typename _TypeMap::template Map<_idx>::Type& get() { deba@414: LEMON_DEBUG(_idx == flag, "Variant wrong index"); deba@414: return *reinterpret_cast::Type*> deba@416: (data); deba@414: } deba@414: kpeter@431: // \brief Returns the current state of the variant. kpeter@431: // kpeter@431: // Returns the current state of the variant. deba@414: int state() const { deba@414: return flag; deba@414: } deba@414: deba@414: private: deba@416: deba@414: char data[_variant_bits::Size::value]; deba@414: int flag; deba@414: }; deba@414: deba@414: namespace _variant_bits { deba@414: deba@414: template deba@414: struct Get { deba@414: typedef typename Get<_index - 1, typename _List::Next>::Type Type; deba@414: }; deba@414: deba@414: template deba@414: struct Get<0, _List> { deba@414: typedef typename _List::Type Type; deba@414: }; deba@414: deba@414: struct List {}; deba@416: deba@414: template deba@414: struct Insert { deba@414: typedef _List Next; deba@414: typedef _Type Type; deba@414: }; deba@414: deba@416: template deba@414: struct Mapper { deba@414: typedef List L10; deba@414: typedef Insert<_T9, L10> L9; deba@414: typedef Insert<_T8, L9> L8; deba@414: typedef Insert<_T7, L8> L7; deba@414: typedef Insert<_T6, L7> L6; deba@414: typedef Insert<_T5, L6> L5; deba@414: typedef Insert<_T4, L5> L4; deba@414: typedef Insert<_T3, L4> L3; deba@414: typedef Insert<_T2, L3> L2; deba@414: typedef Insert<_T1, L2> L1; deba@414: typedef Insert<_T0, L1> L0; deba@414: typedef typename Get<_idx, L0>::Type Type; deba@414: }; deba@416: deba@414: } deba@414: kpeter@431: // \brief Helper class for Variant kpeter@431: // kpeter@431: // Helper class to define type mappings for Variant. This class kpeter@431: // converts the template parameters to be mappable by integer. kpeter@431: // \see Variant deba@414: template < deba@416: typename _T0, deba@414: typename _T1 = void, typename _T2 = void, typename _T3 = void, kpeter@430: typename _T4 = void, typename _T5 = void, typename _T6 = void, deba@414: typename _T7 = void, typename _T8 = void, typename _T9 = void> deba@414: struct VariantTypeMap { deba@414: template deba@414: struct Map { deba@414: typedef typename _variant_bits:: deba@414: Mapper<_idx, _T0, _T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9>::Type deba@414: Type; deba@414: }; deba@414: }; deba@416: deba@414: } deba@414: deba@414: deba@414: #endif