libzypp 17.38.6
format.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8----------------------------------------------------------------------/
9*
10* This file contains private API, this might break at any time between releases.
11* You have been warned!
12*
13*/
14#ifndef ZYPP_NG_LOG_FORMAT_H_INCLUDED
15#define ZYPP_NG_LOG_FORMAT_H_INCLUDED
16
17#include <ostream>
18#include <sstream>
19#include <type_traits>
20
23
24
25namespace zyppng{
26
28
29 namespace log {
30
66 template <typename T, typename Enable = void>
67 struct formatter;
68
69 namespace detail {
70
72 template <typename T>
73 struct view_proxy {
74 const T& obj;
75
76 operator std::string() const {
77 std::ostringstream ss;
78 ss << *this;
79 return ss.str();
80 }
81
82 friend std::ostream& operator<<(std::ostream& os, const view_proxy& v) {
83 return formatter<T>::stream(os, v.obj);
84 }
85 };
86
88 template <typename T>
90 const ContextWeakRef ctx;
91 const T& obj;
92
93 operator std::string() const {
94 std::ostringstream ss;
95 ss << *this;
96 return ss.str();
97 }
98
99 friend std::ostream& operator<<(std::ostream& os, const ctx_bound_view_proxy& v) {
100 auto ctxLock = v.ctx.lock();
101 if ( ctxLock )
102 return formatter<T>::stream(os, ctxLock, v.obj);
103 else {
104 return os << "<<Error: Context Required for streaming " << typeid(T).name() << ">>";
105 }
106 }
107 };
108
110 template <typename T, typename = void>
111 struct has_view_proxy : std::false_type {};
112
113 template <typename T>
114 struct has_view_proxy<T, std::void_t<decltype(formatter<T>::stream(std::declval<std::ostream&>(), std::declval<const T&>()))>>
115 : std::true_type {};
116 }
117
119 template <typename T>
120 auto view(const ContextRef& ctx, const T& obj) {
121 return detail::ctx_bound_view_proxy<T>{ ctx, obj };
122 }
123
125 template <typename T >
126 auto view(const T& obj) {
127 return detail::view_proxy<T>{ obj };
128 }
129 }
130
136 template <typename T>
137 auto operator<<(std::ostream& os, const T& obj)
139 {
140 return os << log::view<T>(obj);
141 }
142}
143
144#endif
Definition ansi.h:855
typename enable_if< B, T >::type enable_if_t
Definition TypeTraits.h:45
std::ostream & operator<<(std::ostream &str, const Capabilities &obj)
relates: Capabilities Stream output
auto view(const ContextRef &ctx, const T &obj)
Unified format call: Context-Aware.
Definition format.h:120
Temporary object for streaming, we prefer direct streaming so we have string allocations only when ne...
Definition format.h:89
friend std::ostream & operator<<(std::ostream &os, const ctx_bound_view_proxy &v)
Definition format.h:99
Detection trait for StaticFormatter implementation.
Definition format.h:111
Temporary object for streaming, we prefer direct streaming so we have string allocations only when ne...
Definition format.h:73
friend std::ostream & operator<<(std::ostream &os, const view_proxy &v)
Definition format.h:82
Primary trait for object formatting.
Definition format.h:67
#define ZYPP_FWD_DECL_TYPE_WITH_REFS(T)
Definition zyppglobal.h:119