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