/usr/include/dcmtk/ofstd/ofvriant.h is in libdcmtk-dev 3.6.2-3build3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 | /*
*
* Copyright (C) 2016-2017, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
*
* OFFIS e.V.
* R&D Division Health
* Escherweg 2
* D-26121 Oldenburg, Germany
*
*
* Module: ofstd
*
* Author: Jan Schlamelcher
*
* Purpose: Implementing tagged unions similar to C++17's std::variant.
*
*/
#ifndef OFVARIANT_H
#define OFVARIANT_H
#include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */
/** @file
* Declares OFvariant and related functionality.
* @defgroup ofvisit_variant OFvisit – OFvariant
* Apply a visitor to an OFvariant object.
* @see @ref ofget_variant "OFget" – @copybrief ofget_variant
* @defgroup ofget_variant OFget – OFvariant
* Get a pointer to the value stored in an OFvariant holding the selected alternative.
* @see @ref ofvisit_variant "OFvisit" – @copybrief ofvisit_variant
*/
#ifdef HAVE_CXX11
#include <type_traits>
#include <cassert>
#include <limits>
template<std::size_t,typename... Alternatives>
class OFvariant_traits
{
public:
using is_constructible = std::false_type;
static constexpr inline std::size_t alignment() { return 1; }
static constexpr inline std::size_t size() { return 0; }
static void index_of();
};
template<std::size_t Index,typename Alternative0,typename... Alternatives>
class OFvariant_traits<Index,Alternative0,Alternatives...>
: OFvariant_traits<Index+1,Alternatives...>
{
public:
using OFvariant_traits<Index+1,Alternatives...>::index_of;
using first_alternative = Alternative0;
static constexpr inline std::size_t alignment()
{
return alignof(aligned);
}
static constexpr inline std::size_t size()
{
return sizeof( first_alternative ) > OFvariant_traits<Index+1,Alternatives...>::size()
?
sizeof( first_alternative )
:
OFvariant_traits<Index+1,Alternatives...>::size()
;
}
static std::integral_constant<std::size_t,Index> index_of( first_alternative );
private:
struct aligned { alignas(OFvariant_traits<Index+1,Alternatives...>::alignment()) char c; first_alternative a; };
};
template<std::size_t AlternativeCount,typename... IndexAlternatives>
struct OFvariant_select_index_type {};
template<std::size_t AlternativeCount,typename IndexAlternative0,typename... IndexAlternatives>
struct OFvariant_select_index_type<AlternativeCount,IndexAlternative0,IndexAlternatives...>
{
using type = typename std::conditional
<
AlternativeCount <= std::numeric_limits<IndexAlternative0>::max(),
std::enable_if<true,IndexAlternative0>,
OFvariant_select_index_type<AlternativeCount,IndexAlternatives...>
>::type::type;
};
template<typename...>
class OFvariant;
template<std::size_t Index,typename Variant>
struct OFvariant_alternative;
template<std::size_t Index,typename Alternative0,typename... Alternatives>
struct OFvariant_alternative<Index,OFvariant<Alternative0,Alternatives...>>
: OFvariant_alternative<Index - 1,OFvariant<Alternatives...>> {};
template<typename Alternative0,typename... Alternatives>
struct OFvariant_alternative<0,OFvariant<Alternative0,Alternatives...>>
{
using type = Alternative0;
};
template<typename... Alternatives>
class OFvariant
{
using traits = OFvariant_traits<0,Alternatives...>;
using index_type = typename OFvariant_select_index_type<sizeof...(Alternatives),std::uint8_t,std::uint16_t,std::uint32_t,std::uint64_t>::type;
template<typename T>
using index_of = decltype(traits::index_of(std::declval<T>()));
public:
using variant = OFvariant;
OFvariant()
: m_Content()
, m_Index( 0 )
{
new (m_Content) typename traits::first_alternative;
}
template<typename T,index_type Index = index_of<T>()>
OFvariant( T&& t )
: m_Content()
, m_Index( Index )
{
new (m_Content) typename OFvariant_alternative<Index,OFvariant>::type( std::forward<T>( t ) );
}
OFvariant( OFvariant& rhs )
: OFvariant( const_cast<const OFvariant&>( rhs ) )
{
}
OFvariant( const OFvariant& rhs )
: m_Content()
, m_Index( rhs.index() )
{
copy_construct( rhs );
}
OFvariant( OFvariant&& rhs )
: m_Content()
, m_Index( rhs.index() )
{
move_construct( std::move( rhs ) );
}
template<typename T,index_type Index = index_of<T>()>
OFvariant& operator=( T&& t )
{
if( m_Index != Index )
{
destroy();
m_Index = Index;
new (m_Content) typename OFvariant_alternative<Index,OFvariant>::type( std::forward<T>( t ) );
}
else
{
*reinterpret_cast<typename OFvariant_alternative<Index,OFvariant>::type*>( m_Content ) = std::forward<T>( t );
}
return *this;
}
OFvariant& operator=( OFvariant& rhs )
{
return *this = const_cast<const OFvariant&>( rhs );
}
OFvariant& operator=( const OFvariant& rhs )
{
if( this != &rhs )
{
if( m_Index != rhs.m_Index )
{
destroy();
m_Index = rhs.m_Index;
copy_construct( rhs );
}
else
{
using functor = void(OFvariant::*)(const OFvariant&);
static const functor assignment[] =
{
&OFvariant::template copy_assign_alternative<Alternatives>...
};
(this->*assignment[m_Index])( rhs );
}
}
return *this;
}
OFvariant& operator=( OFvariant&& rhs )
{
if( this != &rhs )
{
if( m_Index != rhs.m_Index )
{
destroy();
m_Index = rhs.m_Index;
move_construct( std::move( rhs ) );
}
else
{
using functor = void(OFvariant::*)(OFvariant&&);
static const functor assignment[] =
{
&OFvariant::template move_assign_alternative<Alternatives>...
};
(this->*assignment[m_Index])( std::move( rhs ) );
}
}
return *this;
}
~OFvariant()
{
destroy();
}
std::size_t index() const
{
return m_Index;
}
private:
template<typename T,typename... Xs>
friend T* OFget( OFvariant<Xs...>* );
template<typename T,typename... Xs>
friend const T* OFget( const OFvariant<Xs...>* );
template<typename ReturnType,typename FN,typename... Xs>
friend ReturnType OFvisit( FN&&, OFvariant<Xs...>& );
template<typename ReturnType,typename FN,typename... Xs>
friend ReturnType OFvisit( FN&&, const OFvariant<Xs...>& );
template<typename Alternative>
void copy_construct_alternative( const OFvariant& rhs )
{
new (m_Content) Alternative( *reinterpret_cast<const Alternative*>( rhs.m_Content ) );
}
template<typename Alternative>
void move_construct_alternative( OFvariant&& rhs )
{
new (m_Content) Alternative( std::move( *reinterpret_cast<Alternative*>( rhs.m_Content ) ) );
}
template<typename Alternative>
void copy_assign_alternative( const OFvariant& rhs )
{
*reinterpret_cast<Alternative*>( m_Content ) = *reinterpret_cast<const Alternative*>( rhs.m_Content );
}
template<typename Alternative>
void move_assign_alternative( OFvariant&& rhs )
{
*reinterpret_cast<Alternative*>( m_Content ) = std::move( *reinterpret_cast<Alternative*>( rhs.m_Content ) );
}
template<typename Alternative>
void destructor()
{
reinterpret_cast<Alternative*>( m_Content )->~Alternative();
}
template<typename ReturnType,typename Alternative,typename FN>
ReturnType visit_alternative( FN&& fn )
{
return fn( *reinterpret_cast<Alternative*>( m_Content ) );
}
template<typename ReturnType,typename Alternative,typename FN>
ReturnType const_visit_alternative( FN&& fn ) const
{
return fn( *reinterpret_cast<const Alternative*>( m_Content ) );
}
void copy_construct( const OFvariant& rhs )
{
using functor = void(OFvariant::*)(const OFvariant&);
static const functor constructor[] =
{
&OFvariant::template copy_construct_alternative<Alternatives>...
};
assert( m_Index < sizeof...(Alternatives) );
(this->*constructor[m_Index])( rhs );
}
void move_construct( OFvariant&& rhs )
{
using functor = void(OFvariant::*)(OFvariant&&);
static const functor constructor[] =
{
&OFvariant::template move_construct_alternative<Alternatives>...
};
assert( m_Index < sizeof...(Alternatives) );
(this->*constructor[m_Index])( std::move( rhs ) );
}
void destroy()
{
using functor = void(OFvariant::*)();
static const functor destructor[] =
{
&OFvariant::template destructor<Alternatives>...
};
assert( m_Index < sizeof...(Alternatives) );
(this->*destructor[m_Index])();
}
alignas(traits::alignment()) std::uint8_t m_Content[traits::size()];
index_type m_Index;
};
template<std::size_t Index,typename X,typename T0,typename... Tn>
struct OFvariant_index_of_type
: OFvariant_index_of_type<Index + 1,X,Tn...> {};
template<std::size_t Index,typename X,typename... Tn>
struct OFvariant_index_of_type<Index,X,X,Tn...>
: std::integral_constant<std::size_t,Index> {};
template<typename T,typename... Alternatives>
constexpr bool OFholds_alternative( const OFvariant<Alternatives...>& v )
{
return v.index() == OFvariant_index_of_type<0,T,Alternatives...>::value;
}
template<typename T,typename... Alternatives>
T* OFget( OFvariant<Alternatives...>* v )
{
if( OFholds_alternative<T>( *v ) )
return reinterpret_cast<T*>( v->m_Content );
return nullptr;
}
template<typename T,typename... Alternatives>
const T* OFget( const OFvariant<Alternatives...>* v )
{
if( OFholds_alternative<T>( *v ) )
return reinterpret_cast<const T*>( v->m_Content );
return nullptr;
}
template<typename ReturnType,typename FN,typename... Alternatives>
ReturnType OFvisit( FN&& fn, OFvariant<Alternatives...>& v )
{
using functor = ReturnType(OFvariant<Alternatives...>::*)(FN&&);
static const functor visit[] =
{
&OFvariant<Alternatives...>::template visit_alternative<ReturnType,Alternatives,FN>...
};
return (v.*visit[v.index()])( std::forward<FN>( fn ) );
}
template<typename ReturnType,typename FN,typename... Alternatives>
ReturnType OFvisit( FN&& fn, const OFvariant<Alternatives...>& v )
{
using functor = ReturnType(OFvariant<Alternatives...>::*)(FN&&) const;
static const functor visit[] =
{
&OFvariant<Alternatives...>::template const_visit_alternative<ReturnType,Alternatives,FN>...
};
return (v.*visit[v.index()])( std::forward<FN>( fn ) );
}
#elif !defined(DOXYGEN) // fallback implementation
// Include the actual implementation (that emulates variadic templates)
#include "dcmtk/ofstd/variadic/variant.h"
#else // NOT C++11 && NOT DOXYGEN
/** A class template that represents a type-safe union.
* <b><em>#include</em> "dcmtk/ofstd/ofvriant.h"</b> for using this class<br>
* @headerfile ofvriant.h "dcmtk/ofstd/ofvriant.h"
* @tparam Alternatives a set of types that may be stored in this variant. All types must be (possibly
* cv-qualified) object types.
* @details
* OFvariant is a custom implementation of a subset of C++17's std::variant,
* see http://en.cppreference.com/w/cpp/utility/variant for a description of std::variant.
* An instance of OFvariant at any given time holds a value of one of its alternative types.
* As with unions, if a variant holds a value of some object type T, the object representation of T is
* allocated directly within the object representation of the variant itself if possible.
* @note If no suitable alignment specifiers were available for the target platform, OFvariant will
* use a fallback implementation that stores the alternative on the heap – as opposite to std::variant.
* @details
* The preferred way to access an OFvariant object is visitation utilizing @ref ofvisit_variant "OFvisit".
* If a certain alternative is expected to be held by the variant, @ref ofget_variant "OFget" may be used to
* access it directly.
* @see @ref ofvisit_variant "OFvisit" – @copybrief ofvisit_variant
* @see @ref ofget_variant "OFget" – @copybrief ofget_variant
* @see OFmonostate – @copybrief OFmonostate
* @see @ref OFin_place_helpers "OFin_place" – @copydoc OFin_place_helpers_brief
*/
template<typename... Alternatives>
class OFvariant
{
public:
/** Constructs a variant holding a default constructed value of the first alternative.
* @pre The first alternative must be default constructible.
* @see OFmonostate – @copybrief OFmonostate
*/
OFvariant();
/** Copy constructs a variant holding a copy of the value rhs holds.
* @param rhs a const reference to another object of equal type.
* @pre All alternatives must be copy constructible.
*/
OFvariant( const OFvariant& rhs );
/** Move constructs a variant by moving the value rhs holds.
* @param rhs an rvalue reference to another object of equal type.
* @pre All alternatives must be move constructible.
* @note This constructor is currently only available if C++11 support was enabled.
*/
OFvariant( OFvariant&& rhs );
/** Constructs a variant holding the alternative that most closely matches the given
* argument.
* @tparam T the type of the argument, will be deduced automatically.
* @param t an object of type `T` that will be converted to one of the alternatives.
* @precondition There must be at least one alternative that can be constructed from
* the given parameter `t` and there must be exactly one such alternative that
* takes precedence over the others.
* @attention t will be perfectly forwarded if C++11 support is available, i.e. the
* alternative may be move constructed from `t` if possible. Support for perfect
* forwarding is NOT available without C++11 support, therefore the alternative
* will be copy constructed in this case, this means: <b>the selected alternative
* must be copy constructible if pre C++11 compilers shall be supported</b>.
* @details
* <h3>Usage Example:</h3>
* @code{.cpp}
* OFvariant<int,float,long>( 3 ); // OK
* OFvariant<int,int>( 3 ); // ill formed, both alternatives take equal precedence
* OFvariant<OFString,OFBool>( "abc" ); // OK, but chooses OFBool!
* @endcode
*/
template<typename T>
OFvariant( T t );
/** Destroys the value that the variant currently holds.
*/
~OFvariant();
/** Copy assigns the value rhs holds to *this.
* @param rhs a const reference to another object of equal type.
* @pre all alternatives must be copy constructible and copy assignable.
* @return `*this`
* @post
* @li if `*this` and `rhs` hold the same alternative, the value contained in `rhs`
* is copy assigned to the value contained in `*this`.
* @li if `*this` and `rhs` hold different alternatives, the value contained in `*this`
* is destroyed and a new one is copy constructed from the value contained in `rhs`.
*/
OFvariant& operator=( const OFvariant& rhs );
/** Move assigns the value rhs holds to *this.
* @param rhs an rvalue reference to another object of equal type.
* @pre all alternatives must be move constructible and move assignable.
* @return `*this`
* @post
* @li if `*this` and `rhs` hold the same alternative, the value contained in `rhs`
* is move assigned to the value contained in `*this`.
* @li if `*this` and `rhs` hold different alternatives, the value contained in `*this`
* is destroyed and a new one is move constructed from the value contained in `rhs`.
* @note This constructor is currently only available if C++11 support was enabled.
*/
OFvariant& operator=( OFvariant&& rhs );
/** Converts the given argument to one of the alternatives and assigns it to *this.
* @tparam T the type of the argument, will be deduced automatically.
* @param t an object of type `T` that will be converted to one of the alternatives
* for assignment.
* @return `*this`
* @pre There must be at least one alternative that can be constructed from
* the given parameter `t` and there must be exactly one such alternative that
* takes precedence over the others.
* @attention `t` will be perfectly forwarded if C++11 support is available, i.e. the
* alternative may be move constructed from t if possible. Support for perfect
* forwarding is NOT available without C++11 support, therefore the alternative
* will be copy constructed in this case, this means: <b>the selected alternative
* must be copy constructible if pre C++11 compilers shall be supported</b>.
* @details
* <h3>Usage Example:</h3>
* @code{.cpp}
* OFvariant<int,float,long> v1;
* v1 = 3 // OK
* OFvariant<int,int> v2;
* v2 = 3 // ill formed, both alternatives take equal precedence
* OFvariant<OFString,OFBool> v3;
* v3 = "abc"; // OK, but chooses OFBool!
* @endcode
*/
template<typename T>
OFvariant& operator=( T t );
/** Get the index of alternative that is currently being held.
* @return the zero based index of that alternative that is currently being held by
* `*this`, i.e. `0` for the first alternative, `1` for the second, etc.
*/
size_t index() const;
};
/** Try to get a pointer to the given alternative from an OFvariant object.
* @ingroup ofget_variant
* @relates OFvariant
* @tparam Alternative the chosen alternative that shall be accessed.
* @tparam Alternatives the alternatives the given variant could hold, will be deduced
* automatically.
* @param v a reference to an OFvariant object potentially holding `Alternative`.
* @return the address of the contained value of type `Alternative` if such a value is
* contained. `OFnullptr` otherwise.
* @details
* <h3>Usage Example:</h3>
* @code{.cpp}
* OFvariant<int,OFString> v;
* // ... some value is assigned to v ...
* if( int* pI = OFget<int>( v ) )
* {
* COUT << "Yes, it really is an int with the value " << *pI << '!' << OFendl;
* *pI = "27"; // now, let's directly assign something else
* }
* @endcode
*/
template<typename Alternative,typename... Alternatives>
Alternative* OFget( OFvariant<Alternatives...>* v );
/** Try to get a pointer to the given alternative from an OFvariant object.
* @ingroup ofget_variant
* @relates OFvariant
* @tparam Alternative the chosen alternative that shall be accessed.
* @tparam Alternatives the alternatives the given variant could hold, will be deduced
* automatically.
* @param v a const reference to an OFvariant object potentially holding `Alternative`.
* @return the address of the contained value of type `const Alternative` if such a value is
* contained. `OFnullptr` otherwise.
* @details
* <h3>Usage Example:</h3>
* @code{.cpp}
* const OFvariant<int,OFString> v( ... some value is assigned to v ... );
* if( int* pI = OFget<int>( v ) ) // error, the result is const!
* if( const int* pI = OFget<int>( v ) ) // OK
* {
* COUT << "Yes, it really is an int with the value " << *pI << '!' << OFendl;
* *pI = "27"; // Error, *pI is const!
* }
* @endcode
*/
template<typename Alternative,typename... Alternatives>
const Alternative* OFget( const OFvariant<Alternatives...>* v );
/** Applies the given visitor to the given OFvariant object.
* @ingroup ofvisit_variant
* @relates OFvariant
* @details@anchor ofget_alternative_const_variant
* @tparam Result the type of the returned value. Pre C++11 compiles do not allow determining the
* result type automatically in a portable way, therefore, it must be explicitly given by
* the caller.
* @tparam Visitor the type of the visitor, will be deduced automatically.
* @tparam Alternatives the alternatives the given variant could hold, will be deduced
* automatically.
* @param visitor the visitor that will be invoked with the alternative currently being
* held by the given OFvariant object.
* @param v a reference to an OFvariant object that is going to be visited.
* @return Let `CurrentAlternative` be the alternative that v currently holds: the result
* of `visitor( *OFget<CurrentAlternative>( &v ) )` will be converted to `Result` and then
* returned.
* @pre all possible results must be convertible to `Result`.
* @pre `visitor` must be able to take all possible alternatives.
* @note If C++11 support is available, the visitor will be forwarded using perfect forwarding.
* If not, the visitor may be copy constructed at least once, therefore, the visitor needs
* to be copy constructible when pre C++11 compilers are targeted.
* @details
* <h3>Usage Example:</h3>
* @code{.cpp}
* struct PowerVisitor
* {
* template<typename Number>
* Number operator()( Number number )
* {
* return number * number;
* }
* };
* struct PrintVisitor
* {
* template<typename Number>
* void operator()( Number number )
* {
* COUT << number << OFendl;
* }
* };
* struct AssignVisitor
* {
* template<typename Number>
* void operator()( Number& number )
* {
* number *= number;
* }
* };
* // ...
* OFvariant<int,float,double> v( 3.14 );
* OFvariant<int,float,double> result = OFvisit<OFvariant<int,float,double> >( PowerVisitor(), v );
* switch( result.index() )
* {
* case 0: COUT << "int "; break;
* case 1: COUT << "float "; break;
* case 2: COUT << "double "; break;
* }
* OFvisit<void>( PrintVisitor(), result );
* COUT << "double " << OFvisit<double>( PowerVisitor(), v ) << OFendl; // OK, every alternative fits inside double
* COUT << "int " << OFvisit<int>( PowerVisitor(), v ) << OFendl; // OK, value will be truncated!
* COUT << "string " << OFvisit<OFString>( PowerVisitor(), v ) << OFendl; // ERROR!
* OFvisit<void>( AssignVisitor(), v );
* OFvisit<void>( PrintVisitor(), v );
* @endcode
* <b>Output (if the error was removed):</b>
* @verbatim
double 9.8596
double 9.8596
int 9
9.8596
@endverbatim
*/
template<typename Result,typename Visitor,typename... Alternatives>
Result OFvisit( Visitor visitor, OFvariant<Alternatives...>& v );
/** Applies the given visitor to the given OFvariant object.
* @ingroup ofvisit_variant
* @relates OFvariant
* @tparam Result the type of the returned value. Pre C++11 compiles do not allow determining the
* result type automatically in a portable way, therefore, it must be explicitly given by
* the caller.
* @tparam Visitor the type of the visitor, will be deduced automatically.
* @tparam Alternatives the alternatives the given variant could hold, will be deduced
* automatically.
* @param visitor the visitor that will be invoked with the alternative currently being
* held by the given OFvariant object.
* @param v a const reference to an OFvariant object that is going to be visited.
* @return Let `CurrentAlternative` be the alternative that v currently holds: the result
* of `visitor( *OFget<CurrentAlternative>( &v ) )` will be converted to `Result` and then
* returned.
* @pre all possible results must be convertible to `Result`.
* @pre `visitor` must be able to take all possible alternatives.
* @note If C++11 support is available, the visitor will be forwarded using perfect forwarding.
* If not, the visitor may be copy constructed at least once, therefore, the visitor needs
* to be copy constructible when pre C++11 compilers are targeted.
* @details
* <h3>Usage Example:</h3>
* @code{.cpp}
* struct PowerVisitor
* {
* template<typename Number>
* Number operator()( Number number )
* {
* return number * number;
* }
* };
* struct PrintVisitor
* {
* template<typename Number>
* void operator()( Number number )
* {
* COUT << number << OFendl;
* }
* };
* struct AssignVisitor
* {
* template<typename Number>
* void operator()( Number& number )
* {
* number *= number;
* }
* };
* // ...
* const OFvariant<int,float,double> v( 3.14 );
* OFvariant<int,float,double> result = OFvisit<OFvariant<int,float,double> >( PowerVisitor(), v );
* switch( result.index() )
* {
* case 0: COUT << "int "; break;
* case 1: COUT << "float "; break;
* case 2: COUT << "double "; break;
* }
* OFvisit<void>( PrintVisitor(), result );
* COUT << "double " << OFvisit<double>( PowerVisitor(), v ) << OFendl; // OK, every alternative fits inside double
* COUT << "int " << OFvisit<int>( PowerVisitor(), v ) << OFendl; // OK, value will be truncated!
* COUT << "string " << OFvisit<OFString>( PowerVisitor(), v ) << OFendl; // ERROR!
* OFvisit<void>( AssignVisitor(), v ); // ERROR, v is const!
* OFvisit<void>( PrintVisitor(), v );
* @endcode
* <b>Output (if the errors were removed):</b>
* @verbatim
double 9.8596
double 9.8596
int 9
3.14
@endverbatim
*/
template<typename Result,typename Visitor,typename... Alternatives>
Result OFvisit( Visitor visitor, const OFvariant<Alternatives...>& v );
#endif // DOXYGEN
/** A helper type for making OFvariant default constructible.
* @relates OFvariant
* @details
* Use OFmonostate as the first alternative of an OFvariant otherwise holding a non default constructible type as
* the first alternative for making the variant itself default constructible.
* @note Be aware that any visitor applied to such an OFvariant object must also accept OFmonostate as an argument.
* @details
* <h3>Example</h3>
* @code{.cpp}
* template<typename T>
* struct NonDefaultConstructible
* {
* NonDefaultConstructible( T t ) : value( t ) {}
* T value;
* };
* typedef NonDefaultConstructible<int> nint;
* typedef NonDefaultConstructible<float> nfloat;
* // ...
* OFvariant<nint,nfloat> v( 3 ); // OK, but what if we don't know the value yet?
* OFvariant<nint,nfloat> v; // ERROR!
* OFvariant<OFmonostate,nint,nfloat> v; // OK
* v = 3; // assign the value when it's known
* @endcode
*/
struct OFmonostate {};
#endif // OFVARIANT_H
|