Code View

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

#include <string>

/// \file Settings.hh
/// \brief The code-generation settings a `$STREAMABLE` / `$STRING` / `$ERROR` /
///        `$EXCEPTION` / `$THROW` / `$SYSLOG` directive changes.
///
/// The parser keeps one current `MessageSettings`; every `$MESSAGE` takes a
/// copy of it, so a directive affects the messages that follow it only.

/// \brief Settings shared by every generated artefact.
struct Concept
{
  std::string _Prefix;               ///< Prepended to the message name.
  std::string _Postfix;              ///< Appended to the message name.
  bool _Active { false };            ///< `ENABLE` / `DISABLE`.
  bool _Customized { false };        ///< Prefix/postfix changed by a directive.
  bool _LocaleExtra { true };        ///< Also generate the `std::locale` overload.

  Concept( std::string prefix = "", std::string postfix = "", bool active = false );
  virtual ~Concept( ) = default;
};

/// \brief How a generated function is attached to a class.
enum membernaming
{
  NONMEMBER,  ///< A free function.
  MEMBER_AS,  ///< A member of the generated streamable class, named explicitly.
  MEMBER_OF   ///< A member of a user class, declared by the user.
};

/// \brief Settings of an artefact that is a function (`$STRING`, `$ERROR`, `$THROW`, `$SYSLOG`).
struct FunctionConcept
    : public Concept
{
  membernaming _Type { NONMEMBER };
  std::string _Naming;               ///< The class (MEMBER OF) or function name (MEMBER AS).
  bool _Constness { true };          ///< Generate a `const` member function.

  FunctionConcept( std::string prefix = "", std::string postfix = "", bool active = false );
};

/// \brief `$ERROR` settings.
struct ErrorConcept
    : public FunctionConcept
{
  std::string _ExitCall { "std::exit( EXIT_FAILURE )" };  ///< The statement run after printing.

  ErrorConcept( std::string prefix = "", std::string postfix = "", bool active = false );
};

/// \brief `$SYSLOG` settings.
struct LogConcept
    : public FunctionConcept
{
  std::string _Facility { "LOG_USER" };
  std::string _Level { "LOG_INFO" };

  LogConcept( std::string prefix = "", std::string postfix = "", bool active = false );
};

/// \brief Access specifier of a generated class' base.
enum inheritance { PUBLIC_INHERITANCE, PROTECTED_INHERITANCE, PRIVATE_INHERITANCE };

/// \brief Settings of an artefact that is a class (`$STREAMABLE`, `$EXCEPTION`).
struct ClassConcept
    : public Concept
{
  inheritance _InheritanceType { PUBLIC_INHERITANCE };  ///< Default: public.
  std::string _Parent;               ///< Base class; empty for none.
  std::string _Override;             ///< `$EXCEPTION OVERRIDE name`: implement the base's
                                     ///< `virtual std::string name( ) const noexcept`
                                     ///< instead of generating What() / what().

  ClassConcept( std::string prefix = "", std::string postfix = "", bool active = false );
};

/// \brief All settings in effect for one message.
struct MessageSettings
{
  MessageSettings( );

  ClassConcept _Streamable;
  FunctionConcept _String;
  ErrorConcept _Error;
  LogConcept _Log;
  ClassConcept _Exception;
  FunctionConcept _Throw;
};