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