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

fastformat/shims/conversion/filter_type/reals.hpp

Go to the documentation of this file.
00001 /* /////////////////////////////////////////////////////////////////////////
00002  * File:        fastformat/shims/conversion/filter_type/reals.hpp
00003  *
00004  * Purpose:     FastFormat argument conversion shim for floating-point
00005  *              types.
00006  *
00007  * Created:     1st June 2008
00008  * Updated:     7th March 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 
00048 #ifndef FASTFORMAT_INCL_FASTFORMAT_SHIMS_CONVERSION_FILTER_TYPE_HPP_REALS
00049 #define FASTFORMAT_INCL_FASTFORMAT_SHIMS_CONVERSION_FILTER_TYPE_HPP_REALS
00050 
00051 /* /////////////////////////////////////////////////////////////////////////
00052  * Version information
00053  */
00054 
00055 #ifndef FASTFORMAT_DOCUMENTATION_SKIP_SECTION
00056 # define FASTFORMAT_VER_FASTFORMAT_SHIMS_CONVERSION_FILTER_TYPE_HPP_REALS_MAJOR     1
00057 # define FASTFORMAT_VER_FASTFORMAT_SHIMS_CONVERSION_FILTER_TYPE_HPP_REALS_MINOR     2
00058 # define FASTFORMAT_VER_FASTFORMAT_SHIMS_CONVERSION_FILTER_TYPE_HPP_REALS_REVISION  2
00059 # define FASTFORMAT_VER_FASTFORMAT_SHIMS_CONVERSION_FILTER_TYPE_HPP_REALS_EDIT      7
00060 #endif /* !FASTFORMAT_DOCUMENTATION_SKIP_SECTION */
00061 
00062 /* /////////////////////////////////////////////////////////////////////////
00063  * Language
00064  */
00065 
00066 #ifndef __cplusplus
00067 # error This file can only be included in C++ compilation units
00068 #endif /* !__cplusplus */
00069 
00070 /* /////////////////////////////////////////////////////////////////////////
00071  * Includes
00072  */
00073 
00074 #include <fastformat/fastformat.h>
00075 #include <fastformat/internal/stlsoft.h>
00076 #include <fastformat/quality/contract.h>
00077 
00078 #include <stlsoft/string/shim_string.hpp>
00079 
00080 #include <stdio.h>
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  * Overloadable conversion shim functions:
00095  *
00096  * float
00097  * double
00098  */
00099 
00103 inline stlsoft::basic_shim_string<ff_char_t, 32> filter_type(double value, double const*, ff_char_t const volatile*)
00104 {
00105     static const ff_char_t  fmt[] = { '%', 'G', '\0' };
00106 
00107     ff_char_t num[100];
00108 
00109     // TODO: Sort this properly, using detection of the "Safe" string library, a la Pantheios
00110 
00111     // TODO: Sort this properly, using traits instead of all the pre-processor clutter
00112 
00113 #if defined(__STDC_SECURE_LIB__) && \
00114     defined(__STDC_WANT_SECURE_LIB__) && \
00115     __STDC_WANT_SECURE_LIB__ == 1
00116 # ifdef FASTFORMAT_USE_WIDE_STRINGS
00117     int     n = ::swprintf_s(&num[0], STLSOFT_NUM_ELEMENTS(num), fmt, value);
00118 # else /* ? FASTFORMAT_USE_WIDE_STRINGS */
00119     int     n = ::sprintf_s(&num[0], STLSOFT_NUM_ELEMENTS(num), fmt, value);
00120 # endif /* FASTFORMAT_USE_WIDE_STRINGS */
00121 #else /* ? "secure" */
00122 # ifdef FASTFORMAT_USE_WIDE_STRINGS
00123     int     n = ::swprintf(&num[0], fmt, value);
00124 # else /* ? FASTFORMAT_USE_WIDE_STRINGS */
00125     int     n = ::sprintf(&num[0], fmt, value);
00126 # endif /* FASTFORMAT_USE_WIDE_STRINGS */
00127 #endif /* "secure" */
00128 
00129     if(n < 0)
00130     {
00131         n = 0;
00132     }
00133 
00134     return stlsoft::basic_shim_string<ff_char_t, 32>(num, static_cast<size_t>(n));
00135 }
00136 
00140 inline stlsoft::basic_shim_string<ff_char_t, 32> filter_type(float value, float const*, ff_char_t const volatile*)
00141 {
00142     double value2 = value;
00143 
00144     return filter_type(value2, &value2, static_cast<ff_char_t const volatile*>(0));
00145 }
00146 
00147 /* /////////////////////////////////////////////////////////////////////////
00148  * Namespace
00149  */
00150 
00151 #if !defined(FASTFORMAT_NO_NAMESPACE)
00152 } /* namespace filters */
00153 } /* namespace fastformat */
00154 #endif /* !FASTFORMAT_NO_NAMESPACE */
00155 
00156 /* /////////////////////////////////////////////////////////////////////////
00157  * Inclusion
00158  */
00159 
00160 #ifdef STLSOFT_PPF_pragma_once_SUPPORT
00161 # pragma once
00162 #endif /* STLSOFT_PPF_pragma_once_SUPPORT */
00163 
00164 /* ////////////////////////////////////////////////////////////////////// */
00165 
00166 #endif /* FASTFORMAT_INCL_FASTFORMAT_SHIMS_CONVERSION_FILTER_TYPE_HPP_REALS */
00167 
00168 /* ///////////////////////////// end of file //////////////////////////// */

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