eclipse-iceoryx / eclipse-iceoryx/iceoryx
Make `asStringLiteral` a template to specialize for enum to string conversion
@elBoberido is already working on this.
Since Feb 4, 2022.
- Dominant language
- C++
- Stars
- 2.2k
- Forks
- 492
- Avg merge
- 18h 57m
- Merged PRs (30d)
- 1
Description
## Brief feature description
In order to have a nice output with `std::ostream` and `iox::log::LogStream` the stream operator has to be implemented for both of this types. With `asStringLiteral` as a template, this could be specialized for a specific enum and a blanket implementation for `iox::log::LogStream` could be provided. Unfortunately this cannot be done for `std::ostream`.
## Detailed information
This could be the template in hoofs
```cpp
template
constexpr const char* asStringLiteral(const T value)
{
static_assert(always_false_v(T), "'asStringLiteral' is not specialized for the specified type");
}
```
The logger could then do a blanket implementation like
```cpp
template
LogStream& operator<<(LogStream& stream, T value) noexcept
{
stream << asStringLiteral(value);
return stream;
}
```
To ease the implementation for `std::ostream` @elfenpiff suggested the following pattern
```cpp
// in header file
template
T& operator<<(T& stream, ChunkReceiveResult value) noexcept;
// in cpp file
T& operator<<(T& stream, ChunkReceiveResult value) noexcept
{
stream << asStringLiteral(value);
return stream;
}
template log::Stream& operator<<(log::LogStream & stream, ChunkReceiveResult value) noexcept;
template std::ostream& operator<<(std::ostream & stream, ChunkReceiveResult value) noexcept;
```
Maybe these two approaches could be combined to
```cpp
template
S& operator<<(S& stream, E value) noexcept
{
stream << asStringLiteral(value);
return stream;
}
```
With some constraints on `E`
- [ ] make `asStringLiteral` a template
- [ ] create blanket implementation for `iox::log::LogStream`
- [ ] create macro to ease writing tests for `asStringLiteral` enum conversions
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.