///////////////////////////////////////////// // MT1Q5b.cpp - Scope class #include "MT1Q5b.h" #include #include #include #include int main() { std::cout << "\n MT1Q5b - Scope class and sorting by complexity"; std::cout << "\n ================================================\n"; //Scope s1; //s1.name() = "Scope1"; //s1.file() = "MT1Q2.cpp"; //s1.type() = "function"; //s1.start() = 23; //s1.end() = 88; //s1.complexity() = 6; //s1.childType() = Scope::ChildType() = { "for", "if", "for" }; IScope* ps1 = IScope::Create( "Scope1", "MT1Q2.cpp", "function", 23, 88, 6, { "for", "if", "for" } ); IScope* ps2 = IScope::Create( "Scope2", "MT1Q2.cpp", "for", 27, 42, 3, { "for", "if" } ); IScope* ps3 = IScope::Create( "Scope3", "MT1Q2.cpp", "for", 29, 40, 2, { "if" } ); IScope* ps4 = IScope::Create( "Scope4", "MT1Q2.cpp", "if", 35, 39, 1, {} ); IScope* ps5 = IScope::Create( "Scope5", "MT1Q2.cpp", "if", 41, 42, 1, {} ); IScope* ps6 = IScope::Create( "Scope6", "MT1Q2.cpp", "if", 45, 52, 1, {} ); IScope* ps7 = IScope::Create( "Scope7", "MT1Q2.cpp", "for", 60, 84, 1, {} ); // sorting vector of scopes using Scopes = std::vector; Scopes scopes{ ps6, ps4, ps3, ps1, ps2, ps5 }; std::function less = [](IScope* pS1, IScope* pS2) { return pS1->operator<(pS2); }; std::sort(std::begin(scopes), std::end(scopes), less); for (auto pScope : scopes) { std::cout << "\n " << pScope->name() << " - complexity = " << pScope->complexity(); std::cout << ", type = " << pScope->type(); if (pScope->numberChildren() > 0) { std::cout << ", child types = "; for (auto type : pScope->childType()) std::cout << type << " "; } } // non-stressing test of scope matching std::cout << "\n"; for (auto pScope : scopes) { if (ps2->match(pScope)) std::cout << "\n " << pScope->name() << " matches " << ps2->name(); else std::cout << "\n " << pScope->name() << " does not match " << ps2->name(); } delete ps1; delete ps2; delete ps3; delete ps4; delete ps5; delete ps6; delete ps7; std::cout << "\n\n"; }