Alternative to using macros to change old code
I am trying to rephrase old code with RAII pattern . The old code manages resource allocation but expects users to lock / unlock the resource explicitly. So, for example, something like this (just an example, not real code):
void modify(int id){
lock();
modify_data_related_to_id(id);
unlock();
}
becomes something like this:
void modify(int id){
RAIIOBJ obj;
modify_data_related_to_id(id);
}
The advantage is that you don't have to remember the object unlock
. When an item RAIIOBJ
is removed, the unlock will be called automatically in its destructor.
The problem is that there are a ton of places to re-evaluate. I thought I would use macros and replace the call lock
with an RAII call and unlock
become a no-op function, like this:
#define lock() RAIIOBJ obj;
void unlock(){ //empty function
}
This way I don't have to change every place that lock / unlock calls in order to use the RAII method. That said, is there any better way - maybe a template for some problem - to do this than using a macro.
source to share
No one has answered this question yet
Check out similar questions: