⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printf_traits.hpp

📁 新版本TR1的stl
💻 HPP
📖 第 1 页 / 共 2 页
字号:
/* /////////////////////////////////////////////////////////////////////////
 * File:        stlsoft/util/printf_traits.hpp
 *
 * Purpose:     printf_traits classes.
 *
 * Created:     16th January 2002
 * Updated:     12th March 2007
 *
 * Home:        http://stlsoft.org/
 *
 * Copyright (c) 2002-2007, Matthew Wilson and Synesis Software
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
 *   any contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * ////////////////////////////////////////////////////////////////////// */


/** \file stlsoft/util/printf_traits.hpp
 *
 * \brief [C++ only] Definition of the stlsoft::printf_traits class
 *   template
 *   (\ref group__library__utility "Utility" Library).
 */

#ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_PRINTF_TRAITS
#define STLSOFT_INCL_STLSOFT_UTIL_HPP_PRINTF_TRAITS

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define STLSOFT_VER_STLSOFT_UTIL_HPP_PRINTF_TRAITS_MAJOR    4
# define STLSOFT_VER_STLSOFT_UTIL_HPP_PRINTF_TRAITS_MINOR    1
# define STLSOFT_VER_STLSOFT_UTIL_HPP_PRINTF_TRAITS_REVISION 1
# define STLSOFT_VER_STLSOFT_UTIL_HPP_PRINTF_TRAITS_EDIT     57
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

/* /////////////////////////////////////////////////////////////////////////
 * Includes
 */

#ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
# include <stlsoft/stlsoft.h>
#endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
#ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_SIZE_TRAITS
# include <stlsoft/util/size_traits.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_SIZE_TRAITS */

#ifdef STLSOFT_UNITTEST
# include <stlsoft/util/limit_traits.h>
# include <stlsoft/meta/yesno.hpp>
# include <stlsoft/util/std/stdio_overload_detectors.hpp>
# include <stdarg.h>
# include <stdio.h>
# include <string.h>
# include <wchar.h>
#endif /* STLSOFT_UNITTEST */

/* /////////////////////////////////////////////////////////////////////////
 * Namespace
 */

