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

pantheios/inserters/w2m.hpp

Go to the documentation of this file.
00001 /* /////////////////////////////////////////////////////////////////////////
00002  * File:        pantheios/inserters/w2m.hpp
00003  *
00004  * Purpose:     Class for inserting wide strings.
00005  *
00006  * Created:     2nd September 2008
00007  * Updated:     23rd July 2010
00008  *
00009  * Home:        http://www.pantheios.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 name(s) of Matthew Wilson and Synesis Software nor the
00024  *   names of any contributors may be used to endorse or promote products
00025  *   derived 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 PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_W2M
00048 #define PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_W2M
00049 
00050 /* /////////////////////////////////////////////////////////////////////////
00051  * Version information
00052  */
00053 
00054 #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
00055 # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_W2M_MAJOR    1
00056 # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_W2M_MINOR    2
00057 # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_W2M_REVISION 2
00058 # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_W2M_EDIT     13
00059 #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
00060 
00061 /* /////////////////////////////////////////////////////////////////////////
00062  * Includes
00063  */
00064 
00065 #ifndef PANTHEIOS_INCL_PANTHEIOS_H_PANTHEIOS
00066 # include <pantheios/pantheios.h>
00067 #endif /* !PANTHEIOS_INCL_PANTHEIOS_H_PANTHEIOS */
00068 #ifndef PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_FMT
00069 # include <pantheios/inserters/fmt.hpp>
00070 #endif /* !PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_FMT */
00071 
00072 #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
00073 # include <stlsoft/shims/access/string.hpp>
00074 #endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING */
00075 
00076 #ifdef PANTHEIOS_USE_WIDE_STRINGS
00077 # include <stlsoft/string/string_view.hpp>
00078 #endif /* PANTHEIOS_USE_WIDE_STRINGS */
00079 
00080 /* /////////////////////////////////////////////////////////////////////////
00081  * Namespace
00082  */
00083 
00084 #if !defined(PANTHEIOS_NO_NAMESPACE)
00085 namespace pantheios
00086 {
00087 #endif /* !PANTHEIOS_NO_NAMESPACE */
00088 
00089 /* /////////////////////////////////////////////////////////////////////////
00090  * Inserter classes
00091  */
00092 
00115 #ifdef PANTHEIOS_USE_WIDE_STRINGS
00116 
00117 typedef stlsoft_ns_qual(wstring_view)   w2m;
00118 
00119 #else /* ? PANTHEIOS_USE_WIDE_STRINGS */
00120 
00121 class w2m
00122 {
00123 public: // Member Types
00125     typedef w2m     class_type;
00126 
00127 public: // Construction
00129     explicit w2m(wchar_t const* s);
00131     w2m(wchar_t const* s, size_t len);
00139     template <typename WS>
00140     explicit w2m(WS const& ws)
00141     {
00142         // If the init_() call fails to compile with an error message that
00143         // mentions
00144         //  'containing your_wide_type_relies_on_intermediate_shim_conversions_which_are_prohibited_unless_PANTHEIOS_SAFE_ALLOW_SHIM_INTERMEDIATES_is_defined'
00145         // then the (wide) shims for the type WS use intermediate instances
00146         // of conversion classes, and would cause undefined behaviour if
00147         // you were to use it any a non-"inline" log statement. If you are
00148         // sure you are using the inserter correctly, then #define the
00149         // preprocessor symbol PANTHEIOS_SAFE_ALLOW_SHIM_INTERMEDIATES to
00150         // allow it to compile.
00151 
00152 #ifndef PANTHEIOS_SAFE_ALLOW_SHIM_INTERMEDIATES
00153         int unused = 
00154 #endif /* !PANTHEIOS_SAFE_ALLOW_SHIM_INTERMEDIATES */
00155             
00156             init_(::stlsoft::c_str_data_w(ws), ::stlsoft::c_str_len_w(ws));
00157 
00158 #ifndef PANTHEIOS_SAFE_ALLOW_SHIM_INTERMEDIATES
00159         STLSOFT_SUPPRESS_UNUSED(unused);
00160 #endif /* !PANTHEIOS_SAFE_ALLOW_SHIM_INTERMEDIATES */
00161     }
00163     ~w2m() stlsoft_throw_0();
00164 
00165 private:
00166 #if !defined(STLSOFT_COMPILER_IS_GCC)
00167     w2m(class_type const&);
00168 #endif /* compiler */
00169     class_type& operator =(class_type const&);
00170 
00171 public: // Accessors
00173     char const* data() const;
00175     char const* c_str() const;
00177     size_t      length() const;
00178 
00179 private: // Implementation
00180     int  init_(wchar_t const* s, size_t n);
00181 #ifndef PANTHEIOS_SAFE_ALLOW_SHIM_INTERMEDIATES
00182     struct your_wide_type_relies_on_intermediate_shim_conversions_which_are_prohibited_unless_PANTHEIOS_SAFE_ALLOW_SHIM_INTERMEDIATES_is_defined;
00183     template <typename T0, typename T1>
00184     your_wide_type_relies_on_intermediate_shim_conversions_which_are_prohibited_unless_PANTHEIOS_SAFE_ALLOW_SHIM_INTERMEDIATES_is_defined init_(T0 const&, T1 const&);
00185 #endif /* !PANTHEIOS_SAFE_ALLOW_SHIM_INTERMEDIATES */
00186     void construct_() const;
00187     void construct_();
00188     static size_t sentinelLength_()
00189     {
00190         return ~size_t(0);
00191     }
00192 
00193 private: // Member Variables
00194     // We can't use auto_buffer because GCC is a total dullard, "helpfully"
00195     // informing us that it can't generate a copy-constructor when we're
00196     // telling *it* that we're proscribing that very thing.
00197     //
00198     // So, we're just going to have to manage our own memory, and use up
00199     // two more hours that could be more profitably spent on something else
00200     wchar_t const*  m_source;
00201     size_t          m_sourceLen;
00202     char*           m_result;
00203     size_t          m_length;
00204 };
00205 
00206 #endif /* PANTHEIOS_USE_WIDE_STRINGS */
00207 
00208 /* /////////////////////////////////////////////////////////////////////////
00209  * String Access Shims
00210  */
00211 
00212 #ifndef PANTHEIOS_USE_WIDE_STRINGS
00213 
00214 #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
00215 
00216 # if !defined(PANTHEIOS_NO_NAMESPACE)
00217 namespace shims
00218 {
00219 # endif /* !PANTHEIOS_NO_NAMESPACE */
00220 
00221 
00223 inline char const* c_str_data_a(w2m const& r)
00224 {
00225     return r.data();
00226 }
00228 inline pan_char_t const* c_str_data(w2m const& r)
00229 {
00230     return r.data();
00231 }
00232 
00234 inline size_t c_str_len_a(w2m const& r)
00235 {
00236     return r.length();
00237 }
00239 inline size_t c_str_len(w2m const& r)
00240 {
00241     return r.length();
00242 }
00243 
00245 inline char const* c_str_ptr_a(w2m const& r)
00246 {
00247     return r.c_str();
00248 }
00250 inline pan_char_t const* c_str_ptr(w2m const& r)
00251 {
00252     return r.c_str();
00253 }
00254 
00255 # if !defined(PANTHEIOS_NO_NAMESPACE)
00256 } /* namespace shims */
00257 
00258 #  if defined(STLSOFT_COMPILER_IS_GCC)
00259     /* GCC does not seem to correctly handle the phases of
00260      * processing of C++ templates, so we need to 'use' the
00261      * shims into the same namespace as the inserter class
00262      * in order that ADL can suffice instead.
00263      */
00264     using ::pantheios::shims::c_str_data_a;
00265     using ::pantheios::shims::c_str_data;
00266 
00267     using ::pantheios::shims::c_str_len_a;
00268     using ::pantheios::shims::c_str_len;
00269 
00270     using ::pantheios::shims::c_str_ptr_a;
00271     using ::pantheios::shims::c_str_ptr;
00272 #  endif /* compiler */
00273 
00274 # endif /* !PANTHEIOS_NO_NAMESPACE */
00275 
00276 #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
00277 
00278 #endif /* PANTHEIOS_USE_WIDE_STRINGS */
00279 
00280 /* /////////////////////////////////////////////////////////////////////////
00281  * Namespace
00282  */
00283 
00284 #if !defined(PANTHEIOS_NO_NAMESPACE)
00285 } /* namespace pantheios */
00286 
00287 #ifndef PANTHEIOS_USE_WIDE_STRINGS
00288 
00289 namespace stlsoft
00290 {
00291     // 'Export' the string access shims into the STLSoft namespace
00292     //
00293     // c_str_ptr(_a) is not necessary for version 1.0 of Pantheios, but it's
00294     // defined and exported in order to allow for the case where someone
00295     // may find a legitimate use for the conversion classes additional to
00296     // the type-tunneling of the Pantheios API.
00297 
00298     using ::pantheios::shims::c_str_data_a;
00299     using ::pantheios::shims::c_str_data;
00300 
00301     using ::pantheios::shims::c_str_len_a;
00302     using ::pantheios::shims::c_str_len;
00303 
00304     using ::pantheios::shims::c_str_ptr_a;
00305     using ::pantheios::shims::c_str_ptr;
00306 }
00307 
00308 #endif /* PANTHEIOS_USE_WIDE_STRINGS */
00309 
00310 #endif /* !PANTHEIOS_NO_NAMESPACE */
00311 
00312 /* /////////////////////////////////////////////////////////////////////////
00313  * Inclusion
00314  */
00315 
00316 #ifdef STLSOFT_PPF_pragma_once_SUPPORT
00317 # pragma once
00318 #endif /* STLSOFT_PPF_pragma_once_SUPPORT */
00319 
00320 /* ////////////////////////////////////////////////////////////////////// */
00321 
00322 #endif /* !PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_W2M */
00323 
00324 /* ///////////////////////////// end of file //////////////////////////// */

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