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