Panini 1.4.0
Header-only library for generating C++, written in C++17
panini::CommaList< TIterator > Class Template Reference

Command for outputting a list of items, comma-separated by default. More...

#include <CommaList.hpp>

Inheritance diagram for panini::CommaList< TIterator >:
panini::Command

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)
 

Detailed Description

template<class TIterator>
class panini::CommaList< TIterator >

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:

std::vector<std::string> myEnums{ "DUCK_CAR", "DUCK_PLANE", "DUCK_MARINE" };
writer << Scope("enum Vehicles", [](Writer& writer) {
CommaListOptions options;
options.chunkEndSeparator = ",";
options.addNewLines = true;
writer << CommaList(myEnums.begin(), myEnums.end(), options) << NextLine();
}) << ";";
CommaList(TIterator begin, TIterator end, const CommaListOptions &options={})
Definition: CommaList.hpp:151

Output:

enum Vehicles
{
DUCK_CAR,
DUCK_PLANE,
DUCK_MARINE
};

Member Typedef Documentation

◆ TTransform

template<class TIterator >
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.

Parameters
writerActive writer.
itemValue being processed.
listIndexIndex of the value in the list.

◆ TUnderlying

template<class TIterator >
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.

Constructor & Destructor Documentation

◆ CommaList() [1/2]

template<class TIterator >
panini::CommaList< TIterator >::CommaList ( TIterator  begin,
TIterator  end,
const CommaListOptions options = {} 
)
inlineexplicit

Construct a CommaList from a begin and end iterator.

Parameters
beginStarting point for iteration.
endEnd point for iteration.
optionsAdditional options for the command.

◆ CommaList() [2/2]

template<class TIterator >
panini::CommaList< TIterator >::CommaList ( TIterator  begin,
TIterator  end,
const CommaListOptions options,
TTransform &&  transform 
)
inlineexplicitnoexcept

Construct a CommaList and add a transform function.

Parameters
beginStarting point for iteration.
endEnd point for iteration.
optionsAdditional options for the command.
transformTransforms each iterated to a string.

Member Function Documentation

◆ DefaultTransform() [1/2]

template<class TIterator >
template<>
static void panini::CommaList< TIterator >::DefaultTransform ( Writer writer,
const std::string &  item,
size_t  listIndex 
)
inlinestatic

Specialization for transforming string items that passes the input through unchanged.

Parameters
writerActive writer.
itemValue being processed.
listIndexIndex of the value in the list.

◆ DefaultTransform() [2/2]

template<class TIterator >
template<typename TItem >
static void panini::CommaList< TIterator >::DefaultTransform ( Writer writer,
const TItem &  item,
size_t  listIndex 
)
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.

Parameters
writerActive writer.
itemValue being processed.
listIndexIndex of the value in the list.

◆ Visit()

template<class TIterator >
void panini::CommaList< TIterator >::Visit ( Writer writer)
inlineoverridevirtual

Accepts a Writer to write chunks to the output.

Implements panini::Command.


The documentation for this class was generated from the following file: