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

fastformat/sinks/shim_string.hpp

Go to the documentation of this file.
00001 /* /////////////////////////////////////////////////////////////////////////
00002  * File:        fastformat/sinks/shim_string.hpp
00003  *
00004  * Purpose:     A FastFormat sink for STLSoft's shim_string class template.
00005  *
00006  * Created:     12th November 2008
00007  * Updated:     30th April 2010
00008  *
00009  * Home:        http://www.fastformat.org/
00010  *
00011  * Copyright (c) 2008-2010, Matthew Wilson and Synesis Software
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are
00016  * met:
00017  *
00018  * - Redistributions of source code must retain the above copyright notice,
00019  *   this list of conditions and the following disclaimer.
00020  * - Redistributions in binary form must reproduce the above copyright
00021  *   notice, this list of conditions and the following disclaimer in the
00022  *   documentation and/or other materials provided with the distribution.
00023  * - Neither the names of Matthew Wilson and Synesis Software nor the names
00024  *   of any contributors may be used to endorse or promote products derived
00025  *   from this software without specific prior written permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00028  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
00029  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00030  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00031  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00032  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00033  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00034  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00035  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00036  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00037  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00038  *
00039  * ////////////////////////////////////////////////////////////////////// */
00040 
00041 
00047 #ifndef FASTFORMAT_INCL_FASTFORMAT_SINK_HPP_SHIM_STRING
00048 #define FASTFORMAT_INCL_FASTFORMAT_SINK_HPP_SHIM_STRING
00049 
00050 /* /////////////////////////////////////////////////////////////////////////
00051  * Version information
00052  */
00053 
00054 #ifndef FASTFORMAT_DOCUMENTATION_SKIP_SECTION
00055 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_SHIM_STRING_SINK_MAJOR      1
00056 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_SHIM_STRING_SINK_MINOR      0
00057 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_SHIM_STRING_SINK_REVISION   5
00058 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_SHIM_STRING_SINK_EDIT       8
00059 #endif /* !FASTFORMAT_DOCUMENTATION_SKIP_SECTION */
00060 
00061 /* /////////////////////////////////////////////////////////////////////////
00062  * Language
00063  */
00064 
00065 #ifndef __cplusplus
00066 # error This file can only be included in C++ compilation units
00067 #endif /* !__cplusplus */
00068 
00069 /* /////////////////////////////////////////////////////////////////////////
00070  * Includes
00071  */
00072 
00073 #include <fastformat/fastformat.h>
00074 #include <fastformat/quality/contract.h>
00075 #include <fastformat/util/sinks/helpers.hpp>
00076 #include <fastformat/format/standard_flags.hpp>
00077 
00078 #include <stlsoft/shims/access/string/std/c_string.h>
00079 #include <stlsoft/string/shim_string.hpp>
00080 
00081 /* /////////////////////////////////////////////////////////////////////////
00082  * Namespace
00083  */
00084 
00085 #if !defined(FASTFORMAT_NO_NAMESPACE)
00086 namespace fastformat
00087 {
00088 namespace sinks
00089 {
00090 #endif /* !FASTFORMAT_NO_NAMESPACE */
00091 
00092 /* /////////////////////////////////////////////////////////////////////////
00093  * Compatibility
00094  */
00095 
00096 #if _STLSOFT_VER < 0x010a0000 && \
00097     defined(_STLSOFT_1_10_VER)
00098 # if _STLSOFT_1_10_VER < 0x010a0109
00099 #  error This file cannot be used with STLSoft 1.10.1 alpha 1 - STLSoft 1.10.1 alpha 8, and is not necessary with STLSoft 1.10.1 alpha 9+ (for which it is not necessary)
00100 # endif /* _STLSOFT_1_10_VER */
00101 #endif /* _STLSOFT_VER */
00102 
00103 /* /////////////////////////////////////////////////////////////////////////
00104  * Classes
00105  */
00106 
00130 class shim_string_sink
00131     : public stlsoft::basic_shim_string<ff_char_t>
00132 {
00133 public: // Member Types
00135     typedef shim_string_sink                        class_type;
00137     typedef stlsoft::basic_shim_string<ff_char_t>   parent_class_type;
00139     typedef parent_class_type::size_type            size_type;
00140 private:
00141 
00142 public: // Construction
00143 
00145     shim_string_sink()
00146         : parent_class_type(size_type(0))
00147 #if _STLSOFT_VER < 0x010a0000 && \
00148     !defined(_STLSOFT_1_10_VER)
00149         , m_len(0)
00150 #endif
00151     {}
00155     explicit shim_string_sink(size_type n)
00156         : parent_class_type(n)
00157 #if _STLSOFT_VER < 0x010a0000 && \
00158     !defined(_STLSOFT_1_10_VER)
00159         , m_len(0)
00160 #endif
00161     {}
00162 private:
00163     class_type& operator =(class_type const& rhs); // Not supported
00164 
00165 public: // Operations
00166 
00170     void reserve(size_type n)
00171     {
00172 #if _STLSOFT_VER < 0x010a0000 && \
00173     !defined(_STLSOFT_1_10_VER)
00174 
00175         parent_class_type::buffer_type& buffer = parent_class_type::get_buffer();
00176 
00177         if(buffer.resize(n + 1))
00178         {
00179             buffer[n] = '\0';
00180         }
00181 
00182 #else
00183         parent_class_type::reserve(n);
00184 #endif
00185     }
00186 
00188     class_type& append(ff_char_t const* s, size_type n)
00189     {
00190 #if _STLSOFT_VER < 0x010a0000 && \
00191     !defined(_STLSOFT_1_10_VER)
00192 
00193         parent_class_type::buffer_type& buffer = parent_class_type::get_buffer();
00194 
00195         if(m_len + n + 1 > buffer.size())
00196         {
00197             if(!buffer.resize(m_len + n + 1))
00198             {
00199                 buffer[0] = '\0';
00200                 m_len = 0;
00201 
00202                 return *this;
00203             }
00204         }
00205 
00206         ::memcpy(&buffer[0] + m_len, s, sizeof(ff_char_t) * n);
00207         m_len += n;
00208         buffer[m_len] = '\0';
00209 
00210 #else
00211         parent_class_type::append(s, n);
00212 #endif
00213 
00214         return *this;
00215     }
00216 
00218     class_type& append(ff_char_t const* s)
00219     {
00220 #if _STLSOFT_VER < 0x010a0000 && \
00221     !defined(_STLSOFT_1_10_VER)
00222 
00223         append(s, stlsoft::c_str_len(s));
00224 
00225 #else
00226         parent_class_type::append(s);
00227 #endif
00228 
00229         return *this;
00230     }
00231 
00233     void truncate(size_type n)
00234     {
00235         parent_class_type::truncate(n);
00236     }
00237 
00238 #if _STLSOFT_VER < 0x010a0000 && \
00239     !defined(_STLSOFT_1_10_VER)
00240 
00241 public: // Accessors
00242 
00244     size_type size() const
00245     {
00246         STLSOFT_ASSERT(m_len < const_cast<class_type*>(this)->parent_class_type::get_buffer().size());
00247 
00248         return m_len;
00249     }
00250 
00252     operator size_type() const
00253     {
00254         return size();
00255     }
00256 
00257 private:
00258     size_type   m_len;
00259 #endif
00260 };
00261 
00262 /* /////////////////////////////////////////////////////////////////////////
00263  * Action Shims
00264  */
00265 
00266 /* /////////////////////////////////////////////////////////////////////////
00267  * Shims
00268  */
00269 
00271 #ifdef FASTFORMAT_USE_WIDE_STRINGS
00272 inline wchar_t const* c_str_data_w(
00273 #else /* ? FASTFORMAT_USE_WIDE_STRINGS */
00274 inline char const* c_str_data_a(
00275 #endif /* FASTFORMAT_USE_WIDE_STRINGS */
00276     shim_string_sink const& sink
00277 )
00278 {
00279     return sink.data();
00280 }
00281 
00283 inline ff_char_t const* c_str_data(
00284     shim_string_sink const& sink
00285 )
00286 {
00287     return sink.data();
00288 }
00289 
00291 #ifdef FASTFORMAT_USE_WIDE_STRINGS
00292 inline size_t c_str_len_w(
00293 #else /* ? FASTFORMAT_USE_WIDE_STRINGS */
00294 inline size_t c_str_len_a(
00295 #endif /* FASTFORMAT_USE_WIDE_STRINGS */
00296     shim_string_sink const& sink
00297 )
00298 {
00299     return sink.size();
00300 }
00301 
00303 inline size_t c_str_len(
00304     shim_string_sink const& sink
00305 )
00306 {
00307     return size_t(sink.size());
00308 }
00309 
00310 /* /////////////////////////////////////////////////////////////////////////
00311  * Namespace
00312  */
00313 
00314 #if !defined(FASTFORMAT_NO_NAMESPACE)
00315 } /* namespace sinks */
00316 } /* namespace fastformat */
00317 #endif /* !FASTFORMAT_NO_NAMESPACE */
00318 
00319 /* ////////////////////////////////////////////////////////////////////// */
00320 
00321 #endif /* FASTFORMAT_INCL_FASTFORMAT_SINK_HPP_SHIM_STRING_SINK */
00322 
00323 /* ///////////////////////////// end of file //////////////////////////// */

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