gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Bug fix in variant.h (#196)
0 1 0
default
1 file changed with 2 insertions and 2 deletions:
↑ Collapse diff ↑
Ignore white space 384 line context
... ...
@@ -261,234 +261,234 @@
261 261
      }
262 262

	
263 263
    };
264 264

	
265 265
    template <typename _TypeMap>
266 266
    struct Memory<-1, _TypeMap> {
267 267

	
268 268
      static void destroy(int, char*) {
269 269
        LEMON_DEBUG(false, "Variant wrong index.");
270 270
      }
271 271

	
272 272
      static void copy(int, char*, const char*) {
273 273
        LEMON_DEBUG(false, "Variant wrong index.");
274 274
      }
275 275
    };
276 276

	
277 277
    template <int _idx, typename _TypeMap>
278 278
    struct Size {
279 279
      static const int value =
280 280
      CTMax<sizeof(typename _TypeMap::template Map<_idx>::Type),
281 281
            Size<_idx - 1, _TypeMap>::value>::value;
282 282
    };
283 283

	
284 284
    template <typename _TypeMap>
285 285
    struct Size<0, _TypeMap> {
286 286
      static const int value =
287 287
      sizeof(typename _TypeMap::template Map<0>::Type);
288 288
    };
289 289

	
290 290
  }
291 291

	
292 292
  /// \brief Variant type
293 293
  ///
294 294
  /// Simple Variant type. The Variant type is a type safe union. The
295 295
  /// C++ has strong limitations for using unions, for example we
296 296
  /// cannot store type with non default constructor or destructor in
297 297
  /// a union. This class always knowns the current state of the
298 298
  /// variant and it cares for the proper construction and
299 299
  /// destruction.
300 300
  ///
301 301
  /// \param _num The number of the types which can be stored in the
302 302
  /// variant type.
303 303
  /// \param _TypeMap This class describes the types of the Variant. The
304 304
  /// _TypeMap::Map<index>::Type should be a valid type for each index
305 305
  /// in the range {0, 1, ..., _num - 1}. The \c VariantTypeMap is helper
306 306
  /// class to define such type mappings up to 10 types.
307 307
  ///
308 308
  /// And the usage of the class:
309 309
  ///\code
310 310
  /// typedef Variant<3, VariantTypeMap<int, std::string, double> > MyVariant;
311 311
  /// MyVariant var;
312 312
  /// var.set<0>(12);
313 313
  /// std::cout << var.get<0>() << std::endl;
314 314
  /// var.set<1>("alpha");
315 315
  /// std::cout << var.get<1>() << std::endl;
316 316
  /// var.set<2>(0.75);
317 317
  /// std::cout << var.get<2>() << std::endl;
318 318
  ///\endcode
319 319
  ///
320 320
  /// The result of course:
321 321
  ///\code
322 322
  /// 12
323 323
  /// alpha
324 324
  /// 0.75
325 325
  ///\endcode
326 326
  template <int _num, typename _TypeMap>
327 327
  class Variant {
328 328
  public:
329 329

	
330 330
    static const int num = _num;
331 331

	
332 332
    typedef _TypeMap TypeMap;
333 333

	
334 334
    /// \brief Constructor
335 335
    ///
336 336
    /// This constructor initalizes to the default value of the \c type
337 337
    /// with 0 index.
338 338
    Variant() {
339 339
      flag = 0;
340 340
      new(reinterpret_cast<typename TypeMap::template Map<0>::Type*>(data))
341 341
        typename TypeMap::template Map<0>::Type();
342 342
    }
343 343

	
344 344

	
345 345
    /// \brief Copy constructor
346 346
    ///
347 347
    /// Copy constructor
348 348
    Variant(const Variant& variant) {
349 349
      flag = variant.flag;
350 350
      _variant_bits::Memory<num - 1, TypeMap>::copy(flag, data, variant.data);
351 351
    }
352 352

	
353 353
    /// \brief Assign operator
354 354
    ///
355 355
    /// Assign operator
356 356
    Variant& operator=(const Variant& variant) {
357 357
      if (this == &variant) return *this;
358 358
      _variant_bits::Memory<num - 1, TypeMap>::
359 359
        destroy(flag, data);
360 360
      flag = variant.flag;
361 361
      _variant_bits::Memory<num - 1, TypeMap>::
362 362
        copy(flag, data, variant.data);
363 363
      return *this;
364 364
    }
365 365

	
366 366
    /// \brief Destrcutor
367 367
    ///
368 368
    /// Destructor
369 369
    ~Variant() {
370 370
      _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
371 371
    }
372 372

	
373 373
    /// \brief Set to the default value of the type with \c _idx index.
374 374
    ///
375 375
    /// This function sets the variant to the default value of the
376 376
    /// type with \c _idx index.
377 377
    template <int _idx>
378 378
    Variant& set() {
379 379
      _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
380 380
      flag = _idx;
381 381
      new(reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>(data))
382 382
        typename TypeMap::template Map<_idx>::Type();
383 383
      return *this;
384 384
    }
385 385

	
386 386
    /// \brief Set to the given value of the type with \c _idx index.
387 387
    ///
388 388
    /// This function sets the variant to the given value of the type
389 389
    /// with \c _idx index.
390 390
    template <int _idx>
391 391
    Variant& set(const typename _TypeMap::template Map<_idx>::Type& init) {
392 392
      _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data);
393 393
      flag = _idx;
394 394
      new(reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>(data))
395 395
        typename TypeMap::template Map<_idx>::Type(init);
396 396
      return *this;
397 397
    }
