mirror of
https://github.com/Expand-sys/CCash
synced 2026-03-22 12:37:08 +11:00
✨ ChangeFlag class
This commit is contained in:
parent
1ea1618228
commit
ba20e461f6
5 changed files with 43 additions and 33 deletions
|
|
@ -16,12 +16,13 @@ target_sources(${PROJECT_NAME} PRIVATE
|
|||
src/admin_filter.cpp
|
||||
src/bank_api.cpp
|
||||
src/bank.cpp
|
||||
src/base64.c #temp
|
||||
src/change_flag.cpp
|
||||
src/log.cpp
|
||||
src/transaction.cpp
|
||||
src/user_filter.cpp
|
||||
src/user.cpp
|
||||
src/xxhash.c
|
||||
src/base64.c #temp
|
||||
)
|
||||
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC include)
|
||||
|
|
|
|||
|
|
@ -2,12 +2,15 @@
|
|||
#include <iostream> //temporary
|
||||
#include <fstream>
|
||||
#include <shared_mutex>
|
||||
#include <atomic>
|
||||
#include <drogon/HttpTypes.h>
|
||||
#include <parallel-hashmap/parallel_hashmap/phmap.h>
|
||||
#include "error_responses.hpp" //temporary
|
||||
#include "parallel-hashmap/parallel_hashmap/phmap.h"
|
||||
#include "user.h"
|
||||
|
||||
#if CONSERVATIVE_DISK_SAVE
|
||||
#include "change_flag.h"
|
||||
#endif
|
||||
|
||||
using BankResponse = std::pair<drogon::HttpStatusCode, Json::Value>;
|
||||
|
||||
class Bank
|
||||
|
|
@ -22,9 +25,8 @@ private:
|
|||
std::mutex>
|
||||
users;
|
||||
|
||||
std::atomic<bool> change_flag = false; //if true changes have been made
|
||||
ChangeFlag save_flag;
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief size_l should be grabbed if the operation MODIFIES the size (shared), this is so that when save claims unique
|
||||
*
|
||||
|
|
@ -37,17 +39,10 @@ private:
|
|||
*/
|
||||
std::shared_mutex send_funds_l;
|
||||
|
||||
#if CONSERVATIVE_DISK_SAVE
|
||||
void ChangesMade() noexcept; //called after making changes
|
||||
void ChangesSaved() noexcept; //called after saving
|
||||
#endif
|
||||
|
||||
public:
|
||||
std::string admin_pass;
|
||||
|
||||
#if CONSERVATIVE_DISK_SAVE
|
||||
bool GetChangeState() noexcept;
|
||||
#endif
|
||||
bool GetChangeState() const noexcept;
|
||||
|
||||
BankResponse GetBal(const std::string &name) const noexcept;
|
||||
BankResponse GetLogs(const std::string &name) noexcept;
|
||||
|
|
|
|||
13
include/change_flag.h
Normal file
13
include/change_flag.h
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#pragma once
|
||||
#include <atomic>
|
||||
|
||||
class ChangeFlag
|
||||
{
|
||||
private:
|
||||
std::atomic<bool> change_flag = false; //if true changes have been made
|
||||
|
||||
public:
|
||||
void SetChangesOn() noexcept;
|
||||
void SetChangesOff() noexcept;
|
||||
bool GetChangeState() const noexcept;
|
||||
};
|
||||
27
src/bank.cpp
27
src/bank.cpp
|
|
@ -2,20 +2,7 @@
|
|||
|
||||
using namespace drogon;
|
||||
|
||||
#if CONSERVATIVE_DISK_SAVE
|
||||
void Bank::ChangesMade() noexcept
|
||||
{
|
||||
return change_flag.store(1, std::memory_order_release);
|
||||
}
|
||||
void Bank::ChangesSaved() noexcept
|
||||
{
|
||||
return change_flag.store(0, std::memory_order_release);
|
||||
}
|
||||
bool Bank::GetChangeState() noexcept
|
||||
{
|
||||
return change_flag.load(std::memory_order_acquire);
|
||||
}
|
||||
#endif
|
||||
bool Bank::GetChangeState() const noexcept { return save_flag.GetChangeState(); }
|
||||
|
||||
BankResponse Bank::GetBal(const std::string &name) const noexcept
|
||||
{
|
||||
|
|
@ -92,7 +79,7 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
|
|||
b.log.AddTrans(std::move(temp));
|
||||
});
|
||||
#if CONSERVATIVE_DISK_SAVE
|
||||
ChangesMade();
|
||||
save_flag.SetChangesOn();
|
||||
#endif
|
||||
}
|
||||
return state;
|
||||
|
|
@ -118,7 +105,7 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
|
|||
b.balance += amount;
|
||||
});
|
||||
#if CONSERVATIVE_DISK_SAVE
|
||||
ChangesMade();
|
||||
save_flag.SetChangesOn();
|
||||
#endif
|
||||
}
|
||||
return state;
|
||||
|
|
@ -139,7 +126,7 @@ void Bank::ChangePassword(const std::string &name, std::string &&new_pass) noexc
|
|||
u.password = XXH3_64bits(new_pass.data(), new_pass.size());
|
||||
});
|
||||
#if CONSERVATIVE_DISK_SAVE
|
||||
ChangesMade();
|
||||
save_flag.SetChangesOn();
|
||||
#endif
|
||||
}
|
||||
BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept
|
||||
|
|
@ -149,7 +136,7 @@ BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept
|
|||
}))
|
||||
{
|
||||
#if CONSERVATIVE_DISK_SAVE
|
||||
ChangesMade();
|
||||
save_flag.SetChangesOn();
|
||||
#endif
|
||||
return BankResponse(k200OK, "Balance set!");
|
||||
}
|
||||
|
|
@ -271,7 +258,7 @@ bool Bank::AdminVerifyPass(const std::string &attempt) noexcept
|
|||
void Bank::Save()
|
||||
{
|
||||
#if CONSERVATIVE_DISK_SAVE
|
||||
if (GetChangeState())
|
||||
if (save_flag.GetChangeState())
|
||||
{
|
||||
#endif
|
||||
Json::Value temp;
|
||||
|
|
@ -300,7 +287,7 @@ void Bank::Save()
|
|||
user_save.close();
|
||||
}
|
||||
#if CONSERVATIVE_DISK_SAVE
|
||||
ChangesSaved();
|
||||
save_flag.SetChangesOff();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
|
|
|||
14
src/change_flag.cpp
Normal file
14
src/change_flag.cpp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include "change_flag.h"
|
||||
|
||||
void ChangeFlag::SetChangesOn() noexcept
|
||||
{
|
||||
return change_flag.store(1, std::memory_order_release);
|
||||
}
|
||||
void ChangeFlag::SetChangesOff() noexcept
|
||||
{
|
||||
return change_flag.store(0, std::memory_order_release);
|
||||
}
|
||||
bool ChangeFlag::GetChangeState() const noexcept
|
||||
{
|
||||
return change_flag.load(std::memory_order_acquire);
|
||||
}
|
||||
Loading…
Reference in a new issue