In the following code sample, the processes are assigned to different contexts. And each App object will be bound to only one context. Though all the processes run the same code, only those belonging to the context will execute the methods of App. In order to implement this goal, as shown in the code, I have to add if
control statement in all methods of the App class. So my question: are there some elegant ways to do the same work?
class Context {
public:
bool ContainsCurrentProcess();
...
private:
std::vector<int> procs_;
...
};
bool Context::ContainsCurrentProcess() {
if (current_process_id belongs to procs_ )
return true;
else
return false;
}
class App {
public:
App(Context *ctx, ...) {}
void Method1();
void Method2();
void Method3();
...
private:
Context *ctx_;
...
};
void App::Method1() {
if (ctx_->ContainsCurrentProcess()) {
...
}
}
void App::Method2() {
if (ctx_->ContainsCurrentProcess()) {
...
}
}
void App::Method3() {
if (ctx_->ContainsCurrentProcess()) {
...
}
}
Aucun commentaire:
Enregistrer un commentaire