Here.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_HERE_H
10 #define SQUID_SRC_BASE_HERE_H
11 
12 #include <iosfwd>
13 
15 #define Here() SourceLocation(__func__, __FILE__, __LINE__)
16 
18 typedef uint32_t SourceLocationId;
19 
21 typedef SourceLocationId FileNameHasher(const char *fileName);
22 
24 typedef SourceLocationId FileNameHashCacher(const char *fileName, FileNameHasher hasher);
25 
27 
30 {
31 public:
32  SourceLocation(const char *aContext, const char *aFileName, const int aLineNo):
33  context(aContext),
34  fileName(aFileName),
35  lineNo(aLineNo),
37  {}
38 
40  SourceLocationId id() const;
41 
43  std::ostream &print(std::ostream &os) const;
44 
45  const char *context;
46  const char *fileName;
47  int lineNo;
48 
49 private:
52 };
53 
54 inline std::ostream &
55 operator <<(std::ostream &os, const SourceLocation &location)
56 {
57  return location.print(os);
58 }
59 
62 static SourceLocationId
63 UnitFileNameHashCacher(const char *fileName, FileNameHasher hasher)
64 {
65  static SourceLocationId cachedHash = 0;
66  static const char *hashedFilename = nullptr;
67  // Each file #included in a translation unit has its own __FILE__ value.
68  // Keep the cache fresh (and the result correct).
69  if (hashedFilename != fileName) { // cheap pointer comparison
70  hashedFilename = fileName;
71  cachedHash = hasher(fileName);
72  }
73  return cachedHash;
74 }
75 
76 #endif /* SQUID_SRC_BASE_HERE_H */
77 
SourceLocationId FileNameHashCacher(const char *fileName, FileNameHasher hasher)
a caching proxy for hasher results
Definition: Here.h:24
SourceLocationId calculateId(FileNameHasher) const
static FileNameHashCacher UnitFileNameHashCacher
Definition: Here.h:26
SourceLocation(const char *aContext, const char *aFileName, const int aLineNo)
Definition: Here.h:32
SourceLocationId id() const
Definition: Here.cc:67
int lineNo
line number inside the source file name (if positive)
Definition: Here.h:47
uint32_t SourceLocationId
semi-uniquely identifies a source code location; stable across Squid runs
Definition: Here.h:18
const char * context
line-independent location description
Definition: Here.h:45
a source code location that is cheap to create, copy, and store
Definition: Here.h:29
std::ostream & print(std::ostream &os) const
describes location using a compact but human-friendly format
Definition: Here.cc:77
FileNameHashCacher * fileNameHashCacher
Definition: Here.h:51
SourceLocationId FileNameHasher(const char *fileName)
returns a hash of a file name
Definition: Here.h:21
const char * fileName
source file name, often relative to build path
Definition: Here.h:46
std::ostream & operator<<(std::ostream &os, const SourceLocation &location)
Definition: Here.h:55

 

Introduction

Documentation

Support

Miscellaneous