00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef COH_TYPED_HOLDER_HPP
00017 #define COH_TYPED_HOLDER_HPP
00018
00019 #include "coherence/lang/compatibility.hpp"
00020
00021 #include "coherence/lang/ChainedHandleElement.hpp"
00022 #include "coherence/lang/TypedHandle.hpp"
00023
00024 COH_OPEN_NAMESPACE2(coherence,lang)
00025
00026 class Object;
00027 template<class T> class MemberHolder;
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 template<class T>
00056 class TypedHolder
00057 : public ChainedHandleElement
00058 {
00059
00060
00061 public:
00062
00063
00064
00065 typedef const T ValueType;
00066
00067
00068
00069
00070 typedef typename T::Handle ValueHandle;
00071
00072
00073
00074
00075 typedef typename T::View ValueView;
00076
00077
00078
00079
00080 public:
00081
00082
00083
00084 TypedHolder()
00085 : ChainedHandleElement( false), m_cpo(NULL)
00086 {
00087 }
00088
00089
00090
00091
00092
00093
00094
00095 TypedHolder(const TypedHolder& that)
00096 : ChainedHandleElement(that, that.m_fView), m_cpo(that.m_cpo)
00097 {
00098 }
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 template<class DT> TypedHolder(const TypedHandle<DT>& that)
00110 : ChainedHandleElement(that, constness<DT>::applied),
00111 m_cpo(get_pointer(that))
00112 {
00113 }
00114
00115
00116
00117
00118
00119
00120
00121 template<class DT> TypedHolder(const TypedHolder<DT>& that)
00122 : ChainedHandleElement(that, that.m_fView), m_cpo(that.m_cpo)
00123 {
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 TypedHolder(T* po)
00135 : ChainedHandleElement( false),
00136 m_cpo(po == NULL || po->_attach( false) == NULL
00137 ? NULL : po)
00138 {
00139 }
00140
00141
00142
00143
00144
00145
00146 template<class DT> TypedHolder(DT* po)
00147 : ChainedHandleElement( constness<DT>::applied),
00148 m_cpo(po == NULL || po->_attach( false) == NULL
00149 ? NULL : po)
00150 {
00151 }
00152
00153
00154
00155
00156 ~TypedHolder()
00157 {
00158 Action nAction = unlink();
00159 if (nAction != ACTION_NONE)
00160 {
00161 safeDestruct(nAction);
00162 }
00163 }
00164
00165
00166
00167
00168 public:
00169
00170
00171
00172
00173
00174
00175
00176
00177 TypedHolder& operator=(const TypedHolder& that)
00178 {
00179 set(get_pointer(that), that.m_fView, that);
00180 return *this;
00181 }
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 template<class DT> TypedHolder& operator=(const TypedHolder<DT>& that)
00192 {
00193 set(get_pointer(that), that.m_fView, that);
00194 return *this;
00195 }
00196
00197
00198
00199
00200
00201
00202 template<class PT>
00203 operator TypedHandle<const PT>() const
00204 {
00205 return TypedHandle<const PT>(m_cpo, *this);
00206 }
00207
00208
00209
00210
00211
00212
00213
00214
00215 const T& operator*() const
00216 {
00217 const T* po = m_cpo;
00218 if (NULL == po)
00219 {
00220 coh_throw_npe(typeid(T));
00221 }
00222 return *po;
00223 }
00224
00225
00226
00227
00228
00229
00230 const T* operator->() const
00231 {
00232 const T* po = m_cpo;
00233 if (NULL == po)
00234 {
00235 coh_throw_npe(typeid(T));
00236 }
00237 return po;
00238 }
00239
00240
00241
00242
00243 protected:
00244
00245
00246
00247
00248
00249 bool isHandle() const
00250 {
00251 return m_fView == false;
00252 }
00253
00254
00255
00256
00257
00258
00259
00260 TypedHandle<T> getHandle() const
00261 {
00262 if (m_fView)
00263 {
00264 return NULL;
00265 }
00266
00267 return TypedHandle<T>(const_cast<T*>(m_cpo), *this);
00268 }
00269
00270
00271
00272
00273
00274
00275 void set(const T* cp, bool fView, const ChainedHandleElement& that)
00276 {
00277
00278 const T* cpo = m_cpo;
00279 bool fViewOld = m_fView;
00280
00281
00282 Action nAction = link(that);
00283 m_cpo = cp;
00284 m_fView = fView;
00285
00286 if (nAction != ACTION_NONE)
00287 {
00288 performAction(nAction, cpo, fViewOld);
00289 }
00290 }
00291
00292
00293
00294
00295
00296
00297
00298
00299 void performAction(Action nAction, const T* cpo, bool fView)
00300 {
00301 switch (nAction)
00302 {
00303 case ACTION_ERROR:
00304 coh_throw_illegal_state("corrupted ChainedHandleElement");
00305 case ACTION_FLIP:
00306 if (NULL != cpo) cpo->_attach( false);
00307
00308 case ACTION_DETACH:
00309 if (NULL != cpo)
00310 {
00311 if (fView)
00312 {
00313 cpo->_detach( false);
00314 }
00315 else
00316 {
00317 const_cast<T*>(cpo)->_detach( false);
00318 }
00319 }
00320
00321 case ACTION_NONE:
00322 default:
00323 break;
00324 }
00325 }
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335 void safeDestruct(Action nAction)
00336 {
00337 try
00338 {
00339 performAction(nAction, m_cpo, m_fView);
00340 }
00341 catch (const std::exception& e)
00342 {
00343
00344 std::cerr << "Error during ~TypedHolder: " << e.what() << std::endl;
00345 return;
00346 }
00347 }
00348
00349
00350
00351 protected:
00352
00353
00354
00355 const T* m_cpo;
00356
00357
00358
00359
00360
00361
00362
00363 template<class O> friend const O* get_pointer(const TypedHolder<O>& th);
00364
00365
00366
00367
00368 template<class O> friend bool is_handle(const TypedHolder<O>& th);
00369
00370
00371
00372
00373 template<class D, class H> friend D cast(const TypedHolder<H>& th,
00374 bool fTest);
00375
00376
00377
00378
00379 template<class> friend class TypedHolder;
00380
00381
00382
00383
00384 template<class> friend class MemberHolder;
00385 };
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399 template<class T> std::ostream& operator<<(std::ostream& out,
00400 const TypedHolder<T>& th)
00401 {
00402 out << (typename T::View) th;
00403 return out;
00404 }
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420 template<class T>
00421 const T* get_pointer(const TypedHolder<T>& th)
00422 {
00423 return th.m_cpo;
00424 }
00425
00426
00427
00428
00429
00430
00431 template<class T> void clear_handle(TypedHolder<T>& th)
00432 {
00433 th = NULL;
00434 }
00435
00436
00437
00438
00439
00440
00441
00442
00443 template<class T>
00444 bool is_null(const TypedHolder<T>& th)
00445 {
00446 return th == NULL;
00447 }
00448
00449
00450
00451
00452
00453
00454
00455
00456 template<class T>
00457 bool is_handle(const TypedHolder<T>& th)
00458 {
00459 return th.isHandle();
00460 }
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473 template<class D, class T>
00474 D cast(const TypedHolder<T>& th, bool fThrow = true)
00475 {
00476 TypedHandle<T> h = th.getHandle();
00477 if (NULL == h)
00478 {
00479 TypedHandle<const T> v = th;
00480
00481 if (NULL == v)
00482 {
00483
00484 return D();
00485 }
00486
00487
00488 if (constness<typename D::ValueType>::applied)
00489 {
00490
00491
00492
00493
00494 TypedHandle<typename D::ValueType> vD(
00495 (typename D::ValueType*) get_pointer(
00496 cast<typename D::ValueType::View>(v, fThrow)), v);
00497 return vD;
00498 }
00499 else
00500 {
00501
00502 if (fThrow)
00503 {
00504 coh_throw_const_cast(typeid(typename D::ValueType), typeid(*th));
00505 }
00506 return D();
00507 }
00508 }
00509
00510
00511
00512
00513 return cast<typename D::ValueType::Handle>(h, fThrow);
00514 }
00515
00516
00517
00518
00519
00520
00521
00522
00523 template<class D, class T>
00524 bool instanceof(const TypedHolder<T>& th)
00525 {
00526 return NULL !=th && NULL != cast<D>(th, false);
00527 }
00528
00529 COH_CLOSE_NAMESPACE2
00530
00531 #endif // COH_TYPED_HOLDER_HPP
Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.