diff --git a/include/bank.h b/include/bank.h index 242e9b6..d5abb8f 100644 --- a/include/bank.h +++ b/include/bank.h @@ -63,6 +63,6 @@ public: BankResponse AddUser(const std::string &name, uint32_t init_bal, std::string &&init_pass) noexcept; BankResponse DelUser(const std::string &name) noexcept; - void Save(); + const char *Save(); void Load(); }; \ No newline at end of file diff --git a/main.cpp b/main.cpp index 6fb3325..1bd3116 100644 --- a/main.cpp +++ b/main.cpp @@ -21,16 +21,8 @@ static Bank bank; void SaveSig(int s) { - std::cout << "\nSaving on close...\n"; - if (bank.GetChangeState()) - { - std::cout << " to disk...\n"; - bank.Save(); - } - else - { - std::cout << " no changes...\n"; - } + std::cout << "\nSaving on close...\n" + << bank.Save(); exit(1); } @@ -87,23 +79,14 @@ int main(int argc, char **argv) const unsigned long saving_freq = std::stoul(std::string(argv[2])); if (saving_freq) //if saving frequency is 0 then auto saving is turned off { - std::thread([saving_freq]() - { - while (1) - { - std::this_thread::sleep_for(std::chrono::minutes(saving_freq)); - std::cout << "Saving " << std::time(0) << "...\n"; - if (bank.GetChangeState()) - { - std::cout << " to disk...\n"; - bank.Save(); - } - else - { - std::cout << " no changes...\n"; - } - } - }) + std::thread([saving_freq]() { + while (1) + { + std::this_thread::sleep_for(std::chrono::minutes(saving_freq)); + std::cout << "Saving " << std::time(0) << "...\n" + << bank.Save(); + } + }) .detach(); } } //destroying setup variables diff --git a/src/bank.cpp b/src/bank.cpp index 7f7498d..1de7e5b 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -263,37 +263,48 @@ BankResponse Bank::DelUser(const std::string &name) noexcept return {k404NotFound, "\"User not found\""}; } } -void Bank::Save() +const char *Bank::Save() { - static thread_local Json::Value temp; +#if CONSERVATIVE_DISK_SAVE + if (GetChangeState()) + { +#endif + return " to disk...\n"; - //loading info into json temp - { - std::unique_lock lock{save_lock}; - for (const auto &u : users) + static thread_local Json::Value temp; + + //loading info into json temp { - //we know it contains this key but we call this func to grab mutex - users.if_contains(u.first, [&u](const User &u_val) { temp[u.first] = u_val.Serialize(); }); + std::unique_lock lock{save_lock}; + for (const auto &u : users) + { + //we know it contains this key but we call this func to grab mutex + users.if_contains(u.first, [&u](const User &u_val) { temp[u.first] = u_val.Serialize(); }); + } } - } - if (temp.isNull()) - { - throw std::invalid_argument("Saving Failed\n"); + if (temp.isNull()) + { + throw std::invalid_argument("Saving Failed\n"); + } + else + { + static thread_local std::ofstream user_save(users_location); + static thread_local Json::StreamWriterBuilder builder; + static thread_local const std::unique_ptr writer(builder.newStreamWriter()); + writer->write(temp, &user_save); + user_save.close(); + } +#if CONSERVATIVE_DISK_SAVE +#if MULTI_THREADED + save_flag.SetChangesOff(); +#else + save_flag = true; +#endif } else { - static thread_local std::ofstream user_save(users_location); - static thread_local Json::StreamWriterBuilder builder; - static thread_local const std::unique_ptr writer(builder.newStreamWriter()); - writer->write(temp, &user_save); - user_save.close(); + return " no changes...\n"; } -#if CONSERVATIVE_DISK_SAVE -#if MULTI_THREADED - save_flag.SetChangesOff(); -#else - save_flag = true; -#endif #endif }