CodeContext.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2020 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 
21 {
22 public:
24 
26  static const Pointer &Current();
27 
29  static void Reset();
30 
32  static void Reset(const Pointer);
33 
34  virtual ~CodeContext() {}
35 
38  virtual ScopedId codeContextGist() const = 0;
39 
41  virtual std::ostream &detailCodeContext(std::ostream &os) const = 0;
42 
43 private:
44  static void ForgetCurrent();
45  static void Entering(const Pointer &codeCtx);
46  static void Leaving();
47 };
48 
50 inline
51 std::ostream &operator <<(std::ostream &os, const CodeContext &ctx)
52 {
53  return os << ctx.codeContextGist();
54 }
55 
56 /* convenience context-reporting wrappers that also reduce linking problems */
57 std::ostream &CurrentCodeContextBrief(std::ostream &os);
58 std::ostream &CurrentCodeContextDetail(std::ostream &os);
59 
63 {
64 public:
65  CodeContextGuard(const CodeContext::Pointer &newContext): savedCodeContext(CodeContext::Current()) { CodeContext::Reset(newContext); }
67 
68  // no copying of any kind (for simplicity and to prevent accidental copies)
70 
72 };
73 
79 template <typename Fun>
80 inline void
81 CallBack(const CodeContext::Pointer &callbackContext, Fun &&callback)
82 {
83  // TODO: Consider catching exceptions and letting CodeContext handle them.
84  const auto savedCodeContext(CodeContext::Current());
85  CodeContext::Reset(callbackContext);
86  callback();
87  CodeContext::Reset(savedCodeContext);
88 }
89 
96 template <typename Fun>
97 inline void
98 CallService(const CodeContext::Pointer &serviceContext, Fun &&service)
99 {
100  // TODO: Consider catching exceptions and letting CodeContext handle them.
101  CodeContextGuard guard(serviceContext);
102  service();
103 }
104 
111 template <typename Fun>
112 inline void
113 CallContextCreator(Fun &&creator)
114 {
115  const auto savedCodeContext(CodeContext::Current());
116  creator();
117  CodeContext::Reset(savedCodeContext);
118 }
119 
120 #endif
121 
static void Leaving()
Definition: CodeContext.cc:66
CodeContext::Pointer savedCodeContext
Definition: CodeContext.h:71
std::ostream & operator<<(std::ostream &os, const CodeContext &ctx)
by default, only small context gist is printed
Definition: CodeContext.h:51
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:81
virtual ScopedId codeContextGist() const =0
RefCount< CodeContext > Pointer
Definition: CodeContext.h:23
static void Reset()
forgets the current context, setting it to nil/unknown
Definition: CodeContext.cc:75
std::ostream & CurrentCodeContextBrief(std::ostream &os)
CodeContextGuard(const CodeContext::Pointer &newContext)
Definition: CodeContext.h:65
static const Pointer & Current()
Definition: CodeContext.cc:33
void const char HLPCB * callback
Definition: stub_helper.cc:16
std::ostream & CurrentCodeContextDetail(std::ostream &os)
Definition: CodeContext.cc:94
void CallContextCreator(Fun &&creator)
Definition: CodeContext.h:113
void CallService(const CodeContext::Pointer &serviceContext, Fun &&service)
Definition: CodeContext.h:98
static void ForgetCurrent()
Definition: CodeContext.cc:42
virtual ~CodeContext()
Definition: CodeContext.h:34

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors