mirror of
https://github.com/Expand-sys/CCash
synced 2026-03-22 12:37:08 +11:00
🎨 simplified with macro
This commit is contained in:
parent
e610615e5f
commit
a5da4c9a04
1 changed files with 16 additions and 56 deletions
72
src/bank.cpp
72
src/bank.cpp
|
|
@ -2,6 +2,14 @@
|
||||||
|
|
||||||
using namespace drogon;
|
using namespace drogon;
|
||||||
|
|
||||||
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
|
#if MULTI_THREADED
|
||||||
|
#define SET_CHANGES_ON save_flag.SetChangesOn()
|
||||||
|
#else
|
||||||
|
#define SET_CHANGES_ON save_flag = true
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
__attribute__((always_inline)) inline bool ValidUsername(const std::string &name) noexcept
|
__attribute__((always_inline)) inline bool ValidUsername(const std::string &name) noexcept
|
||||||
{
|
{
|
||||||
if (name.size() < min_name_size || name.size() > max_name_size)
|
if (name.size() < min_name_size || name.size() > max_name_size)
|
||||||
|
|
@ -124,13 +132,7 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
|
||||||
#else
|
#else
|
||||||
users.modify_if(b_name, [amount](User &b) { b.balance += amount; });
|
users.modify_if(b_name, [amount](User &b) { b.balance += amount; });
|
||||||
#endif
|
#endif
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
SET_CHANGES_ON;
|
||||||
#if MULTI_THREADED
|
|
||||||
save_flag.SetChangesOn();
|
|
||||||
#else
|
|
||||||
save_flag = true;
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
@ -143,26 +145,14 @@ bool Bank::VerifyPassword(const std::string &name, const std::string_view &attem
|
||||||
|
|
||||||
void Bank::ChangePassword(const std::string &name, const std::string &new_pass) noexcept
|
void Bank::ChangePassword(const std::string &name, const std::string &new_pass) noexcept
|
||||||
{
|
{
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
SET_CHANGES_ON;
|
||||||
#if MULTI_THREADED
|
|
||||||
save_flag.SetChangesOn();
|
|
||||||
#else
|
|
||||||
save_flag = true;
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
users.modify_if(name, [&new_pass](User &u) { u.password = xxHashStringGen{}(new_pass); });
|
users.modify_if(name, [&new_pass](User &u) { u.password = xxHashStringGen{}(new_pass); });
|
||||||
}
|
}
|
||||||
BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept
|
BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept
|
||||||
{
|
{
|
||||||
if (ValidUsername(name) && users.modify_if(name, [amount](User &u) { u.balance = amount; }))
|
if (ValidUsername(name) && users.modify_if(name, [amount](User &u) { u.balance = amount; }))
|
||||||
{
|
{
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
SET_CHANGES_ON;
|
||||||
#if MULTI_THREADED
|
|
||||||
save_flag.SetChangesOn();
|
|
||||||
#else
|
|
||||||
save_flag = true;
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
return {k204NoContent, std::nullopt}; //returns new balance
|
return {k204NoContent, std::nullopt}; //returns new balance
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -179,13 +169,7 @@ BankResponse Bank::ImpactBal(const std::string &name, int64_t amount) noexcept
|
||||||
uint32_t balance;
|
uint32_t balance;
|
||||||
if (ValidUsername(name) && users.modify_if(name, [&balance, amount](User &u) { balance = (u.balance < (amount * -1) ? u.balance = 0 : u.balance += amount); }))
|
if (ValidUsername(name) && users.modify_if(name, [&balance, amount](User &u) { balance = (u.balance < (amount * -1) ? u.balance = 0 : u.balance += amount); }))
|
||||||
{
|
{
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
SET_CHANGES_ON;
|
||||||
#if MULTI_THREADED
|
|
||||||
save_flag.SetChangesOn();
|
|
||||||
#else
|
|
||||||
save_flag = true;
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
return {k200OK, std::to_string(balance)}; //may return new balance
|
return {k200OK, std::to_string(balance)}; //may return new balance
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -210,13 +194,7 @@ BankResponse Bank::PruneUsers(time_t threshold_time, uint32_t threshold_bal) noe
|
||||||
if (u.balance < threshold_bal)
|
if (u.balance < threshold_bal)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
SET_CHANGES_ON;
|
||||||
#if MULTI_THREADED
|
|
||||||
save_flag.SetChangesOn();
|
|
||||||
#else
|
|
||||||
save_flag = true;
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
return ++deleted_count;
|
return ++deleted_count;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -238,13 +216,7 @@ BankResponse Bank::AddUser(const std::string &name, uint32_t init_bal, const std
|
||||||
if (users.try_emplace_l(
|
if (users.try_emplace_l(
|
||||||
name, [](User &) {}, init_bal, init_pass))
|
name, [](User &) {}, init_bal, init_pass))
|
||||||
{
|
{
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
SET_CHANGES_ON;
|
||||||
#if MULTI_THREADED
|
|
||||||
save_flag.SetChangesOn();
|
|
||||||
#else
|
|
||||||
save_flag = true;
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
return {k204NoContent, std::nullopt};
|
return {k204NoContent, std::nullopt};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -267,13 +239,7 @@ BankResponse Bank::DelUser(const std::string &name) noexcept
|
||||||
std::shared_lock<std::shared_mutex> lock{iter_lock};
|
std::shared_lock<std::shared_mutex> lock{iter_lock};
|
||||||
if (ValidUsername(name) && users.erase(name))
|
if (ValidUsername(name) && users.erase(name))
|
||||||
{
|
{
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
SET_CHANGES_ON;
|
||||||
#if MULTI_THREADED
|
|
||||||
save_flag.SetChangesOn();
|
|
||||||
#else
|
|
||||||
save_flag = true;
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
return {k204NoContent, std::nullopt};
|
return {k204NoContent, std::nullopt};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -294,13 +260,7 @@ void Bank::DelSelf(const std::string &name) noexcept
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
SET_CHANGES_ON;
|
||||||
#if MULTI_THREADED
|
|
||||||
save_flag.SetChangesOn();
|
|
||||||
#else
|
|
||||||
save_flag = true;
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
std::shared_lock<std::shared_mutex> lock{iter_lock};
|
std::shared_lock<std::shared_mutex> lock{iter_lock};
|
||||||
users.erase(name);
|
users.erase(name);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue