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

pantheios/inserters/real.hpp

Go to the documentation of this file.
00001 /* /////////////////////////////////////////////////////////////////////////
00002  * File:        pantheios/inserters/real.hpp
00003  *
00004  * Purpose:     String inserters for fundamental types
00005  *
00006  * Created:     21st June 2005
00007  * Updated:     14th February 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_REAL
00050 #define PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_REAL
00051 
00052 /* /////////////////////////////////////////////////////////////////////////
00053  * Version information
00054  */
00055 
00056 #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
00057 # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_REAL_MAJOR       2
00058 # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_REAL_MINOR       3
00059 # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_REAL_REVISION    4
00060 # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_REAL_EDIT        26
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 
00120 class real
00121 {
00122 public:
00124     typedef real    class_type;
00125 
00126 public:
00131     explicit real(float value, int widthAndFormat = 0);
00136     explicit real(double value, int widthAndFormat = 0);
00141     explicit real(long double value, int widthAndFormat = 0);
00142 
00143 private:
00144     explicit real(int); // Prevents implicit conversion from an integer
00145 
00146 public:
00148     pan_char_t const*   data() const;
00150     pan_char_t const*   c_str() const;
00152     size_t              length() const;
00153 
00154 private:
00155     void construct_() const;
00156     void construct_();
00157 
00158 private:
00159     enum RealSize
00160     {
00161             typeIsFloat         =   -1
00162         ,   typeIsDouble        =   -2
00163         ,   typeIsLongDouble    =   -3
00164     };
00165 
00166     union u
00167     {
00168         float       fValue;
00169         double      dValue;
00170         long double ldValue;
00171     };
00172 
00173     u           m_value;            // The value, copied for lazy conversion
00174     size_t      m_len;              // Length, and marker for type: 1 == float; 2 == double; 3 == long double
00175     pan_char_t  m_sz[256];          // Marker for converted, if m_sz[0] == '\0'
00176     const int   m_widthAndFormat;
00177 
00178 private:
00179 #if !defined(STLSOFT_COMPILER_IS_GCC)
00180     real(class_type const&);
00181 #endif /* compiler */
00182     class_type& operator =(class_type const&);
00183 };
00184 
00185 /* /////////////////////////////////////////////////////////////////////////
00186  * String Access Shims
00187  */
00188 
00189 #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
00190 
00191 # if !defined(PANTHEIOS_NO_NAMESPACE)
00192 namespace shims
00193 {
00194 # endif /* !PANTHEIOS_NO_NAMESPACE */
00195 
00197 # ifdef PANTHEIOS_USE_WIDE_STRINGS
00198 inline wchar_t const* c_str_data_w(real const& r)
00199 # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
00200 inline char const* c_str_data_a(real const& r)
00201 # endif /* PANTHEIOS_USE_WIDE_STRINGS */
00202 {
00203     return r.data();
00204 }
00206 inline pan_char_t const* c_str_data(real const& r)
00207 {
00208     return r.data();
00209 }
00210 
00212 # ifdef PANTHEIOS_USE_WIDE_STRINGS
00213 inline size_t c_str_len_w(real const& r)
00214 # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
00215 inline size_t c_str_len_a(real const& r)
00216 # endif /* PANTHEIOS_USE_WIDE_STRINGS */
00217 {
00218     return r.length();
00219 }
00221 inline size_t c_str_len(real const& r)
00222 {
00223     return r.length();
00224 }
00225 
00227 # ifdef PANTHEIOS_USE_WIDE_STRINGS
00228 inline wchar_t const* c_str_ptr_w(real const& r)
00229 # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
00230 inline char const* c_str_ptr_a(real const& r)
00231 # endif /* PANTHEIOS_USE_WIDE_STRINGS */
00232 {
00233     return r.c_str();
00234 }
00236 inline pan_char_t const* c_str_ptr(real const& r)
00237 {
00238     return r.c_str();
00239 }
00240 
00241 # if !defined(PANTHEIOS_NO_NAMESPACE)
00242 } /* namespace shims */
00243 
00244 #  if defined(STLSOFT_COMPILER_IS_GCC)
00245     /* GCC does not seem to correctly handle the phases of
00246      * processing of C++ templates, so we need to 'use' the
00247      * shims into the same namespace as the inserter class
00248      * in order that ADL can suffice instead.
00249      */
00250 #   ifdef PANTHEIOS_USE_WIDE_STRINGS
00251     using ::pantheios::shims::c_str_data_w;
00252     using ::pantheios::shims::c_str_len_w;
00253     using ::pantheios::shims::c_str_ptr_w;
00254 #   else /* ? PANTHEIOS_USE_WIDE_STRINGS */
00255     using ::pantheios::shims::c_str_data_a;
00256     using ::pantheios::shims::c_str_len_a;
00257     using ::pantheios::shims::c_str_ptr_a;
00258 #   endif /* PANTHEIOS_USE_WIDE_STRINGS */
00259     using ::pantheios::shims::c_str_data;
00260     using ::pantheios::shims::c_str_len;
00261     using ::pantheios::shims::c_str_ptr;
00262 #  endif /* compiler */
00263 
00264 # endif /* !PANTHEIOS_NO_NAMESPACE */
00265 
00266 #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
00267 
00268 /* /////////////////////////////////////////////////////////////////////////
00269  * Namespace
00270  */
00271 
00272 #if !defined(PANTHEIOS_NO_NAMESPACE)
00273 } /* namespace pantheios */
00274 
00275 namespace stlsoft
00276 {
00277     // 'Export' the string access shims into the STLSoft namespace
00278     //
00279     // c_str_ptr(_a) is not necessary for version 1.0 of Pantheios, but it's
00280     // defined and exported in order to allow for the case where someone
00281     // may find a legitimate use for the conversion classes additional to
00282     // the type-tunneling of the Pantheios API.
00283 
00284 # ifdef PANTHEIOS_USE_WIDE_STRINGS
00285     using ::pantheios::shims::c_str_data_w;
00286     using ::pantheios::shims::c_str_len_w;
00287     using ::pantheios::shims::c_str_ptr_w;
00288 # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
00289     using ::pantheios::shims::c_str_data_a;
00290     using ::pantheios::shims::c_str_len_a;
00291     using ::pantheios::shims::c_str_ptr_a;
00292 # endif /* PANTHEIOS_USE_WIDE_STRINGS */
00293     using ::pantheios::shims::c_str_data;
00294     using ::pantheios::shims::c_str_len;
00295     using ::pantheios::shims::c_str_ptr;
00296 }
00297 
00298 #endif /* !PANTHEIOS_NO_NAMESPACE */
00299 
00300 /* /////////////////////////////////////////////////////////////////////////
00301  * Inclusion
00302  */
00303 
00304 #ifdef STLSOFT_PPF_pragma_once_SUPPORT
00305 # pragma once
00306 #endif /* STLSOFT_PPF_pragma_once_SUPPORT */
00307 
00308 /* ////////////////////////////////////////////////////////////////////// */
00309 
00310 #endif /* !PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_REAL */
00311 
00312 /* ///////////////////////////// end of file //////////////////////////// */

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