diff --git a/cpp/DBHostObject.cpp b/cpp/DBHostObject.cpp index a9891f3..648dc89 100644 --- a/cpp/DBHostObject.cpp +++ b/cpp/DBHostObject.cpp @@ -60,7 +60,7 @@ void DBHostObject::on_rollback() { } void DBHostObject::on_update(const std::string &table, - const std::string &operation, int row_id) { + const std::string &operation, long long row_id) { if (update_hook_callback != nullptr) { invoker->invokeAsync( [this, callback = update_hook_callback, table, operation, row_id] { @@ -68,7 +68,7 @@ void DBHostObject::on_update(const std::string &table, res.setProperty(rt, "table", jsi::String::createFromUtf8(rt, table)); res.setProperty(rt, "operation", jsi::String::createFromUtf8(rt, operation)); - res.setProperty(rt, "rowId", jsi::Value(row_id)); + res.setProperty(rt, "rowId", jsi::Value(static_cast(row_id))); callback->asObject(rt).asFunction(rt).call(rt, res); }); diff --git a/cpp/DBHostObject.h b/cpp/DBHostObject.h index 3cc4625..b2f8d73 100644 --- a/cpp/DBHostObject.h +++ b/cpp/DBHostObject.h @@ -52,21 +52,23 @@ class JSI_EXPORT DBHostObject : public jsi::HostObject { std::string &auth_token, int sync_interval); #endif - std::vector getPropertyNames(jsi::Runtime &rt); - jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID); + std::vector getPropertyNames(jsi::Runtime &rt) override; + jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID) override; void set(jsi::Runtime &rt, const jsi::PropNameID &name, - const jsi::Value &value); - void on_update(const std::string& table, const std::string &operation, int row_id); + const jsi::Value &value) override; + void on_update(const std::string &table, const std::string &operation, + long long row_id); void on_commit(); void on_rollback(); void invalidate(); - ~DBHostObject(); + ~DBHostObject() override; private: std::set> pending_reactive_queries; void auto_register_update_hook(); void create_jsi_functions(); - void flush_pending_reactive_queries(const std::shared_ptr& resolve); + void + flush_pending_reactive_queries(const std::shared_ptr &resolve); std::unordered_map function_map; std::string base_path; diff --git a/cpp/DumbHostObject.h b/cpp/DumbHostObject.h index c73a677..9b5e406 100644 --- a/cpp/DumbHostObject.h +++ b/cpp/DumbHostObject.h @@ -13,16 +13,17 @@ namespace jsi = facebook::jsi; class JSI_EXPORT DumbHostObject : public jsi::HostObject { public: - DumbHostObject() {}; + DumbHostObject() = default; - DumbHostObject(std::shared_ptr> metadata); + explicit DumbHostObject( + std::shared_ptr> metadata); - std::vector getPropertyNames(jsi::Runtime &rt); + std::vector getPropertyNames(jsi::Runtime &rt) override; - jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID); + jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID) override; void set(jsi::Runtime &rt, const jsi::PropNameID &name, - const jsi::Value &value); + const jsi::Value &value) override; std::vector values; diff --git a/cpp/PreparedStatementHostObject.h b/cpp/PreparedStatementHostObject.h index bf5fb9a..ba4bb72 100644 --- a/cpp/PreparedStatementHostObject.h +++ b/cpp/PreparedStatementHostObject.h @@ -10,6 +10,7 @@ #endif #include "ThreadPool.h" #include +#include namespace opsqlite { namespace jsi = facebook::jsi; @@ -29,26 +30,26 @@ class PreparedStatementHostObject : public jsi::HostObject { sqlite3 *db, std::string name, sqlite3_stmt *stmt, std::shared_ptr js_call_invoker, std::shared_ptr thread_pool) - : _db(db), _name(name), _stmt(stmt), _js_call_invoker(js_call_invoker), - _thread_pool(thread_pool) {}; + : _db(db), _name(std::move(name)), _stmt(stmt), _js_call_invoker(std::move(js_call_invoker)), + _thread_pool(std::move(thread_pool)) {}; #endif - virtual ~PreparedStatementHostObject(); + ~PreparedStatementHostObject() override; - std::vector getPropertyNames(jsi::Runtime &rt); + std::vector getPropertyNames(jsi::Runtime &rt) override; - jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID); + jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID) override; private: - std::shared_ptr _js_call_invoker; - std::shared_ptr _thread_pool; - std::string _name; sqlite3 *_db; + std::string _name; #ifdef OP_SQLITE_USE_LIBSQL libsql_stmt_t _stmt; #else // This shouldn't be de-allocated until sqlite3_finalize is called on it sqlite3_stmt *_stmt; #endif + std::shared_ptr _js_call_invoker; + std::shared_ptr _thread_pool; }; } // namespace opsqlite diff --git a/cpp/SmartHostObject.cpp b/cpp/SmartHostObject.cpp index 4b7d00d..c306da8 100644 --- a/cpp/SmartHostObject.cpp +++ b/cpp/SmartHostObject.cpp @@ -9,8 +9,9 @@ std::vector SmartHostObject::getPropertyNames(jsi::Runtime &rt) { std::vector keys; - for (auto field : fields) { - keys.push_back(jsi::PropNameID::forAscii(rt, field.first)); + keys.reserve(fields.size()); +for (const auto& field : fields) { + keys.emplace_back(jsi::PropNameID::forAscii(rt, field.first)); } return keys; @@ -20,7 +21,7 @@ jsi::Value SmartHostObject::get(jsi::Runtime &rt, const jsi::PropNameID &propNameID) { auto name = propNameID.utf8(rt); - for (auto field : fields) { + for (const auto& field : fields) { auto fieldName = field.first; if (fieldName == name) { return to_jsi(rt, field.second); diff --git a/cpp/SmartHostObject.h b/cpp/SmartHostObject.h index 744594e..87ffddd 100644 --- a/cpp/SmartHostObject.h +++ b/cpp/SmartHostObject.h @@ -11,11 +11,11 @@ namespace jsi = facebook::jsi; class JSI_EXPORT SmartHostObject : public jsi::HostObject { public: - SmartHostObject() {}; + SmartHostObject() = default; - std::vector getPropertyNames(jsi::Runtime &rt); + std::vector getPropertyNames(jsi::Runtime &rt) override; - jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID); + jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID) override; std::vector> fields; }; diff --git a/cpp/ThreadPool.cpp b/cpp/ThreadPool.cpp index e1103da..545da0b 100644 --- a/cpp/ThreadPool.cpp +++ b/cpp/ThreadPool.cpp @@ -16,7 +16,7 @@ ThreadPool::ThreadPool() : done(false) { // The threads will execute the private member `doWork`. Note that we need // to pass a reference to the function (namespaced with the class name) as // the first argument, and the current object as second argument - threads.push_back(std::thread(&ThreadPool::doWork, this)); + threads.emplace_back(&ThreadPool::doWork, this); } } @@ -40,7 +40,7 @@ ThreadPool::~ThreadPool() { // This function will be called by the server every time there is a request // that needs to be processed by the thread pool -void ThreadPool::queueWork(std::function task) { +void ThreadPool::queueWork(const std::function& task) { // Grab the mutex std::lock_guard g(workQueueMutex); @@ -66,7 +66,7 @@ void ThreadPool::doWork() { return !workQueue.empty() || done; }); - // If we are shutting down exit witout trying to process more work + // If we are shutting down exit without trying to process more work if (done) { break; } @@ -110,7 +110,7 @@ void ThreadPool::restartPool() { // The threads will execute the private member `doWork`. Note that we need // to pass a reference to the function (namespaced with the class name) as // the first argument, and the current object as second argument - threads.push_back(std::thread(&ThreadPool::doWork, this)); + threads.emplace_back(&ThreadPool::doWork, this); } done = false; diff --git a/cpp/ThreadPool.h b/cpp/ThreadPool.h index 2e86509..cb84a30 100644 --- a/cpp/ThreadPool.h +++ b/cpp/ThreadPool.h @@ -14,12 +14,12 @@ class ThreadPool { public: ThreadPool(); ~ThreadPool(); - void queueWork(std::function task); + void queueWork(const std::function& task); void waitFinished(); void restartPool(); private: - unsigned int busy; + unsigned int busy{}; // This condition variable is used for the threads to wait until there is work // to do std::condition_variable_any workQueueConditionVariable; diff --git a/cpp/utils.cpp b/cpp/utils.cpp index 032b7dc..b89fe3b 100644 --- a/cpp/utils.cpp +++ b/cpp/utils.cpp @@ -13,7 +13,7 @@ namespace opsqlite { namespace jsi = facebook::jsi; -jsi::Value to_jsi(jsi::Runtime &rt, const JSVariant &value) { +inline jsi::Value to_jsi(jsi::Runtime &rt, const JSVariant &value) { if (std::holds_alternative(value)) { return std::get(value); } else if (std::holds_alternative(value)) { @@ -74,7 +74,7 @@ jsi::Value to_jsi(jsi::Runtime &rt, const JSVariant &value) { // value); } -JSVariant to_variant(jsi::Runtime &rt, const jsi::Value &value) { +inline JSVariant to_variant(jsi::Runtime &rt, const jsi::Value &value) { if (value.isNull() || value.isUndefined()) { return JSVariant(nullptr); } else if (value.isBool()) {