-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93890e3
commit 580f430
Showing
55 changed files
with
2,077 additions
and
451 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
cmake_minimum_required(VERSION 3.16) | ||
|
||
project(13_generator LANGUAGES CXX) | ||
|
||
include(FetchContent) | ||
FetchContent_Declare(concurrencpp SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../..") | ||
FetchContent_MakeAvailable(concurrencpp) | ||
|
||
include(../../cmake/coroutineOptions.cmake) | ||
|
||
add_executable(13_generator source/main.cpp) | ||
|
||
target_compile_features(13_generator PRIVATE cxx_std_20) | ||
|
||
target_link_libraries(13_generator PRIVATE concurrencpp::concurrencpp) | ||
|
||
target_coroutine_options(13_generator) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include <fstream> | ||
#include <iostream> | ||
#include <filesystem> | ||
|
||
#include "concurrencpp/concurrencpp.h" | ||
|
||
concurrencpp::generator<std::string_view> read_lines(std::string_view text) { | ||
std::string_view::size_type pos = 0; | ||
std::string_view::size_type prev = 0; | ||
|
||
while ((pos = text.find('\n', prev)) != std::string::npos) { | ||
co_yield text.substr(prev, pos - prev); | ||
prev = pos + 1; | ||
} | ||
|
||
co_yield text.substr(prev); | ||
} | ||
|
||
concurrencpp::result<void> read_file_lines(const std::filesystem::path& path, | ||
std::shared_ptr<concurrencpp::thread_pool_executor> background_executor, | ||
std::shared_ptr<concurrencpp::thread_pool_executor> thread_pool_executor) { | ||
// make sure we don't block in a thread that is used for cpu-processing | ||
co_await concurrencpp::resume_on(background_executor); | ||
|
||
std::ifstream stream(path.c_str(), std::ios::binary | std::ios::in); | ||
std::string file_content(std::istreambuf_iterator<char>(stream), {}); | ||
|
||
// make sure we don't process cpu-bound tasks on the background executor | ||
co_await concurrencpp::resume_on(thread_pool_executor); | ||
|
||
for (const auto& line : read_lines(file_content)) { | ||
std::cout << "read a new line. size: " << line.size() << std::endl; | ||
std::cout << "line: " << std::endl; | ||
std::cout << line; | ||
std::cout << "\n==============" << std::endl; | ||
} | ||
} | ||
|
||
int main(const int argc, const char* argv[]) { | ||
if (argc < 2) { | ||
const auto help_msg = "please pass all necessary arguments\n argv[1] - the file to be read\n"; | ||
std::cerr << help_msg << std::endl; | ||
return -1; | ||
} | ||
|
||
const auto file_path = std::string(argv[1]); | ||
|
||
concurrencpp::runtime runtime; | ||
const auto thread_pool_executor = runtime.thread_pool_executor(); | ||
const auto background_executor = runtime.background_executor(); | ||
|
||
read_file_lines(file_path, thread_pool_executor, background_executor).get(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.