1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef SQL_STATEMENT_ID_H_
6 #define SQL_STATEMENT_ID_H_
10 #include "base/component_export.h"
14 // Identifies a compiled SQLite statement in a statement cache.
16 // This is a value type with the same performance characteristics as
17 // std::string_view. Instances are thread-unsafe, but not thread-hostile.
19 // StatementID instances should be constructed by using the SQL_FROM_HERE
20 // macro, which produces an unique ID based on the source file name and line.
21 class COMPONENT_EXPORT(SQL) StatementID {
23 // Creates an ID representing a line in the source tree.
25 // SQL_FROM_HERE should be preferred to calling this constructor directly.
27 // |source_file| should point to a C-style string that lives for the duration
29 explicit StatementID(const char* source_file, size_t source_line) noexcept
30 : source_file_(source_file), source_line_(source_line) {}
32 // Copying intentionally allowed.
33 StatementID(const StatementID&) noexcept = default;
34 StatementID& operator=(const StatementID&) noexcept = default;
36 // Facilitates storing StatementID instances in maps.
37 bool operator<(const StatementID& rhs) const noexcept;
40 // Instances cannot be immutable because they support being used as map keys.
42 // It seems tempting to merge source_file_ and source_line_ in a single
43 // source_location_ member, and to have SQL_FROM_HERE use C++ preprocessor
44 // magic to generate strings like "sql/connection.cc:42". This causes a
45 // non-trivial binary size increase, because Chrome uses -fmerge-constants and
46 // SQL_FROM_HERE tends to be used many times in the same few files.
47 const char* source_file_;
53 // Produces a StatementID based on the current line in the source tree.
54 #define SQL_FROM_HERE sql::StatementID(__FILE__, __LINE__)
56 #endif // SQL_STATEMENT_ID_H_