Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:09:39

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/detail/Joined.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <iosfwd>
0010 #include <iterator>
0011 #include <sstream>
0012 #include <string>
0013 
0014 namespace celeritas
0015 {
0016 namespace detail
0017 {
0018 //---------------------------------------------------------------------------//
0019 /*!
0020  * Default operator given an InputIterator: just stream the value.
0021  */
0022 struct StreamValue
0023 {
0024     template<class T>
0025     void operator()(std::ostream& os, T&& v)
0026     {
0027         os << std::forward<T>(v);
0028     }
0029 };
0030 
0031 //---------------------------------------------------------------------------//
0032 /*!
0033  * Convert a unary operator to a functor that writes to a stream.
0034  */
0035 template<class UnaryOp>
0036 struct UnaryToStream
0037 {
0038     UnaryOp op;
0039 
0040     template<class T>
0041     void operator()(std::ostream& os, T&& v)
0042     {
0043         os << op(std::forward<T>(v));
0044     }
0045 };
0046 
0047 //---------------------------------------------------------------------------//
0048 /*!
0049  * Implementation of joining a series of values.
0050  *
0051  * The advantage of this class is not having to create a temporary std::string
0052  * with the fully joined list.
0053  */
0054 template<class InputIterator, class Conjunction, class StreamOp = StreamValue>
0055 struct Joined
0056 {
0057     InputIterator first;
0058     InputIterator last;
0059     Conjunction conjunction;
0060     StreamOp op;
0061 };
0062 
0063 //---------------------------------------------------------------------------//
0064 template<class I, class C, class S>
0065 std::ostream& operator<<(std::ostream& os, Joined<I, C, S> const& j)
0066 {
0067     auto iter = j.first;
0068     auto op = j.op;
0069 
0070     // First element is not preceded by a conjunction
0071     if (iter != j.last)
0072     {
0073         op(os, *iter++);
0074     }
0075 
0076     // Join the rest
0077     while (iter != j.last)
0078     {
0079         os << j.conjunction;
0080         op(os, *iter++);
0081     }
0082 
0083     return os;
0084 }
0085 
0086 //---------------------------------------------------------------------------//
0087 template<class I, class C, class S>
0088 std::string to_string(Joined<I, C, S> const& j)
0089 {
0090     std::ostringstream os;
0091     os << j;
0092     return os.str();
0093 }
0094 
0095 //---------------------------------------------------------------------------//
0096 }  // namespace detail
0097 }  // namespace celeritas