STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ... ATLSTL - Template Software for the Active Template Library COMSTL - The Standard Template Library meets the Component Object Model .netSTL - Standard Template Library meets the Microsoft.NET Common Language Runtime InetSTL - The Standard Template Library meets WinInet MFCSTL - Template Software for the Microsoft Foundation Classes UNIXSTL - Template Software for the UNIX Operating System WinSTL - where the Standard Template Library meets the Win32 API

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

mfcstl_cstring_veneer.h

Go to the documentation of this file.
00001 /* 
00002  * File:        mfcstl_cstring_veneer.h
00003  *
00004  * Purpose:     Contains the definition of the cstring_veneer template.
00005  *
00006  * Created:     1st October 2002
00007  * Updated:     11th September 2004
00008  *
00009  * Home:        http://stlsoft.org/
00010  *
00011  * Copyright (c) 2002-2004, 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 met:
00016  *
00017  * - Redistributions of source code must retain the above copyright notice, this
00018  *   list of conditions and the following disclaimer.
00019  * - Redistributions in binary form must reproduce the above copyright notice,
00020  *   this list of conditions and the following disclaimer in the documentation
00021  *   and/or other materials provided with the distribution.
00022  * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
00023  *   any contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036  * POSSIBILITY OF SUCH DAMAGE.
00037  *
00038  * 
00039 
00040 
00044 
00045 #ifndef MFCSTL_INCL_H_MFCSTL_CSTRING_VENEER
00046 #define MFCSTL_INCL_H_MFCSTL_CSTRING_VENEER
00047 
00048 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00049 # define MFCSTL_VER_H_MFCSTL_CSTRING_VENEER_MAJOR       2
00050 # define MFCSTL_VER_H_MFCSTL_CSTRING_VENEER_MINOR       0
00051 # define MFCSTL_VER_H_MFCSTL_CSTRING_VENEER_REVISION    1
00052 # define MFCSTL_VER_H_MFCSTL_CSTRING_VENEER_EDIT        35
00053 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00054 
00055 /* 
00056  * Compatibility
00057  */
00058 
00059 /*
00060 [Incompatibilies-start]
00061 __STLSOFT_COMPILER_IS_MSVC: _MSC_VER<1200
00062 [Incompatibilies-end]
00063  */
00064 
00065 /* 
00066  * Includes
00067  */
00068 
00069 #ifndef MFCSTL_INCL_H_MFCSTL
00070 # include "mfcstl.h"    // Include the MFCSTL root header
00071 #endif /* !MFCSTL_INCL_H_MFCSTL */
00072 
00073 #if defined(__STLSOFT_COMPILER_IS_MSVC) && \
00074     _MSC_VER < 1200
00075 # error mfcstl_cstring_veneer.h is not compatible with Visual C++ 5.0 or earlier
00076 #endif /* _MSC_VER < 1200 */
00077 
00078 #ifndef STLSOFT_INCL_H_STLSOFT_ITERATOR
00079 # include "stlsoft_iterator.h"
00080 #endif /* !STLSOFT_INCL_H_STLSOFT_ITERATOR */
00081 #ifndef STLSOFT_INCL_H_STLSOFT_CONSTRAINTS
00082 # include "stlsoft_constraints.h"
00083 #endif /* !STLSOFT_INCL_H_STLSOFT_CONSTRAINTS */
00084 #if defined(__STLSOFT_COMPILER_IS_DMC)
00085 #  include "stlsoft_sap_cast.h"
00086 # ifndef STLSOFT_INCL_H_STLSOFT_SAP_CAST
00087 # endif /* !STLSOFT_INCL_H_STLSOFT_SAP_CAST */
00088 #endif /* compiler */
00089 
00090 /* 
00091  * Namespace
00092  *
00093  * The MFCSTL components are contained within the mfcstl namespace. This is
00094  * actually an alias for stlsoft::mfcstl_project,
00095  *
00096  * The definition matrix is as follows:
00097  *
00098  * _STLSOFT_NO_NAMESPACE    _MFCSTL_NO_NAMESPACE    mfcstl definition
00099  * ---------------------    --------------------    -----------------
00100  *  not defined              not defined             = stlsoft::mfcstl_project
00101  *  not defined              defined                 not defined
00102  *  defined                  not defined             mfcstl
00103  *  defined                  defined                 not defined
00104  *
00105  */
00106 
00107 #ifndef _MFCSTL_NO_NAMESPACE
00108 # if defined(_STLSOFT_NO_NAMESPACE) || \
00109      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00110 /* There is no stlsoft namespace, so must define ::mfcstl */
00111 namespace mfcstl
00112 {
00113 # else
00114 /* Define stlsoft::mfcstl_project */
00115 
00116 namespace stlsoft
00117 {
00118 
00119 namespace mfcstl_project
00120 {
00121 
00122 # endif /* _STLSOFT_NO_NAMESPACE */
00123 #endif /* !_MFCSTL_NO_NAMESPACE */
00124 
00125 /* 
00126 
00129 
00133 
00138 
00139 /* 
00140  * Classes
00141  */
00142 
00146 class cstring_veneer
00147 #ifdef _MFCSTL_CSTRING_VENEER_INHERIT_AS_PUBLIC
00148     : public CString
00149 #else
00150     : private CString
00151 #endif /* _MFCSTL_CSTRING_VENEER_INHERIT_AS_PUBLIC */
00152 {
00153 private:
00154     typedef CString         parent_class_type;
00155     typedef cstring_veneer  class_type;
00156 public:
00158     typedef TCHAR       value_type;
00160     typedef LPTSTR      iterator;
00162     typedef LPCTSTR     const_iterator;
00164     typedef LPTSTR      pointer;
00166     typedef LPCTSTR     const_pointer;
00168     typedef TCHAR       &reference;
00170     typedef TCHAR const &const_reference;
00172     typedef ms_size_t   size_type;
00173 
00174 // Construction
00175 public:
00177     cstring_veneer();
00179     cstring_veneer(class_type const &rhs);
00181     ss_explicit_k cstring_veneer(CString const &rhs);
00183     ss_explicit_k cstring_veneer(LPCSTR s);
00185     ss_explicit_k cstring_veneer(LPCWSTR s);
00187     ss_explicit_k cstring_veneer(unsigned char const *s);
00189     cstring_veneer(LPCTSTR from, LPCTSTR to);
00191     cstring_veneer(LPCTSTR from, size_type length);
00193     cstring_veneer(ms_size_t cch, TCHAR ch);
00194 
00196     class_type const &operator =(class_type const &rhs);
00198     class_type const &operator =(CString const &rhs);
00200     class_type const &operator =(LPCSTR s);
00202     class_type const &operator =(LPCWSTR s);
00204     class_type const &operator =(unsigned char const *s);
00205 
00206 // Iteration
00207 public:
00211     const_iterator begin() const;
00215     const_iterator end() const;
00216 
00217 // Access
00218 public:
00219     reference operator [](size_type index);
00220     const_reference operator [](size_type index) const;
00221 
00222 // Attributes
00223 public:
00225     CString         &get_base_type();
00227     CString const   &get_base_type() const;
00229     size_type       length() const;
00231     size_type       size() const;
00233     ms_bool_t       empty() const;
00235     const_pointer   c_str() const;
00237     const_pointer   data() const;
00238 };
00239 
00240 /* 
00241  * Operators
00242  */
00243 
00244 #ifndef _MFCSTL_CSTRING_VENEER_INHERIT_AS_PUBLIC
00245 inline ms_bool_t operator ==(cstring_veneer const &lhs, cstring_veneer const &rhs)
00246 {
00247     CString const  &lhs_ = lhs.get_base_type();
00248     CString const  &rhs_ = rhs.get_base_type();
00249 
00250     return lhs_ == rhs_;
00251 }
00252 inline ms_bool_t operator ==(LPCSTR lhs, cstring_veneer const &rhs)
00253 {
00254     CString const  &rhs_ = rhs.get_base_type();
00255 
00256     return rhs_ == lhs;
00257 }
00258 inline ms_bool_t operator ==(cstring_veneer const &lhs, LPCSTR rhs)
00259 {
00260     CString const  &lhs_ = lhs.get_base_type();
00261 
00262     return lhs_ == rhs;
00263 }
00264 inline ms_bool_t operator ==(LPCWSTR lhs, cstring_veneer const &rhs)
00265 {
00266     CString const  &rhs_ = rhs.get_base_type();
00267 
00268 #if defined(__STLSOFT_COMPILER_IS_BORLAND) || \
00269     (   defined(__STLSOFT_COMPILER_IS_DMC) && \
00270         __DMC__ < 0x0835)
00271     return CString(lhs) == rhs_;
00272 #else /* ? compiler */
00273     return lhs == rhs_;
00274 #endif /* compiler */
00275 }
00276 inline ms_bool_t operator ==(cstring_veneer const &lhs, LPCWSTR rhs)
00277 {
00278     CString const  &lhs_ = lhs.get_base_type();
00279 
00280 #if defined(__STLSOFT_COMPILER_IS_BORLAND) || \
00281     (   defined(__STLSOFT_COMPILER_IS_DMC) && \
00282         __DMC__ < 0x0835)
00283     return lhs_ == CString(rhs);
00284 #else /* ? compiler */
00285     return lhs_ == rhs;
00286 #endif /* compiler */
00287 }
00288 inline ms_bool_t operator !=(cstring_veneer const &lhs, cstring_veneer const &rhs)
00289 {
00290     return !operator ==(lhs, rhs);
00291 }
00292 inline ms_bool_t operator !=(LPCSTR lhs, cstring_veneer const &rhs)
00293 {
00294     return !operator ==(lhs, rhs);
00295 }
00296 inline ms_bool_t operator !=(cstring_veneer const &lhs, LPCSTR rhs)
00297 {
00298     return !operator ==(lhs, rhs);
00299 }
00300 inline ms_bool_t operator !=(LPCWSTR lhs, cstring_veneer const &rhs)
00301 {
00302     return !operator ==(lhs, rhs);
00303 }
00304 inline ms_bool_t operator !=(cstring_veneer const &lhs, LPCWSTR rhs)
00305 {
00306     return !operator ==(lhs, rhs);
00307 }
00308 #endif /* !_MFCSTL_CSTRING_VENEER_INHERIT_AS_PUBLIC */
00309 
00310 /* 
00311  * Shims
00312  */
00313 
00314 inline LPCTSTR c_str_ptr_null(cstring_veneer const &s)
00315 {
00316     return s.empty() ? NULL : s.c_str();
00317 }
00318 
00319 inline LPCTSTR c_str_ptr(cstring_veneer const &s)
00320 {
00321     return s.c_str();
00322 }
00323 
00324 inline ms_size_t c_str_len(cstring_veneer const &s)
00325 {
00326     return s.length();
00327 }
00328 
00329 inline ms_size_t c_str_size(cstring_veneer const &s)
00330 {
00331     return c_str_len(s) * sizeof(TCHAR);
00332 }
00333 
00334 template<ss_typename_param_k S>
00335 inline S &operator <<(S &s, cstring_veneer const &str)
00336 {
00337     s << str.c_str();
00338 
00339     return s;
00340 }
00341 
00342 /* 
00343  * Unit-testing
00344  */
00345 
00346 #ifdef STLSOFT_UNITTEST
00347 
00348 namespace unittest
00349 {
00350     ss_bool_t test_mfcstl_cstring_veneer(unittest_reporter *r)
00351     {
00352         using stlsoft::unittest::unittest_initialiser;
00353 
00354         ss_bool_t               bSuccess    =   true;
00355 
00356         unittest_initialiser    init(r, "MFCSTL", "cstring_veneer", __FILE__);
00357 
00358         cstring_veneer  cs1("Hello");
00359         cstring_veneer  cs2(cs1);
00360 
00361         if(cs1 != cs2)
00362         {
00363             r->report("Copy construction failed ", __LINE__);
00364             bSuccess = false;
00365         }
00366 
00367         cstring_veneer  cs3;
00368 
00369         cs3 = cs1;
00370 
00371         if(cs1 != cs3)
00372         {
00373             r->report("Copy assignment failed ", __LINE__);
00374             bSuccess = false;
00375         }
00376 
00377         if(cs1 != "Hello")
00378         {
00379             r->report("Comparison to LPCSTR failed ", __LINE__);
00380             bSuccess = false;
00381         }
00382 
00383         if(cs1 != L"Hello")
00384         {
00385             r->report("Comparison to LPCWSTR failed ", __LINE__);
00386             bSuccess = false;
00387         }
00388 
00389         return bSuccess;
00390     }
00391 
00392     unittest_registrar    unittest_mfcstl_cstring_veneer(test_mfcstl_cstring_veneer);
00393 
00394 } // namespace unittest
00395 
00396 #endif /* STLSOFT_UNITTEST */
00397 
00398 /* 
00399  * Implementation
00400  */
00401 
00402 inline cstring_veneer::cstring_veneer()
00403     : parent_class_type()
00404 {
00405     stlsoft_constraint_must_be_same_size(CString, class_type);
00406 }
00407 
00408 inline cstring_veneer::cstring_veneer(class_type const &rhs)
00409     : parent_class_type(rhs.get_base_type())
00410 {
00411     stlsoft_constraint_must_be_same_size(CString, class_type);
00412 }
00413 
00414 inline cstring_veneer::cstring_veneer(CString const &rhs)
00415     : parent_class_type(rhs)
00416 {
00417     stlsoft_constraint_must_be_same_size(CString, class_type);
00418 }
00419 
00420 inline cstring_veneer::cstring_veneer(LPCSTR s)
00421     : parent_class_type(s)
00422 {
00423     stlsoft_constraint_must_be_same_size(CString, class_type);
00424 }
00425 
00426 inline cstring_veneer::cstring_veneer(LPCWSTR s)
00427     : parent_class_type(s)
00428 {
00429     stlsoft_constraint_must_be_same_size(CString, class_type);
00430 }
00431 
00432 inline cstring_veneer::cstring_veneer(unsigned char const *s)
00433     : parent_class_type(s)
00434 {
00435     stlsoft_constraint_must_be_same_size(CString, class_type);
00436 }
00437 
00438 inline cstring_veneer::cstring_veneer(LPCTSTR from, LPCTSTR to)
00439     : parent_class_type(from, static_cast<int>(to - from))
00440 {
00441     stlsoft_constraint_must_be_same_size(CString, class_type);
00442 }
00443 
00444 inline cstring_veneer::cstring_veneer(LPCTSTR from, cstring_veneer::size_type length)
00445     : parent_class_type(from, static_cast<int>(length))
00446 {
00447     stlsoft_constraint_must_be_same_size(CString, class_type);
00448 }
00449 
00450 inline cstring_veneer::cstring_veneer(ms_size_t cch, TCHAR ch)
00451     : parent_class_type(ch, static_cast<int>(cch))
00452 {
00453     stlsoft_constraint_must_be_same_size(CString, class_type);
00454 }
00455 
00456 inline cstring_veneer::class_type const &cstring_veneer::operator =(cstring_veneer::class_type const &rhs)
00457 {
00458     parent_class_type::operator =(rhs.get_base_type());
00459 
00460     return *this;
00461 }
00462 
00463 inline cstring_veneer::class_type const &cstring_veneer::operator =(CString const &rhs)
00464 {
00465     parent_class_type::operator =(rhs);
00466 
00467     return *this;
00468 }
00469 
00470 inline cstring_veneer::class_type const &cstring_veneer::operator =(LPCSTR s)
00471 {
00472     parent_class_type::operator =(s);
00473 
00474     return *this;
00475 }
00476 
00477 inline cstring_veneer::class_type const &cstring_veneer::operator =(LPCWSTR s)
00478 {
00479     parent_class_type::operator =(s);
00480 
00481     return *this;
00482 }
00483 
00484 inline cstring_veneer::class_type const &cstring_veneer::operator =(unsigned char const *s)
00485 {
00486     parent_class_type::operator =(s);
00487 
00488     return *this;
00489 }
00490 
00491 inline cstring_veneer::const_iterator cstring_veneer::begin() const
00492 {
00493     return *this;
00494 }
00495 
00496 inline cstring_veneer::const_iterator cstring_veneer::end() const
00497 {
00498     return begin() + GetLength();
00499 }
00500 
00501 inline cstring_veneer::reference cstring_veneer::operator [](cstring_veneer::size_type index)
00502 {
00503     mfcstl_message_assert("Index out of range", index < length());
00504 
00505     return const_cast<reference>(data()[index]);
00506 }
00507 
00508 inline cstring_veneer::const_reference cstring_veneer::operator [](cstring_veneer::size_type index) const
00509 {
00510     mfcstl_message_assert("Index out of range", index < length());
00511 
00512     return data()[index];
00513 }
00514 
00515 inline CString &cstring_veneer::get_base_type()
00516 {
00517 #if defined(__STLSOFT_COMPILER_IS_DMC)
00518     CString *this_  =   stlsoft_ns_qual(sap_cast)<CString*>(this);
00519 #else /* ? compiler */
00520     CString *this_  =   this;
00521 #endif /* compiler */
00522 
00523     return *this_;
00524 }
00525 
00526 inline CString const &cstring_veneer::get_base_type() const
00527 {
00528 #if defined(__STLSOFT_COMPILER_IS_DMC)
00529     CString const   *this_  =   stlsoft_ns_qual(sap_cast)<CString const*>(this);
00530 #else /* ? compiler */
00531     CString const   *this_  =   this;
00532 #endif /* compiler */
00533 
00534     return *this_;
00535 }
00536 
00537 inline cstring_veneer::size_type cstring_veneer::length() const
00538 {
00539     return GetLength();
00540 }
00541 
00542 inline cstring_veneer::size_type cstring_veneer::size() const
00543 {
00544     return length();
00545 }
00546 
00547 inline ms_bool_t cstring_veneer::empty() const
00548 {
00549     return length() == 0;
00550 }
00551 
00552 inline cstring_veneer::const_pointer cstring_veneer::c_str() const
00553 {
00554     return *this;
00555 }
00556 
00557 inline cstring_veneer::const_pointer cstring_veneer::data() const
00558 {
00559     return *this;
00560 }
00561 
00562 /* 
00563 
00565 
00566 /* 
00567 
00568 #ifndef _MFCSTL_NO_NAMESPACE
00569 # if defined(_STLSOFT_NO_NAMESPACE) || \
00570      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00571 } // namespace mfcstl
00572 # else
00573 } // namespace mfcstl_project
00574 } // namespace stlsoft
00575 # endif /* _STLSOFT_NO_NAMESPACE */
00576 #endif /* !_MFCSTL_NO_NAMESPACE */
00577 
00578 /* 
00579 
00580 #endif /* !MFCSTL_INCL_H_MFCSTL_CSTRING_VENEER */
00581 
00582 /* 

STLSoft Libraries documentation © Synesis Software Pty Ltd, 2001-2004