statement_id.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. #ifndef SQL_STATEMENT_ID_H_
  5. #define SQL_STATEMENT_ID_H_
  6. #include <cstddef>
  7. #include "base/component_export.h"
  8. namespace sql {
  9. // Identifies a compiled SQLite statement in a statement cache.
  10. //
  11. // This is a value type with the same performance characteristics as
  12. // std::string_view. Instances are thread-unsafe, but not thread-hostile.
  13. //
  14. // StatementID instances should be constructed by using the SQL_FROM_HERE
  15. // macro, which produces an unique ID based on the source file name and line.
  16. class COMPONENT_EXPORT(SQL) StatementID {
  17. public:
  18. // Creates an ID representing a line in the source tree.
  19. //
  20. // SQL_FROM_HERE should be preferred to calling this constructor directly.
  21. //
  22. // |source_file| should point to a C-style string that lives for the duration
  23. // of the program.
  24. explicit StatementID(const char* source_file, size_t source_line) noexcept
  25. : source_file_(source_file), source_line_(source_line) {}
  26. // Copying intentionally allowed.
  27. StatementID(const StatementID&) noexcept = default;
  28. StatementID& operator=(const StatementID&) noexcept = default;
  29. // Facilitates storing StatementID instances in maps.
  30. bool operator<(const StatementID& rhs) const noexcept;
  31. private:
  32. // Instances cannot be immutable because they support being used as map keys.
  33. //
  34. // It seems tempting to merge source_file_ and source_line_ in a single
  35. // source_location_ member, and to have SQL_FROM_HERE use C++ preprocessor
  36. // magic to generate strings like "sql/connection.cc:42". This causes a
  37. // non-trivial binary size increase, because Chrome uses -fmerge-constants and
  38. // SQL_FROM_HERE tends to be used many times in the same few files.
  39. const char* source_file_;
  40. size_t source_line_;
  41. };
  42. } // namespace sql
  43. // Produces a StatementID based on the current line in the source tree.
  44. #define SQL_FROM_HERE sql::StatementID(__FILE__, __LINE__)
  45. #endif // SQL_STATEMENT_ID_H_