lemon/bits/variant.h
changeset 437 6c1ac149ebf8
parent 430 09e416d35896
child 440 88ed40ad0d4f
equal deleted inserted replaced
2:e2f126ac4886 3:b058cce8e06b
    19 #ifndef LEMON_BITS_VARIANT_H
    19 #ifndef LEMON_BITS_VARIANT_H
    20 #define LEMON_BITS_VARIANT_H
    20 #define LEMON_BITS_VARIANT_H
    21 
    21 
    22 #include <lemon/assert.h>
    22 #include <lemon/assert.h>
    23 
    23 
    24 /// \file
    24 // \file
    25 /// \brief Variant types
    25 // \brief Variant types
    26 
    26 
    27 namespace lemon {
    27 namespace lemon {
    28 
    28 
    29   namespace _variant_bits {
    29   namespace _variant_bits {
    30 
    30 
    34     };
    34     };
    35 
    35 
    36   }
    36   }
    37 
    37 
    38 
    38 
    39   /// \brief Simple Variant type for two types
    39   // \brief Simple Variant type for two types
    40   ///
    40   //
    41   /// Simple Variant type for two types. The Variant type is a type
    41   // Simple Variant type for two types. The Variant type is a type-safe
    42   /// safe union. The C++ has strong limitations for using unions, by
    42   // union. C++ has strong limitations for using unions, for
    43   /// example we can not store type with non default constructor or
    43   // example you cannot store a type with non-default constructor or
    44   /// destructor in an union. This class always knowns the current
    44   // destructor in a union. This class always knowns the current
    45   /// state of the variant and it cares for the proper construction
    45   // state of the variant and it cares for the proper construction
    46   /// and destruction.
    46   // and destruction.
    47   template <typename _First, typename _Second>
    47   template <typename _First, typename _Second>
    48   class BiVariant {
    48   class BiVariant {
    49   public:
    49   public:
    50 
    50 
    51     /// \brief The \c First type.
    51     // \brief The \c First type.
    52     typedef _First First;
    52     typedef _First First;
    53     /// \brief The \c Second type.
    53     // \brief The \c Second type.
    54     typedef _Second Second;
    54     typedef _Second Second;
    55 
    55 
    56     /// \brief Constructor
    56     // \brief Constructor
    57     ///
    57     //
    58     /// This constructor initalizes to the default value of the \c First
    58     // This constructor initalizes to the default value of the \c First
    59     /// type.
    59     // type.
    60     BiVariant() {
    60     BiVariant() {
    61       flag = true;
    61       flag = true;
    62       new(reinterpret_cast<First*>(data)) First();
    62       new(reinterpret_cast<First*>(data)) First();
    63     }
    63     }
    64 
    64 
    65     /// \brief Constructor
    65     // \brief Constructor
    66     ///
    66     //
    67     /// This constructor initalizes to the given value of the \c First
    67     // This constructor initalizes to the given value of the \c First
    68     /// type.
    68     // type.
    69     BiVariant(const First& f) {
    69     BiVariant(const First& f) {
    70       flag = true;
    70       flag = true;
    71       new(reinterpret_cast<First*>(data)) First(f);
    71       new(reinterpret_cast<First*>(data)) First(f);
    72     }
    72     }
    73 
    73 
    74     /// \brief Constructor
    74     // \brief Constructor
    75     ///
    75     //
    76     /// This constructor initalizes to the given value of the \c
    76     // This constructor initalizes to the given value of the \c
    77     /// Second type.
    77     // Second type.
    78     BiVariant(const Second& s) {
    78     BiVariant(const Second& s) {
    79       flag = false;
    79       flag = false;
    80       new(reinterpret_cast<Second*>(data)) Second(s);
    80       new(reinterpret_cast<Second*>(data)) Second(s);
    81     }
    81     }
    82 
    82 
    83     /// \brief Copy constructor
    83     // \brief Copy constructor
    84     ///
    84     //
    85     /// Copy constructor
    85     // Copy constructor
    86     BiVariant(const BiVariant& bivariant) {
    86     BiVariant(const BiVariant& bivariant) {
    87       flag = bivariant.flag;
    87       flag = bivariant.flag;
    88       if (flag) {
    88       if (flag) {
    89         new(reinterpret_cast<First*>(data)) First(bivariant.first());
    89         new(reinterpret_cast<First*>(data)) First(bivariant.first());
    90       } else {
    90       } else {
    91         new(reinterpret_cast<Second*>(data)) Second(bivariant.second());
    91         new(reinterpret_cast<Second*>(data)) Second(bivariant.second());
    92       }
    92       }
    93     }
    93     }
    94 
    94 
    95     /// \brief Destrcutor
    95     // \brief Destrcutor
    96     ///
    96     //
    97     /// Destructor
    97     // Destructor
    98     ~BiVariant() {
    98     ~BiVariant() {
    99       destroy();
    99       destroy();
   100     }
   100     }
   101 
   101 
   102     /// \brief Set to the default value of the \c First type.
   102     // \brief Set to the default value of the \c First type.
   103     ///
   103     //
   104     /// This function sets the variant to the default value of the \c
   104     // This function sets the variant to the default value of the \c
   105     /// First type.
   105     // First type.
   106     BiVariant& setFirst() {
   106     BiVariant& setFirst() {
   107       destroy();
   107       destroy();
   108       flag = true;
   108       flag = true;
   109       new(reinterpret_cast<First*>(data)) First();
   109       new(reinterpret_cast<First*>(data)) First();
   110       return *this;
   110       return *this;
   111     }
   111     }
   112 
   112 
   113     /// \brief Set to the given value of the \c First type.
   113     // \brief Set to the given value of the \c First type.
   114     ///
   114     //
   115     /// This function sets the variant to the given value of the \c
   115     // This function sets the variant to the given value of the \c
   116     /// First type.
   116     // First type.
   117     BiVariant& setFirst(const First& f) {
   117     BiVariant& setFirst(const First& f) {
   118       destroy();
   118       destroy();
   119       flag = true;
   119       flag = true;
   120       new(reinterpret_cast<First*>(data)) First(f);
   120       new(reinterpret_cast<First*>(data)) First(f);
   121       return *this;
   121       return *this;
   122     }
   122     }
   123 
   123 
   124     /// \brief Set to the default value of the \c Second type.
   124     // \brief Set to the default value of the \c Second type.
   125     ///
   125     //
   126     /// This function sets the variant to the default value of the \c
   126     // This function sets the variant to the default value of the \c
   127     /// Second type.
   127     // Second type.
   128     BiVariant& setSecond() {
   128     BiVariant& setSecond() {
   129       destroy();
   129       destroy();
   130       flag = false;
   130       flag = false;
   131       new(reinterpret_cast<Second*>(data)) Second();
   131       new(reinterpret_cast<Second*>(data)) Second();
   132       return *this;
   132       return *this;
   133     }
   133     }
   134 
   134 
   135     /// \brief Set to the given value of the \c Second type.
   135     // \brief Set to the given value of the \c Second type.
   136     ///
   136     //
   137     /// This function sets the variant to the given value of the \c
   137     // This function sets the variant to the given value of the \c
   138     /// Second type.
   138     // Second type.
   139     BiVariant& setSecond(const Second& s) {
   139     BiVariant& setSecond(const Second& s) {
   140       destroy();
   140       destroy();
   141       flag = false;
   141       flag = false;
   142       new(reinterpret_cast<Second*>(data)) Second(s);
   142       new(reinterpret_cast<Second*>(data)) Second(s);
   143       return *this;
   143       return *this;
   144     }
   144     }
   145 
   145 
   146     /// \brief Operator form of the \c setFirst()
   146     // \brief Operator form of the \c setFirst()
   147     BiVariant& operator=(const First& f) {
   147     BiVariant& operator=(const First& f) {
   148       return setFirst(f);
   148       return setFirst(f);
   149     }
   149     }
   150 
   150 
   151     /// \brief Operator form of the \c setSecond()
   151     // \brief Operator form of the \c setSecond()
   152     BiVariant& operator=(const Second& s) {
   152     BiVariant& operator=(const Second& s) {
   153       return setSecond(s);
   153       return setSecond(s);
   154     }
   154     }
   155 
   155 
   156     /// \brief Assign operator
   156     // \brief Assign operator
   157     BiVariant& operator=(const BiVariant& bivariant) {
   157     BiVariant& operator=(const BiVariant& bivariant) {
   158       if (this == &bivariant) return *this;
   158       if (this == &bivariant) return *this;
   159       destroy();
   159       destroy();
   160       flag = bivariant.flag;
   160       flag = bivariant.flag;
   161       if (flag) {
   161       if (flag) {
   164         new(reinterpret_cast<Second*>(data)) Second(bivariant.second());
   164         new(reinterpret_cast<Second*>(data)) Second(bivariant.second());
   165       }
   165       }
   166       return *this;
   166       return *this;
   167     }
   167     }
   168 
   168 
   169     /// \brief Reference to the value
   169     // \brief Reference to the value
   170     ///
   170     //
   171     /// Reference to the value of the \c First type.
   171     // Reference to the value of the \c First type.
   172     /// \pre The BiVariant should store value of \c First type.
   172     // \pre The BiVariant should store value of \c First type.
   173     First& first() {
   173     First& first() {
   174       LEMON_DEBUG(flag, "Variant wrong state");
   174       LEMON_DEBUG(flag, "Variant wrong state");
   175       return *reinterpret_cast<First*>(data); 
   175       return *reinterpret_cast<First*>(data);
   176     }
   176     }
   177 
   177 
   178     /// \brief Const reference to the value
   178     // \brief Const reference to the value
   179     ///
   179     //
   180     /// Const reference to the value of the \c First type.
   180     // Const reference to the value of the \c First type.
   181     /// \pre The BiVariant should store value of \c First type.
   181     // \pre The BiVariant should store value of \c First type.
   182     const First& first() const { 
   182     const First& first() const {
   183       LEMON_DEBUG(flag, "Variant wrong state");
   183       LEMON_DEBUG(flag, "Variant wrong state");
   184       return *reinterpret_cast<const First*>(data); 
   184       return *reinterpret_cast<const First*>(data);
   185     }
   185     }
   186 
   186 
   187     /// \brief Operator form of the \c first()
   187     // \brief Operator form of the \c first()
   188     operator First&() { return first(); }
   188     operator First&() { return first(); }
   189     /// \brief Operator form of the const \c first()
   189     // \brief Operator form of the const \c first()
   190     operator const First&() const { return first(); }
   190     operator const First&() const { return first(); }
   191 
   191 
   192     /// \brief Reference to the value
   192     // \brief Reference to the value
   193     ///
   193     //
   194     /// Reference to the value of the \c Second type.
   194     // Reference to the value of the \c Second type.
   195     /// \pre The BiVariant should store value of \c Second type.
   195     // \pre The BiVariant should store value of \c Second type.
   196     Second& second() { 
   196     Second& second() {
   197       LEMON_DEBUG(!flag, "Variant wrong state");
   197       LEMON_DEBUG(!flag, "Variant wrong state");
   198       return *reinterpret_cast<Second*>(data); 
   198       return *reinterpret_cast<Second*>(data);
   199     }
   199     }
   200 
   200 
   201     /// \brief Const reference to the value
   201     // \brief Const reference to the value
   202     ///
   202     //
   203     /// Const reference to the value of the \c Second type.
   203     // Const reference to the value of the \c Second type.
   204     /// \pre The BiVariant should store value of \c Second type.
   204     // \pre The BiVariant should store value of \c Second type.
   205     const Second& second() const { 
   205     const Second& second() const {
   206       LEMON_DEBUG(!flag, "Variant wrong state");
   206       LEMON_DEBUG(!flag, "Variant wrong state");
   207       return *reinterpret_cast<const Second*>(data); 
   207       return *reinterpret_cast<const Second*>(data);
   208     }
   208     }
   209 
   209 
   210     /// \brief Operator form of the \c second()
   210     // \brief Operator form of the \c second()
   211     operator Second&() { return second(); }
   211     operator Second&() { return second(); }
   212     /// \brief Operator form of the const \c second()
   212     // \brief Operator form of the const \c second()
   213     operator const Second&() const { return second(); }
   213     operator const Second&() const { return second(); }
   214 
   214 
   215     /// \brief %True when the variant is in the first state
   215     // \brief %True when the variant is in the first state
   216     ///
   216     //
   217     /// %True when the variant stores value of the \c First type.
   217     // %True when the variant stores value of the \c First type.
   218     bool firstState() const { return flag; }
   218     bool firstState() const { return flag; }
   219 
   219 
   220     /// \brief %True when the variant is in the second state
   220     // \brief %True when the variant is in the second state
   221     ///
   221     //
   222     /// %True when the variant stores value of the \c Second type.
   222     // %True when the variant stores value of the \c Second type.
   223     bool secondState() const { return !flag; }
   223     bool secondState() const { return !flag; }
   224 
   224 
   225   private:
   225   private:
   226 
   226 
   227     void destroy() {
   227     void destroy() {
   287       sizeof(typename _TypeMap::template Map<0>::Type);
   287       sizeof(typename _TypeMap::template Map<0>::Type);
   288     };
   288     };
   289 
   289 
   290   }
   290   }
   291 
   291 
   292   /// \brief Variant type
   292   // \brief Variant type
   293   ///
   293   //
   294   /// Simple Variant type. The Variant type is a type safe union. The
   294   // Simple Variant type. The Variant type is a type-safe union.
   295   /// C++ has strong limitations for using unions, for example we
   295   // C++ has strong limitations for using unions, for example you
   296   /// cannot store type with non default constructor or destructor in
   296   // cannot store type with non-default constructor or destructor in
   297   /// a union. This class always knowns the current state of the
   297   // a union. This class always knowns the current state of the
   298   /// variant and it cares for the proper construction and
   298   // variant and it cares for the proper construction and
   299   /// destruction.
   299   // destruction.
   300   ///
   300   //
   301   /// \param _num The number of the types which can be stored in the
   301   // \param _num The number of the types which can be stored in the
   302   /// variant type.
   302   // variant type.
   303   /// \param _TypeMap This class describes the types of the Variant. 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
   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
   305   // in the range {0, 1, ..., _num - 1}. The \c VariantTypeMap is helper
   306   /// class to define such type mappings up to 10 types.
   306   // class to define such type mappings up to 10 types.
   307   ///
   307   //
   308   /// And the usage of the class:
   308   // And the usage of the class:
   309   ///\code
   309   //\code
   310   /// typedef Variant<3, VariantTypeMap<int, std::string, double> > MyVariant;
   310   // typedef Variant<3, VariantTypeMap<int, std::string, double> > MyVariant;
   311   /// MyVariant var;
   311   // MyVariant var;
   312   /// var.set<0>(12);
   312   // var.set<0>(12);
   313   /// std::cout << var.get<0>() << std::endl;
   313   // std::cout << var.get<0>() << std::endl;
   314   /// var.set<1>("alpha");
   314   // var.set<1>("alpha");
   315   /// std::cout << var.get<1>() << std::endl;
   315   // std::cout << var.get<1>() << std::endl;
   316   /// var.set<2>(0.75);
   316   // var.set<2>(0.75);
   317   /// std::cout << var.get<2>() << std::endl;
   317   // std::cout << var.get<2>() << std::endl;
   318   ///\endcode
   318   //\endcode
   319   ///
   319   //
   320   /// The result of course:
   320   // The result of course:
   321   ///\code
   321   //\code
   322   /// 12
   322   // 12
   323   /// alpha
   323   // alpha
   324   /// 0.75
   324   // 0.75
   325   ///\endcode
   325   //\endcode
   326   template <int _num, typename _TypeMap>
   326   template <int _num, typename _TypeMap>
   327   class Variant {
   327   class Variant {
   328   public:
   328   public:
   329 
   329 
   330     static const int num = _num;
   330     static const int num = _num;
   331 
   331 
   332     typedef _TypeMap TypeMap;
   332     typedef _TypeMap TypeMap;
   333 
   333 
   334     /// \brief Constructor
   334     // \brief Constructor
   335     ///
   335     //
   336     /// This constructor initalizes to the default value of the \c type
   336     // This constructor initalizes to the default value of the \c type
   337     /// with 0 index.
   337     // with 0 index.
   338     Variant() {
   338     Variant() {
   339       flag = 0;
   339       flag = 0;
   340       new(reinterpret_cast<typename TypeMap::template Map<0>::Type*>(data))
   340       new(reinterpret_cast<typename TypeMap::template Map<0>::Type*>(data))
   341         typename TypeMap::template Map<0>::Type();
   341         typename TypeMap::template Map<0>::Type();
   342     }
   342     }
   343 
   343 
   344 
   344 
   345     /// \brief Copy constructor
   345     // \brief Copy constructor
   346     ///
   346     //
   347     /// Copy constructor
   347     // Copy constructor
   348     Variant(const Variant& variant) {
   348     Variant(const Variant& variant) {
   349       flag = variant.flag;
   349       flag = variant.flag;
   350       _variant_bits::Memory<num - 1, TypeMap>::copy(flag, data, variant.data);
   350       _variant_bits::Memory<num - 1, TypeMap>::copy(flag, data, variant.data);
   351     }
   351     }
   352 
   352 
   353     /// \brief Assign operator
   353     // \brief Assign operator
   354     ///
   354     //
   355     /// Assign operator
   355     // Assign operator
   356     Variant& operator=(const Variant& variant) {
   356     Variant& operator=(const Variant& variant) {
   357       if (this == &variant) return *this;
   357       if (this == &variant) return *this;
   358       _variant_bits::Memory<num - 1, TypeMap>::
   358       _variant_bits::Memory<num - 1, TypeMap>::
   359         destroy(flag, data);
   359         destroy(flag, data);
   360       flag = variant.flag;
   360       flag = variant.flag;
   361       _variant_bits::Memory<num - 1, TypeMap>::
   361       _variant_bits::Memory<num - 1, TypeMap>::
   362         copy(flag, data, variant.data);
   362         copy(flag, data, variant.data);
   363       return *this;
   363       return *this;
   364     }
   364     }
   365 
   365 
   366     /// \brief Destrcutor
   366     // \brief Destrcutor
   367     ///
   367     //
   368     /// Destructor
   368     // Destructor
   369     ~Variant() {
   369     ~Variant() {
   370       _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
   370       _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
   371     }
   371     }
   372 
   372 
   373     /// \brief Set to the default value of the type with \c _idx index.
   373     // \brief Set to the default value of the type with \c _idx index.
   374     ///
   374     //
   375     /// This function sets the variant to the default value of the
   375     // This function sets the variant to the default value of the
   376     /// type with \c _idx index.
   376     // type with \c _idx index.
   377     template <int _idx>
   377     template <int _idx>
   378     Variant& set() {
   378     Variant& set() {
   379       _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
   379       _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
   380       flag = _idx;
   380       flag = _idx;
   381       new(reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>(data))
   381       new(reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>(data))
   382         typename TypeMap::template Map<_idx>::Type();
   382         typename TypeMap::template Map<_idx>::Type();
   383       return *this;
   383       return *this;
   384     }
   384     }
   385 
   385 
   386     /// \brief Set to the given value of the type with \c _idx index.
   386     // \brief Set to the given value of the type with \c _idx index.
   387     ///
   387     //
   388     /// This function sets the variant to the given value of the type
   388     // This function sets the variant to the given value of the type
   389     /// with \c _idx index.
   389     // with \c _idx index.
   390     template <int _idx>
   390     template <int _idx>
   391     Variant& set(const typename _TypeMap::template Map<_idx>::Type& init) {
   391     Variant& set(const typename _TypeMap::template Map<_idx>::Type& init) {
   392       _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
   392       _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
   393       flag = _idx;
   393       flag = _idx;
   394       new(reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>(data))
   394       new(reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>(data))
   395         typename TypeMap::template Map<_idx>::Type(init);
   395         typename TypeMap::template Map<_idx>::Type(init);
   396       return *this;
   396       return *this;
   397     }
   397     }
   398 
   398 
   399     /// \brief Gets the current value of the type with \c _idx index.
   399     // \brief Gets the current value of the type with \c _idx index.
   400     ///
   400     //
   401     /// Gets the current value of the type with \c _idx index.
   401     // Gets the current value of the type with \c _idx index.
   402     template <int _idx>
   402     template <int _idx>
   403     const typename TypeMap::template Map<_idx>::Type& get() const {
   403     const typename TypeMap::template Map<_idx>::Type& get() const {
   404       LEMON_DEBUG(_idx == flag, "Variant wrong index");
   404       LEMON_DEBUG(_idx == flag, "Variant wrong index");
   405       return *reinterpret_cast<const typename TypeMap::
   405       return *reinterpret_cast<const typename TypeMap::
   406         template Map<_idx>::Type*>(data);
   406         template Map<_idx>::Type*>(data);
   407     }
   407     }
   408 
   408 
   409     /// \brief Gets the current value of the type with \c _idx index.
   409     // \brief Gets the current value of the type with \c _idx index.
   410     ///
   410     //
   411     /// Gets the current value of the type with \c _idx index.
   411     // Gets the current value of the type with \c _idx index.
   412     template <int _idx>
   412     template <int _idx>
   413     typename _TypeMap::template Map<_idx>::Type& get() {
   413     typename _TypeMap::template Map<_idx>::Type& get() {
   414       LEMON_DEBUG(_idx == flag, "Variant wrong index");
   414       LEMON_DEBUG(_idx == flag, "Variant wrong index");
   415       return *reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>
   415       return *reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>
   416         (data);
   416         (data);
   417     }
   417     }
   418 
   418 
   419     /// \brief Returns the current state of the variant.
   419     // \brief Returns the current state of the variant.
   420     ///
   420     //
   421     /// Returns the current state of the variant.
   421     // Returns the current state of the variant.
   422     int state() const {
   422     int state() const {
   423       return flag;
   423       return flag;
   424     }
   424     }
   425 
   425 
   426   private:
   426   private:
   467       typedef typename Get<_idx, L0>::Type Type;
   467       typedef typename Get<_idx, L0>::Type Type;
   468     };
   468     };
   469 
   469 
   470   }
   470   }
   471 
   471 
   472   /// \brief Helper class for Variant
   472   // \brief Helper class for Variant
   473   ///
   473   //
   474   /// Helper class to define type mappings for Variant. This class
   474   // Helper class to define type mappings for Variant. This class
   475   /// converts the template parameters to be mappable by integer.
   475   // converts the template parameters to be mappable by integer.
   476   /// \see Variant
   476   // \see Variant
   477   template <
   477   template <
   478     typename _T0,
   478     typename _T0,
   479     typename _T1 = void, typename _T2 = void, typename _T3 = void,
   479     typename _T1 = void, typename _T2 = void, typename _T3 = void,
   480     typename _T4 = void, typename _T5 = void, typename _T6 = void,
   480     typename _T4 = void, typename _T5 = void, typename _T6 = void,
   481     typename _T7 = void, typename _T8 = void, typename _T9 = void>
   481     typename _T7 = void, typename _T8 = void, typename _T9 = void>