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

fastformat/shims/conversion/filter_type/fast_integers.hpp

Go to the documentation of this file.
00001 /* /////////////////////////////////////////////////////////////////////////
00002  * File:        fastformat/shims/conversion/filter_type/fast_integers.hpp
00003  *
00004  * Purpose:     FastFormat argument conversion shim for fast, non-L10N,
00005  *              conversion of integral types.
00006  *
00007  * Created:     1st June 2008
00008  * Updated:     22nd June 2010
00009  *
00010  * Home:        http://www.fastformat.org/
00011  *
00012  * Copyright (c) 2008-2010, Matthew Wilson and Synesis Software
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 names of Matthew Wilson and Synesis Software nor the names
00025  *   of any contributors may be used to endorse or promote products derived
00026  *   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 FASTFORMAT_INCL_FASTFORMAT_SHIMS_CONVERSION_FILTER_TYPE_HPP_FAST_INTEGERS
00050 #define FASTFORMAT_INCL_FASTFORMAT_SHIMS_CONVERSION_FILTER_TYPE_HPP_FAST_INTEGERS
00051 
00052 /* /////////////////////////////////////////////////////////////////////////
00053  * Version information
00054  */
00055 
00056 #ifndef FASTFORMAT_DOCUMENTATION_SKIP_SECTION
00057 # define FASTFORMAT_VER_FASTFORMAT_SHIMS_CONVERSION_FILTER_TYPE_HPP_FAST_INTEGERS_MAJOR     2
00058 # define FASTFORMAT_VER_FASTFORMAT_SHIMS_CONVERSION_FILTER_TYPE_HPP_FAST_INTEGERS_MINOR     1
00059 # define FASTFORMAT_VER_FASTFORMAT_SHIMS_CONVERSION_FILTER_TYPE_HPP_FAST_INTEGERS_REVISION  1
00060 # define FASTFORMAT_VER_FASTFORMAT_SHIMS_CONVERSION_FILTER_TYPE_HPP_FAST_INTEGERS_EDIT      11
00061 #endif /* !FASTFORMAT_DOCUMENTATION_SKIP_SECTION */
00062 
00063 /* /////////////////////////////////////////////////////////////////////////
00064  * Language
00065  */
00066 
00067 #ifndef __cplusplus
00068 # error This file can only be included in C++ compilation units
00069 #endif /* !__cplusplus */
00070 
00071 /* /////////////////////////////////////////////////////////////////////////
00072  * Includes
00073  */
00074 
00075 #include <fastformat/fastformat.h>
00076 #include <fastformat/internal/stlsoft.h>
00077 #include <fastformat/quality/contract.h>
00078 
00079 #include <stlsoft/conversion/integer_to_string.hpp>
00080 #include <stlsoft/string/shim_string.hpp>
00081 
00082 /* /////////////////////////////////////////////////////////////////////////
00083  * Namespace
00084  */
00085 
00086 #if !defined(FASTFORMAT_NO_NAMESPACE)
00087 namespace fastformat
00088 {
00089 namespace filters
00090 {
00091 #endif /* !FASTFORMAT_NO_NAMESPACE */
00092 
00093 /* /////////////////////////////////////////////////////////////////////////
00094  * Typedefs
00095  */
00096 
00097 namespace ximpl_fastformat_filters_fast_integers
00098 {
00099 #if 1
00100     template <size_t N>
00101     struct integer_shim
00102     {
00103     public:
00104         ff_char_t const*    ptr;
00105         ff_char_t           buff[N];
00106         size_t              len;
00107 
00108     public:
00109         integer_shim()
00110         {}
00111 
00112         integer_shim(integer_shim const& rhs)
00113             : ptr(&buff[0] + (rhs.ptr - &rhs.buff[0]))
00114             , len(rhs.len)
00115         {
00116             ::memcpy(const_cast<ff_char_t*>(ptr), rhs.ptr, sizeof(ff_char_t) * len);
00117         }
00118 
00119     public:
00120         void assign(ff_char_t const* p)
00121         {
00122             this->ptr = p;
00123         }
00124 
00125     public:
00126         ff_char_t const*    data() const
00127         {
00128             return ptr;
00129         }
00130         size_t              size() const
00131         {
00132             return len;
00133         }
00134     };
00135 #else /* ? 0 */
00136     template <size_t N>
00137     struct integer_shim
00138     {
00139     public:
00140         ptrdiff_t           offset;
00141         ff_char_t           buff[N];
00142         size_t              len;
00143 
00144     public:
00145         void assign(ff_char_t const* p)
00146         {
00147             this->offset = p - &buff[0];
00148         }
00149 
00150     public:
00151         ff_char_t const*    data() const
00152         {
00153             return &buff[0] + offset;
00154         }
00155         size_t              size() const
00156         {
00157             return len;
00158         }
00159     };
00160 #endif /* 0 */
00161 
00162 } /* namespace ximpl_fastformat_filters_fast_integers */
00163 
00164 
00165 /* /////////////////////////////////////////////////////////////////////////
00166  * Overloadable conversion shim functions:
00167  *
00168  * 64-bit/long long signed
00169  * 64-bit/long long unsigned
00170  * long signed
00171  * long unsigned
00172  * int signed
00173  * int unsigned
00174  * short signed
00175  * short unsigned
00176  */
00177 
00178 #if defined(STLSOFT_CF_BUILTIN_long_long_SUPPORT) || \
00179     defined(STLSOFT_CF_64BIT_INT_SUPPORT)
00180 
00185 inline ximpl_fastformat_filters_fast_integers::integer_shim<21> filter_type(
00186 # if defined(STLSOFT_CF_BUILTIN_long_long_SUPPORT)
00187     signed long long value
00188 ,   signed long long const*
00189 # elif defined(STLSOFT_CF_64BIT_INT_SUPPORT)
00190     ::stlsoft::sint64_t value
00191 ,   ::stlsoft::sint64_t const*
00192 # else /* ? 64-bit / long long */
00193 #  error Bad discrimination
00194 # endif /* 64-bit / long long */
00195 ,   ff_char_t const volatile*
00196 )
00197 {
00198     ximpl_fastformat_filters_fast_integers::integer_shim<21> r;
00199 
00200     r.assign(stlsoft::integer_to_string(r.buff, STLSOFT_NUM_ELEMENTS(r.buff), value, r.len));
00201 
00202     return r;
00203 }
00204 
00209 inline ximpl_fastformat_filters_fast_integers::integer_shim<21> filter_type(
00210 # if defined(STLSOFT_CF_BUILTIN_long_long_SUPPORT)
00211     unsigned long long value
00212 ,   unsigned long long const*
00213 # elif defined(STLSOFT_CF_64BIT_INT_SUPPORT)
00214     ::stlsoft::uint64_t value
00215 ,   ::stlsoft::uint64_t const*
00216 # else /* ? 64-bit / long long */
00217 #  error Bad discrimination
00218 # endif /* 64-bit / long long */
00219 , ff_char_t const volatile*
00220 )
00221 {
00222     ximpl_fastformat_filters_fast_integers::integer_shim<21> r;
00223 
00224     r.assign(stlsoft::integer_to_string(r.buff, STLSOFT_NUM_ELEMENTS(r.buff), value, r.len));
00225 
00226     return r;
00227 }
00228 
00229 #endif /* STLSOFT_CF_BUILTIN_long_long_SUPPORT || STLSOFT_CF_64BIT_INT_SUPPORT */
00230 
00234 inline ximpl_fastformat_filters_fast_integers::integer_shim<12> filter_type(
00235     signed long value
00236 ,   signed long const*
00237 ,   ff_char_t const volatile*
00238 )
00239 {
00240     ximpl_fastformat_filters_fast_integers::integer_shim<12> r;
00241 
00242     r.assign(stlsoft::integer_to_string(r.buff, STLSOFT_NUM_ELEMENTS(r.buff), value, r.len));
00243 
00244     return r;
00245 }
00246 
00250 inline ximpl_fastformat_filters_fast_integers::integer_shim<12> filter_type(
00251     unsigned long value
00252 ,   unsigned long const*
00253 ,   ff_char_t const volatile*
00254 )
00255 {
00256     ximpl_fastformat_filters_fast_integers::integer_shim<12> r;
00257 
00258     r.assign(stlsoft::integer_to_string(r.buff, STLSOFT_NUM_ELEMENTS(r.buff), value, r.len));
00259 
00260     return r;
00261 }
00262 
00266 inline ximpl_fastformat_filters_fast_integers::integer_shim<12> filter_type(
00267     signed int value
00268 ,   signed int const*
00269 ,   ff_char_t const volatile*
00270 )
00271 {
00272     signed long value2 = value;
00273 
00274     return filter_type(value2, &value2, static_cast<ff_char_t const volatile*>(0));
00275 }
00276 
00280 inline ximpl_fastformat_filters_fast_integers::integer_shim<12> filter_type(
00281     unsigned int value
00282 ,   unsigned int const*
00283 ,   ff_char_t const volatile*
00284 )
00285 {
00286     unsigned long value2 = value;
00287 
00288     return filter_type(value2, &value2, static_cast<ff_char_t const volatile*>(0));
00289 }
00290 
00294 inline ximpl_fastformat_filters_fast_integers::integer_shim<12> filter_type(
00295     signed short value
00296 ,   signed short const*
00297 ,   ff_char_t const volatile*
00298 )
00299 {
00300     signed long value2 = value;
00301 
00302     return filter_type(value2, &value2, static_cast<ff_char_t const volatile*>(0));
00303 }
00304 
00308 inline ximpl_fastformat_filters_fast_integers::integer_shim<12> filter_type(
00309     unsigned short value
00310 ,   unsigned short const*
00311 ,   ff_char_t const volatile*
00312 )
00313 {
00314     unsigned long value2 = value;
00315 
00316     return filter_type(value2, &value2, static_cast<ff_char_t const volatile*>(0));
00317 }
00318 
00319 /* ////////////////////////////////////////////////////////////////////// */
00320 
00321 #ifdef STLSOFT_CF_INT_DISTINCT_INT_TYPE
00322 inline ximpl_fastformat_filters_fast_integers::integer_shim<12> filter_type(
00323     ::stlsoft::sint32_t value
00324 ,   ::stlsoft::sint32_t const*
00325 ,   ff_char_t const volatile*
00326 )
00327 {
00328     signed long value2 = value;
00329 
00330     return filter_type(value2, &value2, static_cast<ff_char_t const volatile*>(0));
00331 }
00332 
00333 inline ximpl_fastformat_filters_fast_integers::integer_shim<12> filter_type(
00334     ::stlsoft::uint32_t value
00335 ,   ::stlsoft::uint32_t const*
00336 ,   ff_char_t const volatile*
00337 )
00338 {
00339     unsigned long value2 = value;
00340 
00341     return filter_type(value2, &value2, static_cast<ff_char_t const volatile*>(0));
00342 }
00343 #endif /* STLSOFT_CF_INT_DISTINCT_INT_TYPE */
00344 
00345 /* /////////////////////////////////////////////////////////////////////////
00346  * Shims
00347  */
00348 
00350 template <size_t N>
00351 #ifdef FASTFORMAT_USE_WIDE_STRINGS
00352 inline wchar_t const* c_str_data_w(
00353 #else /* ? FASTFORMAT_USE_WIDE_STRINGS */
00354 inline char const* c_str_data_a(
00355 #endif /* !FASTFORMAT_USE_WIDE_STRINGS */
00356 #if !defined(FASTFORMAT_NO_NAMESPACE)
00357     ::fastformat::filters::
00358 #endif /* !FASTFORMAT_NO_NAMESPACE */
00359         ximpl_fastformat_filters_fast_integers::integer_shim<N> const& s
00360 )
00361 {
00362 #if 1
00363     return s.data();
00364 #else /* ? 0 */
00365     return s.ptr;
00366 #endif /* 0 */
00367 }
00368 
00370 template <size_t N>
00371 inline ff_char_t const* c_str_data(
00372 #if !defined(FASTFORMAT_NO_NAMESPACE)
00373     ::fastformat::filters::
00374 #endif /* !FASTFORMAT_NO_NAMESPACE */
00375         ximpl_fastformat_filters_fast_integers::integer_shim<N> const& s
00376 )
00377 {
00378 #ifdef FASTFORMAT_USE_WIDE_STRINGS
00379     return c_str_data_w(s);
00380 #else /* ? FASTFORMAT_USE_WIDE_STRINGS */
00381     return c_str_data_a(s);
00382 #endif /* !FASTFORMAT_USE_WIDE_STRINGS */
00383 }
00384 
00386 template <size_t N>
00387 #ifdef FASTFORMAT_USE_WIDE_STRINGS
00388 inline size_t c_str_len_w(
00389 #else /* ? FASTFORMAT_USE_WIDE_STRINGS */
00390 inline size_t c_str_len_a(
00391 #endif /* !FASTFORMAT_USE_WIDE_STRINGS */
00392 #if !defined(FASTFORMAT_NO_NAMESPACE)
00393     ::fastformat::filters::
00394 #endif /* !FASTFORMAT_NO_NAMESPACE */
00395         ximpl_fastformat_filters_fast_integers::integer_shim<N> const& s
00396 )
00397 {
00398 #if 0
00399     return s.size();
00400 #else /* ? 0 */
00401     return s.len;
00402 #endif /* 0 */
00403 }
00404 
00406 template <size_t N>
00407 inline size_t c_str_len(
00408 #if !defined(FASTFORMAT_NO_NAMESPACE)
00409     ::fastformat::filters::
00410 #endif /* !FASTFORMAT_NO_NAMESPACE */
00411         ximpl_fastformat_filters_fast_integers::integer_shim<N> const& s
00412 )
00413 {
00414 #ifdef FASTFORMAT_USE_WIDE_STRINGS
00415     return c_str_len_w(s);
00416 #else /* ? FASTFORMAT_USE_WIDE_STRINGS */
00417     return c_str_len_a(s);
00418 #endif /* !FASTFORMAT_USE_WIDE_STRINGS */
00419 }
00420 
00421 /* /////////////////////////////////////////////////////////////////////////
00422  * Namespace
00423  */
00424 
00425 #if !defined(FASTFORMAT_NO_NAMESPACE)
00426 } /* namespace filters */
00427 } /* namespace fastformat */
00428 
00429 # ifndef _STLSOFT_NO_NAMESPACE
00430 namespace stlsoft
00431 {
00432 # endif /* !_STLSOFT_NO_NAMESPACE */
00433 
00434 # ifdef FASTFORMAT_USE_WIDE_STRINGS
00435     using ::fastformat::filters::c_str_data_w;
00436     using ::fastformat::filters::c_str_len_w;
00437 # else /* ? FASTFORMAT_USE_WIDE_STRINGS */
00438     using ::fastformat::filters::c_str_data_a;
00439     using ::fastformat::filters::c_str_len_a;
00440 # endif /* FASTFORMAT_USE_WIDE_STRINGS */
00441     using ::fastformat::filters::c_str_data;
00442     using ::fastformat::filters::c_str_len;
00443 
00444 # ifndef _STLSOFT_NO_NAMESPACE
00445 } /* namespace stlsoft */
00446 # endif /* !_STLSOFT_NO_NAMESPACE */
00447 
00448 #else /* ? !FASTFORMAT_NO_NAMESPACE */
00449 
00450 # ifndef _STLSOFT_NO_NAMESPACE
00451 namespace stlsoft
00452 {
00453 
00455 template <size_t N>
00456 #  ifdef FASTFORMAT_USE_WIDE_STRINGS
00457 inline wchar_t const* c_str_data_w(
00458 #  else /* ? FASTFORMAT_USE_WIDE_STRINGS */
00459 inline char const* c_str_data_a(
00460 #  endif /* !FASTFORMAT_USE_WIDE_STRINGS */
00461 #if !defined(FASTFORMAT_NO_NAMESPACE)
00462     ::fastformat::filters::
00463 #endif /* !FASTFORMAT_NO_NAMESPACE */
00464         ximpl_fastformat_filters_fast_integers::integer_shim<N> const& s
00465 )
00466 {
00467 #if 1
00468     return s.data();
00469 #else /* ? 0 */
00470     return s.ptr;
00471 #endif /* 0 */
00472 }
00473 
00475 template <size_t N>
00476 inline ff_char_t const* c_str_data(
00477 #if !defined(FASTFORMAT_NO_NAMESPACE)
00478     ::fastformat::filters::
00479 #endif /* !FASTFORMAT_NO_NAMESPACE */
00480         ximpl_fastformat_filters_fast_integers::integer_shim<N> const& s
00481 )
00482 {
00483 #ifdef FASTFORMAT_USE_WIDE_STRINGS
00484     return c_str_data_w(s);
00485 #else /* ? FASTFORMAT_USE_WIDE_STRINGS */
00486     return c_str_data_a(s);
00487 #endif /* !FASTFORMAT_USE_WIDE_STRINGS */
00488 }
00489 
00491 template <size_t N>
00492 #  ifdef FASTFORMAT_USE_WIDE_STRINGS
00493 inline size_t c_str_len_w(
00494 #  else /* ? FASTFORMAT_USE_WIDE_STRINGS */
00495 inline size_t c_str_len_a(
00496 #  endif /* !FASTFORMAT_USE_WIDE_STRINGS */
00497 #if !defined(FASTFORMAT_NO_NAMESPACE)
00498     ::fastformat::filters::
00499 #endif /* !FASTFORMAT_NO_NAMESPACE */
00500         ximpl_fastformat_filters_fast_integers::integer_shim<N> const& s
00501 )
00502 {
00503 #if 0
00504     return s.size();
00505 #else /* ? 0 */
00506     return s.len;
00507 #endif /* 0 */
00508 }
00509 
00511 template <size_t N>
00512 inline size_t c_str_len(
00513 #if !defined(FASTFORMAT_NO_NAMESPACE)
00514     ::fastformat::filters::
00515 #endif /* !FASTFORMAT_NO_NAMESPACE */
00516         ximpl_fastformat_filters_fast_integers::integer_shim<N> const& s
00517 )
00518 {
00519 #ifdef FASTFORMAT_USE_WIDE_STRINGS
00520     return c_str_len_w(s);
00521 #else /* ? FASTFORMAT_USE_WIDE_STRINGS */
00522     return c_str_len_a(s);
00523 #endif /* !FASTFORMAT_USE_WIDE_STRINGS */
00524 }
00525 
00526 } /* namespace stlsoft */
00527 # endif /* !_STLSOFT_NO_NAMESPACE */
00528 
00529 #endif /* !FASTFORMAT_NO_NAMESPACE */
00530 
00531 /* /////////////////////////////////////////////////////////////////////////
00532  * Inclusion
00533  */
00534 
00535 #ifdef STLSOFT_PPF_pragma_once_SUPPORT
00536 # pragma once
00537 #endif /* STLSOFT_PPF_pragma_once_SUPPORT */
00538 
00539 /* ////////////////////////////////////////////////////////////////////// */
00540 
00541 #endif /* FASTFORMAT_INCL_FASTFORMAT_SHIMS_CONVERSION_FILTER_TYPE_HPP_FAST_INTEGERS */
00542 
00543 /* ///////////////////////////// end of file //////////////////////////// */

FastFormat Library documentation © Matthew Wilson, 2006-2009 SourceForge.net Logo