CodeContext.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2021 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 code_contexts for details.
7  */
8 
9 #ifndef SQUID_BASE_CODE_CONTEXT_H
10 #define SQUID_BASE_CODE_CONTEXT_H
11 
12 #include "base/InstanceId.h"
13 #include "base/RefCount.h"
14 
15 #include <iosfwd>
16 
48 class CodeContext: public RefCountable
52 {
53 public:
55 
57  static const Pointer &Current();
58 
60  static void Reset();
61 
63  static void Reset(const Pointer);
64 
65  virtual ~CodeContext() {}
66 
69  virtual ScopedId codeContextGist() const = 0;
70 
72  virtual std::ostream &detailCodeContext(std::ostream &os) const = 0;
73 
74 private:
75  static void ForgetCurrent();
76  static void Entering(const Pointer &codeCtx);
77  static void Leaving();
78 };
79 
81 inline
82 std::ostream &operator <<(std::ostream &os, const CodeContext &ctx)
83 {
84  return os << ctx.codeContextGist();
85 }
86 
87 /* convenience context-reporting wrappers that also reduce linking problems */
88 std::ostream &CurrentCodeContextBrief(std::ostream &os);
89 std::ostream &CurrentCodeContextDetail(std::ostream &os);
90 
94 {
95 public:
96  CodeContextGuard(const CodeContext::Pointer &newContext): savedCodeContext(CodeContext::Current()) { CodeContext::Reset(newContext); }
98 
99  // no copying of any kind (for simplicity and to prevent accidental copies)
101 
103 };
104 
110 template <typename Fun>
111 inline void
112 CallBack(const CodeContext::Pointer &callbackContext, Fun &&callback)
113 {
114  // TODO: Consider catching exceptions and letting CodeContext handle them.
115  const auto savedCodeContext(CodeContext::Current());
116  CodeContext::Reset(callbackContext);
117  callback();
118  CodeContext::Reset(savedCodeContext);
119 }
120 
127 template <typename Fun>
128 inline void
129 CallService(const CodeContext::Pointer &serviceContext, Fun &&service)
130 {
131  // TODO: Consider catching exceptions and letting CodeContext handle them.
132  CodeContextGuard guard(serviceContext);
133  service();
134 }
135 
142 template <typename Fun>
143 inline void
144 CallContextCreator(Fun &&creator)
145 {
146  const auto savedCodeContext(CodeContext::Current());
147  creator();
148  CodeContext::Reset(savedCodeContext);
149 }
150 
152 
153 #endif
154 
static void Leaving()
Definition: CodeContext.cc:66
CodeContext::Pointer savedCodeContext
Definition: CodeContext.h:102
std::ostream & operator<<(std::ostream &os, const CodeContext &ctx)
by default, only small context gist is printed
Definition: CodeContext.h:82
static void Entering(const Pointer &codeCtx)
Definition: CodeContext.cc:54
virtual std::ostream & detailCodeContext(std::ostream &os) const =0
appends human-friendly context description line(s) to a cache.log record
void CallBack(const CodeContext::Pointer &callbackContext, Fun &&callback)
Definition: CodeContext.h:112
virtual ScopedId codeContextGist() const =0
RefCount< CodeContext > Pointer
Definition: CodeContext.h:54
static void Reset()
forgets the current context, setting it to nil/unknown
Definition: CodeContext.cc:75
std::ostream & CurrentCodeContextBrief(std::ostream &os)
CodeContextGuard(CodeContextGuard &&)=delete
CodeContextGuard(const CodeContext::Pointer &newContext)
Definition: CodeContext.h:96
static const Pointer & Current()
Definition: CodeContext.cc:33
std::ostream & CurrentCodeContextDetail(std::ostream &os)
Definition: CodeContext.cc:94
void CallContextCreator(Fun &&creator)
Definition: CodeContext.h:144
void CallService(const CodeContext::Pointer &serviceContext, Fun &&service)
Definition: CodeContext.h:129
static void ForgetCurrent()
Definition: CodeContext.cc:42
virtual ~CodeContext()
Definition: CodeContext.h:65

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors