Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TList.h
Go to the documentation of this file.
1// @(#)root/cont:$Id$
2// Author: Fons Rademakers 10/08/95
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#ifndef ROOT_TList
13#define ROOT_TList
14
15
16//////////////////////////////////////////////////////////////////////////
17// //
18// TList //
19// //
20// A doubly linked list. All classes inheriting from TObject can be //
21// inserted in a TList. //
22// //
23//////////////////////////////////////////////////////////////////////////
24
25#include "TSeqCollection.h"
26#include "TString.h"
27
28#include <iterator>
29#include <memory>
30
33
34class TObjLink;
35class TListIter;
36
37
38class TList : public TSeqCollection {
39
40friend class TListIter;
41
42protected:
43 using TObjLinkPtr_t = std::shared_ptr<TObjLink>;
44 using TObjLinkWeakPtr_t = std::weak_ptr<TObjLink>;
45
46 TObjLinkPtr_t fFirst; //! pointer to first entry in linked list
47 TObjLinkPtr_t fLast; //! pointer to last entry in linked list
48 TObjLinkWeakPtr_t fCache; //! cache to speedup sequential calling of Before() and After() functions
49 Bool_t fAscending; //! sorting order (when calling Sort() or for TSortedList)
50
51 TObjLink *LinkAt(Int_t idx) const;
52 TObjLink *FindLink(const TObject *obj, Int_t &idx) const;
53
55
56 Bool_t LnkCompare(const TObjLinkPtr_t &l1, const TObjLinkPtr_t &l2);
57 TObjLinkPtr_t NewLink(TObject *obj, const TObjLinkPtr_t &prev = nullptr);
58 TObjLinkPtr_t NewOptLink(TObject *obj, Option_t *opt, const TObjLinkPtr_t &prev = nullptr);
61 // virtual void DeleteLink(TObjLink *lnk);
62
63 void InsertAfter(const TObjLinkPtr_t &newlink, const TObjLinkPtr_t &prev);
64
65private:
66 TList(const TList&) = delete;
67 TList& operator=(const TList&) = delete;
68
69public:
71
73 TList(TObject *) : fAscending(kTRUE) { } // for backward compatibility, don't use
74 virtual ~TList();
75 void Clear(Option_t *option="") override;
76 void Delete(Option_t *option="") override;
77 TObject *FindObject(const char *name) const override;
78 TObject *FindObject(const TObject *obj) const override;
79 TIterator *MakeIterator(Bool_t dir = kIterForward) const override;
80
81 void Add(TObject *obj) override { AddLast(obj); }
82 virtual void Add(TObject *obj, Option_t *opt) { AddLast(obj, opt); }
83 void AddFirst(TObject *obj) override;
84 virtual void AddFirst(TObject *obj, Option_t *opt);
85 void AddLast(TObject *obj) override;
86 virtual void AddLast(TObject *obj, Option_t *opt);
87 void AddAt(TObject *obj, Int_t idx) override;
88 void AddAfter(const TObject *after, TObject *obj) override;
89 virtual void AddAfter(TObjLink *after, TObject *obj);
90 void AddBefore(const TObject *before, TObject *obj) override;
91 virtual void AddBefore(TObjLink *before, TObject *obj);
92 TObject *Remove(TObject *obj) override;
93 virtual TObject *Remove(TObjLink *lnk);
94 TObject *Remove(const TObjLinkPtr_t &lnk) { return Remove(lnk.get()); }
95 void RemoveLast() override;
96 void RecursiveRemove(TObject *obj) override;
97
98 TObject *At(Int_t idx) const override;
99 TObject *After(const TObject *obj) const override;
100 TObject *Before(const TObject *obj) const override;
101 TObject *First() const override;
102 virtual TObjLink *FirstLink() const { return fFirst.get(); }
103 TObject **GetObjectRef(const TObject *obj) const override;
104 TObject *Last() const override;
105 virtual TObjLink *LastLink() const { return fLast.get(); }
106
107 virtual void Sort(Bool_t order = kSortAscending);
109
110 ClassDefOverride(TList,5) //Doubly linked list
111};
112
113
114//////////////////////////////////////////////////////////////////////////
115// //
116// TObjLink //
117// //
118// Wrapper around a TObject so it can be stored in a TList. //
119// //
120//////////////////////////////////////////////////////////////////////////
121class TObjLink : public std::enable_shared_from_this<TObjLink> {
122
123friend class TList;
124
125private:
126 using TObjLinkPtr_t = std::shared_ptr<TObjLink>;
127 using TObjLinkWeakPtr_t = std::weak_ptr<TObjLink>;
128
131
132 TObject *fObject; // should be atomic ...
133
134 TObjLink(const TObjLink&) = delete;
135 TObjLink& operator=(const TObjLink&) = delete;
136 TObjLink() = delete;
137
138public:
139
140 TObjLink(TObject *obj) : fObject(obj) { }
141 virtual ~TObjLink() { }
142
143 TObject *GetObject() const { return fObject; }
144 TObject **GetObjectRef() { return &fObject; }
145 void SetObject(TObject *obj) { fObject = obj; }
146 virtual Option_t *GetAddOption() const { return ""; }
147 virtual Option_t *GetOption() const { return fObject->GetOption(); }
148 virtual void SetOption(Option_t *) { }
149 TObjLink *Next() { return fNext.get(); }
150 TObjLink *Prev() { return fPrev.lock().get(); }
152 TObjLinkPtr_t PrevSP() { return fPrev.lock(); }
153};
154
155
156//////////////////////////////////////////////////////////////////////////
157// //
158// TObjOptLink //
159// //
160// Wrapper around a TObject so it can be stored in a TList including //
161// an option string. //
162// //
163//////////////////////////////////////////////////////////////////////////
164class TObjOptLink : public TObjLink {
165
166private:
168
169public:
170 TObjOptLink(TObject *obj, Option_t *opt) : TObjLink(obj), fOption(opt) { }
172 Option_t *GetAddOption() const override { return fOption.Data(); }
173 Option_t *GetOption() const override { return fOption.Data(); }
174 void SetOption(Option_t *option) override { fOption = option; }
175};
176
177
178// Preventing warnings with -Weffc++ in GCC since it is a false positive for the TListIter destructor.
179#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
180#pragma GCC diagnostic push
181#pragma GCC diagnostic ignored "-Weffc++"
182#endif
183
184//////////////////////////////////////////////////////////////////////////
185// //
186// TListIter //
187// //
188// Iterator of linked list. //
189// //
190//////////////////////////////////////////////////////////////////////////
191class TListIter : public TIterator {
192
193protected:
194 using TObjLinkPtr_t = std::shared_ptr<TObjLink>;
195
196 const TList *fList; //list being iterated
197 TObjLinkPtr_t fCurCursor; //current position in list
198 TObjLinkPtr_t fCursor; //next position in list
199 Bool_t fDirection; //iteration direction
200 Bool_t fStarted; //iteration started
201
203 fStarted(kFALSE) { }
204
205public:
206 using iterator_category = std::bidirectional_iterator_tag;
208 using difference_type = std::ptrdiff_t;
209 using pointer = TObject **;
210 using const_pointer = const TObject **;
211 using reference = const TObject *&;
212
213 TListIter(const TList *l, Bool_t dir = kIterForward);
214 TListIter(const TListIter &iter);
216 TIterator &operator=(const TIterator &rhs) override;
217 TListIter &operator=(const TListIter &rhs);
218
219 const TCollection *GetCollection() const override { return fList; }
220 Option_t *GetOption() const override;
222 TObject *Next() override;
223 void Reset() override;
224 Bool_t operator!=(const TIterator &aIter) const override;
225 Bool_t operator!=(const TListIter &aIter) const;
226 TObject *operator*() const override { return (fCurCursor ? fCurCursor->GetObject() : nullptr); }
227
228 ClassDefOverride(TListIter,0) //Linked list iterator
229};
230
231inline bool operator==(TObjOptLink *l, const std::shared_ptr<TObjLink> &r) {
232 return l == r.get();
233}
234
235inline bool operator==(const std::shared_ptr<TObjLink> &l, TObjOptLink *r) {
236 return r == l;
237}
238
240 return NewLink(obj, prev ? prev->shared_from_this() : nullptr);
241}
243 return NewOptLink(obj, opt, prev ? prev->shared_from_this() : nullptr);
244}
245
246
247#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
248#pragma GCC diagnostic pop
249#endif
250
251#endif
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassDefOverride(name, id)
Definition Rtypes.h:341
const Bool_t kIterForward
Definition TCollection.h:42
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
char name[80]
Definition TGX11.cxx:110
bool operator==(TObjOptLink *l, const std::shared_ptr< TObjLink > &r)
Definition TList.h:231
const Bool_t kSortAscending
Definition TList.h:31
const Bool_t kSortDescending
Definition TList.h:32
Collection abstract base class.
Definition TCollection.h:65
Iterator abstract base class.
Definition TIterator.h:30
Iterator of linked list.
Definition TList.h:191
TObject * operator*() const override
Return current object or nullptr.
Definition TList.h:226
Option_t * GetOption() const override
Returns the object option stored in the list.
Definition TList.cxx:1142
std::ptrdiff_t difference_type
Definition TList.h:208
void Reset() override
Reset list iterator.
Definition TList.cxx:1159
const TList * fList
Definition TList.h:196
TObjLinkPtr_t fCursor
Definition TList.h:198
TObjLinkPtr_t fCurCursor
Definition TList.h:197
std::shared_ptr< TObjLink > TObjLinkPtr_t
Definition TList.h:194
const TCollection * GetCollection() const override
Definition TList.h:219
TObject * Next() override
Return next object in the list. Returns 0 when no more objects in list.
Definition TList.cxx:1111
TListIter()
Definition TList.h:202
TIterator & operator=(const TIterator &rhs) override
Overridden assignment operator.
Definition TList.cxx:1077
void SetOption(Option_t *option)
Sets the object option stored in the list.
Definition TList.cxx:1151
Bool_t fStarted
Definition TList.h:200
~TListIter()
Definition TList.h:215
Bool_t operator!=(const TIterator &aIter) const override
This operator compares two TIterator objects.
Definition TList.cxx:1168
std::bidirectional_iterator_tag iterator_category
Definition TList.h:206
Bool_t fDirection
Definition TList.h:199
A doubly linked list.
Definition TList.h:38
Bool_t LnkCompare(const TObjLinkPtr_t &l1, const TObjLinkPtr_t &l2)
Compares the objects stored in the TObjLink objects.
Definition TList.cxx:972
void AddAfter(const TObject *after, TObject *obj) override
Insert object after object after in the list.
Definition TList.cxx:250
TObject * After(const TObject *obj) const override
Returns the object after object obj.
Definition TList.cxx:330
TObjLinkPtr_t NewOptLink(TObject *obj, Option_t *opt, const TObjLinkPtr_t &prev=nullptr)
Return a new TObjOptLink (a TObjLink that also stores the option).
Definition TList.cxx:748
void Clear(Option_t *option="") override
Remove all objects from the list.
Definition TList.cxx:402
TObject ** GetObjectRef(const TObject *obj) const override
Return address of pointer to obj.
Definition TList.cxx:671
virtual TObjLink * LastLink() const
Definition TList.h:105
TList & operator=(const TList &)=delete
TList(const TList &)=delete
virtual ~TList()
Delete the list.
Definition TList.cxx:92
std::weak_ptr< TObjLink > TObjLinkWeakPtr_t
Definition TList.h:44
Bool_t fAscending
cache to speedup sequential calling of Before() and After() functions
Definition TList.h:49
void AddAt(TObject *obj, Int_t idx) override
Insert object at position idx in the list.
Definition TList.cxx:306
TObject * Before(const TObject *obj) const override
Returns the object before object obj.
Definition TList.cxx:371
TObject * FindObject(const char *name) const override
Find an object in this list using its name.
Definition TList.cxx:578
TList(TObject *)
Definition TList.h:73
void RecursiveRemove(TObject *obj) override
Remove object from this collection and recursively remove the object from all other objects (and coll...
Definition TList.cxx:764
TObjLink * FindLink(const TObject *obj, Int_t &idx) const
Returns the TObjLink object that contains object obj.
Definition TList.cxx:628
void Add(TObject *obj) override
Definition TList.h:81
TObject * Remove(TObject *obj) override
Remove object from the list.
Definition TList.cxx:822
void AddLast(TObject *obj) override
Add object at the end of the list.
Definition TList.cxx:152
TObject * Last() const override
Return the last object in the list. Returns 0 when list is empty.
Definition TList.cxx:693
Bool_t IsAscending()
Definition TList.h:108
std::shared_ptr< TObjLink > TObjLinkPtr_t
Definition TList.h:43
TObject * Remove(const TObjLinkPtr_t &lnk)
Definition TList.h:94
TObject * First() const override
Return the first object in the list. Returns 0 when list is empty.
Definition TList.cxx:659
TObjLinkPtr_t fLast
pointer to first entry in linked list
Definition TList.h:47
void InsertAfter(const TObjLinkPtr_t &newlink, const TObjLinkPtr_t &prev)
Insert a new link in the chain.
Definition TList.cxx:1035
TIterator * MakeIterator(Bool_t dir=kIterForward) const override
Return a list iterator.
Definition TList.cxx:722
void AddBefore(const TObject *before, TObject *obj) override
Insert object before object before in the list.
Definition TList.cxx:196
TObjLink * LinkAt(Int_t idx) const
sorting order (when calling Sort() or for TSortedList)
Definition TList.cxx:705
virtual TObjLink * FirstLink() const
Definition TList.h:102
TListIter Iterator_t
Definition TList.h:70
TObjLinkPtr_t NewLink(TObject *obj, const TObjLinkPtr_t &prev=nullptr)
Return a new TObjLink.
Definition TList.cxx:733
TObjLinkPtr_t fFirst
Definition TList.h:46
void Delete(Option_t *option="") override
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:470
virtual void Add(TObject *obj, Option_t *opt)
Definition TList.h:82
TObjLinkWeakPtr_t fCache
pointer to last entry in linked list
Definition TList.h:48
TList()
Definition TList.h:72
TObject * At(Int_t idx) const override
Returns the object at position idx. Returns 0 if idx is out of range.
Definition TList.cxx:357
void RemoveLast() override
Remove the last object of the list.
Definition TList.cxx:909
TObjLinkPtr_t * DoSort(TObjLinkPtr_t *head, Int_t n)
Sort linked list.
Definition TList.cxx:984
void AddFirst(TObject *obj) override
Add object at the beginning of the list.
Definition TList.cxx:100
virtual void Sort(Bool_t order=kSortAscending)
Sort linked list.
Definition TList.cxx:937
Mother of all ROOT objects.
Definition TObject.h:41
virtual Option_t * GetOption() const
Definition TObject.h:139
Sequenceable collection abstract base class.
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:380
const Int_t n
Definition legend1.C:16
TLine l
Definition textangle.C:4