00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef CNTM_HANDLEDLIST_H
00015 #define CNTM_HANDLEDLIST_H
00016 #include <list>
00017 #include <boost/thread/recursive_mutex.hpp>
00018 #include <Cntm/SystemUtils/AtomicUtils.h>
00019 #include <Cntm/SystemUtils/ObjectAllocateBuffer.h>
00020 #include <Cntm/SystemUtils/SyncUtils.h>
00021 #include <Cntm/Exceptions.h>
00022 #include <Cntm/Utils.h>
00023
00024 namespace Cntm
00025 {
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 template < typename ValueT, typename StoreT = ValueT >
00041 class HandledList: boost::noncopyable
00042 {
00043 public:
00044
00045 class Enumerator;
00046 class Handle;
00047 class AddResult;
00048
00049 typedef ValueT Type;
00050
00051 private:
00052
00053 friend class Enumerator;
00054 friend class Handle;
00055 friend class AddResult;
00056
00057 typedef typename std::list < StoreT > ::iterator ItemId;
00058
00059
00060
00061
00062
00063 class EnumeratorBase
00064 {
00065 protected:
00066
00067 friend class HandledList;
00068
00069
00070
00071
00072 EnumeratorBase(): prev(this), next(this) {}
00073
00074
00075
00076
00077 EnumeratorBase* PrevEnum() const { return prev; }
00078
00079
00080
00081
00082 EnumeratorBase* NextEnum() const { return next; }
00083
00084
00085
00086
00087 void InsertAfter(EnumeratorBase* Enum)
00088 {
00089 Enum->next->prev = this;
00090 next = Enum->next;
00091 Enum->next = this;
00092 prev = Enum;
00093 }
00094
00095
00096
00097
00098 void Remove()
00099 {
00100 prev->next = next;
00101 next->prev = prev;
00102 prev = next = this;
00103 }
00104
00105 private:
00106
00107
00108
00109
00110 EnumeratorBase* prev;
00111
00112
00113
00114
00115 EnumeratorBase* next;
00116 };
00117
00118 public:
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 class Enumerator: EnumeratorBase
00134 {
00135 public:
00136
00137 typedef ValueT Type;
00138
00139
00140
00141
00142 Enumerator(const Enumerator& Src);
00143
00144
00145
00146
00147 Enumerator& operator = (const Enumerator& Src);
00148
00149
00150
00151
00152 Enumerator& operator ++ () { Next(); return *this; }
00153
00154
00155
00156
00157 ~Enumerator();
00158
00159
00160
00161
00162 operator bool () const { return Enumerated(); }
00163
00164
00165
00166
00167 bool operator ! () const { return !Enumerated(); }
00168
00169
00170
00171
00172 const Type& operator * () const { return Current(); }
00173
00174
00175
00176
00177 const Type* operator -> () const { return &Current(); }
00178
00179
00180
00181
00182 bool Enumerated() const { return enumerated; }
00183
00184
00185
00186
00187 const Type& Current() const
00188 {
00189 return *(reinterpret_cast<const Type*>(currentBuffer.Place()));
00190 }
00191
00192
00193
00194
00195
00196
00197 bool Next();
00198
00199
00200
00201
00202 void Reset();
00203
00204 protected:
00205
00206 friend class HandledList;
00207
00208
00209
00210
00211 Enumerator(HandledList* List);
00212
00213
00214
00215
00216 HandledList* List() const { return list; }
00217
00218
00219
00220
00221 const ItemId& CurrentId() const { return currentId; }
00222
00223
00224
00225
00226 ItemId& CurrentId() { return currentId; }
00227
00228 private:
00229
00230
00231
00232
00233 HandledList* list;
00234
00235
00236
00237
00238 bool enumerated;
00239
00240
00241
00242
00243 SpecUtils::ObjectAllocateBuffer<Type> currentBuffer;
00244
00245
00246
00247
00248 ItemId currentId;
00249 };
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260 class Handle
00261 {
00262 public:
00263
00264
00265
00266
00267 Handle(): list(NULL) {}
00268
00269
00270
00271
00272 Handle(const AddResult& Src): list(Src.List()), id(Src.Id()) {}
00273
00274
00275
00276
00277 Handle(const Handle& Src);
00278
00279
00280
00281
00282 ~Handle() throw() { try { if (list) list->DeleteItem(id); } catch (...) { } }
00283
00284
00285
00286
00287 Handle& operator = (const AddResult& Src);
00288
00289
00290
00291
00292 Handle& operator = (const Handle& Src);
00293
00294
00295
00296
00297 bool IsReleased() const { return list; }
00298
00299
00300
00301
00302 void Release();
00303
00304 private:
00305
00306
00307
00308
00309 HandledList* list;
00310
00311
00312
00313
00314 ItemId id;
00315
00316
00317
00318
00319 HandledList* Enter()
00320 {
00321 return SpecUtils::FastWaitPtrNoEqualAndSet(&list, reinterpret_cast<HandledList*>(this));
00322 }
00323
00324
00325
00326
00327 void Leave(HandledList* UnlockValue)
00328 {
00329 list = UnlockValue;
00330 }
00331
00332
00333
00334
00335 void AtomicCopyTo(HandledList** DestList, StoreT* DestValue) const;
00336
00337
00338
00339
00340 void Duplicate(const Handle& Src, HandledList** DestList, ItemId* DestId) const;
00341
00342
00343
00344
00345 void SetNewValues(HandledList* List, const ItemId& Id);
00346 };
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359 class AddResult
00360 {
00361 public:
00362
00363
00364
00365
00366
00367
00368 AddResult(const AddResult& Src);
00369
00370
00371
00372
00373 ~AddResult() throw() { try { if (list) list->DeleteItem(id); } catch (...) { } }
00374
00375 protected:
00376
00377 friend class HandledList;
00378 friend class Handle;
00379
00380
00381
00382
00383 AddResult(HandledList* List, const ItemId& Id):
00384 list(List), id(Id) {}
00385
00386
00387
00388
00389 HandledList* List() const
00390 {
00391 return SpecUtils::AtomicSwapPtr(const_cast<HandledList**>(&list), (HandledList*)NULL);
00392 }
00393
00394
00395
00396
00397 const ItemId& Id() const { return id; }
00398
00399 private:
00400
00401
00402
00403
00404 HandledList* list;
00405
00406
00407
00408
00409 ItemId id;
00410 };
00411
00412
00413
00414
00415
00416 uint Count() const
00417 {
00418 boost::recursive_mutex::scoped_lock lock(mutex);
00419 return items.count();
00420 }
00421
00422
00423
00424
00425 bool IsEmpty() const { return Count() == 0; }
00426
00427
00428
00429
00430 AddResult Add(const Type& Value)
00431 {
00432 return AddResult(this, AddStoreValue(Value));
00433 }
00434
00435
00436
00437
00438 Enumerator GetEnumerator() const
00439 {
00440 return Enumerator(const_cast<HandledList*>(this));
00441 }
00442
00443 private:
00444
00445
00446
00447
00448 ItemId AddStoreValue(const StoreT& Value);
00449
00450
00451
00452
00453 void DeleteItem(const ItemId& Id);
00454
00455
00456
00457
00458 void AddEnumerator(Enumerator* Enum);
00459
00460
00461
00462
00463 void DeleteEnumerator(Enumerator* Enum);
00464
00465
00466
00467
00468 void ResetItemId(ItemId* DestId);
00469
00470
00471
00472
00473 void CopyItemId(const Enumerator& Src, ItemId* DestId);
00474
00475
00476
00477
00478 bool FirstItem(ItemId* DestId, void* DestCurrentBuf);
00479
00480
00481
00482
00483 bool NextItem(ItemId* VarId, void* DestCurrentBuf);
00484
00485 private:
00486
00487
00488
00489
00490 std::list<StoreT> items;
00491
00492
00493
00494
00495 boost::recursive_mutex mutex;
00496
00497
00498
00499
00500 EnumeratorBase enumsHead;
00501
00502
00503
00504
00505 bool ReturnItem(const ItemId& Id, void* DestCurrentBuf);
00506 };
00507
00508 }
00509
00510
00511
00512
00513 template < typename ValueT, typename StoreT >
00514 Cntm::HandledList < ValueT, StoreT > ::Enumerator::Enumerator(const Enumerator& Src):
00515 EnumeratorBase(), list(Src.List()), enumerated(Src.Enumerated())
00516 {
00517 if (enumerated)
00518 new(currentBuffer.Place()) Type(Src.Current());
00519 list->ResetItemId(¤tId);
00520 list->AddEnumerator(this);
00521 list->CopyItemId(Src, ¤tId);
00522 }
00523
00524 template < typename ValueT, typename StoreT >
00525 Cntm::HandledList < ValueT, StoreT > ::Enumerator::~Enumerator()
00526 {
00527 try
00528 {
00529 try { list->DeleteEnumerator(this); } catch (...) {}
00530 Reset();
00531 }
00532 catch (...)
00533 {
00534 }
00535 }
00536
00537 template < typename ValueT, typename StoreT >
00538 typename Cntm::HandledList < ValueT, StoreT > ::Enumerator&
00539 Cntm::HandledList < ValueT, StoreT > ::Enumerator::operator = (const Enumerator& Src)
00540 {
00541 if (this == &Src) return *this;
00542
00543
00544
00545 if (list != Src.List())
00546 {
00547 list->DeleteEnumerator(this);
00548 Reset();
00549 list = Src.List();
00550 list->ResetItemId(¤tId);
00551 list->AddEnumerator(this);
00552 }
00553 else
00554 {
00555 Reset();
00556 }
00557
00558
00559 enumerated = Src.Enumerated();
00560 if (enumerated)
00561 new(currentBuffer.Place()) Type(Src.Current());
00562 list->CopyItemId(Src, ¤tId);
00563
00564 return *this;
00565 }
00566
00567 template < typename ValueT, typename StoreT >
00568 bool Cntm::HandledList < ValueT, StoreT > ::Enumerator::Next()
00569 {
00570 if (enumerated)
00571 {
00572 try
00573 {
00574 reinterpret_cast<Type*>(currentBuffer.Place())->~Type();
00575 enumerated = list->NextItem(¤tId, currentBuffer.Place());
00576 }
00577 catch (...)
00578 {
00579 enumerated = false;
00580 throw;
00581 }
00582 }
00583 else
00584 {
00585 enumerated = list->FirstItem(¤tId, currentBuffer.Place());
00586 }
00587
00588 return enumerated;
00589 }
00590
00591 template < typename ValueT, typename StoreT >
00592 void Cntm::HandledList < ValueT, StoreT > ::Enumerator::Reset()
00593 {
00594 if (enumerated)
00595 {
00596 enumerated = false;
00597 reinterpret_cast<Type*>(currentBuffer.Place())->~Type();
00598 list->ResetItemId(¤tId);
00599 }
00600 }
00601
00602 template < typename ValueT, typename StoreT >
00603 Cntm::HandledList < ValueT, StoreT > ::Enumerator::Enumerator(HandledList* List):
00604 list(List), enumerated(false)
00605 {
00606 list->ResetItemId(¤tId);
00607 list->AddEnumerator(this);
00608 }
00609
00610
00611
00612
00613 template < typename ValueT, typename StoreT >
00614 Cntm::HandledList < ValueT, StoreT > ::Handle::Handle(const Handle& Src)
00615 {
00616 if (this != &Src) Duplicate(Src, &list, &id);
00617 }
00618
00619 template < typename ValueT, typename StoreT >
00620 typename Cntm::HandledList < ValueT, StoreT > ::Handle&
00621 Cntm::HandledList < ValueT, StoreT > ::Handle::operator = (const AddResult& Src)
00622 {
00623 SetNewValues(Src.List(), Src.Id());
00624
00625 return *this;
00626 }
00627
00628 template < typename ValueT, typename StoreT >
00629 typename Cntm::HandledList < ValueT, StoreT > ::Handle&
00630 Cntm::HandledList < ValueT, StoreT > ::Handle::operator = (const Handle& Src)
00631 {
00632 if (this != &Src)
00633 {
00634
00635 HandledList* newList;
00636 ItemId newId;
00637 Duplicate(Src, &newList, &newId);
00638
00639
00640 SetNewValues(newList, newId);
00641 }
00642
00643 return *this;
00644 }
00645
00646 template < typename ValueT, typename StoreT >
00647 void Cntm::HandledList < ValueT, StoreT > ::Handle::Release()
00648 {
00649
00650 SetNewValues(NULL, id);
00651 }
00652
00653 template < typename ValueT, typename StoreT >
00654 inline void Cntm::HandledList < ValueT, StoreT > ::Handle::AtomicCopyTo
00655 (HandledList** DestList, StoreT * DestValue) const
00656 {
00657 *DestList = const_cast<Handle*>(this)->Enter();
00658 try
00659 {
00660 if (*DestList) *DestValue = *id;
00661 }
00662 catch (...)
00663 {
00664 const_cast<Handle*>(this)->Leave(*DestList);
00665 throw;
00666 }
00667
00668 const_cast<Handle*>(this)->Leave(*DestList);
00669 }
00670
00671 template < typename ValueT, typename StoreT >
00672 inline void Cntm::HandledList < ValueT, StoreT > ::Handle::Duplicate
00673 (const Handle& Src, HandledList** DestList, ItemId* DestId) const
00674 {
00675
00676 StoreT val;
00677 Src.AtomicCopyTo(DestList, &val);
00678
00679
00680 if (*DestList) *DestId = (*DestList)->AddStoreValue(val);
00681 }
00682
00683 template < typename ValueT, typename StoreT >
00684 inline void Cntm::HandledList < ValueT, StoreT > ::Handle::SetNewValues
00685 (HandledList* List, const ItemId& Id)
00686 {
00687
00688 ItemId oldId;
00689 HandledList* oldList = Enter();
00690 try
00691 {
00692 oldId = id;
00693 id = Id;
00694 }
00695 catch(...)
00696 {
00697 Leave(NULL);
00698 throw;
00699 }
00700
00701 Leave(List);
00702
00703
00704 if (oldList) oldList->DeleteItem(oldId);
00705 }
00706
00707
00708
00709
00710 template < typename ValueT, typename StoreT >
00711 Cntm::HandledList < ValueT, StoreT > ::AddResult::AddResult(const AddResult& Src):
00712 list(Src.List()), id(Src.Id())
00713 {
00714 if (list == NULL)
00715 throw BadArgException("Cntm::HandledList::AddResult::AddResult", "Пустой источник");
00716 }
00717
00718
00719
00720
00721 template < typename ValueT, typename StoreT >
00722 inline typename Cntm::HandledList < ValueT, StoreT > ::ItemId
00723 Cntm::HandledList < ValueT, StoreT > ::AddStoreValue(const StoreT& Value)
00724 {
00725 boost::recursive_mutex::scoped_lock lock(mutex);
00726 return items.insert(items.end(), Value);
00727 }
00728
00729 template < typename ValueT, typename StoreT >
00730 inline void Cntm::HandledList < ValueT, StoreT > ::DeleteItem(const ItemId& Id)
00731 {
00732
00733 boost::recursive_mutex::scoped_lock lock(mutex);
00734
00735
00736
00737
00738 for (EnumeratorBase* e = enumsHead.NextEnum(); e != &enumsHead; e = e->NextEnum())
00739 if (static_cast<Enumerator*>(e)->CurrentId() == Id)
00740 --(static_cast<Enumerator*>(e)->CurrentId());
00741
00742
00743 items.erase(Id);
00744 }
00745
00746 template < typename ValueT, typename StoreT >
00747 inline void Cntm::HandledList < ValueT, StoreT > ::AddEnumerator(Enumerator* Enum)
00748 {
00749 boost::recursive_mutex::scoped_lock lock(mutex);
00750 Enum->InsertAfter(&enumsHead);
00751 }
00752
00753 template < typename ValueT, typename StoreT >
00754 inline void Cntm::HandledList < ValueT, StoreT > ::DeleteEnumerator(Enumerator* Enum)
00755 {
00756 boost::recursive_mutex::scoped_lock lock(mutex);
00757 Enum->Remove();
00758 }
00759
00760 template < typename ValueT, typename StoreT >
00761 inline void Cntm::HandledList < ValueT, StoreT > ::ResetItemId(ItemId* DestId)
00762 {
00763 boost::recursive_mutex::scoped_lock lock(mutex);
00764 *DestId = items.end();
00765 }
00766
00767
00768 template < typename ValueT, typename StoreT >
00769 inline void Cntm::HandledList < ValueT, StoreT > ::CopyItemId(const Enumerator& Src, ItemId* DestId)
00770 {
00771 boost::recursive_mutex::scoped_lock lock(mutex);
00772 *DestId = Src.CurrentId();
00773 }
00774
00775 template < typename ValueT, typename StoreT >
00776 inline bool Cntm::HandledList < ValueT, StoreT > ::FirstItem(ItemId* DestId, void* DestCurrentBuf)
00777 {
00778 boost::recursive_mutex::scoped_lock lock(mutex);
00779 *DestId = items.begin();
00780 return ReturnItem(*DestId, DestCurrentBuf);
00781 }
00782
00783 template < typename ValueT, typename StoreT >
00784 inline bool Cntm::HandledList < ValueT, StoreT > ::NextItem(ItemId* VarId, void* DestCurrentBuf)
00785 {
00786 boost::recursive_mutex::scoped_lock lock(mutex);
00787 ++(*VarId);
00788 return ReturnItem(*VarId, DestCurrentBuf);
00789 }
00790
00791 template < typename ValueT, typename StoreT >
00792 inline bool Cntm::HandledList < ValueT, StoreT > ::ReturnItem(const ItemId& Id, void* DestCurrentBuf)
00793 {
00794 bool res = (Id != items.end());
00795 if (res) new(DestCurrentBuf) Type(*Id);
00796 return res;
00797 }
00798
00799
00800 #endif //CNTM_HANDLEDLIST_H