|
| 1 | +``` |
| 2 | +Here is the example of using yaml-cpp to read in yaml node to class object |
| 3 | +
|
| 4 | +class ModelSettings { |
| 5 | +public: |
| 6 | + // Getters |
| 7 | + [[nodiscard]] int get_days_between_stdout_output() const { |
| 8 | + return days_between_stdout_output_; |
| 9 | + } |
| 10 | + // Setters with validation |
| 11 | + void set_days_between_stdout_output(int value) { |
| 12 | + if (value <= 0) |
| 13 | + throw std::invalid_argument( |
| 14 | + "days_between_stdout_output must be greater than 0"); |
| 15 | + days_between_stdout_output_ = value; |
| 16 | + } |
| 17 | + [[nodiscard]] int get_initial_seed_number() const { |
| 18 | + return initial_seed_number_; |
| 19 | + } |
| 20 | + void set_initial_seed_number(int value) { |
| 21 | + if (value < 0) |
| 22 | + throw std::invalid_argument("initial_seed_number must be non-negative"); |
| 23 | + initial_seed_number_ = value; |
| 24 | + } |
| 25 | + [[nodiscard]] bool get_record_genome_db() const { return record_genome_db_; } |
| 26 | + void set_record_genome_db(bool value) { record_genome_db_ = value; } |
| 27 | + [[nodiscard]] const date::year_month_day &get_starting_date() const { |
| 28 | + return starting_date_; |
| 29 | + } |
| 30 | + void set_starting_date(const date::year_month_day &value) { |
| 31 | + starting_date_ = value; |
| 32 | + } |
| 33 | + [[nodiscard]] const date::year_month_day &get_start_of_comparison_period() |
| 34 | + const { |
| 35 | + return start_of_comparison_period_; |
| 36 | + } |
| 37 | + void set_start_of_comparison_period(const date::year_month_day &value) { |
| 38 | + if (value < starting_date_) |
| 39 | + throw std::invalid_argument( |
| 40 | + "start_of_comparison_period cannot be before starting_date"); |
| 41 | + start_of_comparison_period_ = value; |
| 42 | + } |
| 43 | + [[nodiscard]] const date::year_month_day &get_ending_date() const { |
| 44 | + return ending_date_; |
| 45 | + } |
| 46 | + void set_ending_date(const date::year_month_day &value) { |
| 47 | + if (value < start_of_comparison_period_) |
| 48 | + throw std::invalid_argument( |
| 49 | + "ending_date cannot be before start_of_comparison_period"); |
| 50 | + ending_date_ = value; |
| 51 | + } |
| 52 | + [[nodiscard]] int get_start_collect_data_day() const { |
| 53 | + return start_collect_data_day_; |
| 54 | + } |
| 55 | + void set_start_collect_data_day(int value) { |
| 56 | + if (value < 0) |
| 57 | + throw std::invalid_argument( |
| 58 | + "start_collect_data_day must be non-negative"); |
| 59 | + start_collect_data_day_ = value; |
| 60 | + } |
| 61 | +
|
| 62 | +private: |
| 63 | + int days_between_stdout_output_; |
| 64 | + int initial_seed_number_; |
| 65 | + bool record_genome_db_; |
| 66 | + date::year_month_day starting_date_; |
| 67 | + date::year_month_day start_of_comparison_period_; |
| 68 | + date::year_month_day ending_date_; |
| 69 | + int start_collect_data_day_; |
| 70 | +}; |
| 71 | +
|
| 72 | +
|
| 73 | +template <> |
| 74 | +struct convert<ModelSettings> { |
| 75 | + static Node encode(const ModelSettings &rhs) { |
| 76 | + Node node; |
| 77 | + node["days_between_stdout_output"] = rhs.get_days_between_stdout_output(); |
| 78 | + node["initial_seed_number"] = rhs.get_initial_seed_number(); |
| 79 | + node["record_genome_db"] = rhs.get_record_genome_db(); |
| 80 | + node["starting_date"] = rhs.get_starting_date(); |
| 81 | + node["start_of_comparison_period"] = rhs.get_start_of_comparison_period(); |
| 82 | + node["ending_date"] = rhs.get_ending_date(); |
| 83 | + node["start_collect_data_day"] = rhs.get_start_collect_data_day(); |
| 84 | + return node; |
| 85 | + } |
| 86 | +
|
| 87 | + static bool decode(const Node &node, ModelSettings &rhs) { |
| 88 | + if (!node["days_between_stdout_output"]) { |
| 89 | + throw std::runtime_error("Missing 'days_between_stdout_output' field."); |
| 90 | + } |
| 91 | + if (!node["initial_seed_number"]) { |
| 92 | + throw std::runtime_error("Missing 'initial_seed_number' field."); |
| 93 | + } |
| 94 | + if (!node["record_genome_db"]) { |
| 95 | + throw std::runtime_error("Missing 'record_genome_db' field."); |
| 96 | + } |
| 97 | + if (!node["starting_date"]) { |
| 98 | + throw std::runtime_error("Missing 'starting_date' field."); |
| 99 | + } |
| 100 | + if (!node["start_of_comparison_period"]) { |
| 101 | + throw std::runtime_error("Missing 'start_of_comparison_period' field."); |
| 102 | + } |
| 103 | + if (!node["ending_date"]) { |
| 104 | + throw std::runtime_error("Missing 'ending_date' field."); |
| 105 | + } |
| 106 | + if (!node["start_collect_data_day"]) { |
| 107 | + throw std::runtime_error("Missing 'start_collect_data_day' field."); |
| 108 | + } |
| 109 | +
|
| 110 | + // TODO: Add more error checking for each field |
| 111 | +
|
| 112 | + rhs.set_days_between_stdout_output( |
| 113 | + node["days_between_stdout_output"].as<int>()); |
| 114 | + rhs.set_initial_seed_number(node["initial_seed_number"].as<int>()); |
| 115 | + rhs.set_record_genome_db(node["record_genome_db"].as<bool>()); |
| 116 | + rhs.set_starting_date(node["starting_date"].as<date::year_month_day>()); |
| 117 | + rhs.set_start_of_comparison_period( |
| 118 | + node["start_of_comparison_period"].as<date::year_month_day>()); |
| 119 | + rhs.set_ending_date(node["ending_date"].as<date::year_month_day>()); |
| 120 | + rhs.set_start_collect_data_day(node["start_collect_data_day"].as<int>()); |
| 121 | + return true; |
| 122 | + } |
| 123 | +}; |
| 124 | +
|
| 125 | +As an expert in C++ developer, would you mind make the similar convert function for the following class: |
| 126 | +``` |
0 commit comments