Guest User

Untitled

a guest
Mar 13th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <functional>
  2.  
  3. List * prescriptions; // List is a test header, but ihats not where the problem is
  4.  
  5. using cb_prescription = std::function<void(Prescription * prescription)>;
  6. void loop_prescriptions (cb_prescription callback, bool add);
  7.  
  8. int main ()
  9. {
  10.   char * name = new char[6];
  11.   strcpy(name, "abcdef");
  12.  
  13.     prescriptions = new List(Sorted); //creates an empty list, but lets say there are some elements
  14.   Prescription * paper = new Prescription(); //has method hasPatient, which compares to char arrays and returns boolean
  15.  
  16.   loop_prescriptions ([&] (Prescription * paper) { //and somewhere in here
  17.     if (paper->hasPatient(name)) {
  18.         paper->display();
  19.     }
  20.   });
  21.  
  22.  
  23.   return 0;
  24. }
  25.  
  26.  
  27.  
  28. void loop_prescriptions (cb_prescription callback, bool add = true)
  29. {
  30.   //loop through all the elements
  31.     for (int i = 1; i <= prescriptions->noOfElements(); i++) {
  32.       //get the element and send it to callback for usage
  33.         Prescription * prescription = (Prescription *) prescriptions->removeNo(i); //removeNo, returns a pointer to the prescription class (Prescription *)
  34.         if (add) { //since the element has been removed, it should be readded unless the developer says so
  35.             prescriptions->add(prescription);
  36.         }
  37.         callback(prescription); //return the element
  38.     }
  39. }
Add Comment
Please, Sign In to add comment