/
UCS
/
sqlite_orm
Обзор
Документация
Войти
/
UCS
/
sqlite_orm
Код
Запросы
0
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
dev/connection_holder.h
75 строк
2 KB
klaus triendl
`std::atomic` requires explicit initialization syntax
16 июн 2022, 11:29
16 июн 2022, 11:29
9f945bc
Код
Авторство
О чём код?
#pragma once #include <sqlite3.h> #include <atomic> #include <string> // std::string #include "error_code.h" namespace sqlite_orm { namespace internal { struct connection_holder { connection_holder(std::string filename_) : filename(move(filename_)) {} void retain() { if(1 == ++this->_retain_count) { auto rc = sqlite3_open(this->filename.c_str(), &this->db); if(rc != SQLITE_OK) { throw_translated_sqlite_error(db); } } } void release() { if(0 == --this->_retain_count) { auto rc = sqlite3_close(this->db); if(rc != SQLITE_OK) { throw_translated_sqlite_error(db); } } } sqlite3* get() const { return this->db; } int retain_count() const { return this->_retain_count; } const std::string filename; protected: sqlite3* db = nullptr; std::atomic_int _retain_count{}; }; struct connection_ref { connection_ref(connection_holder& holder_) : holder(holder_) { this->holder.retain(); } connection_ref(const connection_ref& other) : holder(other.holder) { this->holder.retain(); } connection_ref(connection_ref&& other) : holder(other.holder) { this->holder.retain(); } ~connection_ref() { this->holder.release(); } sqlite3* get() const { return this->holder.get(); } protected: connection_holder& holder; }; } }