00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #ifndef COMSTL_INCL_H_COMSTL
00060 # include "comstl.h"
00061 #endif
00062 #ifndef COMSTL_INCL_H_COMSTL_REFCOUNT_FUNCTIONS
00063 # include "comstl_refcount_functions.h"
00064 #endif
00065 #ifndef STLSOFT_INCL_H_STLSOFT_OPERATOR_BOOL
00066 # include "stlsoft_operator_bool.h"
00067 #endif
00068 #include <algorithm>
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 #ifndef _COMSTL_NO_NAMESPACE
00088 # if defined(_STLSOFT_NO_NAMESPACE) || \
00089 defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00090
00091 namespace comstl
00092 {
00093 # else
00094
00095
00096 namespace stlsoft
00097 {
00098
00099 namespace comstl_project
00100 {
00101
00102 # endif
00103 #endif
00104
00105
00106
00111
00112
00113
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 );
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
00171
00174
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
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
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
00232
00233 return bSuccess;
00234 }
00235
00236 unittest_registrar unittest_comstl_interface_ptr(test_comstl_interface_ptr);
00237
00238 }
00239
00240 #endif
00241
00242
00243
00244
00245
00246
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 )
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
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
00426
00427
00428
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441 #endif
00442
00443
00444
00445
00446
00447