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  

atlstl_ccombstr_veneer.h

Go to the documentation of this file.
00001 /* 
00002  * File:        atlstl_ccombstr_veneer.h
00003  *
00004  * Purpose:     Contains the definition of the ccombstr_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 ATLSTL_INCL_H_ATLSTL_CCOMBSTR_VENEER
00046 #define ATLSTL_INCL_H_ATLSTL_CCOMBSTR_VENEER
00047 
00048 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00049 # define ATLSTL_VER_H_ATLSTL_CCOMBSTR_VENEER_MAJOR       2
00050 # define ATLSTL_VER_H_ATLSTL_CCOMBSTR_VENEER_MINOR       0
00051 # define ATLSTL_VER_H_ATLSTL_CCOMBSTR_VENEER_REVISION    1
00052 # define ATLSTL_VER_H_ATLSTL_CCOMBSTR_VENEER_EDIT        20
00053 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00054 
00055 /* 
00056  * Includes
00057  */
00058 
00059 #ifndef ATLSTL_INCL_H_ATLSTL
00060 # include "atlstl.h"    // Include the ATLSTL root header
00061 #endif /* !ATLSTL_INCL_H_ATLSTL */
00062 #ifndef STLSOFT_INCL_H_STLSOFT_ITERATOR
00063 # include "stlsoft_iterator.h"
00064 #endif /* !STLSOFT_INCL_H_STLSOFT_ITERATOR */
00065 #ifndef STLSOFT_INCL_H_STLSOFT_CONSTRAINTS
00066 # include "stlsoft_constraints.h"
00067 #endif /* !STLSOFT_INCL_H_STLSOFT_CONSTRAINTS */
00068 #ifndef STLSOFT_INCL_H_STLSOFT_AUTO_BUFFER
00069 # include "stlsoft_auto_buffer.h"
00070 #endif /* !STLSOFT_INCL_H_STLSOFT_AUTO_BUFFER */
00071 #ifndef STLSOFT_INCL_H_STLSOFT_NEW_ALLOCATOR
00072 # include "stlsoft_new_allocator.h"
00073 #endif /* !STLSOFT_INCL_H_STLSOFT_NEW_ALLOCATOR */
00074 #if defined(__STLSOFT_COMPILER_IS_DMC)
00075 #  include "stlsoft_sap_cast.h"
00076 # ifndef STLSOFT_INCL_H_STLSOFT_SAP_CAST
00077 # endif /* !STLSOFT_INCL_H_STLSOFT_SAP_CAST */
00078 #endif /* compiler */
00079 #include <wchar.h>  // _wcsnset()
00080 
00081 /* 
00082  * Namespace
00083  *
00084  * The ATLSTL components are contained within the atlstl namespace. This is
00085  * actually an alias for stlsoft::atlstl_project,
00086  *
00087  * The definition matrix is as follows:
00088  *
00089  * _STLSOFT_NO_NAMESPACE    _ATLSTL_NO_NAMESPACE    atlstl definition
00090  * ---------------------    --------------------    -----------------
00091  *  not defined              not defined             = stlsoft::atlstl_project
00092  *  not defined              defined                 not defined
00093  *  defined                  not defined             atlstl
00094  *  defined                  defined                 not defined
00095  *
00096  */
00097 
00098 #ifndef _ATLSTL_NO_NAMESPACE
00099 # if defined(_STLSOFT_NO_NAMESPACE) || \
00100      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00101 /* There is no stlsoft namespace, so must define ::atlstl */
00102 namespace atlstl
00103 {
00104 # else
00105 /* Define stlsoft::atlstl_project */
00106 
00107 namespace stlsoft
00108 {
00109 
00110 namespace atlstl_project
00111 {
00112 
00113 # endif /* _STLSOFT_NO_NAMESPACE */
00114 #endif /* !_ATLSTL_NO_NAMESPACE */
00115 
00116 /* 
00117 
00120 
00124 
00129 
00130 /* 
00131  * Classes
00132  */
00133 
00137 class ccombstr_veneer
00138 #ifdef _ATLSTL_CCOMBSTR_VENEER_INHERIT_AS_PUBLIC
00139     : public CComBSTR
00140 #else
00141     : private CComBSTR
00142 #endif /* _ATLSTL_CCOMBSTR_VENEER_INHERIT_AS_PUBLIC */
00143 {
00144 private:
00145     typedef CComBSTR        parent_class_type;
00146     typedef ccombstr_veneer class_type;
00147 public:
00149     typedef OLECHAR         value_type;
00151     typedef LPOLESTR        iterator;
00153     typedef LPCOLESTR       const_iterator;
00155     typedef LPOLESTR        pointer;
00157     typedef LPCOLESTR       const_pointer;
00159     typedef OLECHAR         &reference;
00161     typedef OLECHAR const   &const_reference;
00163     typedef as_size_t       size_type;
00164 
00165 // Construction
00166 public:
00168     ccombstr_veneer();
00170     ccombstr_veneer(class_type const &rhs);
00172     ss_explicit_k ccombstr_veneer(CComBSTR const &rhs);
00174     ss_explicit_k ccombstr_veneer(LPCSTR s);
00176     ss_explicit_k ccombstr_veneer(LPCWSTR s);
00178     ccombstr_veneer(LPCOLESTR from, LPCOLESTR to);
00180     ccombstr_veneer(size_type length, LPCOLESTR from);
00182     ccombstr_veneer(as_size_t cch, OLECHAR ch);
00183 
00185     class_type const &operator =(class_type const &rhs);
00187     class_type const &operator =(CComBSTR const &rhs);
00189     class_type const &operator =(LPCSTR s);
00191     class_type const &operator =(LPCWSTR s);
00192 
00193 // Iteration
00194 public:
00198     const_iterator begin() const;
00202     const_iterator end() const;
00203 
00204 // Access
00205 public:
00206     reference operator [](size_type index);
00207     const_reference operator [](size_type index) const;
00208 
00209 // Operations
00210 public:
00211     void swap(class_type &rhs);
00212 
00213 // Attributes
00214 public:
00216     CComBSTR        &get_base_type();
00218     CComBSTR const  &get_base_type() const;
00220     size_type       length() const;
00222     size_type       size() const;
00224     as_bool_t       empty() const;
00226     const_pointer   c_str() const;
00228     const_pointer   data() const;
00229 };
00230 
00231 /* 
00232  * Operators
00233  */
00234 
00235 #ifndef _ATLSTL_CCOMBSTR_VENEER_INHERIT_AS_PUBLIC
00236 inline as_bool_t operator ==(ccombstr_veneer const &lhs, ccombstr_veneer const &rhs)
00237 {
00238     CComBSTR const  &lhs_ = lhs.get_base_type();
00239     CComBSTR const  &rhs_ = rhs.get_base_type();
00240 
00241 #if (_ATL_VER < 0x0300)
00242     return operator == (ccombstr_veneer(lhs), rhs);
00243 #else /* ? _ATL_VER */
00244     return rhs_ == lhs_;
00245 #endif /* _ATL_VER */
00246 }
00247 inline as_bool_t operator ==(LPCSTR lhs, ccombstr_veneer const &rhs)
00248 {
00249     CComBSTR const  &rhs_ = rhs.get_base_type();
00250 
00251 #if (_ATL_VER < 0x0300)
00252     return operator == (ccombstr_veneer(lhs), rhs);
00253 #else /* ? _ATL_VER */
00254     return rhs_ == lhs;
00255 #endif /* _ATL_VER */
00256 }
00257 inline as_bool_t operator ==(ccombstr_veneer const &lhs, LPCSTR rhs)
00258 {
00259     CComBSTR const  &lhs_ = lhs.get_base_type();
00260 
00261 #if (_ATL_VER < 0x0300)
00262     return operator == (lhs, ccombstr_veneer(rhs));
00263 #else /* ? _ATL_VER */
00264     return lhs_ == rhs;
00265 #endif /* _ATL_VER */
00266 }
00267 inline as_bool_t operator ==(LPCWSTR lhs, ccombstr_veneer const &rhs)
00268 {
00269     CComBSTR const  &rhs_ = rhs.get_base_type();
00270 
00271     return lhs == rhs_;
00272 }
00273 inline as_bool_t operator ==(ccombstr_veneer const &lhs, LPCWSTR rhs)
00274 {
00275     CComBSTR const  &lhs_   =   lhs.get_base_type();
00276 #if _ATL_VER >= 0x0700
00277 
00278     return lhs_ == rhs;
00279 #else /* ? _ATL_VER */
00280 
00281     if(NULL == lhs_.m_str)
00282     {
00283         return (NULL == rhs);
00284     }
00285     else if(NULL == rhs)
00286     {
00287         return (NULL == lhs_.m_str);
00288     }
00289     else
00290     {
00291         return 0 == wcscmp(lhs_.m_str, rhs);
00292     }
00293 #endif /* _ATL_VER */
00294 }
00295 
00296 inline as_bool_t operator !=(ccombstr_veneer const &lhs, ccombstr_veneer const &rhs)
00297 {
00298     return !operator ==(lhs, rhs);
00299 }
00300 inline as_bool_t operator !=(LPCSTR lhs, ccombstr_veneer const &rhs)
00301 {
00302     return !operator ==(lhs, rhs);
00303 }
00304 inline as_bool_t operator !=(ccombstr_veneer const &lhs, LPCSTR rhs)
00305 {
00306     return !operator ==(lhs, rhs);
00307 }
00308 inline as_bool_t operator !=(LPCWSTR lhs, ccombstr_veneer const &rhs)
00309 {
00310     return !operator ==(lhs, rhs);
00311 }
00312 inline as_bool_t operator !=(ccombstr_veneer const &lhs, LPCWSTR rhs)
00313 {
00314     return !operator ==(lhs, rhs);
00315 }
00316 #endif /* !_ATLSTL_CCOMBSTR_VENEER_INHERIT_AS_PUBLIC */
00317 
00318 /* 
00319  * Shims
00320  */
00321 
00322 inline LPCOLESTR c_str_ptr_null(ccombstr_veneer const &str)
00323 {
00324     return str.empty() ? NULL : str.c_str();
00325 }
00326 
00327 inline LPCOLESTR c_str_ptr(ccombstr_veneer const &str)
00328 {
00329     return str.c_str();
00330 }
00331 
00332 inline as_size_t c_str_len(ccombstr_veneer const &str)
00333 {
00334     return str.length();
00335 }
00336 
00337 inline as_size_t c_str_size(ccombstr_veneer const &str)
00338 {
00339     return c_str_len(str) * sizeof(OLECHAR);
00340 }
00341 
00342 template<ss_typename_param_k S>
00343 inline S &operator <<(S &s, ccombstr_veneer const &str)
00344 {
00345     s << str.c_str();
00346 
00347     return s;
00348 }
00349 
00350 /* 
00351  * Unit-testing
00352  */
00353 
00354 #ifdef STLSOFT_UNITTEST
00355 
00356 namespace unittest
00357 {
00358     ss_bool_t test_atlstl_ccombstr_veneer(unittest_reporter *r)
00359     {
00360         using stlsoft::unittest::unittest_initialiser;
00361 
00362         ss_bool_t               bSuccess    =   true;
00363 
00364         unittest_initialiser    init(r, "ATLSTL", "ccombstr_veneer", __FILE__);
00365 
00366         ccombstr_veneer         bs1( "Hello");
00367         ccombstr_veneer         bs2(L"Hello");
00368 
00369         if(bs1 != L"Hello")
00370         {
00371             r->report("Construction (from ANSI) failed ", __LINE__);
00372             bSuccess = false;
00373         }
00374 
00375         if(bs2 != L"Hello")
00376         {
00377 //          wprintf(L"bs2: %u, %s\n", bs2.length(), bs2);
00378 
00379             r->report("Construction (from Unicode) failed ", __LINE__);
00380             bSuccess = false;
00381         }
00382 
00383 //      wprintf(L"bs1: %u, %s\nbs2: %u, %s\n", bs1.length(), bs1, bs2.length(), bs2);
00384 
00385         if(bs1 != bs2)
00386         {
00387 //          wprintf(L"bs1: %u, %s\nbs2: %u, %s\n", bs1.length(), bs1, bs2.length(), bs2);
00388 
00389             r->report("Copy construction failed ", __LINE__);
00390             bSuccess = false;
00391         }
00392 
00393         ccombstr_veneer         bs3(bs1);
00394 
00395         if(bs1 != bs3)
00396         {
00397             r->report("Copy assignment failed ", __LINE__);
00398             bSuccess = false;
00399         }
00400 
00401         bs3 = bs1;
00402 
00403         if(bs3 != "Hello")
00404         {
00405             r->report("Comparison to LPCSTR failed ", __LINE__);
00406             bSuccess = false;
00407         }
00408 
00409         if(bs1 != L"Hello")
00410         {
00411             r->report("Comparison to LPCWSTR failed ", __LINE__);
00412             bSuccess = false;
00413         }
00414 
00415         bs3 = "Hello";
00416 
00417         if(bs3 != L"Hello")
00418         {
00419             r->report("Assigment to LPCSTR failed ", __LINE__);
00420             bSuccess = false;
00421         }
00422 
00423         return bSuccess;
00424     }
00425 
00426     unittest_registrar    unittest_atlstl_ccombstr_veneer(test_atlstl_ccombstr_veneer);
00427 
00428 } // namespace unittest
00429 
00430 #endif /* STLSOFT_UNITTEST */
00431 
00432 /* 
00433  * Implementation
00434  */
00435 
00436 inline ccombstr_veneer::ccombstr_veneer()
00437     : parent_class_type()
00438 {
00439     stlsoft_constraint_must_be_same_size(CComBSTR, class_type);
00440 }
00441 
00442 inline ccombstr_veneer::ccombstr_veneer(class_type const &rhs)
00443     : parent_class_type(rhs)
00444 {
00445     stlsoft_constraint_must_be_same_size(CComBSTR, class_type);
00446 }
00447 
00448 inline ccombstr_veneer::ccombstr_veneer(CComBSTR const &rhs)
00449     : parent_class_type(rhs)
00450 {
00451     stlsoft_constraint_must_be_same_size(CComBSTR, class_type);
00452 }
00453 
00454 inline ccombstr_veneer::ccombstr_veneer(LPCSTR s)
00455 /* There's a bug in some implementations of ATL 3.x, so we go for the assigment,
00456  * doing the conversion ourselves
00457  */
00458 #if _ATL_VER >= 0x0700
00459     : parent_class_type(s)
00460 #endif /* _ATL_VER */
00461 {
00462 #if _ATL_VER < 0x0700
00463     USES_CONVERSION;
00464     *this = A2COLE(s);
00465 #endif /* _ATL_VER */
00466 
00467     atlstl_assert(s == NULL || length() == strlen(s));
00468 
00469     stlsoft_constraint_must_be_same_size(CComBSTR, class_type);
00470 }
00471 
00472 inline ccombstr_veneer::ccombstr_veneer(LPCWSTR s)
00473 #if _ATL_VER >= 0x0300
00474     : parent_class_type(s)
00475 #endif /* _ATL_VER */
00476 {
00477 #if _ATL_VER < 0x0300
00478     USES_CONVERSION;
00479     *this = s;
00480 #endif /* _ATL_VER */
00481 
00482     atlstl_assert(s == NULL || length() == wcslen(s));
00483 
00484     stlsoft_constraint_must_be_same_size(CComBSTR, class_type);
00485 }
00486 
00487 inline ccombstr_veneer::ccombstr_veneer(LPCOLESTR from, LPCOLESTR to)
00488     : parent_class_type(static_cast<int>(to - from), from)
00489 {
00490     stlsoft_constraint_must_be_same_size(CComBSTR, class_type);
00491 }
00492 
00493 inline void ccombstr_veneer::swap(ccombstr_veneer::class_type &rhs)
00494 {
00495     BSTR    str         =   rhs.m_str;
00496             rhs.m_str   =   m_str;
00497             m_str       =   str;
00498 }
00499 
00500 inline ccombstr_veneer::ccombstr_veneer(ccombstr_veneer::size_type length, LPCOLESTR from)
00501     : parent_class_type(static_cast<int>(length), from)
00502 {
00503     stlsoft_constraint_must_be_same_size(CComBSTR, class_type);
00504 }
00505 
00506 inline ccombstr_veneer::ccombstr_veneer(as_size_t cch, OLECHAR ch)
00507 {
00508     stlsoft_constraint_must_be_same_size(CComBSTR, class_type);
00509 
00510     typedef stlsoft_ns_qual(auto_buffer)<OLECHAR, stlsoft_ns_qual(new_allocator)<OLECHAR> > buffer_t;
00511 
00512     buffer_t    buffer(cch);
00513 
00514     _wcsnset(buffer, ch, cch);
00515 
00516     class_type(cch, buffer).swap(*this);
00517 }
00518 
00519 inline ccombstr_veneer::class_type const &ccombstr_veneer::operator =(ccombstr_veneer::class_type const &rhs)
00520 {
00521     parent_class_type::operator =(rhs);
00522 
00523     return *this;
00524 }
00525 
00526 inline ccombstr_veneer::class_type const &ccombstr_veneer::operator =(CComBSTR const &rhs)
00527 {
00528     parent_class_type::operator =(rhs);
00529 
00530     return *this;
00531 }
00532 
00533 inline ccombstr_veneer::class_type const &ccombstr_veneer::operator =(LPCSTR s)
00534 {
00535 /* There's a bug in some implementations of ATL 3.x, so we go for the assigment,
00536  * doing the conversion ourselves
00537  */
00538 #if _ATL_VER >= 0x0700
00539     parent_class_type::operator =(s);
00540 #else /* ? _ATL_VER */
00541     USES_CONVERSION;
00542     *this = A2COLE(s);
00543 #endif /* _ATL_VER */
00544 
00545     return *this;
00546 }
00547 
00548 inline ccombstr_veneer::class_type const &ccombstr_veneer::operator =(LPCWSTR s)
00549 {
00550     parent_class_type::operator =(s);
00551 
00552     return *this;
00553 }
00554 
00555 inline ccombstr_veneer::const_iterator ccombstr_veneer::begin() const
00556 {
00557     return *this;
00558 }
00559 
00560 inline ccombstr_veneer::const_iterator ccombstr_veneer::end() const
00561 {
00562     return begin() + Length();
00563 }
00564 
00565 inline ccombstr_veneer::reference ccombstr_veneer::operator [](ccombstr_veneer::size_type index)
00566 {
00567     atlstl_message_assert("Index out of range", index < length());
00568 
00569     return const_cast<reference>(data()[index]);
00570 }
00571 
00572 inline ccombstr_veneer::const_reference ccombstr_veneer::operator [](ccombstr_veneer::size_type index) const
00573 {
00574     atlstl_message_assert("Index out of range", index < length());
00575 
00576     return data()[index];
00577 }
00578 
00579 inline CComBSTR &ccombstr_veneer::get_base_type()
00580 {
00581 #if defined(__STLSOFT_COMPILER_IS_DMC)
00582     CComBSTR    *this_  =   stlsoft_ns_qual(sap_cast)<CComBSTR*>(this);
00583 #else /* ? compiler */
00584     CComBSTR    *this_  =   this;
00585 #endif /* compiler */
00586 
00587     return *this_;
00588 }
00589 
00590 inline CComBSTR const &ccombstr_veneer::get_base_type() const
00591 {
00592 #if defined(__STLSOFT_COMPILER_IS_DMC)
00593     CComBSTR const  *this_  =   stlsoft_ns_qual(sap_cast)<CComBSTR const*>(this);
00594 #else /* ? compiler */
00595     CComBSTR const  *this_  =   this;
00596 #endif /* compiler */
00597 
00598     return *this_;
00599 }
00600 
00601 inline ccombstr_veneer::size_type ccombstr_veneer::length() const
00602 {
00603     return Length();
00604 }
00605 
00606 inline ccombstr_veneer::size_type ccombstr_veneer::size() const
00607 {
00608     return length();
00609 }
00610 
00611 inline as_bool_t ccombstr_veneer::empty() const
00612 {
00613     return length() == 0;
00614 }
00615 
00616 inline ccombstr_veneer::const_pointer ccombstr_veneer::c_str() const
00617 {
00618     return *this;
00619 }
00620 
00621 inline ccombstr_veneer::const_pointer ccombstr_veneer::data() const
00622 {
00623     return *this;
00624 }
00625 
00626 /* 
00627 
00629 
00630 /* 
00631 
00632 #ifndef _ATLSTL_NO_NAMESPACE
00633 # if defined(_STLSOFT_NO_NAMESPACE) || \
00634      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00635 } // namespace atlstl
00636 # else
00637 } // namespace atlstl_project
00638 } // namespace stlsoft
00639 # endif /* _STLSOFT_NO_NAMESPACE */
00640 #endif /* !_ATLSTL_NO_NAMESPACE */
00641 
00642 /* 
00643 
00644 #endif /* !ATLSTL_INCL_H_ATLSTL_CCOMBSTR_VENEER */
00645 
00646 /* 

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