Synesis Software STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ...

pantheios/inserters/integer.hpp

Go to the documentation of this file.
00001 /* /////////////////////////////////////////////////////////////////////////
00002  * File:        pantheios/inserters/integer.hpp
00003  *
00004  * Purpose:     String inserters for fundamental types
00005  *
00006  * Created:     21st June 2005
00007  * Updated:     18th October 2010
00008  *
00009  * Home:        http://www.pantheios.org/
00010  *
00011  * Copyright (c) 2005-2010, Matthew Wilson and Synesis Software
00012  * Copyright (c) 1999-2005, Synesis Software and Matthew Wilson
00013  * All rights reserved.
00014  *
00015  * Redistribution and use in source and binary forms, with or without
00016  * modification, are permitted provided that the following conditions are
00017  * met:
00018  *
00019  * - Redistributions of source code must retain the above copyright notice,
00020  *   this list of conditions and the following disclaimer.
00021  * - Redistributions in binary form must reproduce the above copyright
00022  *   notice, this list of conditions and the following disclaimer in the
00023  *   documentation and/or other materials provided with the distribution.
00024  * - Neither the name(s) of Matthew Wilson and Synesis Software nor the
00025  *   names of any contributors may be used to endorse or promote products
00026  *   derived from this software without specific prior written permission.
00027  *
00028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00029  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
00030  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00031  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00032  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00033  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00034  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00035  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00036  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00037  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00038  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00039  *
00040  * ////////////////////////////////////////////////////////////////////// */
00041 
00042 
00049 #ifndef PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_INTEGER
00050 #define PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_INTEGER
00051 
00052 /* /////////////////////////////////////////////////////////////////////////
00053  * Version information
00054  */
00055 
00056 #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
00057 # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_INTEGER_MAJOR    2
00058 # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_INTEGER_MINOR    5
00059 # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_INTEGER_REVISION 5
00060 # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_INTEGER_EDIT     36
00061 #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
00062 
00063 /* /////////////////////////////////////////////////////////////////////////
00064  * Includes
00065  */
00066 
00067 #ifndef PANTHEIOS_INCL_PANTHEIOS_H_PANTHEIOS
00068 # include <pantheios/pantheios.h>
00069 #endif /* !PANTHEIOS_INCL_PANTHEIOS_H_PANTHEIOS */
00070 #ifndef PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_FMT
00071 # include <pantheios/inserters/fmt.hpp>
00072 #endif /* !PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_FMT */
00073 
00074 #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_STRING_H_FWD
00075 # include <stlsoft/shims/access/string/fwd.h>
00076 #endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_STRING_H_FWD */
00077 
00078 /* /////////////////////////////////////////////////////////////////////////
00079  * Namespace
00080  */
00081 
00082 #if !defined(PANTHEIOS_NO_NAMESPACE)
00083 namespace pantheios
00084 {
00085 
00086 #endif /* !PANTHEIOS_NO_NAMESPACE */
00087 
00088 /* /////////////////////////////////////////////////////////////////////////
00089  * Inserter classes
00090  */
00091 
00125 class integer
00126 {
00129 public:
00130     typedef integer     class_type;
00132 
00135 private:
00136     size_t init_(::stlsoft::sint8_t i);
00137     size_t init_(::stlsoft::uint8_t i);
00138     size_t init_(::stlsoft::sint16_t i);
00139     size_t init_(::stlsoft::uint16_t i);
00140     size_t init_(::stlsoft::sint32_t i);
00141     size_t init_(::stlsoft::uint32_t i);
00142 #ifdef STLSOFT_CF_64BIT_INT_SUPPORT
00143     size_t init_(::stlsoft::sint64_t i);
00144     size_t init_(::stlsoft::uint64_t i);
00145 #endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
00146 #ifdef STLSOFT_CF_INT_DISTINCT_INT_TYPE
00147     size_t init_(int i);
00148     size_t init_(unsigned int i);
00149 #endif /* STLSOFT_CF_INT_DISTINCT_INT_TYPE */
00150 #ifdef STLSOFT_CF_LONG_DISTINCT_INT_TYPE
00151     size_t init_(long i);
00152     size_t init_(unsigned long i);
00153 #endif /* STLSOFT_CF_LONG_DISTINCT_INT_TYPE */
00155 
00158 public:
00163     explicit integer(::stlsoft::sint8_t i, int widthAndFormat);
00168     explicit integer(::stlsoft::uint8_t i, int widthAndFormat);
00173     explicit integer(::stlsoft::sint16_t i, int widthAndFormat);
00178     explicit integer(::stlsoft::uint16_t i, int widthAndFormat);
00183     explicit integer(::stlsoft::sint32_t i, int widthAndFormat);
00188     explicit integer(::stlsoft::uint32_t i, int widthAndFormat);
00189 #ifdef STLSOFT_CF_64BIT_INT_SUPPORT
00194     explicit integer(::stlsoft::sint64_t i, int widthAndFormat);
00199     explicit integer(::stlsoft::uint64_t i, int widthAndFormat);
00200 #endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
00201 #ifdef STLSOFT_CF_INT_DISTINCT_INT_TYPE
00206     explicit integer(int i, int widthAndFormat);
00211     explicit integer(unsigned int i, int widthAndFormat);
00212 #endif /* STLSOFT_CF_INT_DISTINCT_INT_TYPE */
00213 #ifdef STLSOFT_CF_LONG_DISTINCT_INT_TYPE
00218     explicit integer(long i, int widthAndFormat);
00223     explicit integer(unsigned long i, int widthAndFormat);
00224 #endif /* STLSOFT_CF_LONG_DISTINCT_INT_TYPE */
00225 
00227     explicit integer(::stlsoft::sint8_t i);
00229     explicit integer(::stlsoft::uint8_t i);
00231     explicit integer(::stlsoft::sint16_t i);
00233     explicit integer(::stlsoft::uint16_t i);
00235     explicit integer(::stlsoft::sint32_t i);
00237     explicit integer(::stlsoft::uint32_t i);
00238 #ifdef STLSOFT_CF_64BIT_INT_SUPPORT
00240     explicit integer(::stlsoft::sint64_t i);
00242     explicit integer(::stlsoft::uint64_t i);
00243 #endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
00244 #ifdef STLSOFT_CF_INT_DISTINCT_INT_TYPE
00246     explicit integer(int i);
00248     explicit integer(unsigned int i);
00249 #endif /* STLSOFT_CF_INT_DISTINCT_INT_TYPE */
00250 #ifdef STLSOFT_CF_LONG_DISTINCT_INT_TYPE
00252     explicit integer(long i);
00254     explicit integer(unsigned long i);
00255 #endif /* STLSOFT_CF_LONG_DISTINCT_INT_TYPE */
00256 
00263     explicit integer(::stlsoft::sint8_t i, int minWidth, int format);
00270     explicit integer(::stlsoft::uint8_t i, int minWidth, int format);
00277     explicit integer(::stlsoft::sint16_t i, int minWidth, int format);
00284     explicit integer(::stlsoft::uint16_t i, int minWidth, int format);
00291     explicit integer(::stlsoft::sint32_t i, int minWidth, int format);
00298     explicit integer(::stlsoft::uint32_t i, int minWidth, int format);
00299 #ifdef STLSOFT_CF_64BIT_INT_SUPPORT
00306     explicit integer(::stlsoft::sint64_t i, int minWidth, int format);
00313     explicit integer(::stlsoft::uint64_t i, int minWidth, int format);
00314 #endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
00315 #ifdef STLSOFT_CF_INT_DISTINCT_INT_TYPE
00322     explicit integer(int i, int minWidth, int format);
00329     explicit integer(unsigned int i, int minWidth, int format);
00330 #endif /* STLSOFT_CF_INT_DISTINCT_INT_TYPE */
00331 #ifdef STLSOFT_CF_LONG_DISTINCT_INT_TYPE
00338     explicit integer(long i, int minWidth, int format);
00345     explicit integer(unsigned long i, int minWidth, int format);
00346 #endif /* STLSOFT_CF_LONG_DISTINCT_INT_TYPE */
00348 
00351 public:
00353     pan_char_t const*   data() const;
00355     pan_char_t const*   c_str() const;
00357     size_t              length() const;
00359 
00362 private:
00363     void construct_() const;
00364     void construct_();
00365 
00366     static int validate_width_(int minWidth);
00368 
00371 private:
00372     union u
00373     {
00374         ::stlsoft::sint32_t      s32;   //  typeIsS32
00375         ::stlsoft::uint32_t      u32;   //  typeIsU32
00376 #ifdef STLSOFT_CF_64BIT_INT_SUPPORT
00377         ::stlsoft::sint64_t      s64;   //  typeIsS64
00378         ::stlsoft::uint64_t      u64;   //  typeIsU64
00379 #endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
00380     };
00381 
00382     u           m_value;    // The value, copied for lazy conversion
00383     size_t      m_len;      // Length, and marker for type: 1 == float; 2 == double; 3 == long double
00384     const int   m_minWidth; // The minimum width
00385     const int   m_format;   // The format
00386     pan_char_t  m_sz[129];  // Marker for converted, if m_sz[0] == '\0'
00388 
00391 private:
00392 #if !defined(STLSOFT_COMPILER_IS_GCC)
00393     integer(class_type const&);
00394 #endif /* compiler */
00395     class_type& operator =(class_type const&);
00397 };
00398 
00399 /* /////////////////////////////////////////////////////////////////////////
00400  * String Access Shims
00401  */
00402 
00403 #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
00404 
00405 # if !defined(PANTHEIOS_NO_NAMESPACE)
00406 namespace shims
00407 {
00408 # endif /* !PANTHEIOS_NO_NAMESPACE */
00409 
00411 # ifdef PANTHEIOS_USE_WIDE_STRINGS
00412 inline wchar_t const* c_str_data_w(integer const& i)
00413 # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
00414 inline char const* c_str_data_a(integer const& i)
00415 # endif /* PANTHEIOS_USE_WIDE_STRINGS */
00416 {
00417     return i.data();
00418 }
00420 inline pan_char_t const* c_str_data(integer const& i)
00421 {
00422     return i.data();
00423 }
00424 
00426 # ifdef PANTHEIOS_USE_WIDE_STRINGS
00427 inline size_t c_str_len_w(integer const& i)
00428 # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
00429 inline size_t c_str_len_a(integer const& i)
00430 # endif /* PANTHEIOS_USE_WIDE_STRINGS */
00431 {
00432     return i.length();
00433 }
00435 inline size_t c_str_len(integer const& i)
00436 {
00437     return i.length();
00438 }
00439 
00441 # ifdef PANTHEIOS_USE_WIDE_STRINGS
00442 inline wchar_t const* c_str_ptr_w(integer const& i)
00443 # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
00444 inline char const* c_str_ptr_a(integer const& i)
00445 # endif /* PANTHEIOS_USE_WIDE_STRINGS */
00446 {
00447     return i.c_str();
00448 }
00450 inline pan_char_t const* c_str_ptr(integer const& i)
00451 {
00452     return i.c_str();
00453 }
00454 
00455 # if !defined(PANTHEIOS_NO_NAMESPACE)
00456 } /* namespace shims */
00457 
00458 #  if defined(STLSOFT_COMPILER_IS_GCC)
00459     /* GCC does not seem to correctly handle the phases of
00460      * processing of C++ templates, so we need to 'use' the
00461      * shims into the same namespace as the inserter class
00462      * in order that ADL can suffice instead.
00463      */
00464 #   ifdef PANTHEIOS_USE_WIDE_STRINGS
00465     using ::pantheios::shims::c_str_data_w;
00466     using ::pantheios::shims::c_str_len_w;
00467     using ::pantheios::shims::c_str_ptr_w;
00468 #   else /* ? PANTHEIOS_USE_WIDE_STRINGS */
00469     using ::pantheios::shims::c_str_data_a;
00470     using ::pantheios::shims::c_str_len_a;
00471     using ::pantheios::shims::c_str_ptr_a;
00472 #   endif /* PANTHEIOS_USE_WIDE_STRINGS */
00473     using ::pantheios::shims::c_str_data;
00474     using ::pantheios::shims::c_str_len;
00475     using ::pantheios::shims::c_str_ptr;
00476 #  endif /* compiler */
00477 
00478 # endif /* !PANTHEIOS_NO_NAMESPACE */
00479 
00480 #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
00481 
00482 /* /////////////////////////////////////////////////////////////////////////
00483  * Namespace
00484  */
00485 
00486 #if !defined(PANTHEIOS_NO_NAMESPACE)
00487 } /* namespace pantheios */
00488 
00489 namespace stlsoft
00490 {
00491     // 'Export' the string access shims into the STLSoft namespace
00492     //
00493     // c_str_ptr(_a) is not necessary for version 1.0 of Pantheios, but it's
00494     // defined and exported in order to allow for the case where someone
00495     // may find a legitimate use for the conversion classes additional to
00496     // the type-tunneling of the Pantheios API.
00497 
00498 # ifdef PANTHEIOS_USE_WIDE_STRINGS
00499     using ::pantheios::shims::c_str_data_w;
00500     using ::pantheios::shims::c_str_len_w;
00501     using ::pantheios::shims::c_str_ptr_w;
00502 # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
00503     using ::pantheios::shims::c_str_data_a;
00504     using ::pantheios::shims::c_str_len_a;
00505     using ::pantheios::shims::c_str_ptr_a;
00506 # endif /* PANTHEIOS_USE_WIDE_STRINGS */
00507     using ::pantheios::shims::c_str_data;
00508     using ::pantheios::shims::c_str_len;
00509     using ::pantheios::shims::c_str_ptr;
00510 }
00511 
00512 #endif /* !PANTHEIOS_NO_NAMESPACE */
00513 
00514 /* /////////////////////////////////////////////////////////////////////////
00515  * Inclusion
00516  */
00517 
00518 #ifdef STLSOFT_PPF_pragma_once_SUPPORT
00519 # pragma once
00520 #endif /* STLSOFT_PPF_pragma_once_SUPPORT */
00521 
00522 /* ////////////////////////////////////////////////////////////////////// */
00523 
00524 #endif /* !PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_INTEGER */
00525 
00526 /* ///////////////////////////// end of file //////////////////////////// */

pantheios Library documentation © Matthew Wilson & Synesis Software, 2006-2011 SourceForge.net Logo