Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: overload stream insertion operator(<<) to support formatting by {fmt} #1225

Merged

Conversation

empiredan
Copy link
Contributor

@empiredan empiredan commented Nov 5, 2022

This PR is to solve #1220.

The purpose of this PR is to solve some problems in overloading stream insertion operator(<<) to support formatting by {fmt}, including following points:

1. Just use to_string() without converting it to std::string

Some classes, such as gpid and task_code, while implementing their stream insertion operators, have chosen to convert const char * to a temporary std::string object. For example, gpid has implemented as below:

    friend std::ostream &operator<<(std::ostream &os, gpid id)
    {
        return os << std::string(id.to_string());
    }

However, actually since ostream has supported outputting char and const char *, there's no need to converting it again to std::string. According to this answer we can just include <ostream> in the header files.

2. Friend declarations for stream insertion operators

Actually most of stream insertion operators we have implemented are using to_string() which is declared as public. Thus all of them can be declared without friend keyword. However, once the friend keyword are dropped they will have to be defined as non-member template functions and moved out of the definition of the classes. Therefore we can just keep current declarations with friend.

3. Implement stream insertion operator for customized_id

Since some classes such as rpc_channel and network_header_format which have been wrapped based on customized_id support to_string() and are sometimes formatted as output stream, to simplify code we can just implement stream insertion operators for them.

It is noticeable that without implementing stream insertion operator for customized_id the code will be compiled successfully. The reason is that customized_id can be converted to int implicitly by operator int() const for it. However, the formatted information that we really need is the string-typed message; therefore we still need to implement stream insertion operator for customized_id, which is prioritized over operator int() const to be used for formatting by {fmt}.

4. Implement stream insertion operator for threadpool_code

Similar with customized_id, we can also support stream insertion operator for threadpool_code by using to_string() which has been implemented.

Likewise, without implementing stream insertion operator for threadpool_code the code will also be compiled successfully. threadpool_code has implemented operator int() const. Therefore, it's also necessary to implement stream insertion operator for threadpool_code.

@github-actions github-actions bot added the cpp label Nov 5, 2022
@empiredan empiredan marked this pull request as draft November 5, 2022 18:55
acelyc111
acelyc111 previously approved these changes Nov 6, 2022
@acelyc111 acelyc111 marked this pull request as ready for review November 6, 2022 15:28
@acelyc111 acelyc111 merged commit ba723a9 into apache:master Nov 7, 2022
@empiredan empiredan mentioned this pull request Aug 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants