Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:48

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