#pragma once #include #include #include #include #include class MT2Q4 { public: using FPtr = bool(*)(const std::string&, const std::string&); static bool findText(const std::string& text, const std::string& filePath); static std::future findTextAsync(FPtr pFinder, const std::string& text, const std::string& filePath); }; bool MT2Q4::findText(const std::string& text, const std::string& filePath) { std::ifstream in(filePath); if (!in.good()) { std::cout << "\n can't open \"" << filePath << "\""; return false; } std::string fileText; while (in.good()) { char ch; in >> ch; fileText += ch; } size_t pos = fileText.find(text); return pos < fileText.size(); } std::future MT2Q4::findTextAsync(FPtr pFinder, const std::string& text, const std::string& filePath) { std::future f; std::thread t( [&]() { std::promise p; std::future f_thread(p.get_future()); f = std::move(f_thread); bool result = (*pFinder)(text, filePath); p.set_value(result); } ); t.detach(); return f; }