ben-strasser / fast-cpp-csv-parser

fast-cpp-csv-parser
BSD 3-Clause "New" or "Revised" License
2.11k stars 440 forks source link

Lots of code duplication in the line-reading exceptions #45

Closed eyalroz closed 7 years ago

eyalroz commented 7 years ago

Example:

struct extra_column_in_header :
    base,
    with_file_name,
    with_column_name{
    void format_error_message()const{
                std::snprintf(error_message_buffer, sizeof(error_message_buffer),
                        "Extra column \"%s\" in header of file \"%s\"."
                        , column_name, file_name);
        }
};

struct missing_column_in_header :
        base,
        with_file_name,
        with_column_name{
        void format_error_message()const{
                std::snprintf(error_message_buffer, sizeof(error_message_buffer),
                        "Missing column \"%s\" in header of file \"%s\"."
                        , column_name, file_name);
        }
};

Essentially the same code but a different string. Considering how exceptions are, well, exceptional, I think it should not be impossible to get to something like:

using extra_column_in_header = exception<file_name, column_name>;
const char* exception_message_traits<extra_column_in_header>::format_string =
     "Extra column \"%s\" in header of file \"%s\".";
using missing_column_in_header  = exception<file_name, column_name>;
const char* exception_message_traits<missing_column_in_header >::format_string =
     "Missing column \"%s\" in header of file \"%s\".";

or whatever.

ben-strasser commented 7 years ago

Hi,

thanks for comment. However, I will not replace the comparatively easy to understand code with something that is only a few lines shorter and uses template-traits-voodoo. Without templates I do not see how one can shorten the code.

Best Regards Ben Strasser