Code View

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

#include <memory>
#include <string>
#include <vector>

#include "Settings.hh"

/// \file Message.hh
/// \brief The parsed form of a `$MESSAGE`: its parameters and its texts per locale.

/// \brief A `@ name type` parameter line.
struct Parameter
{
  std::string Name;
  std::string Type;

  Parameter( std::string name, std::string type );
};

typedef std::vector<Parameter> ParameterList;

typedef std::string Format;
typedef std::vector<Format> FormatList;

/// \brief A run of literal text, or a `{$expression;format;...}` macro.
struct TextElement
{
  bool Macro;
  std::string Text;       ///< The literal text (unescaped), or the macro expression.
  FormatList Formats;     ///< Macro only: stream formats applied before the expression.

  explicit TextElement( bool m = false );
  TextElement( std::string text, bool m );
};

typedef std::vector<TextElement> TextElementList;

/// \brief The text of a message in one language / charset.
struct LocaleMessage
{
  std::string Language;
  std::string Encode;
  TextElementList TextElements;

  LocaleMessage( std::string language, std::string encode );

  bool operator== ( const LocaleMessage &locale ) const;

  /// \brief Append literal text, merging it with a preceding literal run.
  void AppendText( const std::string &text );
};

typedef std::vector<LocaleMessage> LocaleMessageList;

/// \brief A `$MESSAGE` block.
struct Message
{
  std::string Name;
  std::string DefaultLanguage;   ///< `$LANGUAGE` in effect when the message was declared.
  ParameterList Parameters;
  LocaleMessageList Locales;
  std::shared_ptr<const MessageSettings> Settings;
  std::string File;              ///< Where `$MESSAGE` is, for diagnostics.
  unsigned long int Line { 0 };
  unsigned long int Col { 0 };

  Message( std::string name, std::string defaultLanguage, const MessageSettings &settings );

  /// \brief The locale text used when no other language matches: the one in
  ///        `DefaultLanguage`, else the first one. `nullptr` for a message without text.
  const LocaleMessage *DefaultLocale( ) const;

  /// \brief The locale of \p language / \p encode, created on first use.
  LocaleMessage &Locale( const std::string &language, const std::string &encode );

  const Parameter *FindParameter( const std::string &name ) const;
};

typedef std::vector<Message> MessageList;