Changeset 416:76287c8caa26 in lemon-1.2 for lemon/bits/variant.h
- Timestamp:
- 11/30/08 19:18:32 (16 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/bits/variant.h
r414 r416 1 /* -*- C++-*-1 /* -*- mode: C++; indent-tabs-mode: nil; -*- 2 2 * 3 * This file is a part of LEMON, a generic C++ optimization library 3 * This file is a part of LEMON, a generic C++ optimization library. 4 4 * 5 5 * Copyright (C) 2003-2008 … … 28 28 29 29 namespace _variant_bits { 30 30 31 31 template <int left, int right> 32 32 struct CTMax { … … 87 87 flag = bivariant.flag; 88 88 if (flag) { 89 new(reinterpret_cast<First*>(data)) First(bivariant.first()); 89 new(reinterpret_cast<First*>(data)) First(bivariant.first()); 90 90 } else { 91 new(reinterpret_cast<Second*>(data)) Second(bivariant.second()); 91 new(reinterpret_cast<Second*>(data)) Second(bivariant.second()); 92 92 } 93 93 } … … 107 107 destroy(); 108 108 flag = true; 109 new(reinterpret_cast<First*>(data)) First(); 109 new(reinterpret_cast<First*>(data)) First(); 110 110 return *this; 111 111 } … … 118 118 destroy(); 119 119 flag = true; 120 new(reinterpret_cast<First*>(data)) First(f); 120 new(reinterpret_cast<First*>(data)) First(f); 121 121 return *this; 122 122 } … … 129 129 destroy(); 130 130 flag = false; 131 new(reinterpret_cast<Second*>(data)) Second(); 131 new(reinterpret_cast<Second*>(data)) Second(); 132 132 return *this; 133 133 } … … 140 140 destroy(); 141 141 flag = false; 142 new(reinterpret_cast<Second*>(data)) Second(s); 142 new(reinterpret_cast<Second*>(data)) Second(s); 143 143 return *this; 144 144 } … … 160 160 flag = bivariant.flag; 161 161 if (flag) { 162 new(reinterpret_cast<First*>(data)) First(bivariant.first()); 162 new(reinterpret_cast<First*>(data)) First(bivariant.first()); 163 163 } else { 164 new(reinterpret_cast<Second*>(data)) Second(bivariant.second()); 164 new(reinterpret_cast<Second*>(data)) Second(bivariant.second()); 165 165 } 166 166 return *this; … … 232 232 } 233 233 } 234 234 235 235 char data[_variant_bits::CTMax<sizeof(First), sizeof(Second)>::value]; 236 236 bool flag; … … 238 238 239 239 namespace _variant_bits { 240 240 241 241 template <int _idx, typename _TypeMap> 242 242 struct Memory { … … 277 277 template <int _idx, typename _TypeMap> 278 278 struct Size { 279 static const int value = 280 CTMax<sizeof(typename _TypeMap::template Map<_idx>::Type), 279 static const int value = 280 CTMax<sizeof(typename _TypeMap::template Map<_idx>::Type), 281 281 Size<_idx - 1, _TypeMap>::value>::value; 282 282 }; … … 284 284 template <typename _TypeMap> 285 285 struct Size<0, _TypeMap> { 286 static const int value = 286 static const int value = 287 287 sizeof(typename _TypeMap::template Map<0>::Type); 288 288 }; … … 302 302 /// variant type. 303 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 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. … … 338 338 Variant() { 339 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 341 typename TypeMap::template Map<0>::Type(); 342 342 } … … 379 379 _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data); 380 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 382 typename TypeMap::template Map<_idx>::Type(); 383 383 return *this; … … 392 392 _variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data); 393 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 395 typename TypeMap::template Map<_idx>::Type(init); 396 396 return *this; … … 404 404 LEMON_DEBUG(_idx == flag, "Variant wrong index"); 405 405 return *reinterpret_cast<const typename TypeMap:: 406 template Map<_idx>::Type*>(data); 406 template Map<_idx>::Type*>(data); 407 407 } 408 408 … … 414 414 LEMON_DEBUG(_idx == flag, "Variant wrong index"); 415 415 return *reinterpret_cast<typename TypeMap::template Map<_idx>::Type*> 416 (data); 416 (data); 417 417 } 418 418 … … 425 425 426 426 private: 427 427 428 428 char data[_variant_bits::Size<num - 1, TypeMap>::value]; 429 429 int flag; … … 443 443 444 444 struct List {}; 445 445 446 446 template <typename _Type, typename _List> 447 447 struct Insert { … … 450 450 }; 451 451 452 template <int _idx, typename _T0, typename _T1, typename _T2, 452 template <int _idx, typename _T0, typename _T1, typename _T2, 453 453 typename _T3, typename _T5, typename _T4, typename _T6, 454 454 typename _T7, typename _T8, typename _T9> … … 467 467 typedef typename Get<_idx, L0>::Type Type; 468 468 }; 469 469 470 470 } 471 471 … … 476 476 /// \see Variant 477 477 template < 478 typename _T0, 478 typename _T0, 479 479 typename _T1 = void, typename _T2 = void, typename _T3 = void, 480 480 typename _T5 = void, typename _T4 = void, typename _T6 = void, … … 488 488 }; 489 489 }; 490 490 491 491 } 492 492
Note: See TracChangeset
for help on using the changeset viewer.