You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit f53139f
Browse filesBrowse the repository at this point in the historyBrowse files
Make Database mutators non-const and Instance() return a writable handle (#26) (#36)
Instance() returned std::shared_ptr<const Database>, yet Save,
SaveAutoIncrement, Update, Delete, and UnsafeSql were all declared const -
so a handle typed as pointer-to-const could freely mutate persistent state.
It compiled because the connection is a raw sqlite3* (const does not
propagate through the pointer) and db_mutex_ is mutable. The API
misrepresented intent: const looked read-only but wasn't.
Make const mean read-only:
- Instance() now returns std::shared_ptr<Database>.
- The write methods (Save/SaveAutoIncrement/Update/Delete overloads,
UnsafeSql) and their private void* worker helpers are now non-const.
- The genuine reads (FetchAll, Fetch, private FetchRecords) stay const, and
db_mutex_ stays mutable so they can still lock it.
The ownership/lifecycle model is unchanged: the alive-across-Finalize()
guarantee comes from Instance() returning a strong shared_ptr, not from the
pointee being const. Serialized access via db_mutex_, the retired_
reinitialization guard, and all CRUD semantics are untouched. A read-only
view is still available via the implicit shared_ptr<Database> ->
shared_ptr<const Database> conversion, through which only the const fetch
methods compile; README updated accordingly.
Existing call sites of the form `const auto db = Database::Instance()`
still compile (const handle, non-const pointee).
Tests: compile-time SFINAE checks in database_test.cc assert the new
contract (Save is NOT invocable through const Database, FetchAll IS), with
the trait machinery verified to detect the positive case on non-const
Database so the negative assert isn't vacuous; plus a runtime test that a
shared_ptr<const Database> view can still fetch. Full suite: 76/76 pass in
both C++11 and C++20 (including the concurrency/lifecycle tests).
Note this is an ABI change (method const-ness and Instance()'s return type
alter mangled names) - acceptable for a source library.
Claude-Session: https://claude.ai/code/session_01NUt3c1wdseRtRSSXfg3MCK
Co-authored-by: Claude <noreply@anthropic.com>
0 commit comments