Panini 1.4.0
Header-only library for generating C++, written in C++17
CommaList.hpp
Go to the documentation of this file.
1/*
2 MIT No Attribution
3
4 Copyright 2021-2023 Mr. Hands
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to
8 deal in the Software without restriction, including without limitation the
9 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 sell copies of the Software, and to permit persons to whom the Software is
11 furnished to do so.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 DEALINGS IN THE SOFTWARE.
20*/
21
22#pragma once
23
24#include "commands/Command.hpp"
26
27#include <type_traits>
28
29namespace panini
30{
31
81 template <class TIterator>
83 : public Command
84 {
85
86 public:
91 using TUnderlying = typename std::conditional<
92 std::is_pointer_v<TIterator>,
93 std::remove_pointer_t<TIterator>,
94 typename std::iterator_traits<TIterator>::value_type
95 >::type;
96
105 using TTransform = std::function<void(Writer& writer, const TUnderlying& item, size_t listIndex)>;
106
120 template <typename TItem>
121 static void DefaultTransform(Writer& writer, const TItem& item, size_t listIndex)
122 {
123 (void)listIndex;
124
125 writer << std::to_string(item);
126 }
127
136 template <>
137 static void DefaultTransform(Writer& writer, const std::string& item, size_t listIndex)
138 {
139 (void)listIndex;
140
141 writer << item;
142 }
143
151 inline explicit CommaList(
152 TIterator begin,
153 TIterator end,
154 const CommaListOptions& options = {})
155 : m_begin(begin)
156 , m_end(end)
157 , m_options(options)
158 , m_transform(DefaultTransform<TUnderlying>)
159 {
160 }
161
170 inline explicit CommaList(
171 TIterator begin,
172 TIterator end,
173 const CommaListOptions& options,
174 TTransform&& transform) noexcept
175 : m_begin(begin)
176 , m_end(end)
177 , m_options(options)
178 , m_transform(std::move(transform))
179 {
180 }
181
182 inline void Visit(Writer& writer) override
183 {
184 size_t index = 0;
185 for (TIterator item = m_begin; item != m_end; ++item)
186 {
187 if (index > 0)
188 {
189 writer << m_options.chunkEndSeparator;
190
191 if (m_options.addNewLines)
192 {
193 writer << NextLine();
194 }
195 }
196
197 if (index > 0 ||
199 {
200 writer << m_options.chunkBeginSeparator;
201 }
202
203 m_transform(writer, *item, index);
204
205 index++;
206 }
207
208 if (index > 0 &&
209 !m_options.skipLastItemEndSeparator)
210 {
211 writer << m_options.chunkEndSeparator;
212 }
213 }
214
215 private:
216 TIterator m_begin;
217 TIterator m_end;
218 CommaListOptions m_options;
219 TTransform m_transform;
220
221 };
222
223};
Command for outputting a list of items, comma-separated by default.
Definition: CommaList.hpp:84
CommaList(TIterator begin, TIterator end, const CommaListOptions &options, TTransform &&transform) noexcept
Definition: CommaList.hpp:170
std::function< void(Writer &writer, const TUnderlying &item, size_t listIndex)> TTransform
Definition: CommaList.hpp:105
typename std::conditional< std::is_pointer_v< TIterator >, std::remove_pointer_t< TIterator >, typename std::iterator_traits< TIterator >::value_type >::type TUnderlying
Definition: CommaList.hpp:95
static void DefaultTransform(Writer &writer, const std::string &item, size_t listIndex)
Definition: CommaList.hpp:137
CommaList(TIterator begin, TIterator end, const CommaListOptions &options={})
Definition: CommaList.hpp:151
static void DefaultTransform(Writer &writer, const TItem &item, size_t listIndex)
Default transform function for the command.
Definition: CommaList.hpp:121
void Visit(Writer &writer) override
Definition: CommaList.hpp:182
Base class for commands.
Definition: Command.hpp:44
Pure virtual interface for writers.
Definition: Writer.hpp:44
Definition: Braces.hpp:29
Options for the CommaList command.
Definition: CommaListOptions.hpp:36
std::string chunkBeginSeparator
Definition: CommaListOptions.hpp:43
bool skipFirstItemBeginSeparator
Definition: CommaListOptions.hpp:61
bool addNewLines
Definition: CommaListOptions.hpp:56
bool skipLastItemEndSeparator
Definition: CommaListOptions.hpp:66
std::string chunkEndSeparator
Definition: CommaListOptions.hpp:51
Command for outtputing a new line chunk.
Definition: NextLine.hpp:36