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
74 TList(TObject *) R__DEPRECATED(6, 34, "The argument is ignored. Use the default constructor TList::TList().") : fAscending(kTRUE) { } // for backward compatibility, don't use
75
76 virtual ~TList();
77 void Clear(Option_t *option="") override;
78 void Delete(Option_t *option="") override;
79 TObject *FindObject(const char *name) const override;
80 TObject *FindObject(const TObject *obj) const override;
81 TIterator *MakeIterator(Bool_t dir = kIterForward) const override;
82
83 void Add(TObject *obj) override { AddLast(obj); }
84 virtual void Add(TObject *obj, Option_t *opt) { AddLast(obj, opt); }
85 void AddFirst(TObject *obj) override;
86 virtual void AddFirst(TObject *obj, Option_t *opt);
87 void AddLast(TObject *obj) override;
88 virtual void AddLast(TObject *obj, Option_t *opt);
89 void AddAt(TObject *obj, Int_t idx) override;
90 void AddAfter(const TObject *after, TObject *obj) override;
91 virtual void AddAfter(TObjLink *after, TObject *obj);
92 void AddBefore(const TObject *before, TObject *obj) override;
93 virtual void AddBefore(TObjLink *before, TObject *obj);
94 TObject *Remove(TObject *obj) override;
95 virtual TObject *Remove(TObjLink *lnk);
96 TObject *Remove(const TObjLinkPtr_t &lnk) { return Remove(lnk.get()); }
97 void RemoveLast() override;
98 void RecursiveRemove(TObject *obj) override;
99
100 TObject *At(Int_t idx) const override;
101 TObject *After(const TObject *obj) const override;
102 TObject *Before(const TObject *obj) const override;
103 TObject *First() const override;
104 virtual TObjLink *FirstLink() const { return fFirst.get(); }
105 TObject **GetObjectRef(const TObject *obj) const override;
106 TObject *Last() const override;
107 virtual TObjLink *LastLink() const { return fLast.get(); }
108
109 virtual void Sort(Bool_t order = kSortAscending);
111
112 ClassDefOverride(TList,5) //Doubly linked list
113};
114
115
116//////////////////////////////////////////////////////////////////////////
117// //
118// TObjLink //
119// //
120// Wrapper around a TObject so it can be stored in a TList. //
121// //
122//////////////////////////////////////////////////////////////////////////
123class TObjLink : public std::enable_shared_from_this<TObjLink> {
124
125friend class TList;
126
127private:
128 using TObjLinkPtr_t = std::shared_ptr<TObjLink>;
129 using TObjLinkWeakPtr_t = std::weak_ptr<TObjLink>;
130
133
134 TObject *fObject; // should be atomic ...
135
136 TObjLink(const TObjLink&) = delete;
137 TObjLink& operator=(const TObjLink&) = delete;
138 TObjLink() = delete;
139
140public:
141
142 TObjLink(TObject *obj) : fObject(obj) { }
143 virtual ~TObjLink() { }
144
145 TObject *GetObject() const { return fObject; }
146 TObject **GetObjectRef() { return &fObject; }
147 void SetObject(TObject *obj) { fObject = obj; }
148 virtual Option_t *GetAddOption() const { return ""; }
149 virtual Option_t *GetOption() const { return fObject->GetOption(); }
150 virtual void SetOption(Option_t *) { }
151 TObjLink *Next() { return fNext.get(); }
152 TObjLink *Prev() { return fPrev.lock().get(); }
154 TObjLinkPtr_t PrevSP() { return fPrev.lock(); }
155};
156
157
158//////////////////////////////////////////////////////////////////////////
159// //
160// TObjOptLink //
161// //
162// Wrapper around a TObject so it can be stored in a TList including //
163// an option string. //
164// //
165//////////////////////////////////////////////////////////////////////////
166class TObjOptLink : public TObjLink {
167
168private:
170
171public:
172 TObjOptLink(TObject *obj, Option_t *opt) : TObjLink(obj), fOption(opt) { }
174 Option_t *GetAddOption() const override { return fOption.Data(); }
175 Option_t *GetOption() const override { return fOption.Data(); }
176 void SetOption(Option_t *option) override { fOption = option; }
177};
178
179
180// Preventing warnings with -Weffc++ in GCC since it is a false positive for the TListIter destructor.
181#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
182#pragma GCC diagnostic push
183#pragma GCC diagnostic ignored "-Weffc++"
184#endif
185
186//////////////////////////////////////////////////////////////////////////
187// //
188// TListIter //
189// //
190// Iterator of linked list. //
191// //
192//////////////////////////////////////////////////////////////////////////
193class TListIter : public TIterator {
194
195protected:
196 using TObjLinkPtr_t = std::shared_ptr<TObjLink>;
197
198 const TList *fList; //list being iterated
199 TObjLinkPtr_t fCurCursor; //current position in list
200 TObjLinkPtr_t fCursor; //next position in list
201 Bool_t fDirection; //iteration direction
202 Bool_t fStarted; //iteration started
203
205 fStarted(kFALSE) { }
206
207public:
208 using iterator_category = std::bidirectional_iterator_tag;
210 using difference_type = std::ptrdiff_t;
211 using pointer = TObject **;
212 using const_pointer = const TObject **;
213 using reference = const TObject *&;
214
215 TListIter(const TList *l, Bool_t dir = kIterForward);
216 TListIter(const TListIter &iter);
218 TIterator &operator=(const TIterator &rhs) override;
219 TListIter &operator=(const TListIter &rhs);
220
221 const TCollection *GetCollection() const override { return fList; }
222 Option_t *GetOption() const override;
224 TObject *Next() override;
225 void Reset() override;
226 Bool_t operator!=(const TIterator &aIter) const override;
227 Bool_t operator!=(const TListIter &aIter) const;
228 TObject *operator*() const override { return (fCurCursor ? fCurCursor->GetObject() : nullptr); }
229
230 ClassDefOverride(TListIter,0) //Linked list iterator
231};
232
233inline bool operator==(TObjOptLink *l, const std::shared_ptr<TObjLink> &r) {
234 return l == r.get();
235}
236
237inline bool operator==(const std::shared_ptr<TObjLink> &l, TObjOptLink *r) {
238 return r == l;
239}
240
242 return NewLink(obj, prev ? prev->shared_from_this() : nullptr);
243}
245 return NewOptLink(obj, opt, prev ? prev->shared_from_this() : nullptr);
246}
247
248
249#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
250#pragma GCC diagnostic pop
251#endif
252
253#endif
#define R__DEPRECATED(MAJOR, MINOR, REASON)
Definition RConfig.hxx:504
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
const char Option_t
Definition RtypesCore.h:66
#define ClassDefOverride(name, id)
Definition Rtypes.h:346
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:233
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:193
TObject * operator*() const override
Return current object or nullptr.
Definition TList.h:228
Option_t * GetOption() const override
Returns the object option stored in the list.
Definition TList.cxx:1140
std::ptrdiff_t difference_type
Definition TList.h:210
void Reset() override
Reset list iterator.
Definition TList.cxx:1157
const TList * fList
Definition TList.h:198
TObjLinkPtr_t fCursor
Definition TList.h:200
TObjLinkPtr_t fCurCursor
Definition TList.h:199
std::shared_ptr< TObjLink > TObjLinkPtr_t
Definition TList.h:196
const TCollection * GetCollection() const override
Definition TList.h:221
TObject * Next() override
Return next object in the list. Returns 0 when no more objects in list.
Definition TList.cxx:1109
TListIter()
Definition TList.h:204
TIterator & operator=(const TIterator &rhs) override
Overridden assignment operator.
Definition TList.cxx:1075
void SetOption(Option_t *option)
Sets the object option stored in the list.
Definition TList.cxx:1149
Bool_t fStarted
Definition TList.h:202
~TListIter()
Definition TList.h:217
Bool_t operator!=(const TIterator &aIter) const override
This operator compares two TIterator objects.
Definition TList.cxx:1166
std::bidirectional_iterator_tag iterator_category
Definition TList.h:208
Bool_t fDirection
Definition TList.h:201
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:970
void AddAfter(const TObject *after, TObject *obj) override
Insert object after object after in the list.
Definition TList.cxx:248
TObject * After(const TObject *obj) const override
Returns the object after object obj.
Definition TList.cxx:328
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:746
void Clear(Option_t *option="") override
Remove all objects from the list.
Definition TList.cxx:400
TObject ** GetObjectRef(const TObject *obj) const override
Return address of pointer to obj.
Definition TList.cxx:669
virtual TObjLink * LastLink() const
Definition TList.h:107
TList & operator=(const TList &)=delete
TList(const TList &)=delete
virtual ~TList()
Delete the list.
Definition TList.cxx:90
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:304
TObject * Before(const TObject *obj) const override
Returns the object before object obj.
Definition TList.cxx:369
TObject * FindObject(const char *name) const override
Find an object in this list using its name.
Definition TList.cxx:576
void RecursiveRemove(TObject *obj) override
Remove object from this collection and recursively remove the object from all other objects (and coll...
Definition TList.cxx:762
TObjLink * FindLink(const TObject *obj, Int_t &idx) const
Returns the TObjLink object that contains object obj.
Definition TList.cxx:626
void Add(TObject *obj) override
Definition TList.h:83
TObject * Remove(TObject *obj) override
Remove object from the list.
Definition TList.cxx:820
void AddLast(TObject *obj) override
Add object at the end of the list.
Definition TList.cxx:150
TObject * Last() const override
Return the last object in the list. Returns 0 when list is empty.
Definition TList.cxx:691
Bool_t IsAscending()
Definition TList.h:110
std::shared_ptr< TObjLink > TObjLinkPtr_t
Definition TList.h:43
TObject * Remove(const TObjLinkPtr_t &lnk)
Definition TList.h:96
TObject * First() const override
Return the first object in the list. Returns 0 when list is empty.
Definition TList.cxx:657
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:1033
TIterator * MakeIterator(Bool_t dir=kIterForward) const override
Return a list iterator.
Definition TList.cxx:720
void AddBefore(const TObject *before, TObject *obj) override
Insert object before object before in the list.
Definition TList.cxx:194
TObjLink * LinkAt(Int_t idx) const
sorting order (when calling Sort() or for TSortedList)
Definition TList.cxx:703
virtual TObjLink * FirstLink() const
Definition TList.h:104
TListIter Iterator_t
Definition TList.h:70
TObjLinkPtr_t NewLink(TObject *obj, const TObjLinkPtr_t &prev=nullptr)
Return a new TObjLink.
Definition TList.cxx:731
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:468
virtual void Add(TObject *obj, Option_t *opt)
Definition TList.h:84
TObjLinkWeakPtr_t fCache
pointer to last entry in linked list
Definition TList.h:48
TList(TObject *) R__DEPRECATED(6
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:355
void RemoveLast() override
Remove the last object of the list.
Definition TList.cxx:907
TObjLinkPtr_t * DoSort(TObjLinkPtr_t *head, Int_t n)
Sort linked list.
Definition TList.cxx:982
void AddFirst(TObject *obj) override
Add object at the beginning of the list.
Definition TList.cxx:98
virtual void Sort(Bool_t order=kSortAscending)
Sort linked list.
Definition TList.cxx:935
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:376
const Int_t n
Definition legend1.C:16
TLine l
Definition textangle.C:4