Code View

mstring / mstring-1.3.1.2 / src / MWriter.hh
// SPDX-License-Identifier: MIT
#pragma once

#include <iosfwd>
#include <string>

/// \file MWriter.hh
/// \brief Base of the code writers: an output stream plus a shared indentation.

/// \brief How definitions are emitted. Into a source file: helpers in an
///        unnamed namespace, text functions `static`. Into the header
///        (`$MODULE ... INLINE`): every definition `inline`, helpers in a
///        named namespace behind an include guard, so several INLINE modules
///        can share a translation unit.
struct GenerationMode
{
  bool inlineDefinitions { false };
  std::string exportMacro;    ///< `$EXPORT`: put before classes and free-function declarations.

  /// "MACRO " or "".
  std::string Export( ) const;

  /// Prefix of a function definition: "inline " or "".
  const char *Linkage( ) const;

  /// Prefix of a per-message text function: "inline " or "static ".
  const char *TextFunctionLinkage( ) const;

  /// The qualified name of helper \p name.
  std::string Helper( const std::string &name ) const;

  /// Open / close the namespace (and guard) the helper \p guard lives in.
  void OpenHelpers( std::ostream &stream, const std::string &guard ) const;
  void CloseHelpers( std::ostream &stream ) const;
};

/// The mode of the current generation run.
extern GenerationMode Mode;

class Writer
{
  public:
    Writer( std::ostream &stream, std::string &indent );
    virtual ~Writer( ) = default;

    /// \brief Indent one level deeper.
    void right( ) const;

    /// \brief Indent one level less.
    void left( ) const;

  protected:
    std::ostream &_Stream;
    std::string &_Indent;
};