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 virtual void Clear(Option_t *option="");
76 virtual void Delete(Option_t *option="");
77 virtual TObject *FindObject(const char *name) const;
78 virtual TObject *FindObject(const TObject *obj) const;
79 virtual TIterator *MakeIterator(Bool_t dir = kIterForward) const;
80
81 virtual void Add(TObject *obj) { AddLast(obj); }
82 virtual void Add(TObject *obj, Option_t *opt) { AddLast(obj, opt); }
83 virtual void AddFirst(TObject *obj);
84 virtual void AddFirst(TObject *obj, Option_t *opt);
85 virtual void AddLast(TObject *obj);
86 virtual void AddLast(TObject *obj, Option_t *opt);
87 virtual void AddAt(TObject *obj, Int_t idx);
88 virtual void AddAfter(const TObject *after, TObject *obj);
89 virtual void AddAfter(TObjLink *after, TObject *obj);
90 virtual void AddBefore(const TObject *before, TObject *obj);
91 virtual void AddBefore(TObjLink *before, TObject *obj);
92 virtual TObject *Remove(TObject *obj);
93 virtual TObject *Remove(TObjLink *lnk);
94 TObject *Remove(const TObjLinkPtr_t &lnk) { return Remove(lnk.get()); }
95 virtual void RemoveLast();
96 virtual void RecursiveRemove(TObject *obj);
97
98 virtual TObject *At(Int_t idx) const;
99 virtual TObject *After(const TObject *obj) const;
100 virtual TObject *Before(const TObject *obj) const;
101 virtual TObject *First() const;
102 virtual TObjLink *FirstLink() const { return fFirst.get(); }
103 virtual TObject **GetObjectRef(const TObject *obj) const;
104 virtual TObject *Last() const;
105 virtual TObjLink *LastLink() const { return fLast.get(); }
106
107 virtual void Sort(Bool_t order = kSortAscending);
109
110 ClassDef(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 { return fOption.Data(); }
173 Option_t *GetOption() const { return fOption.Data(); }
174 void SetOption(Option_t *option) { 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);
217 TListIter &operator=(const TListIter &rhs);
218
219 const TCollection *GetCollection() const { return fList; }
220 Option_t *GetOption() const;
221 void SetOption(Option_t *option);
222 TObject *Next();
223 void Reset();
224 Bool_t operator!=(const TIterator &aIter) const;
225 Bool_t operator!=(const TListIter &aIter) const;
226 TObject *operator*() const { return (fCurCursor ? fCurCursor->GetObject() : nullptr); }
227
228 ClassDef(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
ROOT::R::TRInterface & r
Definition Object.C:4
int Int_t
Definition RtypesCore.h:45
const Bool_t kFALSE
Definition RtypesCore.h:101
bool Bool_t
Definition RtypesCore.h:63
const Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassDef(name, id)
Definition Rtypes.h:325
const Bool_t kIterForward
Definition TCollection.h:42
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 * Next()
Return next object in the list. Returns 0 when no more objects in list.
Definition TList.cxx:1111
std::ptrdiff_t difference_type
Definition TList.h:208
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
void Reset()
Reset list iterator.
Definition TList.cxx:1159
TListIter()
Definition TList.h:202
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
const TCollection * GetCollection() const
Definition TList.h:219
std::bidirectional_iterator_tag iterator_category
Definition TList.h:206
Bool_t operator!=(const TIterator &aIter) const
This operator compares two TIterator objects.
Definition TList.cxx:1168
TObject * operator*() const
Return current object or nullptr.
Definition TList.h:226
TIterator & operator=(const TIterator &rhs)
Overridden assignment operator.
Definition TList.cxx:1077
Option_t * GetOption() const
Returns the object option stored in the list.
Definition TList.cxx:1142
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
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
virtual void Add(TObject *obj)
Definition TList.h:81
virtual TObject * After(const TObject *obj) const
Returns the object after object obj.
Definition TList.cxx:330
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
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition TList.cxx:822
virtual TObject ** GetObjectRef(const TObject *obj) const
Return address of pointer to obj.
Definition TList.cxx:671
virtual void AddAt(TObject *obj, Int_t idx)
Insert object at position idx in the list.
Definition TList.cxx:306
TList(TObject *)
Definition TList.h:73
TObjLink * FindLink(const TObject *obj, Int_t &idx) const
Returns the TObjLink object that contains object obj.
Definition TList.cxx:628
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
virtual void AddFirst(TObject *obj)
Add object at the beginning of the list.
Definition TList.cxx:100
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
virtual void RemoveLast()
Remove the last object of the list.
Definition TList.cxx:909
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
Definition TList.cxx:578
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
virtual TObject * At(Int_t idx) const
Returns the object at position idx. Returns 0 if idx is out of range.
Definition TList.cxx:357
TObjLinkPtr_t NewLink(TObject *obj, const TObjLinkPtr_t &prev=nullptr)
Return a new TObjLink.
Definition TList.cxx:733
virtual TObject * Last() const
Return the last object in the list. Returns 0 when list is empty.
Definition TList.cxx:693
virtual void AddAfter(const TObject *after, TObject *obj)
Insert object after object after in the list.
Definition TList.cxx:250
TObjLinkPtr_t fFirst
Definition TList.h:46
virtual void RecursiveRemove(TObject *obj)
Remove object from this collection and recursively remove the object from all other objects (and coll...
Definition TList.cxx:764
virtual void Add(TObject *obj, Option_t *opt)
Definition TList.h:82
virtual void AddBefore(const TObject *before, TObject *obj)
Insert object before object before in the list.
Definition TList.cxx:196
virtual TIterator * MakeIterator(Bool_t dir=kIterForward) const
Return a list iterator.
Definition TList.cxx:722
TObjLinkWeakPtr_t fCache
pointer to last entry in linked list
Definition TList.h:48
virtual TObject * Before(const TObject *obj) const
Returns the object before object obj.
Definition TList.cxx:371
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:470
virtual TObject * First() const
Return the first object in the list. Returns 0 when list is empty.
Definition TList.cxx:659
virtual void AddLast(TObject *obj)
Add object at the end of the list.
Definition TList.cxx:152
TList()
Definition TList.h:72
TObjLinkPtr_t * DoSort(TObjLinkPtr_t *head, Int_t n)
Sort linked list.
Definition TList.cxx:984
virtual void Clear(Option_t *option="")
Remove all objects from the list.
Definition TList.cxx:402
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:136
const char * Data() const
Definition TString.h:369
const Int_t n
Definition legend1.C:16
auto * l
Definition textangle.C:4