Code View

mstring / mstring-1.3.1.2 / src / MWriter.cpp
// SPDX-License-Identifier: MIT
#include "MWriter.hh"

#include <ostream>

GenerationMode Mode;

namespace
{
  // Versioned: a later mstring with different helpers gets its own names.
  const char *const LocalHelpers = "mstring_local_v1";
  const char *const SharedHelpers = "mstring_detail_v1";
}

std::string GenerationMode::Export( ) const
{
  return exportMacro.empty( ) ? "" : exportMacro + ' ';
}

const char *GenerationMode::Linkage( ) const
{
  return inlineDefinitions ? "inline " : "";
}

const char *GenerationMode::TextFunctionLinkage( ) const
{
  return inlineDefinitions ? "inline " : "static ";
}

std::string GenerationMode::Helper( const std::string &name ) const
{
  return std::string( inlineDefinitions ? SharedHelpers : LocalHelpers ) + "::" + name;
}

void GenerationMode::OpenHelpers( std::ostream &stream, const std::string &guard ) const
{
  if ( inlineDefinitions )
  {
    std::string const macro = "MSTRING_DETAIL_V1_" + guard;
    stream << "#ifndef " << macro << std::endl
           << "#define " << macro << std::endl
           << "namespace " << SharedHelpers << std::endl
           << "{" << std::endl;
  }
  else
  {
    stream << "namespace" << std::endl
           << "{" << std::endl
           << "namespace " << LocalHelpers << std::endl
           << "{" << std::endl;
  }
}

void GenerationMode::CloseHelpers( std::ostream &stream ) const
{
  if ( inlineDefinitions )
  {
    stream << "}" << std::endl
           << "#endif" << std::endl
           << std::endl;
  }
  else
  {
    stream << "}" << std::endl
           << "}" << std::endl
           << std::endl;
  }
}

Writer::Writer( std::ostream &stream, std::string &indent )
  : _Stream( stream ),
    _Indent( indent )
{
  // blank
}

void Writer::right( ) const
{
  _Indent.append( "  " );
}

void Writer::left( ) const
{
  _Indent.erase( 0, 2 );
}