00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef COH_MEMBER_VIEW_HPP
00017 #define COH_MEMBER_VIEW_HPP
00018
00019 #include "coherence/lang/compatibility.hpp"
00020
00021 #include "coherence/lang/IllegalStateException.hpp"
00022 #include "coherence/lang/MemberHandle.hpp"
00023 #include "coherence/lang/Object.hpp"
00024 #include "coherence/lang/SmartMember.hpp"
00025 #include "coherence/lang/SynchronizedMemberReadBlock.hpp"
00026 #include "coherence/lang/SynchronizedMemberWriteBlock.hpp"
00027 #include "coherence/lang/TypedHandle.hpp"
00028 #include "coherence/lang/TypedHolder.hpp"
00029
00030 #include <ostream>
00031
00032 COH_OPEN_NAMESPACE2(coherence,lang)
00033
00034 class Object;
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 template<class T>
00047 class MemberView
00048 : public SmartMember
00049 {
00050
00051
00052 public:
00053
00054
00055
00056 typedef T ValueType;
00057
00058
00059
00060
00061 typedef typename T::View ValueView;
00062
00063
00064
00065
00066 typedef ValueView GetType;
00067
00068
00069
00070
00071 public:
00072
00073
00074
00075
00076
00077
00078 MemberView(Object& oParent)
00079 : SmartMember(oParent), m_cpo(NULL)
00080 {
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090 template<class DT> MemberView(Object& oParent, const TypedHandle<DT>& h)
00091 : SmartMember(oParent), m_cpo(NULL)
00092 {
00093 set(h);
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 template<class DT> MemberView(Object& oParent, const TypedHolder<DT>& th)
00105 : SmartMember(oParent), m_cpo(NULL)
00106 {
00107 operator=(th);
00108 }
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 template<class DT> MemberView(Object& oParent, const MemberHolder<DT>& mh)
00119 : SmartMember(oParent), m_cpo(NULL)
00120 {
00121 operator=(mh);
00122 }
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 template<class DT> MemberView(Object& oParent, const MemberView<DT>& that)
00133 : SmartMember(oParent), m_cpo(NULL)
00134 {
00135 operator=(that);
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 template<class DT> MemberView(Object& oParent, const MemberHandle<DT>& that)
00147 : SmartMember(oParent), m_cpo(NULL)
00148 {
00149 operator=(that);
00150 }
00151
00152
00153
00154
00155 ~MemberView()
00156 {
00157 set(ValueView());
00158 }
00159
00160 private:
00161
00162
00163
00164 MemberView(const MemberView&);
00165
00166
00167
00168
00169 public:
00170
00171
00172
00173
00174
00175
00176
00177
00178 MemberView& operator=(const MemberView& that)
00179 {
00180 operator=((ValueView) that);
00181 return *this;
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 template<class DT> MemberView& operator=(const MemberView<DT>& mv)
00193 {
00194 operator=((TypedHandle<const DT>) mv);
00195 return *this;
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 template<class DT> MemberView& operator=(const TypedHolder<DT>& th)
00207 {
00208 set(th);
00209 return *this;
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220 template<class DT> MemberView& operator=(const MemberHolder<DT>& mh)
00221 {
00222 set(mh);
00223 return *this;
00224 }
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 template<class DT> MemberView& operator=(const MemberHandle<DT>& mh)
00235 {
00236 set((typename MemberHandle<DT>::ValueView) mh);
00237 return *this;
00238 }
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248 template<class DT> MemberView& operator=(const TypedHandle<DT>& h)
00249 {
00250 set(h);
00251 return *this;
00252 }
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262 MemberView& operator=(const T* cp)
00263 {
00264 return operator=((ValueView) cp);
00265 }
00266
00267
00268
00269
00270
00271
00272 operator ValueView() const
00273 {
00274 SynchronizedMemberReadBlock syncRead(getParent());
00275 return m_cpo;
00276 }
00277
00278
00279
00280
00281
00282
00283 template<class PT>
00284 operator TypedHandle<const PT>() const
00285 {
00286 return (ValueView) *this;
00287 }
00288
00289
00290
00291
00292
00293
00294 template<class PT>
00295 operator TypedHolder<PT>() const
00296 {
00297 return (ValueView) *this;
00298 }
00299
00300
00301
00302
00303
00304
00305 ValueView operator->() const
00306 {
00307 return (ValueView) *this;
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317 template<class AT>
00318 bool operator==(const MemberView<AT>& mv) const
00319 {
00320 typename MemberView<AT>::ValueView v = mv;
00321 return operator==(v);
00322 }
00323
00324
00325
00326
00327
00328
00329
00330
00331 template<class AT>
00332 bool operator!=(const MemberView<AT>& mv) const
00333 {
00334 return !operator==(mv);
00335 }
00336
00337
00338
00339
00340
00341
00342
00343
00344 template<class AT>
00345 bool operator==(const MemberHolder<AT>& mh) const
00346 {
00347 typename TypedHolder<AT>::ValueView v = mh;
00348 return operator==(v);
00349 }
00350
00351
00352
00353
00354
00355
00356
00357
00358 template<class AT>
00359 bool operator!=(const MemberHolder<AT>& mh) const
00360 {
00361 return !operator==(mh);
00362 }
00363
00364
00365
00366
00367
00368
00369
00370
00371 template<class AT>
00372 bool operator==(const MemberHandle<AT>& mh) const
00373 {
00374 typename MemberHandle<AT>::ValueView v = mh;
00375 return operator==(v);
00376 }
00377
00378
00379
00380
00381
00382
00383
00384
00385 template<class AT>
00386 bool operator!=(const MemberHandle<AT>& mh) const
00387 {
00388 return !operator==(mh);
00389 }
00390
00391
00392
00393
00394
00395
00396
00397
00398 template<class AT>
00399 bool operator==(const TypedHolder<AT>& th) const
00400 {
00401 return operator==(get_pointer((typename TypedHolder<AT>::ValueView) th));
00402 }
00403
00404
00405
00406
00407
00408
00409
00410
00411 template<class AT>
00412 bool operator!=(const TypedHolder<AT>& th) const
00413 {
00414 return !operator==(th);
00415 }
00416
00417
00418
00419
00420
00421
00422
00423
00424 template<class AT>
00425 bool operator==(const TypedHandle<AT>& h) const
00426 {
00427 return operator==(get_pointer(h));
00428 }
00429
00430
00431
00432
00433
00434
00435
00436
00437 template<class AT>
00438 bool operator!=(const TypedHandle<AT>& h) const
00439 {
00440 return !operator==(h);
00441 }
00442
00443
00444
00445
00446
00447
00448
00449
00450 bool operator==(const Object* cpThat) const
00451 {
00452 SynchronizedMemberReadBlock syncRead(getParent());
00453 return ((const Object*) m_cpo) == cpThat;
00454 }
00455
00456
00457
00458
00459
00460
00461
00462
00463 bool operator!=(const Object* cpThat) const
00464 {
00465 return !operator==(cpThat);
00466 }
00467
00468
00469
00470
00471 protected:
00472
00473
00474
00475
00476
00477
00478
00479
00480 void set(ValueView v, SynchronizedMemberWriteBlock* pSync = NULL)
00481 {
00482 const T* cpo = get_pointer(v);
00483 const Object* cpAttach = NULL == cpo ? NULL : cpo->_attach();
00484 const T* cpDetach = NULL;
00485
00486
00487 {
00488 SynchronizedMemberWriteBlock syncWrite(getParent(), pSync);
00489 cpDetach = m_cpo;
00490 m_cpo = NULL == cpAttach ? NULL : cpo;
00491 }
00492
00493 if (cpDetach)
00494 {
00495 cpDetach->_detach();
00496 }
00497 }
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507 ValueView get(SynchronizedMemberReadBlock* pSync = NULL) const
00508 {
00509 SynchronizedMemberReadBlock syncRead(getParent(), pSync);
00510 return m_cpo;
00511 }
00512
00513
00514
00515
00516 private:
00517
00518
00519
00520 const T* m_cpo;
00521
00522
00523
00524
00525
00526
00527
00528 friend class SynchronizedMemberReadBlock;
00529
00530
00531
00532
00533 friend class SynchronizedMemberWriteBlock;
00534 };
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548 template<class T> std::ostream& operator<<(std::ostream& out, const MemberView<T>& mv)
00549 {
00550 out << (typename T::View) mv;
00551 return out;
00552 }
00553
00554
00555
00556
00557
00558
00559 template<class T> void clear_handle(MemberView<T>& mv)
00560 {
00561 static TypedHandle<const T> vNull;
00562 mv = vNull;
00563 }
00564
00565
00566
00567
00568
00569
00570
00571
00572 template<class T>
00573 bool is_null(const MemberView<T>& mv)
00574 {
00575 return NULL == mv;
00576 }
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589 template<class D, class T>
00590 D cast(const MemberView<T>& mv, bool fThrow = true)
00591 {
00592 return cast<D>((typename MemberView<T>::ValueView) mv, fThrow);
00593 }
00594
00595
00596
00597
00598
00599
00600
00601
00602 template<class D, class T>
00603 bool instanceof(const MemberView<T>& mv)
00604 {
00605 return NULL != cast<D>(mv, false);
00606 }
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616 template<class AT, class T>
00617 bool operator==(const TypedHandle<AT>& h, const MemberView<T>& mv)
00618 {
00619 return mv == h;
00620 }
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630 template<class T>
00631 bool operator==(const Object* cpo, const MemberView<T>& mv)
00632 {
00633 return mv == cpo;
00634 }
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644 template<class AT, class T>
00645 bool operator!=(const TypedHandle<AT>& h, const MemberView<T>& mv)
00646 {
00647 return mv != h;
00648 }
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658 template<class T>
00659 bool operator!=(const Object* cpo, const MemberView<T>& mv)
00660 {
00661 return mv != cpo;
00662 }
00663
00664 COH_CLOSE_NAMESPACE2
00665
00666 #endif // COH_MEMBER_VIEW_HPP
Copyright (c) 2000-2008 Oracle. All rights reserved.