#ifndef _STLSOFT_NO_NAMESPACE
namespace stlsoft
{
#endif /* _STLSOFT_NO_NAMESPACE */

/* /////////////////////////////////////////////////////////////////////////
 * Constants & definitions
 */

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION

#define STLSOFT_PRINTF_TRAITS_SINT8_MIN         -128
#define STLSOFT_PRINTF_TRAITS_SINT8_MAX         127

#define STLSOFT_PRINTF_TRAITS_UINT8_MIN         0
#define STLSOFT_PRINTF_TRAITS_UINT8_MAX         255

#define STLSOFT_PRINTF_TRAITS_SINT16_MIN        -32768
#define STLSOFT_PRINTF_TRAITS_SINT16_MAX        32767

#define STLSOFT_PRINTF_TRAITS_UINT16_MIN        0
#define STLSOFT_PRINTF_TRAITS_UINT16_MAX        65535

#define STLSOFT_PRINTF_TRAITS_SINT32_MIN        -2147483648
#define STLSOFT_PRINTF_TRAITS_SINT32_MAX        2147483647

#define STLSOFT_PRINTF_TRAITS_UINT32_MIN        0
#define STLSOFT_PRINTF_TRAITS_UINT32_MAX        4294967295

#define STLSOFT_PRINTF_TRAITS_SINT64_MIN        -9223372036854775808
#define STLSOFT_PRINTF_TRAITS_SINT64_MAX        9223372036854775807

#define STLSOFT_PRINTF_TRAITS_UINT64_MIN        0
#define STLSOFT_PRINTF_TRAITS_UINT64_MAX        18446744073709551615

#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION

# define __STLSOFT_PRINTF_TRAITS__SINT8_MIN     STLSOFT_PRINTF_TRAITS_SINT8_MIN
# define __STLSOFT_PRINTF_TRAITS__SINT8_MAX     STLSOFT_PRINTF_TRAITS_SINT8_MAX

# define __STLSOFT_PRINTF_TRAITS__UINT8_MIN     STLSOFT_PRINTF_TRAITS_UINT8_MIN
# define __STLSOFT_PRINTF_TRAITS__UINT8_MAX     STLSOFT_PRINTF_TRAITS_UINT8_MAX

# define __STLSOFT_PRINTF_TRAITS__SINT16_MIN    STLSOFT_PRINTF_TRAITS_SINT16_MIN
# define __STLSOFT_PRINTF_TRAITS__SINT16_MAX    STLSOFT_PRINTF_TRAITS_SINT16_MAX

# define __STLSOFT_PRINTF_TRAITS__UINT16_MIN    STLSOFT_PRINTF_TRAITS_UINT16_MIN
# define __STLSOFT_PRINTF_TRAITS__UINT16_MAX    STLSOFT_PRINTF_TRAITS_UINT16_MAX

# define __STLSOFT_PRINTF_TRAITS__SINT32_MIN    STLSOFT_PRINTF_TRAITS_SINT32_MIN
# define __STLSOFT_PRINTF_TRAITS__SINT32_MAX    STLSOFT_PRINTF_TRAITS_SINT32_MAX

# define __STLSOFT_PRINTF_TRAITS__UINT32_MIN    STLSOFT_PRINTF_TRAITS_UINT32_MIN
# define __STLSOFT_PRINTF_TRAITS__UINT32_MAX    STLSOFT_PRINTF_TRAITS_UINT32_MAX

# define __STLSOFT_PRINTF_TRAITS__SINT64_MIN    STLSOFT_PRINTF_TRAITS_SINT64_MIN
# define __STLSOFT_PRINTF_TRAITS__SINT64_MAX    STLSOFT_PRINTF_TRAITS_SINT64_MAX

# define __STLSOFT_PRINTF_TRAITS__UINT64_MIN    STLSOFT_PRINTF_TRAITS_UINT64_MIN
# define __STLSOFT_PRINTF_TRAITS__UINT64_MAX    STLSOFT_PRINTF_TRAITS_UINT64_MAX

#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

/* /////////////////////////////////////////////////////////////////////////
 * Macros
 */

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION

# define ___stringise_(x)           # x
# define ___stringise(x)            ___stringise_(x)

# if defined(STLSOFT_COMPILER_IS_BORLAND)
#  define ___stringise_w_(x)        L"" ## #x
# else /* ? compiler */
#  define ___stringise_w_(x)        L ## #x
# endif /* compiler */
# define ___stringise_w(x)          ___stringise_w_(x)

#ifdef STLSOFT_CF_64_BIT_PRINTF_USES_I64
# undef STLSOFT_CF_64_BIT_PRINTF_USES_I64
#endif /* STLSOFT_CF_64_BIT_PRINTF_USES_I64 */

#ifdef STLSOFT_CF_64_BIT_PRINTF_USES_LL
# undef STLSOFT_CF_64_BIT_PRINTF_USES_LL
#endif /* STLSOFT_CF_64_BIT_PRINTF_USES_LL */

# if defined(STLSOFT_COMPILER_IS_BORLAND) || \
     (  defined(STLSOFT_COMPILER_IS_COMO) && \
        defined(_MSC_VER)) || \
     (  defined(WIN32) && \
        (   defined(STLSOFT_COMPILER_IS_GCC)) || \
            defined(STLSOFT_COMPILER_IS_INTEL)) || \
     defined(STLSOFT_COMPILER_IS_MSVC) || \
     defined(STLSOFT_COMPILER_IS_VECTORC) || \
     defined(STLSOFT_COMPILER_IS_WATCOM)
#  define STLSOFT_CF_64_BIT_PRINTF_USES_I64
# elif defined(STLSOFT_COMPILER_IS_COMO) || \
       defined(STLSOFT_COMPILER_IS_DMC) || \
       defined(STLSOFT_COMPILER_IS_INTEL) || \
       defined(STLSOFT_COMPILER_IS_MSVC) || \
       defined(STLSOFT_COMPILER_IS_GCC) || \
       defined(STLSOFT_COMPILER_IS_MWERKS)
#  define STLSOFT_CF_64_BIT_PRINTF_USES_LL
# else /* ? compiler */
#  error Further compiler discrimination is required
# endif /* printf-64 */

#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

/* /////////////////////////////////////////////////////////////////////////
 * Classes
 */

// struct printf_traits

#ifdef STLSOFT_DOCUMENTATION_SKIP_SECTION

/** \brief Traits for determining the size, in printf-ed form, of the minimum and
 * maximum values of types
 *
 * \ingroup group__library__utility
 *
 * printf_traits is a traits class for acquiring enum values representing the
 * lengths, when expressed in string form, of the minimum and maximum values of
 * the type, and the maximum of the two. The lengths are inclusive of the
 * null terminator.
 *
\code
  assert(stlsoft::printf_traits<ss_sint16_t>::size_min == 6);
  assert(stlsoft::printf_traits<ss_sint16_t>::size_max == 7);
  assert(stlsoft::printf_traits<ss_sint16_t>::size == 7);
\endcode
 *
 * \param T The type
 *
 */
template <ss_typename_param_k T>
struct printf_traits
{
    enum
    {
            size_min    //!< The number of characters (& null) in the minimum value
        ,   size_max    //!< The number of characters (& null) in the maximum value
        ,   size        //!< The maximum of \c size_min and \c size_max
    };

    /// Returns the appropriate printf format for the type
    static ss_char_a_t const* format_a();
    /// Returns the appropriate wprintf format for the type
    static ss_char_w_t const* format_w();
};

#else /* ? STLSOFT_DOCUMENTATION_SKIP_SECTION */

template <ss_typename_param_k T>
struct printf_traits;

# ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION

template <ss_typename_param_k T>
struct printf_traits_fixed;

/* s/uint8 */
STLSOFT_TEMPLATE_SPECIALISATION
struct printf_traits_fixed<ss_sint8_t>
{
    enum
    {
            size_min = sizeof(___stringise(STLSOFT_PRINTF_TRAITS_SINT8_MIN))
        ,   size_max = sizeof(___stringise(STLSOFT_PRINTF_TRAITS_SINT8_MAX))
        ,   size = (size_min < size_max) ? size_max : size_min
    };

    static ss_char_a_t const* format_a()
    {
        return "%d";
    }
    static ss_char_w_t const* format_w()
    {
        return L"%d";
    }
};

STLSOFT_TEMPLATE_SPECIALISATION
struct printf_traits_fixed<ss_uint8_t>
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -