[英]How do I do a check before every read and write of a variable, c++, access control
我試圖在調試模式下每當寫入或讀取值時進行檢查,以在發送前緩存數據競爭和其他線程錯誤。 該系統僅適用於調試模式,只能作為信息收集系統使用,因此在發布構建時沒有開銷。
須藤代碼:
template<T>
struct Resource
{
/*
Here I need to somehow do a check on all reads and writes?
How would this class look if it just where to print "read" and "write",
on their corresponding actions, and is this even possible?
Edit: The resource class works like wrapper of another type,
this means the it should also function as so. This can be achieved
by overriding the &operator.
Maybe there is something else that can be overwritten in order
to intercept writes?
*/
explicit operator T&() { return resource; }
}
class SomeType()
{
public:
uint32_t GetSomething();
uint32_t somethingElse;
}
Resource<SomeType> resource1;
Resource<SomeType> resource2;
Resource<SomeType> resource3;
AccessPolicy accessPolicy;
accessPolicy.AddResource(resource1, EAccessPolicy::Read);
accessPolicy.AddResource(resource2, EAccessPolicy::Write);
Task task = Task(&ExampleTask, accessPolicy);
static void ExampleTask()
{
auto something = resource1.GetSomething(); // Allowed
resource2 = SomeClass(); // Allowed
resource2.somethingElse = 4;
resource3.GetSomething(); // Unauthorized access assert
}
你的意思是這樣的嗎?
template<T>
struct Resource
{
Resource(const T &value) { v = value; }
T & GetSomething(Task & task) {
if (task.is_allowed(EAccessPolicy::Read, this)) {
std::cout << "read" << std::endl;
return v;
} else {
// error
}
}
void SetSomething(Task & task, const T & t) {
if (task.is_allowed(EAccessPolicy::Write, this)) {
std::cout << "write" << std::endl;
v = t;
} else {
// error
}
}
private:
T v;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.