IoManip.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2022 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 
19 template <class Pointer>
20 class RawPointerT {
21 public:
22  RawPointerT(const char *aLabel, const Pointer &aPtr):
23  label(aLabel), ptr(aPtr) {}
24 
26  RawPointerT<Pointer> &asExtra() { onExtraLine = true; return *this; }
27 
28  const char *label;
29  const Pointer &ptr;
30  bool onExtraLine = false;
31 };
32 
34 template <class Pointer>
36 RawPointer(const char *label, const Pointer &ptr)
37 {
38  return RawPointerT<Pointer>(label, ptr);
39 }
40 
42 template <class Pointer>
43 inline std::ostream &
44 operator <<(std::ostream &os, const RawPointerT<Pointer> &pd)
45 {
46  if (!pd.ptr)
47  return os;
48 
49  if (pd.onExtraLine)
50  os << Debug::Extra;
51 
52  if (pd.label)
53  os << pd.label;
54 
55  os << *pd.ptr;
56 
57  return os;
58 }
59 
61 template <class Integer>
62 class AsHex
63 {
64 public:
65  explicit AsHex(const Integer n): io_manip(n) {}
66  Integer io_manip;
67 };
68 
69 template <class Integer>
70 inline std::ostream &
71 operator <<(std::ostream &os, const AsHex<Integer> number)
72 {
73  const auto oldFlags = os.flags();
74  os << std::hex << std::showbase << number.io_manip;
75  os.setf(oldFlags);
76  return os;
77 }
78 
80 template <class Integer>
81 inline AsHex<Integer> asHex(const Integer n) { return AsHex<Integer>(n); }
82 
84 void PrintHex(std::ostream &, const char *data, size_t n);
85 
86 #endif /* SQUID_SRC_BASE_IO_MANIP_H */
87 
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:44
RawPointerT< Pointer > RawPointer(const char *label, const Pointer &ptr)
convenience wrapper for creating RawPointerT<> objects
Definition: IoManip.h:36
AsHex< Integer > asHex(const Integer n)
a helper to ease AsHex object creation
Definition: IoManip.h:81
std::ostream manipulator to print integers as hex numbers prefixed by 0x
Definition: IoManip.h:63
AsHex(const Integer n)
Definition: IoManip.h:65
Integer io_manip
the integer to print
Definition: IoManip.h:66
static std::ostream & Extra(std::ostream &os)
prefixes each grouped debugs() line after the first one in the group
Definition: Stream.h:117
RawPointerT(const char *aLabel, const Pointer &aPtr)
Definition: IoManip.h:22
RawPointerT< Pointer > & asExtra()
Report the pointed-to-object on a dedicated Debug::Extra line.
Definition: IoManip.h:26
const Pointer & ptr
the name or description of the being-debugged object
Definition: IoManip.h:29
bool onExtraLine
a possibly nil pointer to the being-debugged object
Definition: IoManip.h:30
const char * label
Definition: IoManip.h:28
number
Definition: testStatHist.cc:16

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors