feat: overload stream insertion operator(<<) to support formatting by {fmt} #1225
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 tostd::string
Some classes, such as
gpid
andtask_code
, while implementing their stream insertion operators, have chosen to convertconst char *
to a temporarystd::string
object. For example,gpid
has implemented as below:However, actually since
ostream
has supported outputtingchar
andconst char *
, there's no need to converting it again tostd::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 withoutfriend
keyword. However, once thefriend
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 withfriend
.3. Implement stream insertion operator for
customized_id
Since some classes such as
rpc_channel
andnetwork_header_format
which have been wrapped based oncustomized_id
supportto_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 thatcustomized_id
can be converted to int implicitly byoperator 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 forcustomized_id
, which is prioritized overoperator 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 forthreadpool_code
by usingto_string()
which has been implemented.Likewise, without implementing stream insertion operator for
threadpool_code
the code will also be compiled successfully.threadpool_code
has implementedoperator int() const
. Therefore, it's also necessary to implement stream insertion operator forthreadpool_code
.