1.1 --- a/lemon/bits/variant.h Mon Dec 08 15:47:20 2008 +0000
1.2 +++ b/lemon/bits/variant.h Fri Dec 12 21:41:05 2008 +0000
1.3 @@ -21,8 +21,8 @@
1.4
1.5 #include <lemon/assert.h>
1.6
1.7 -/// \file
1.8 -/// \brief Variant types
1.9 +// \file
1.10 +// \brief Variant types
1.11
1.12 namespace lemon {
1.13
1.14 @@ -36,53 +36,53 @@
1.15 }
1.16
1.17
1.18 - /// \brief Simple Variant type for two types
1.19 - ///
1.20 - /// Simple Variant type for two types. The Variant type is a type
1.21 - /// safe union. The C++ has strong limitations for using unions, by
1.22 - /// example we can not store type with non default constructor or
1.23 - /// destructor in an union. This class always knowns the current
1.24 - /// state of the variant and it cares for the proper construction
1.25 - /// and destruction.
1.26 + // \brief Simple Variant type for two types
1.27 + //
1.28 + // Simple Variant type for two types. The Variant type is a type-safe
1.29 + // union. C++ has strong limitations for using unions, for
1.30 + // example you cannot store a type with non-default constructor or
1.31 + // destructor in a union. This class always knowns the current
1.32 + // state of the variant and it cares for the proper construction
1.33 + // and destruction.
1.34 template <typename _First, typename _Second>
1.35 class BiVariant {
1.36 public:
1.37
1.38 - /// \brief The \c First type.
1.39 + // \brief The \c First type.
1.40 typedef _First First;
1.41 - /// \brief The \c Second type.
1.42 + // \brief The \c Second type.
1.43 typedef _Second Second;
1.44
1.45 - /// \brief Constructor
1.46 - ///
1.47 - /// This constructor initalizes to the default value of the \c First
1.48 - /// type.
1.49 + // \brief Constructor
1.50 + //
1.51 + // This constructor initalizes to the default value of the \c First
1.52 + // type.
1.53 BiVariant() {
1.54 flag = true;
1.55 new(reinterpret_cast<First*>(data)) First();
1.56 }
1.57
1.58 - /// \brief Constructor
1.59 - ///
1.60 - /// This constructor initalizes to the given value of the \c First
1.61 - /// type.
1.62 + // \brief Constructor
1.63 + //
1.64 + // This constructor initalizes to the given value of the \c First
1.65 + // type.
1.66 BiVariant(const First& f) {
1.67 flag = true;
1.68 new(reinterpret_cast<First*>(data)) First(f);
1.69 }
1.70
1.71 - /// \brief Constructor
1.72 - ///
1.73 - /// This constructor initalizes to the given value of the \c
1.74 - /// Second type.
1.75 + // \brief Constructor
1.76 + //
1.77 + // This constructor initalizes to the given value of the \c
1.78 + // Second type.
1.79 BiVariant(const Second& s) {
1.80 flag = false;
1.81 new(reinterpret_cast<Second*>(data)) Second(s);
1.82 }
1.83
1.84 - /// \brief Copy constructor
1.85 - ///
1.86 - /// Copy constructor
1.87 + // \brief Copy constructor
1.88 + //
1.89 + // Copy constructor
1.90 BiVariant(const BiVariant& bivariant) {
1.91 flag = bivariant.flag;
1.92 if (flag) {
1.93 @@ -92,17 +92,17 @@
1.94 }
1.95 }
1.96
1.97 - /// \brief Destrcutor
1.98 - ///
1.99 - /// Destructor
1.100 + // \brief Destrcutor
1.101 + //
1.102 + // Destructor
1.103 ~BiVariant() {
1.104 destroy();
1.105 }
1.106
1.107 - /// \brief Set to the default value of the \c First type.
1.108 - ///
1.109 - /// This function sets the variant to the default value of the \c
1.110 - /// First type.
1.111 + // \brief Set to the default value of the \c First type.
1.112 + //
1.113 + // This function sets the variant to the default value of the \c
1.114 + // First type.
1.115 BiVariant& setFirst() {
1.116 destroy();
1.117 flag = true;
1.118 @@ -110,10 +110,10 @@
1.119 return *this;
1.120 }
1.121
1.122 - /// \brief Set to the given value of the \c First type.
1.123 - ///
1.124 - /// This function sets the variant to the given value of the \c
1.125 - /// First type.
1.126 + // \brief Set to the given value of the \c First type.
1.127 + //
1.128 + // This function sets the variant to the given value of the \c
1.129 + // First type.
1.130 BiVariant& setFirst(const First& f) {
1.131 destroy();
1.132 flag = true;
1.133 @@ -121,10 +121,10 @@
1.134 return *this;
1.135 }
1.136
1.137 - /// \brief Set to the default value of the \c Second type.
1.138 - ///
1.139 - /// This function sets the variant to the default value of the \c
1.140 - /// Second type.
1.141 + // \brief Set to the default value of the \c Second type.
1.142 + //
1.143 + // This function sets the variant to the default value of the \c
1.144 + // Second type.
1.145 BiVariant& setSecond() {
1.146 destroy();
1.147 flag = false;
1.148 @@ -132,10 +132,10 @@
1.149 return *this;
1.150 }
1.151
1.152 - /// \brief Set to the given value of the \c Second type.
1.153 - ///
1.154 - /// This function sets the variant to the given value of the \c
1.155 - /// Second type.
1.156 + // \brief Set to the given value of the \c Second type.
1.157 + //
1.158 + // This function sets the variant to the given value of the \c
1.159 + // Second type.
1.160 BiVariant& setSecond(const Second& s) {
1.161 destroy();
1.162 flag = false;
1.163 @@ -143,17 +143,17 @@
1.164 return *this;
1.165 }
1.166
1.167 - /// \brief Operator form of the \c setFirst()
1.168 + // \brief Operator form of the \c setFirst()
1.169 BiVariant& operator=(const First& f) {
1.170 return setFirst(f);
1.171 }
1.172
1.173 - /// \brief Operator form of the \c setSecond()
1.174 + // \brief Operator form of the \c setSecond()
1.175 BiVariant& operator=(const Second& s) {
1.176 return setSecond(s);
1.177 }
1.178
1.179 - /// \brief Assign operator
1.180 + // \brief Assign operator
1.181 BiVariant& operator=(const BiVariant& bivariant) {
1.182 if (this == &bivariant) return *this;
1.183 destroy();
1.184 @@ -166,60 +166,60 @@
1.185 return *this;
1.186 }
1.187
1.188 - /// \brief Reference to the value
1.189 - ///
1.190 - /// Reference to the value of the \c First type.
1.191 - /// \pre The BiVariant should store value of \c First type.
1.192 + // \brief Reference to the value
1.193 + //
1.194 + // Reference to the value of the \c First type.
1.195 + // \pre The BiVariant should store value of \c First type.
1.196 First& first() {
1.197 LEMON_DEBUG(flag, "Variant wrong state");
1.198 - return *reinterpret_cast<First*>(data);
1.199 + return *reinterpret_cast<First*>(data);
1.200 }
1.201
1.202 - /// \brief Const reference to the value
1.203 - ///
1.204 - /// Const reference to the value of the \c First type.
1.205 - /// \pre The BiVariant should store value of \c First type.
1.206 - const First& first() const {
1.207 + // \brief Const reference to the value
1.208 + //
1.209 + // Const reference to the value of the \c First type.
1.210 + // \pre The BiVariant should store value of \c First type.
1.211 + const First& first() const {
1.212 LEMON_DEBUG(flag, "Variant wrong state");
1.213 - return *reinterpret_cast<const First*>(data);
1.214 + return *reinterpret_cast<const First*>(data);
1.215 }
1.216
1.217 - /// \brief Operator form of the \c first()
1.218 + // \brief Operator form of the \c first()
1.219 operator First&() { return first(); }
1.220 - /// \brief Operator form of the const \c first()
1.221 + // \brief Operator form of the const \c first()
1.222 operator const First&() const { return first(); }
1.223
1.224 - /// \brief Reference to the value
1.225 - ///
1.226 - /// Reference to the value of the \c Second type.
1.227 - /// \pre The BiVariant should store value of \c Second type.
1.228 - Second& second() {
1.229 + // \brief Reference to the value
1.230 + //
1.231 + // Reference to the value of the \c Second type.
1.232 + // \pre The BiVariant should store value of \c Second type.
1.233 + Second& second() {
1.234 LEMON_DEBUG(!flag, "Variant wrong state");
1.235 - return *reinterpret_cast<Second*>(data);
1.236 + return *reinterpret_cast<Second*>(data);
1.237 }
1.238
1.239 - /// \brief Const reference to the value
1.240 - ///
1.241 - /// Const reference to the value of the \c Second type.
1.242 - /// \pre The BiVariant should store value of \c Second type.
1.243 - const Second& second() const {
1.244 + // \brief Const reference to the value
1.245 + //
1.246 + // Const reference to the value of the \c Second type.
1.247 + // \pre The BiVariant should store value of \c Second type.
1.248 + const Second& second() const {
1.249 LEMON_DEBUG(!flag, "Variant wrong state");
1.250 - return *reinterpret_cast<const Second*>(data);
1.251 + return *reinterpret_cast<const Second*>(data);
1.252 }
1.253
1.254 - /// \brief Operator form of the \c second()
1.255 + // \brief Operator form of the \c second()
1.256 operator Second&() { return second(); }
1.257 - /// \brief Operator form of the const \c second()
1.258 + // \brief Operator form of the const \c second()
1.259 operator const Second&() const { return second(); }
1.260
1.261 - /// \brief %True when the variant is in the first state
1.262 - ///
1.263 - /// %True when the variant stores value of the \c First type.
1.264 + // \brief %True when the variant is in the first state
1.265 + //
1.266 + // %True when the variant stores value of the \c First type.
1.267 bool firstState() const { return flag; }
1.268
1.269 - /// \brief %True when the variant is in the second state
1.270 - ///
1.271 - /// %True when the variant stores value of the \c Second type.
1.272 + // \brief %True when the variant is in the second state
1.273 + //
1.274 + // %True when the variant stores value of the \c Second type.
1.275 bool secondState() const { return !flag; }
1.276
1.277 private:
1.278 @@ -289,40 +289,40 @@
1.279
1.280 }
1.281
1.282 - /// \brief Variant type
1.283 - ///
1.284 - /// Simple Variant type. The Variant type is a type safe union. The
1.285 - /// C++ has strong limitations for using unions, for example we
1.286 - /// cannot store type with non default constructor or destructor in
1.287 - /// a union. This class always knowns the current state of the
1.288 - /// variant and it cares for the proper construction and
1.289 - /// destruction.
1.290 - ///
1.291 - /// \param _num The number of the types which can be stored in the
1.292 - /// variant type.
1.293 - /// \param _TypeMap This class describes the types of the Variant. The
1.294 - /// _TypeMap::Map<index>::Type should be a valid type for each index
1.295 - /// in the range {0, 1, ..., _num - 1}. The \c VariantTypeMap is helper
1.296 - /// class to define such type mappings up to 10 types.
1.297 - ///
1.298 - /// And the usage of the class:
1.299 - ///\code
1.300 - /// typedef Variant<3, VariantTypeMap<int, std::string, double> > MyVariant;
1.301 - /// MyVariant var;
1.302 - /// var.set<0>(12);
1.303 - /// std::cout << var.get<0>() << std::endl;
1.304 - /// var.set<1>("alpha");
1.305 - /// std::cout << var.get<1>() << std::endl;
1.306 - /// var.set<2>(0.75);
1.307 - /// std::cout << var.get<2>() << std::endl;
1.308 - ///\endcode
1.309 - ///
1.310 - /// The result of course:
1.311 - ///\code
1.312 - /// 12
1.313 - /// alpha
1.314 - /// 0.75
1.315 - ///\endcode
1.316 + // \brief Variant type
1.317 + //
1.318 + // Simple Variant type. The Variant type is a type-safe union.
1.319 + // C++ has strong limitations for using unions, for example you
1.320 + // cannot store type with non-default constructor or destructor in
1.321 + // a union. This class always knowns the current state of the
1.322 + // variant and it cares for the proper construction and
1.323 + // destruction.
1.324 + //
1.325 + // \param _num The number of the types which can be stored in the
1.326 + // variant type.
1.327 + // \param _TypeMap This class describes the types of the Variant. The
1.328 + // _TypeMap::Map<index>::Type should be a valid type for each index
1.329 + // in the range {0, 1, ..., _num - 1}. The \c VariantTypeMap is helper
1.330 + // class to define such type mappings up to 10 types.
1.331 + //
1.332 + // And the usage of the class:
1.333 + //\code
1.334 + // typedef Variant<3, VariantTypeMap<int, std::string, double> > MyVariant;
1.335 + // MyVariant var;
1.336 + // var.set<0>(12);
1.337 + // std::cout << var.get<0>() << std::endl;
1.338 + // var.set<1>("alpha");
1.339 + // std::cout << var.get<1>() << std::endl;
1.340 + // var.set<2>(0.75);
1.341 + // std::cout << var.get<2>() << std::endl;
1.342 + //\endcode
1.343 + //
1.344 + // The result of course:
1.345 + //\code
1.346 + // 12
1.347 + // alpha
1.348 + // 0.75
1.349 + //\endcode
1.350 template <int _num, typename _TypeMap>
1.351 class Variant {
1.352 public:
1.353 @@ -331,10 +331,10 @@
1.354
1.355 typedef _TypeMap TypeMap;
1.356
1.357 - /// \brief Constructor
1.358 - ///
1.359 - /// This constructor initalizes to the default value of the \c type
1.360 - /// with 0 index.
1.361 + // \brief Constructor
1.362 + //
1.363 + // This constructor initalizes to the default value of the \c type
1.364 + // with 0 index.
1.365 Variant() {
1.366 flag = 0;
1.367 new(reinterpret_cast<typename TypeMap::template Map<0>::Type*>(data))
1.368 @@ -342,17 +342,17 @@
1.369 }
1.370
1.371
1.372 - /// \brief Copy constructor
1.373 - ///
1.374 - /// Copy constructor
1.375 + // \brief Copy constructor
1.376 + //
1.377 + // Copy constructor
1.378 Variant(const Variant& variant) {
1.379 flag = variant.flag;
1.380 _variant_bits::Memory<num - 1, TypeMap>::copy(flag, data, variant.data);
1.381 }
1.382
1.383 - /// \brief Assign operator
1.384 - ///
1.385 - /// Assign operator
1.386 + // \brief Assign operator
1.387 + //
1.388 + // Assign operator
1.389 Variant& operator=(const Variant& variant) {
1.390 if (this == &variant) return *this;
1.391 _variant_bits::Memory<num - 1, TypeMap>::
1.392 @@ -363,17 +363,17 @@
1.393 return *this;
1.394 }
1.395
1.396 - /// \brief Destrcutor
1.397 - ///
1.398 - /// Destructor
1.399 + // \brief Destrcutor
1.400 + //
1.401 + // Destructor
1.402 ~Variant() {
1.403 _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
1.404 }
1.405
1.406 - /// \brief Set to the default value of the type with \c _idx index.
1.407 - ///
1.408 - /// This function sets the variant to the default value of the
1.409 - /// type with \c _idx index.
1.410 + // \brief Set to the default value of the type with \c _idx index.
1.411 + //
1.412 + // This function sets the variant to the default value of the
1.413 + // type with \c _idx index.
1.414 template <int _idx>
1.415 Variant& set() {
1.416 _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
1.417 @@ -383,10 +383,10 @@
1.418 return *this;
1.419 }
1.420
1.421 - /// \brief Set to the given value of the type with \c _idx index.
1.422 - ///
1.423 - /// This function sets the variant to the given value of the type
1.424 - /// with \c _idx index.
1.425 + // \brief Set to the given value of the type with \c _idx index.
1.426 + //
1.427 + // This function sets the variant to the given value of the type
1.428 + // with \c _idx index.
1.429 template <int _idx>
1.430 Variant& set(const typename _TypeMap::template Map<_idx>::Type& init) {
1.431 _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
1.432 @@ -396,9 +396,9 @@
1.433 return *this;
1.434 }
1.435
1.436 - /// \brief Gets the current value of the type with \c _idx index.
1.437 - ///
1.438 - /// Gets the current value of the type with \c _idx index.
1.439 + // \brief Gets the current value of the type with \c _idx index.
1.440 + //
1.441 + // Gets the current value of the type with \c _idx index.
1.442 template <int _idx>
1.443 const typename TypeMap::template Map<_idx>::Type& get() const {
1.444 LEMON_DEBUG(_idx == flag, "Variant wrong index");
1.445 @@ -406,9 +406,9 @@
1.446 template Map<_idx>::Type*>(data);
1.447 }
1.448
1.449 - /// \brief Gets the current value of the type with \c _idx index.
1.450 - ///
1.451 - /// Gets the current value of the type with \c _idx index.
1.452 + // \brief Gets the current value of the type with \c _idx index.
1.453 + //
1.454 + // Gets the current value of the type with \c _idx index.
1.455 template <int _idx>
1.456 typename _TypeMap::template Map<_idx>::Type& get() {
1.457 LEMON_DEBUG(_idx == flag, "Variant wrong index");
1.458 @@ -416,9 +416,9 @@
1.459 (data);
1.460 }
1.461
1.462 - /// \brief Returns the current state of the variant.
1.463 - ///
1.464 - /// Returns the current state of the variant.
1.465 + // \brief Returns the current state of the variant.
1.466 + //
1.467 + // Returns the current state of the variant.
1.468 int state() const {
1.469 return flag;
1.470 }
1.471 @@ -450,7 +450,7 @@
1.472 };
1.473
1.474 template <int _idx, typename _T0, typename _T1, typename _T2,
1.475 - typename _T3, typename _T5, typename _T4, typename _T6,
1.476 + typename _T3, typename _T4, typename _T5, typename _T6,
1.477 typename _T7, typename _T8, typename _T9>
1.478 struct Mapper {
1.479 typedef List L10;
1.480 @@ -469,15 +469,15 @@
1.481
1.482 }
1.483
1.484 - /// \brief Helper class for Variant
1.485 - ///
1.486 - /// Helper class to define type mappings for Variant. This class
1.487 - /// converts the template parameters to be mappable by integer.
1.488 - /// \see Variant
1.489 + // \brief Helper class for Variant
1.490 + //
1.491 + // Helper class to define type mappings for Variant. This class
1.492 + // converts the template parameters to be mappable by integer.
1.493 + // \see Variant
1.494 template <
1.495 typename _T0,
1.496 typename _T1 = void, typename _T2 = void, typename _T3 = void,
1.497 - typename _T5 = void, typename _T4 = void, typename _T6 = void,
1.498 + typename _T4 = void, typename _T5 = void, typename _T6 = void,
1.499 typename _T7 = void, typename _T8 = void, typename _T9 = void>
1.500 struct VariantTypeMap {
1.501 template <int _idx>