IoManip.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9#ifndef SQUID_SRC_BASE_IO_MANIP_H
10#define SQUID_SRC_BASE_IO_MANIP_H
11
12#include "debug/Stream.h"
13
14#include <iostream>
15#include <iomanip>
16
19template <class Pointer>
21public:
22 RawPointerT(const char *aLabel, const Pointer &aPtr):
23 label(aLabel), ptr(aPtr) {}
24
26 RawPointerT<Pointer> &asExtra() { onExtraLine = true; return *this; }
27
29 RawPointerT<Pointer> &orNil(const char *nilTextToUse = "[nil]") { nilText = nilTextToUse; return *this; }
30
31 const char *label;
32
34 const char *nilText = nullptr;
35
36 const Pointer &ptr;
37 bool onExtraLine = false;
38};
39
41template <class Pointer>
43RawPointer(const char *label, const Pointer &ptr)
44{
45 return RawPointerT<Pointer>(label, ptr);
46}
47
49template <class Pointer>
51RawPointer(const Pointer &ptr)
52{
53 return RawPointerT<Pointer>(nullptr, ptr);
54}
55
57template <class Pointer>
58inline std::ostream &
59operator <<(std::ostream &os, const RawPointerT<Pointer> &pd)
60{
61 if (!pd.ptr) {
62 if (pd.nilText)
63 os << pd.nilText;
64 return os;
65 }
66
67 if (pd.onExtraLine)
68 os << Debug::Extra;
69
70 if (pd.label)
71 os << pd.label;
72
73 os << *pd.ptr;
74
75 return os;
76}
77
79template <class Integer>
80class AsHex
81{
82public:
83 explicit AsHex(const Integer n): io_manip(n) {}
84 Integer io_manip;
85};
86
87template <class Integer>
88inline std::ostream &
89operator <<(std::ostream &os, const AsHex<Integer> number)
90{
91 const auto oldFlags = os.flags();
92 os << std::hex << std::showbase << number.io_manip;
93 os.setf(oldFlags);
94 return os;
95}
96
98template <class Integer>
99inline AsHex<Integer> asHex(const Integer n) { return AsHex<Integer>(n); }
100
102void PrintHex(std::ostream &, const char *data, size_t n);
103
105template <typename Container>
107{
108public:
109 explicit AsList(const Container &c): container(c) {}
110
112 auto &prefixedBy(const char * const p) { prefix = p; return *this; }
113
115 auto &delimitedBy(const char * const d) { delimiter = d; return *this; }
116
117public:
118 const Container &container;
119
120 const char *prefix = nullptr;
121 const char *delimiter = nullptr;
122};
123
124template <class Container>
125inline std::ostream &
126operator <<(std::ostream &os, const AsList<Container> &manipulator)
127{
128 bool opened = false;
129 for (const auto &item: manipulator.container) {
130 if (!opened) {
131 if (manipulator.prefix)
132 os << manipulator.prefix;
133 opened = true;
134 } else {
135 if (manipulator.delimiter)
136 os << manipulator.delimiter;
137 }
138 os << item;
139 }
140 return os;
141}
142
144template <typename Container>
145inline auto asList(const Container &c) { return AsList<Container>(c); }
146
147#endif /* SQUID_SRC_BASE_IO_MANIP_H */
148
void PrintHex(std::ostream &, const char *data, size_t n)
Prints the first n data bytes using hex notation. Does nothing if n is 0.
Definition: IoManip.cc:16
std::ostream & operator<<(std::ostream &os, const RawPointerT< Pointer > &pd)
prints RawPointerT<>, dereferencing the io_manip pointer if possible
Definition: IoManip.h:59
auto asList(const Container &c)
a helper to ease AsList object creation
Definition: IoManip.h:145
RawPointerT< Pointer > RawPointer(const char *label, const Pointer &ptr)
convenience wrapper for creating RawPointerT<> objects
Definition: IoManip.h:43
AsHex< Integer > asHex(const Integer n)
a helper to ease AsHex object creation
Definition: IoManip.h:99
std::ostream manipulator to print integers as hex numbers prefixed by 0x
Definition: IoManip.h:81
AsHex(const Integer n)
Definition: IoManip.h:83
Integer io_manip
the integer to print
Definition: IoManip.h:84
std::ostream manipulator to print containers as flat lists
Definition: IoManip.h:107
auto & prefixedBy(const char *const p)
a c-string to print before the first item (if any). Caller must ensure lifetime.
Definition: IoManip.h:112
const Container & container
zero or more items to print
Definition: IoManip.h:118
const char * delimiter
a c-string to print between consecutive items (if any). Caller must ensure lifetime.
Definition: IoManip.h:121
auto & delimitedBy(const char *const d)
a c-string to print between consecutive items (if any). Caller must ensure lifetime.
Definition: IoManip.h:115
const char * prefix
a c-string to print before the first item (if any). Caller must ensure lifetime.
Definition: IoManip.h:120
AsList(const Container &c)
Definition: IoManip.h:109
static std::ostream & Extra(std::ostream &)
Definition: debug.cc:1313
RawPointerT(const char *aLabel, const Pointer &aPtr)
Definition: IoManip.h:22
const Pointer & ptr
Definition: IoManip.h:36
bool onExtraLine
a possibly nil pointer to the being-debugged object
Definition: IoManip.h:37
const char * label
Definition: IoManip.h:31
RawPointerT< Pointer > & orNil(const char *nilTextToUse="[nil]")
enable and, optionally, customize reporting of nil pointers
Definition: IoManip.h:29
const char * nilText
the name or description of the being-debugged object
Definition: IoManip.h:34
RawPointerT< Pointer > & asExtra()
Report the pointed-to-object on a dedicated Debug::Extra line.
Definition: IoManip.h:26
number
Definition: testStatHist.cc:32

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors