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_BASE_HERE_H
10#define SQUID_BASE_HERE_H
11
12#include <iosfwd>
13
15#define Here() SourceLocation(__FUNCTION__, __FILE__, __LINE__)
16
18typedef uint32_t SourceLocationId;
19
21typedef SourceLocationId FileNameHasher(const char *fileName);
22
24typedef SourceLocationId FileNameHashCacher(const char *fileName, FileNameHasher hasher);
25
27
30{
31public:
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
49private:
52};
53
54inline std::ostream &
55operator <<(std::ostream &os, const SourceLocation &location)
56{
57 return location.print(os);
58}
59
63UnitFileNameHashCacher(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
77
std::ostream & operator<<(std::ostream &os, const SourceLocation &location)
Definition: Here.h:55
static FileNameHashCacher UnitFileNameHashCacher
Definition: Here.h:26
SourceLocationId FileNameHashCacher(const char *fileName, FileNameHasher hasher)
a caching proxy for hasher results
Definition: Here.h:24
SourceLocationId FileNameHasher(const char *fileName)
returns a hash of a file name
Definition: Here.h:21
uint32_t SourceLocationId
semi-uniquely identifies a source code location; stable across Squid runs
Definition: Here.h:18
a source code location that is cheap to create, copy, and store
Definition: Here.h:30
const char * fileName
source file name, often relative to build path
Definition: Here.h:46
SourceLocationId id() const
Definition: Here.cc:67
int lineNo
line number inside the source file name (if positive)
Definition: Here.h:47
std::ostream & print(std::ostream &os) const
describes location using a compact but human-friendly format
Definition: Here.cc:77
const char * context
line-independent location description
Definition: Here.h:45
SourceLocationId calculateId(FileNameHasher) const
SourceLocation(const char *aContext, const char *aFileName, const int aLineNo)
Definition: Here.h:32
FileNameHashCacher * fileNameHashCacher
Definition: Here.h:51

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors