///////////////////////////////////////////////////////////////////// // Executive.cpp - directs execution of all program tasks // #include "Executive.h" #include #include #include const std::string globalFileSpec = "../Executive/Executive.h"; /*----< implicitly calls constructors for composed members >-------*/ Executive::Executive() { std::cout << "\n Executive starting up"; } /*----< implicitly calls destructiors for composed members >-------*/ Executive::~Executive() { std::cout << "\n Executive shutting down"; } /*----< directs execution of application >-------------------------*/ void Executive::doWork() { t1.doWork(); // trivial safeInvoke(t2, &Task2::doWork, 27); // throws double num = 1.0, denom = 3.0, result; std::function arithOp = [&]() { result = t3.doWork(num, denom); }; safeInvoke(arithOp); std::cout << "\n " << 1.0 << "\\" << denom << " = " << result; std::string fileName = "../Executive/Executive.h"; std::function fileOp = [&]() { t3.doWork(fileName); }; safeInvoke(fileOp); } /*----< defines ATest's pure virtual test method >-----------------*/ bool Executive::test() { bool tst1 = checkResult(t1.testPackage(), "Task1", "always passes"); bool tst2 = checkResult(t2.testPackage(), "Task2", "always fails"); bool tst3 = checkResult(t3.testPackage(), "Task3", "testing file access and arithmetic"); return tst1 && tst2 && tst3; } /*----< private test application test class focused on test >------*/ /* * often test class will do some test specific initialization */ class TestExecutive : public ATest { public: TestExecutive() { std::cout << "\n Testing Executive"; std::cout << "\n ==================="; } /*----< invokes executive testPackage >--------------------------*/ /* * - Executive::testPackage() invokes the testPackage function * for each composed member, e.g., Task1 and Task2. * - This causes a chaining of tests for the entire application. */ bool test() { Executive ex; return ex.testPackage(); } }; ///////////////////////////////////////////////////////////////////// // Cosmetic struct simply ensures that program output text // ends with two newlines struct Cosmetic { ~Cosmetic() { std::cout << "\n\n"; } }; /*----< application entry point >----------------------------------*/ int main(int argc, char* argv[]) { Cosmetic cos; // first object constructed is last to be destroyed std::cout << "\n Demonstrating Executive"; std::cout << "\n ========================="; Executive ex; ex.doWork(); std::cout << "\n"; TestExecutive te; if (te.test()) std::cout << "\n--Executive and its minions passed their tests"; else std::cout << "\n--Executive or at least one of its minions failed their tests"; }