SpECTRE Documentation Coverage Report
Current view: top level - Utilities - PrintHelpers.hpp Hit Total Coverage
Commit: 817e13c5144619b701c7cd870655d8dbf94ab8ce Lines: 5 6 83.3 %
Date: 2024-07-19 22:17:05
Legend: Lines: hit not hit

          Line data    Source code
       1           0 : // Distributed under the MIT License.
       2             : // See LICENSE.txt for details.
       3             : 
       4             : #pragma once
       5             : 
       6             : #include <algorithm>
       7             : #include <iterator>
       8             : #include <sstream>
       9             : #include <string>
      10             : #include <utility>
      11             : #include <vector>
      12             : 
      13             : #include "Utilities/Requires.hpp"
      14             : #include "Utilities/TypeTraits/IsStreamable.hpp"
      15             : 
      16             : /// \ingroup UtilitiesGroup
      17             : /// \brief Either streams `value` or the string "UNSTREAMABLE"
      18             : template <typename StreamType, typename T>
      19           1 : StreamType& print_value(StreamType& os, const T& value) {
      20             :   if constexpr (tt::is_streamable_v<StreamType, T>) {
      21             :     os << value;
      22             :   } else {
      23             :     os << "UNSTREAMABLE";
      24             :   }
      25             :   return os;
      26             : }
      27             : 
      28             : /*!
      29             :  * \ingroup UtilitiesGroup
      30             :  * \brief Applies the function f(out, it) to each item from begin to
      31             :  * end, separated by commas and surrounded by parens.
      32             :  */
      33             : template <typename ForwardIt, typename Func>
      34           1 : void sequence_print_helper(std::ostream& out, ForwardIt begin,
      35             :                            const ForwardIt& end, Func f) {
      36             :   out << "(";
      37             :   if (begin != end) {
      38             :     while (true) {
      39             :       f(out, *begin++);
      40             :       if (begin == end) {
      41             :         break;
      42             :       }
      43             :       out << ",";
      44             :     }
      45             :   }
      46             :   out << ")";
      47             : }
      48             : 
      49             : /*!
      50             :  * \ingroup UtilitiesGroup
      51             :  * \brief Prints all the items as a comma separated list surrounded by parens.
      52             :  */
      53             : template <typename ForwardIt>
      54           1 : void sequence_print_helper(std::ostream& out, ForwardIt begin,
      55             :                            const ForwardIt& end) {
      56             :   sequence_print_helper(
      57             :       out, std::move(begin), end,
      58             :       [](std::ostream& os,
      59             :          const typename std::iterator_traits<ForwardIt>::value_type& it) {
      60             :         print_value(os, it);
      61             :       });
      62             : }
      63             : 
      64             : /// @{
      65             : /*!
      66             :  * \ingroup UtilitiesGroup
      67             :  * Like sequence_print_helper, but sorts the string representations.
      68             :  */
      69             : template <typename ForwardIt, typename Func>
      70           1 : void unordered_print_helper(std::ostream& out, ForwardIt begin,
      71             :                             const ForwardIt& end, Func f) {
      72             :   std::vector<std::string> entries;
      73             :   while (begin != end) {
      74             :     std::ostringstream ss;
      75             :     f(ss, *begin++);
      76             :     entries.push_back(ss.str());
      77             :   }
      78             :   std::sort(entries.begin(), entries.end());
      79             :   sequence_print_helper(out, entries.begin(), entries.end());
      80             : }
      81             : 
      82             : template <typename ForwardIt>
      83           1 : void unordered_print_helper(std::ostream& out, ForwardIt begin,
      84             :                             const ForwardIt& end) {
      85             :   unordered_print_helper(
      86             :       out, std::move(begin), end,
      87             :       [](std::ostream& os,
      88             :          const typename std::iterator_traits<ForwardIt>::value_type& it) {
      89             :         print_value(os, it);
      90             :       });
      91             : }
      92             : /// @}

Generated by: LCOV version 1.14