398 398

	
399 399
    /// \brief Gets the current value of the type with \c _idx index.
400 400
    ///
401 401
    /// Gets the current value of the type with \c _idx index.
402 402
    template <int _idx>
403 403
    const typename TypeMap::template Map<_idx>::Type& get() const {
404 404
      LEMON_DEBUG(_idx == flag, "Variant wrong index");
405 405
      return *reinterpret_cast<const typename TypeMap::
406 406
        template Map<_idx>::Type*>(data);
407 407
    }
408 408

	
409 409
    /// \brief Gets the current value of the type with \c _idx index.
410 410
    ///
411 411
    /// Gets the current value of the type with \c _idx index.
412 412
    template <int _idx>
413 413
    typename _TypeMap::template Map<_idx>::Type& get() {
414 414
      LEMON_DEBUG(_idx == flag, "Variant wrong index");
415 415
      return *reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>
416 416
        (data);
417 417
    }
418 418

	
419 419
    /// \brief Returns the current state of the variant.
420 420
    ///
421 421
    /// Returns the current state of the variant.
422 422
    int state() const {
423 423
      return flag;
424 424
    }
425 425

	
426 426
  private:
427 427

	
428 428
    char data[_variant_bits::Size<num - 1, TypeMap>::value];
429 429
    int flag;
430 430
  };
431 431

	
432 432
  namespace _variant_bits {
433 433

	
434 434
    template <int _index, typename _List>
435 435
    struct Get {
436 436
      typedef typename Get<_index - 1, typename _List::Next>::Type Type;
437 437
    };
438 438

	
439 439
    template <typename _List>
440 440
    struct Get<0, _List> {
441 441
      typedef typename _List::Type Type;
442 442
    };
443 443

	
444 444
    struct List {};
445 445

	
446 446
    template <typename _Type, typename _List>
447 447
    struct Insert {
448 448
      typedef _List Next;
449 449
      typedef _Type Type;
450 450
    };
451 451

	
452 452
    template <int _idx, typename _T0, typename _T1, typename _T2,
453
              typename _T3, typename _T5, typename _T4, typename _T6,
453
              typename _T3, typename _T4, typename _T5, typename _T6,
454 454
              typename _T7, typename _T8, typename _T9>
455 455
    struct Mapper {
456 456
      typedef List L10;
457 457
      typedef Insert<_T9, L10> L9;
458 458
      typedef Insert<_T8, L9> L8;
459 459
      typedef Insert<_T7, L8> L7;
460 460
      typedef Insert<_T6, L7> L6;
461 461
      typedef Insert<_T5, L6> L5;
462 462
      typedef Insert<_T4, L5> L4;
463 463
      typedef Insert<_T3, L4> L3;
464 464
      typedef Insert<_T2, L3> L2;
465 465
      typedef Insert<_T1, L2> L1;
466 466
      typedef Insert<_T0, L1> L0;
467 467
      typedef typename Get<_idx, L0>::Type Type;
468 468
    };
469 469

	
470 470
  }
471 471

	
472 472
  /// \brief Helper class for Variant
473 473
  ///
474 474
  /// Helper class to define type mappings for Variant. This class
475 475
  /// converts the template parameters to be mappable by integer.
476 476
  /// \see Variant
477 477
  template <
478 478
    typename _T0,
479 479
    typename _T1 = void, typename _T2 = void, typename _T3 = void,
480
    typename _T5 = void, typename _T4 = void, typename _T6 = void,
480
    typename _T4 = void, typename _T5 = void, typename _T6 = void,
481 481
    typename _T7 = void, typename _T8 = void, typename _T9 = void>
482 482
  struct VariantTypeMap {
483 483
    template <int _idx>
484 484
    struct Map {
485 485
      typedef typename _variant_bits::
486 486
      Mapper<_idx, _T0, _T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9>::Type
487 487
      Type;
488 488
    };
489 489
  };
490 490

	
491 491
}
492 492

	
493 493

	
494 494
#endif
0 comments (0 inline)