lundi 16 août 2021

C++ QT terminate to parent

I am looking for a nice way to stop running instructions of different classes methods(called from, let's say, manager) and return to manager in case of an error in these classes methods.

What I have built is a structure consisting of manager class, which runs methods of different workers classes, these workers classes sometimes have their own workers, workers of workers can have their own workers (typical tree structure, but there's a point why I call them workers), etc.

So let's say we have this type of structure, where each class delegates part of their job to different classes, while also doing something on their own. All workers work on, let's say Message structure with fields text and error, as if an error occurs, they have to report what type of error it is, and if possible provide a text explanation of this error. (in case of success, the error is NO_ERROR and text is filled with the work they have done)

Manager->WorkerA->Worker_B->WorkerC->WorkerD

So, in case of an error in WorkerD, which will prevent all supervisors from finishing their jobs, we would have to:

  1. Check for error in WorkerD - there is an error!
  2. Change message.error from NO_ERROR to ERROR, change message.text if it is necessary
  3. Return message //here begins the part I would like to omit somehow:
  4. WorkerC has to check after WorkerD has finished, if there's an error, if there is, immediately return(workerC shouldn't continue his work)
  5. WorkerB has to check after WorkerC has finished, if there's an error, if there is, immediately return(workerB shouldn't continue his work)
  6. WorkerA has to check after WorkerC has finished, if there's an error, if there is, immediately return(workerA shouldn't continue his work)
  7. Manager checks the error after WorkerA has finished. If there is no error, he does some additional job, if there is - he just passes the message with text and error further.

I am looking for a simpler way to omit steps 4-6, so once there is an error in any worker job, he fulfills the error and text, and returns this message through all the intermediate methods up to the Manager. Something like return, but also return parent functions.

I know it can be done with exceptions, but is there a way to NOT do it with exceptions and still reducing the need to constantly add 'do func1(), if there is an error, stop working and return this error'?

Example pseudocode:

manager, worker1, worker2

manager.work{
//do something
switch(delegate_to) {
case worker:
 message=worker1.work
case accountant:
 message=accountant1.work
case differentworker:
 message=differentworker.work
}
if(message.error=NO_ERROR)
   all_ok
else
   not_all_ok
}

worker1.work
{
message=do_something()
if(message.error) //this is what I am trying to omit
   return message;
//some work
message=worker2.work
if(message.error) //this is what I am trying to omit
   return message;
//some work

return message;
}

And a part of my program (I am looking for a solution to skip this as I have this type of instructions on multiple layers)


    // lots of code
    out << getOperators(controlList[2], &msg); 
    if(msg.error!="") return msg;
    out << getProductDetails(controlList[3], &msg); 
    if(msg.error!="") return msg;
    //lots of code
    return msg;

Aucun commentaire:

Enregistrer un commentaire