Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:55:05

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/ReprImpl.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <iomanip>
0010 #include <iostream>
0011 #include <sstream>
0012 #include <string>
0013 #include <type_traits>
0014 
0015 #include "corecel/Assert.hh"
0016 
0017 #include "../ScopedStreamFormat.hh"
0018 
0019 namespace celeritas
0020 {
0021 template<class T>
0022 struct ReprTraits;
0023 
0024 namespace detail
0025 {
0026 //---------------------------------------------------------------------------//
0027 // STREAMABLE
0028 //---------------------------------------------------------------------------//
0029 /*!
0030  * Thin temporary wrapper for printing a complex class to a stream.
0031  */
0032 template<class T>
0033 struct Repr
0034 {
0035     T const& obj;
0036     char const* name = nullptr;
0037 };
0038 
0039 //---------------------------------------------------------------------------//
0040 /*!
0041  * Write a streamable object to a stream.
0042  */
0043 template<class T>
0044 std::ostream& operator<<(std::ostream& os, Repr<T> const& s)
0045 {
0046     ScopedStreamFormat save_fmt(&os);
0047     ReprTraits<T>::init(os);
0048     if (s.name)
0049     {
0050         ReprTraits<T>::print_type(os, s.name);
0051         os << '{';
0052     }
0053     ReprTraits<T>::print_value(os, s.obj);
0054     if (s.name)
0055     {
0056         os << '}';
0057     }
0058     return os;
0059 }
0060 
0061 //---------------------------------------------------------------------------//
0062 /*!
0063  * Convert a streamable object to a string.
0064  */
0065 template<class T>
0066 std::string to_string(Repr<T> const& s)
0067 {
0068     std::ostringstream os;
0069     os << s;
0070     return os.str();
0071 }
0072 
0073 //---------------------------------------------------------------------------//
0074 // HELPER FUNCTION DECLARATIONS
0075 //---------------------------------------------------------------------------//
0076 bool all_printable(std::string_view s);
0077 
0078 void repr_char(std::ostream& os, char value);
0079 
0080 std::string char_to_hex_string(unsigned char value);
0081 
0082 void print_simple_type(std::ostream& os, char const* type, char const* name);
0083 
0084 template<class T>
0085 inline void
0086 print_container_type(std::ostream& os, char const* type, char const* name)
0087 {
0088     os << type << '<';
0089     ReprTraits<T>::print_type(os);
0090     os << '>';
0091     if (name)
0092     {
0093         os << ' ' << name;
0094     }
0095 }
0096 
0097 //---------------------------------------------------------------------------//
0098 }  // namespace detail
0099 }  // namespace celeritas