162 virtual void Write(
const std::string& chunk) = 0;
182 template <
typename TConfig>
187 static_assert(std::is_base_of<WriterConfig, TConfig>::value,
"TConfig must inherit from WriterConfig");
198 const size_t indentReserveSize = 8 *
m_config.chunkIndent.size();
199 m_lineIndentCached.reserve(indentReserveSize);
200 m_commentIndentCached.reserve(indentReserveSize);
206 TConfig defaultConfig;
207 m_config.braceBreakingStyle = defaultConfig.braceBreakingStyle;
212 WriterConfig defaultConfig;
213 m_config.includeStyle = defaultConfig.includeStyle;
263 return m_state == State::NewLine;
275 if (m_state == State::NewLine)
277 if (!m_lineIndentCached.empty())
279 Write(m_lineIndentCached);
288 m_state = State::Chunk;
290 if (m_isInCommentBlock)
294 if (!m_commentIndentCached.empty())
296 Write(m_commentIndentCached);
303 m_lineChunkCountWritten += chunk.size();
317 return *
this << std::string(chunkString);
333 if (m_isInCommentBlock &&
334 m_lineChunkCountWritten == 0)
341 m_state = State::NewLine;
343 m_lineChunkCountWritten = 0;
359 if (!m_isInCommentBlock)
361 CacheIndentation(m_lineIndentCached, ++m_lineIndentCount);
365 CacheIndentation(m_commentIndentCached, ++m_commentIndentCount);
383 if (!m_isInCommentBlock)
385 if (m_lineIndentCount > 0)
395 if (m_commentIndentCount > 0)
398 m_commentIndentCached,
399 --m_commentIndentCount
418 command.Visit(*
this);
429 m_isInCommentBlock = value;
431 m_commentIndentCount = 0;
432 m_commentIndentCached.clear();
453 inline bool Commit(
bool force =
false)
override
474 inline void CacheIndentation(std::string& target, int32_t count)
478 for (int32_t i = 0; i < count; ++i)
485 size_t m_lineChunkCountWritten = 0;
487 int32_t m_lineIndentCount = 0;
488 std::string m_lineIndentCached;
495 State m_state = State::NewLine;
497 bool m_isInCommentBlock =
false;
498 int32_t m_commentIndentCount = 0;
499 std::string m_commentIndentCached;
Base class for commands.
Definition: Command.hpp:44
Pure virtual interface for writers.
Definition: Writer.hpp:44
virtual bool OnCommit(bool force=false)=0
virtual void SetIsInCommentBlock(bool value)=0
virtual bool IsOnNewLine() const =0
virtual void WriteNewLine()=0
virtual Writer & operator<<(const std::string &chunk)=0
virtual const WriterConfig & GetConfig() const =0
virtual ~Writer()=default
virtual bool Commit(bool force)=0
virtual IncludeStyle GetIncludeStyle() const =0
virtual BraceBreakingStyle GetBraceBreakingStyle() const =0
virtual void Write(const std::string &chunk)=0
virtual bool IsChanged() const =0
BraceBreakingStyle
Brace breaking style to use when writing to output.
Definition: BraceBreakingStyle.hpp:34
IncludeStyle
Include style to use when writing to output.
Definition: IncludeStyle.hpp:36
@ Inherit
Inherit setting from the config, not valid on Writer.
@ Inherit
Inherit setting from the config, not valid on Writer.
Definition: Braces.hpp:29
Command for decrementing the indentation level on the writer.
Definition: IndentPop.hpp:36
Command for incrementing the indentation level on the writer.
Definition: IndentPush.hpp:36
Command for outtputing a new line chunk.
Definition: NextLine.hpp:36
Global configuration applied to writers.
Definition: WriterConfig.hpp:39