Advertisement
mfgnik

Untitled

Mar 6th, 2023 (edited)
843
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. struct FilterConfig {
  2.     std::string name;
  3.     std::vector<std::string> arguments;
  4. };
  5.  
  6. struct ParserResults {
  7.     std::string input_path;
  8.     std::string output_path;
  9.     std::vector<FilterConfig> filters;
  10. };
  11.  
  12. ParserResults Parse(int argc, char *argv[]) {
  13.     ParserResults parser_results;
  14.     parser_results.input_path = argv[1];
  15.     parser_results.output_path = argv[2];
  16.     FilterConfig config;
  17.     for (int index = 3; index < argc; ++index) {
  18.         if (argv[index][0] == '-') {
  19.             // не забыть добавить конфиг
  20.             config.name = std::string(argv[index]).substr(1);
  21.         }
  22.         // доделать остальную логику
  23.     }
  24.     return parser_results;
  25. }
  26.  
  27. class Image {
  28.     //
  29. };
  30.  
  31.  
  32.  
  33. class Filter {
  34. public:
  35.     virtual Image Apply(const Image &image) = 0;
  36. };
  37.  
  38. class FilterWithMatrix : public Filter {
  39. private:
  40.     std::vector<std::vector<int>> matrix_;
  41. };
  42.  
  43. class Crop : public Filter {
  44. public:
  45.     Crop(size_t height, size_t width) : heigth_(height), width_(width) {}
  46.  
  47.     Image Apply(const Image &image) {
  48.         // логика работы фильтра
  49.         return image;
  50.     }
  51.  
  52. private:
  53.     size_t heigth_;
  54.     size_t width_;
  55. };
  56.  
  57. // можно сделать отдельную функцию CreateFilter, которая будет выкидывать исключение, которое потом уже будет обрабатываться в CreateFilters
  58. std::vector<std::shared_ptr<Filter>> CreateFilters(const std::vector<FilterConfig> &filter_configs) {
  59.     std::vector<std::shared_ptr<Filter>> filters;
  60.     for (const auto& config: filter_configs) {
  61.         if (config.name == "crop" && config.arguments.size() == 2) {
  62.             if (config.arguments.size() != 2) {
  63.                 std::cerr <<  "Invalid amount arguments for crop\n";
  64.                 continue;
  65.             }
  66.             size_t heigth, width;
  67.             try {
  68.                 heigth = std::stoull(config.arguments[0]);
  69.                 width = std::stoull(config.arguments[1]);
  70.             } catch (std::invalid_argument& exc) {
  71.                 std::cerr << "Invalid argument for crop\n";
  72.                 continue;
  73.             }
  74.             filters.push_back(std::make_shared<Crop>(heigth, width);
  75.         }
  76.     }
  77.     return filters;
  78. }
  79.  
  80. Image ApplyFilters(Image image, const std::vector<std::shared_ptr<Filter>>& filters) {
  81.     for (const auto& filter: filters) {
  82.         image = filter->Apply(image);
  83.     }
  84.     return image;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement