Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-01 10:04:15

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file corecel/io/StreamableVariant.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <ostream>
0010 #include <variant>
0011 
0012 #include "corecel/Assert.hh"
0013 
0014 #include "StreamUtils.hh"
0015 
0016 namespace celeritas
0017 {
0018 //---------------------------------------------------------------------------//
0019 /*!
0020  * Helper class to print a variant to a stream.
0021  *
0022  * Example:
0023  * \code
0024    std::cout << StreamableVariant{surface} << std::endl;
0025    \endcode
0026  */
0027 template<class T>
0028 struct StreamableVariant
0029 {
0030     T value;
0031 
0032     //! Write to stream
0033     friend std::ostream& operator<<(std::ostream& os,
0034                                     StreamableVariant const& svar)
0035     {
0036         CELER_ASSUME(!svar.value.valueless_by_exception());
0037         std::visit(GenericToStream{os}, svar.value);
0038         return os;
0039     }
0040 
0041     //! Save as a string
0042     friend std::string to_string(StreamableVariant const& svar)
0043     {
0044         return stream_to_string(svar);
0045     }
0046 };
0047 
0048 //---------------------------------------------------------------------------//
0049 // Deduction guide
0050 template<class T>
0051 StreamableVariant(T&&) -> StreamableVariant<T>;
0052 
0053 //---------------------------------------------------------------------------//
0054 }  // namespace celeritas