Victor Costan | 12daa3ac9 | 2018-07-19 01:05:58 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef SQL_STATEMENT_ID_H_ |
| 6 | #define SQL_STATEMENT_ID_H_ |
| 7 | |
| 8 | #include <cstddef> |
| 9 | |
| 10 | #include "sql/sql_export.h" |
| 11 | |
| 12 | namespace sql { |
| 13 | |
| 14 | // Identifies a compiled SQLite statement in a statement cache. |
| 15 | // |
| 16 | // This is a value type with the same performance characteristics as |
| 17 | // std::string_view. Instances are thread-unsafe, but not thread-hostile. |
| 18 | // |
| 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 SQL_EXPORT StatementID { |
| 22 | public: |
| 23 | // Creates an ID representing a line in the source tree. |
| 24 | // |
| 25 | // SQL_FROM_HERE should be preferred to calling this constructor directly. |
| 26 | // |
| 27 | // |source_file| should point to a C-style string that lives for the duration |
| 28 | // of the program. |
| 29 | explicit StatementID(const char* source_file, size_t source_line) noexcept |
| 30 | : source_file_(source_file), source_line_(source_line) {} |
| 31 | |
| 32 | // Copying intentionally allowed. |
| 33 | StatementID(const StatementID&) noexcept = default; |
| 34 | StatementID& operator=(const StatementID&) noexcept = default; |
| 35 | |
| 36 | // Facilitates storing StatementID instances in maps. |
| 37 | bool operator<(const StatementID& rhs) const noexcept; |
| 38 | |
| 39 | private: |
| 40 | // Instances cannot be immutable because they support being used as map keys. |
| 41 | // |
| 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_; |
| 48 | size_t source_line_; |
| 49 | }; |
| 50 | |
| 51 | } // namespace sql |
| 52 | |
| 53 | // Produces a StatementID based on the current line in the source tree. |
| 54 | #define SQL_FROM_HERE sql::StatementID(__FILE__, __LINE__) |
| 55 | |
| 56 | #endif // SQL_STATEMENT_ID_H_ |