|
Panini 1.4.0
Header-only library for generating C++, written in C++17
|
Command for outputting a list of items, comma-separated by default. More...
#include <CommaList.hpp>
Public Types | |
| using | TUnderlying = typename std::conditional< std::is_pointer_v< TIterator >, std::remove_pointer_t< TIterator >, typename std::iterator_traits< TIterator >::value_type >::type |
| using | TTransform = std::function< void(Writer &writer, const TUnderlying &item, size_t listIndex)> |
Public Member Functions | |
| CommaList (TIterator begin, TIterator end, const CommaListOptions &options={}) | |
| CommaList (TIterator begin, TIterator end, const CommaListOptions &options, TTransform &&transform) noexcept | |
| void | Visit (Writer &writer) override |
Public Member Functions inherited from panini::Command | |
| virtual | ~Command ()=default |
Static Public Member Functions | |
| template<typename TItem > | |
| static void | DefaultTransform (Writer &writer, const TItem &item, size_t listIndex) |
| Default transform function for the command. More... | |
| template<> | |
| static void | DefaultTransform (Writer &writer, const std::string &item, size_t listIndex) |
Command for outputting a list of items, comma-separated by default.
The CommaList command makes it easy to print a list of items that should be separated after the first item, e.g. function parameters.
The command works with pointers of any type and iterators of any STL container. Custom iterators are supported as well, as long as the type implements the interface required for std::iterator_traits to derive the underlying type.
Using the CommaListOptions struct, you can specify the separator chunk that should come before each item and the one that should come after the first item, which is ", " by default. It's also possible to add a new line after each item, which is disabled by default.
Finally, you can add a transform function to the command, which will be called once for each item in the list and is used to transform your data to a format that the active writer can process.
Example:
Output:
| using panini::CommaList< TIterator >::TTransform = std::function<void(Writer& writer, const TUnderlying& item, size_t listIndex)> |
Function for transforming elements in the list to chunks for a Writer instance.
| writer | Active writer. |
| item | Value being processed. |
| listIndex | Index of the value in the list. |
| using panini::CommaList< TIterator >::TUnderlying = typename std::conditional< std::is_pointer_v<TIterator>, std::remove_pointer_t<TIterator>, typename std::iterator_traits<TIterator>::value_type >::type |
Underlying type as derived from TIterator, which should be either a pointer type or an iterator one.
|
inlineexplicit |
Construct a CommaList from a begin and end iterator.
| begin | Starting point for iteration. |
| end | End point for iteration. |
| options | Additional options for the command. |
|
inlineexplicitnoexcept |
Construct a CommaList and add a transform function.
| begin | Starting point for iteration. |
| end | End point for iteration. |
| options | Additional options for the command. |
| transform | Transforms each iterated to a string. |
|
inlinestatic |
Specialization for transforming string items that passes the input through unchanged.
| writer | Active writer. |
| item | Value being processed. |
| listIndex | Index of the value in the list. |
|
inlinestatic |
Default transform function for the command.
Each iterated item is passed through a function that "transforms" it to an std::string before passing it to the active writer.
The default implementation calls std::to_string, which will handle most standard types.
| writer | Active writer. |
| item | Value being processed. |
| listIndex | Index of the value in the list. |
|
inlineoverridevirtual |
Accepts a Writer to write chunks to the output.
Implements panini::Command.