Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-26 09:34:35

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/StreamableLazy.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <ostream>
0010 
0011 #include "StreamUtils.hh"
0012 
0013 namespace celeritas
0014 {
0015 //---------------------------------------------------------------------------//
0016 /*!
0017  * Lazily evaluate a functor that streams content.
0018  *
0019  * \par Example:
0020  * \code
0021    std::cout << StreamableLazy{[]() {
0022         return do_something_expensive_and_printable();
0023     }} << std::endl;
0024    \endcode
0025  */
0026 template<class F>
0027 struct StreamableLazy
0028 {
0029     F func;
0030 
0031     //! Write to stream
0032     inline friend std::ostream& operator<<(std::ostream& os,
0033                                            StreamableLazy const& lazy)
0034     {
0035         return (os << lazy.func());
0036     }
0037 
0038     //! Save as a string
0039     inline friend std::string to_string(StreamableLazy const& svar)
0040     {
0041         return stream_to_string(svar);
0042     }
0043 };
0044 
0045 //---------------------------------------------------------------------------//
0046 // Deduction guide (C++17)
0047 template<class F>
0048 StreamableLazy(F&&) -> StreamableLazy<F>;
0049 
0050 //---------------------------------------------------------------------------//
0051 }  // namespace celeritas