-
Notifications
You must be signed in to change notification settings - Fork 31
/
ReprImpl.hh
98 lines (86 loc) · 2.46 KB
/
ReprImpl.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//----------------------------------*-C++-*----------------------------------//
// Copyright 2021-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file corecel/io/detail/ReprImpl.hh
//---------------------------------------------------------------------------//
#pragma once
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <type_traits>
#include "corecel/Assert.hh"
#include "../ScopedStreamFormat.hh"
namespace celeritas
{
template<class T>
struct ReprTraits;
namespace detail
{
//---------------------------------------------------------------------------//
// STREAMABLE
//---------------------------------------------------------------------------//
/*!
* Thin temporary wrapper for printing a complex class to a stream.
*/
template<class T>
struct Repr
{
T const& obj;
char const* name = nullptr;
};
//---------------------------------------------------------------------------//
/*!
* Write a streamable object to a stream.
*/
template<class T>
std::ostream& operator<<(std::ostream& os, Repr<T> const& s)
{
ScopedStreamFormat save_fmt(&os);
ReprTraits<T>::init(os);
if (s.name)
{
ReprTraits<T>::print_type(os, s.name);
os << '{';
}
ReprTraits<T>::print_value(os, s.obj);
if (s.name)
{
os << '}';
}
return os;
}
//---------------------------------------------------------------------------//
/*!
* Convert a streamable object to a string.
*/
template<class T>
std::string to_string(Repr<T> const& s)
{
std::ostringstream os;
os << s;
return os.str();
}
//---------------------------------------------------------------------------//
// HELPER FUNCTION DECLARATIONS
//---------------------------------------------------------------------------//
void repr_char(std::ostream& os, char value);
std::string char_to_hex_string(unsigned char value);
void print_simple_type(std::ostream& os, char const* type, char const* name);
template<class T>
inline void
print_container_type(std::ostream& os, char const* type, char const* name)
{
os << type << '<';
ReprTraits<T>::print_type(os);
os << '>';
if (name)
{
os << ' ' << name;
}
}
//---------------------------------------------------------------------------//
} // namespace detail
} // namespace celeritas