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
104#endif /* SQUID_SRC_BASE_IO_MANIP_H */
105
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
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
static std::ostream & Extra(std::ostream &os)
prefixes each grouped debugs() line after the first one in the group
Definition: Stream.h:114
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:16

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors