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  

comstl_interface_ptr.h

Go to the documentation of this file.
00001 /* 
00002  * File:        comstl_interface_ptr.h (formerly MLRelItf.h; ::SynesisCom)
00003  *
00004  * Purpose:     Interface management helper classes.
00005  *
00006  * Created:     2nd November 1994
00007  * Updated:     11th September 2004
00008  *
00009  * Home:        http://stlsoft.org/
00010  *
00011  * Copyright (c) 1994-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 COMSTL_INCL_H_COMSTL_INTERFACE_PTR
00046 #define COMSTL_INCL_H_COMSTL_INTERFACE_PTR
00047 
00048 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00049 # define COMSTL_VER_H_COMSTL_INTERFACE_PTR_MAJOR        4
00050 # define COMSTL_VER_H_COMSTL_INTERFACE_PTR_MINOR        0
00051 # define COMSTL_VER_H_COMSTL_INTERFACE_PTR_REVISION     1
00052 # define COMSTL_VER_H_COMSTL_INTERFACE_PTR_EDIT         453
00053 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00054 
00055 /* 
00056  * Includes
00057  */
00058 
00059 #ifndef COMSTL_INCL_H_COMSTL
00060 # include "comstl.h"                    // Include the COMSTL root header
00061 #endif /* !COMSTL_INCL_H_COMSTL */
00062 #ifndef COMSTL_INCL_H_COMSTL_REFCOUNT_FUNCTIONS
00063 # include "comstl_refcount_functions.h" // addref, release, etc.
00064 #endif /* !COMSTL_INCL_H_COMSTL_REFCOUNT_FUNCTIONS */
00065 #ifndef STLSOFT_INCL_H_STLSOFT_OPERATOR_BOOL
00066 # include "stlsoft_operator_bool.h"     // operator_bool_generator
00067 #endif /* !STLSOFT_INCL_H_STLSOFT_OPERATOR_BOOL */
00068 #include <algorithm>
00069 
00070 /* 
00071  * Namespace
00072  *
00073  * The COMSTL components are contained within the comstl namespace. This is
00074  * actually an alias for stlsoft::comstl_project,
00075  *
00076  * The definition matrix is as follows:
00077  *
00078  * _STLSOFT_NO_NAMESPACE    _COMSTL_NO_NAMESPACE    comstl definition
00079  * ---------------------    --------------------    -----------------
00080  *  not defined              not defined             = stlsoft::comstl_project
00081  *  not defined              defined                 not defined
00082  *  defined                  not defined             comstl
00083  *  defined                  defined                 not defined
00084  *
00085  */
00086 
00087 #ifndef _COMSTL_NO_NAMESPACE
00088 # if defined(_STLSOFT_NO_NAMESPACE) || \
00089      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00090 /* There is no stlsoft namespace, so must define ::comstl */
00091 namespace comstl
00092 {
00093 # else
00094 /* Define stlsoft::comstl_project */
00095 
00096 namespace stlsoft
00097 {
00098 
00099 namespace comstl_project
00100 {
00101 
00102 # endif /* _STLSOFT_NO_NAMESPACE */
00103 #endif /* !_COMSTL_NO_NAMESPACE */
00104 
00105 /* 
00106 
00111 
00112 /* 
00113  * Classes
00114  */
00115 
00117 template <ss_typename_param_k T>
00118 class interface_ptr
00119 {
00122 public:
00123     typedef interface_ptr<T>        class_type;
00124 
00125     typedef T                       value_type;
00126     typedef value_type              *pointer;
00127     typedef value_type const        *const_pointer;
00128     typedef value_type              &reference;
00129     typedef value_type const        &const_reference;
00131 
00134 public:
00135      interface_ptr();
00136      interface_ptr(pointer p, cs_bool_t bAddRef);
00137      interface_ptr(reference r, cs_bool_t bAddRef);
00138      interface_ptr(class_type const &rhs);
00139     ~interface_ptr() comstl_throw_0();
00140 
00141     class_type &operator =(class_type const &rhs);
00143 
00146 public:
00147     void    set(pointer p, cs_bool_t bAddRef);
00148     void    set(reference r, cs_bool_t bAddRef);
00149     void    release();
00150     void    release(cs_bool_t bDecRef /* = true */);
00151     pointer detach();
00152     void    swap(class_type &rhs);
00154 
00157 public:
00158     cs_bool_t operator ==(class_type const &rhs) const;
00159     cs_bool_t operator !=(class_type const &rhs) const;
00161 
00164 private:
00165     STLSOFT_DEFINE_OPERATOR_BOOL_TYPES_T(class_type, operator_bool_generator_type, operator_bool_type);
00166 public:
00167     operator operator_bool_type() const;
00168 
00169 //    cs_bool_t operator !() const;
00171 
00174 public:
00175     const_pointer   operator ->() const;
00176     pointer         operator ->();
00177     const_reference operator *() const;
00178     reference       operator *();
00179 
00180     const_pointer   get_interface_ptr() const;
00181     pointer         get_interface_ptr();
00183 
00184 private:
00185     pointer m_p;
00186 };
00187 
00189 // Shims
00190 
00191 template <ss_typename_param_k T>
00192 inline cs_bool_t is_empty(interface_ptr<T> const &p)
00193 {
00194     return NULL == p.get_interface_ptr();
00195 }
00196 
00197 template <ss_typename_param_k T>
00198 inline T const *get_ptr(interface_ptr<T> const &p)
00199 {
00200     return p.get_interface_ptr();
00201 }
00202 
00203 template <ss_typename_param_k T>
00204 inline T *get_ptr(interface_ptr<T> &p)
00205 {
00206     return p.get_interface_ptr();
00207 }
00208 
00209 /* 
00210  * Unit-testing
00211  */
00212 
00213 #ifdef STLSOFT_UNITTEST
00214 
00215 namespace unittest
00216 {
00217     ss_bool_t test_comstl_interface_ptr(unittest_reporter *r)
00218     {
00219         using stlsoft::unittest::unittest_initialiser;
00220 
00221         ss_bool_t               bSuccess    =   true;
00222 
00223         unittest_initialiser    init(r, "COMSTL", "interface_ptr", __FILE__);
00224 
00225 #if 0
00226         if(<<TODO>>)
00227         {
00228             r->report("<<TODO>> failed ", __LINE__);
00229             bSuccess = false;
00230         }
00231 #endif /* 0 */
00232 
00233         return bSuccess;
00234     }
00235 
00236     unittest_registrar    unittest_comstl_interface_ptr(test_comstl_interface_ptr);
00237 
00238 } // namespace unittest
00239 
00240 #endif /* STLSOFT_UNITTEST */
00241 
00242 /* 
00243  * Implementation
00244  */
00245 
00246 // interface_ptr
00247 
00248 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00249 
00250 template <ss_typename_param_k T>
00251 inline void interface_ptr<T>::swap(interface_ptr<T> &rhs)
00252 {
00253     comstl_ns_qual_std(swap)(m_p, rhs.m_p);
00254 }
00255 
00256 template <ss_typename_param_k T>
00257 inline interface_ptr<T>::interface_ptr()
00258     : m_p(NULL)
00259 {}
00260 
00261 template <ss_typename_param_k T>
00262 inline interface_ptr<T>::interface_ptr(ss_typename_type_k interface_ptr<T>::pointer p, cs_bool_t bAddRef)
00263     : m_p(p)
00264 {
00265     if( bAddRef &&
00266         NULL != m_p)
00267     {
00268         addref(m_p);
00269     }
00270 }
00271 
00272 template <ss_typename_param_k T>
00273 inline interface_ptr<T>::interface_ptr(ss_typename_type_k interface_ptr<T>::reference r, cs_bool_t bAddRef)
00274     : m_p(&r)
00275 {
00276     stlsoft_message_assert("Attempting to dereference a null pointer", NULL != m_p);
00277 
00278     if(bAddRef)
00279     {
00280         addref(m_p);
00281     }
00282 }
00283 
00284 template <ss_typename_param_k T>
00285 inline interface_ptr<T>::interface_ptr(ss_typename_type_k interface_ptr<T>::class_type const &rhs)
00286     : m_p(rhs.m_p)
00287 {
00288     safe_addref(m_p);
00289 }
00290 
00291 template <ss_typename_param_k T>
00292 inline void interface_ptr<T>::release()
00293 {
00294     release_set_null(m_p);
00295 }
00296 
00297 template <ss_typename_param_k T>
00298 inline void interface_ptr<T>::release(cs_bool_t bDecRef /* = true */)
00299 {
00300     if(NULL != m_p)
00301     {
00302         if(bDecRef)
00303         {
00304             release(m_p);
00305         }
00306 
00307         m_p = NULL;
00308     }
00309 }
00310 
00311 template <ss_typename_param_k T>
00312 inline interface_ptr<T>::~interface_ptr() comstl_throw_0()
00313 {
00314     release();
00315 }
00316 
00317 template <ss_typename_param_k T>
00318 inline ss_typename_type_k interface_ptr<T>::class_type &interface_ptr<T>::operator =(ss_typename_type_k interface_ptr<T>::class_type const &rhs)
00319 {
00320     class_type   t(SyCastConst(class_type &, rhs).m_p);
00321 
00322     swap(t);
00323 
00324     return *this;
00325 }
00326 
00327 template <ss_typename_param_k T>
00328 inline void interface_ptr<T>::set(ss_typename_type_k interface_ptr<T>::pointer p, cs_bool_t bAddRef)
00329 {
00330     class_type   t(p, bAddRef);
00331 
00332     swap(t);
00333 }
00334 
00335 template <ss_typename_param_k T>
00336 inline void interface_ptr<T>::set(ss_typename_type_k interface_ptr<T>::reference r, cs_bool_t bAddRef)
00337 {
00338     stlsoft_message_assert("Attempting to dereference a null pointer", &r != NULL);
00339 
00340     class_type   t(p, bAddRef);
00341 
00342     swap(t);
00343 }
00344 
00345 template <ss_typename_param_k T>
00346 inline ss_typename_type_k interface_ptr<T>::pointer interface_ptr<T>::detach()
00347 {
00348     pointer p = m_p;
00349 
00350     m_p = NULL;
00351 
00352     return p;
00353 }
00354 
00355 template <ss_typename_param_k T>
00356 inline cs_bool_t interface_ptr<T>::operator ==(ss_typename_type_k interface_ptr<T>::class_type const &rhs) const
00357 {
00358     return m_p == rhs.m_p;
00359 }
00360 
00361 template <ss_typename_param_k T>
00362 inline cs_bool_t interface_ptr<T>::operator !=(ss_typename_type_k interface_ptr<T>::class_type const &rhs) const
00363 {
00364     return m_p != rhs.m_p;
00365 }
00366 
00367 template <ss_typename_param_k T>
00368 inline interface_ptr<T>::operator ss_typename_type_k interface_ptr<T>::operator_bool_type() const
00369 {
00370     return operator_bool_generator_type::translate(NULL != m_p);
00371 }
00372 
00373 template <ss_typename_param_k T>
00374 inline ss_typename_type_k interface_ptr<T>::pointer interface_ptr<T>::get_interface_ptr()
00375 {
00376     return m_p;
00377 }
00378 
00379 template <ss_typename_param_k T>
00380 inline ss_typename_type_k interface_ptr<T>::const_pointer interface_ptr<T>::get_interface_ptr() const
00381 {
00382     return m_p;
00383 }
00384 
00385 #if 0
00386 template <ss_typename_param_k T>
00387 inline cs_bool_t interface_ptr<T>::operator !() const
00388 {
00389     return NULL == m_p;
00390 }
00391 #endif /* 0 */
00392 
00393 template <ss_typename_param_k T>
00394 inline ss_typename_type_k interface_ptr<T>::const_pointer interface_ptr<T>::operator ->() const
00395 {
00396     stlsoft_message_assert("Attempting to dereference a null pointer", NULL != m_p);
00397 
00398     return m_p;
00399 }
00400 
00401 template <ss_typename_param_k T>
00402 inline ss_typename_type_k interface_ptr<T>::pointer interface_ptr<T>::operator ->()
00403 {
00404     stlsoft_message_assert("Attempting to dereference a null pointer", NULL != m_p);
00405 
00406     return m_p;
00407 }
00408 
00409 template <ss_typename_param_k T>
00410 inline ss_typename_type_k interface_ptr<T>::const_reference interface_ptr<T>::operator *() const
00411 {
00412     stlsoft_message_assert("Attempting to dereference a null pointer", NULL != m_p);
00413 
00414     return *m_p;
00415 }
00416 
00417 template <ss_typename_param_k T>
00418 inline ss_typename_type_k interface_ptr<T>::reference interface_ptr<T>::operator *()
00419 {
00420     stlsoft_message_assert("Attempting to dereference a null pointer", NULL != m_p);
00421 
00422     return *m_p;
00423 }
00424 
00425 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00426 
00427 /* 
00428 
00430 
00431 /* 
00432 
00433 #ifndef _COMSTL_NO_NAMESPACE
00434 # if defined(_STLSOFT_NO_NAMESPACE) || \
00435      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00436 } // namespace comstl
00437 # else
00438 } // namespace comstl_project
00439 } // namespace stlsoft
00440 # endif /* _STLSOFT_NO_NAMESPACE */
00441 #endif /* !_COMSTL_NO_NAMESPACE */
00442 
00443 /* 
00444 
00445 #endif /* !COMSTL_INCL_H_COMSTL_INTERFACE_PTR */
00446 
00447 /* 

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