// xlocmes internal header (from <locale>) -*-c++-*-
// Copyright 2009-2017 IAR Systems AB.
#ifndef _XLOCMES_
#define _XLOCMES_

#ifndef _SYSTEM_BUILD
#pragma system_include
#endif

#include <xiosbase>

#if _DLIB_FULL_LOCALE_SUPPORT

namespace std {

// STRUCT messages_base

struct messages_base
  : public locale::facet
{       // base class for messages
  typedef int catalog;

  explicit messages_base(size_t _Refs = 0)
    : locale::facet(_Refs)
  {       // default constructor
  }
};

// TEMPLATE CLASS messages
template<class _Elem>
class messages
  : public messages_base
{       // facet for obtaining messages from a catalog
public:
  typedef _Elem char_type;
  typedef basic_string<_Elem, char_traits<_Elem>, allocator<_Elem> >
  string_type;

  catalog open(const string& _Catname, const locale& _Loc) const
  {       // open catalog
    return do_open(_Catname, _Loc);
  }

  string_type get(catalog _Catval, int _Set, int _Message,
                  const string_type& _Dflt) const
  {       // get message from set in catalog
    return do_get(_Catval, _Set, _Message, _Dflt);
  }

  void close(catalog _Catval) const
  {       // close catalog
    do_close(_Catval);
  }

  static locale::id id;   // unique facet id

  explicit messages(size_t _Refs = 0)
    : messages_base(_Refs)
  {       // construct from current locale
    // Possibly lock thread
    _Locinfo _Lobj;
    _Init(_Lobj);
  }

  messages(const _Locinfo& _Lobj, size_t _Refs = 0)
    : messages_base(_Refs)
  {       // construct from specified locale
    _Init(_Lobj);
  }

  static size_t _Getcat(const locale::facet **_Ppf = 0,
                        const locale *_Ploc = 0)
  {       // return locale category mask and construct standard facet
    if (_Ppf != 0 && *_Ppf == 0)
      *_Ppf = new messages<_Elem>(_Ploc->_GetLocinfo());
    return _X_MESSAGES;
  }

protected:
  messages(const char *_Locname, size_t _Refs = 0)
    : messages_base(_Refs)
  {       // construct from specified locale
    // Possibly lock thread
    _Locinfo _Lobj(_Locname);
    _Init(_Lobj);
  }

  virtual ~messages() _NOEXCEPT
  {       // destroy the object
  }

  void _Init(const _Locinfo&)
  {       // initialize from _Locinfo object (do nothing)
  }

  virtual catalog do_open(const string&, const locale&) const
  {       // open catalog (do nothing)
    return -1;
  }

  virtual string_type do_get(catalog, int, int,
                             const string_type& _Dflt) const
  {       // get message from set in catalog (return default)
    return _Dflt;
  }

  virtual void do_close(catalog) const
  {       // close catalog (do nothing)
  }
};

// STATIC messages::id OBJECT
template<class _Elem>
locale::id messages<_Elem>::id;

// TEMPLATE CLASS messages_byname
template<class _Elem>
class messages_byname
  : public messages<_Elem>
{       // messages for named locale
public:
  explicit messages_byname(const char *_Locname, size_t _Refs = 0)
    : messages<_Elem>(_Locname, _Refs)
  {       // construct for named locale
  }

  explicit messages_byname(const string& _Str, size_t _Refs = 0)
    : messages<_Elem>(_Str.c_str(), _Refs)
  {       // construct for named locale
  }

protected:
  virtual ~messages_byname() _NOEXCEPT
  {       // destroy the object
  }
};

} /* namespace std */

#endif /* _DLIB_FULL_LOCALE_SUPPORT */

#endif /* _XLOCMES_ */

/*
 * Copyright (c) by P.J. Plauger. All rights reserved.
 * Consult your license regarding permissions and restrictions.
V6.50:0576 */
