Free Electron
RecordArrayView.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __datatool_RecordArrayView_h__
8 #define __datatool_RecordArrayView_h__
9 
10 namespace fe
11 {
12 namespace ext
13 {
14 
15 /**************************************************************************//**
16  @brief RecordView Iteration over a RecordArray
17 
18  @ingroup datatool
19 
20 @code
21 RecordArrayView<MyRecordView> rav;
22 sp<RecordArray> spRA=spRG->getArray("my_layout");
23 rav.bind(spRA);
24 
25 for(MyRecordView& rMyView: rav)
26 {
27  feLog("%d\n",rMyView.myI32());
28 }
29 @endcode
30 
31  The RecordView reference obtained by dereferencing an iterator is reused.
32  It will simply bind a new Record when stepping the iterator.
33  A RecordArrayView instance has its own RecordView instance,
34  for at() and operator[](), which is also reused.
35 *//***************************************************************************/
36 template<typename T>
37 class FE_DL_EXPORT RecordArrayView:
38  virtual public Component,
39  public CastableAs< RecordArrayView<T> >
40 {
41  public:
42 
43  class Iterator
44  {
45  public:
46 
47 #if FE_CPLUSPLUS >= 201103L
48  Iterator(RecordArrayView<T>::Iterator&& a_iterator):
49  m_spRecordArray(a_iterator.m_spRecordArray),
50  m_index(a_iterator.m_index)
51  { m_recordView.bind(
52  a_iterator.m_recordView.scope()); }
53 #endif
54 
55  Iterator(void):
56  m_spRecordArray(NULL),
57  m_index(0) {}
58 
59  Iterator(sp<RecordArray> a_spRecordArray,I32 a_index):
60  m_spRecordArray(a_spRecordArray),
61  m_index(a_index)
62  {
63  sp<Scope> spScope(
64  a_spRecordArray->layout()->scope());
65  m_recordView.bind(spScope);
66  }
67 
68  Iterator& operator=(const Iterator& other) const
69  { m_spRecordArray=other.m_spRecordArray;
70  m_index=other.m_index;
71  m_recordView.bind(
72  m_spRecordArray->layout()->scope()); }
73 
74  bool operator==(const Iterator& other) const
75  { return m_spRecordArray==other.m_spRecordArray &&
76  m_index==other.m_index; }
77 
78  bool operator!=(const Iterator& other) const
79  { return m_spRecordArray!=other.m_spRecordArray ||
80  m_index!=other.m_index; }
81 
82  T& operator*(void)
83  { m_recordView.setWeakRecord(
84  m_spRecordArray->getWeakRecord(m_index));
85  return m_recordView; }
86 
87  const Iterator& operator++(void)
88  {
89 #if FE_CODEGEN<=FE_DEBUG
90  if(m_index>=m_spRecordArray->length())
91  {
92  feX("RecordArrayView::Iterator::operator++"
93  "incrementing past end");
94  }
95 #endif
96  ++m_index;
97  return *this;
98  }
99 
100  const Iterator operator++(int)
101  {
102 #if FE_CODEGEN<=FE_DEBUG
103  if(m_index>=m_spRecordArray->length())
104  {
105  feX("RecordArrayView::Iterator::operator++(int)"
106  "incrementing past end");
107  }
108 #endif
109  return Iterator(m_spRecordArray,m_index++);
110  }
111 
112  const Iterator& operator--(void)
113  {
114 #if FE_CODEGEN<=FE_DEBUG
115  if(m_index<=0)
116  {
117  feX("RecordArrayView::Iterator::operator--"
118  "decrementing ahead of begin");
119  }
120 #endif
121  --m_index;
122  return *this;
123  }
124 
125  const Iterator operator--(int)
126  {
127 #if FE_CODEGEN<=FE_DEBUG
128  if(m_index<=0)
129  {
130  feX("RecordArrayView::Iterator::operator--(int)"
131  "decrementing ahead of begin");
132  }
133 #endif
134  return Iterator(m_spRecordArray,m_index--);
135  }
136 
137  I32 index(void) const { return m_index; }
138 
139  private:
140  sp<RecordArray> m_spRecordArray;
141  I32 m_index;
142 
143  T m_recordView;
144  };
145 
146  /** @brief Associate with a specific RecordArray
147 
148  Access a RecordView using an Iterator or
149  the bracket operator.
150  The RecordView will bind to an indexed Record
151  in the RecordArray.
152 
153  Binding to a RecordArray will also bind to its Scope. */
154  void bind(const sp<RecordArray>& rspRecordArray)
155  {
156  m_spRecordArray=rspRecordArray;
157  sp<Scope> spScope=
158  m_spRecordArray->layout()->scope();
159  bind(spScope);
160  }
161 
162  /// @brief Check if bound to a non-zero length RecordArray
163  BWORD isBound(void) const
164  {
165  return m_spRecordArray.isValid() &&
166  m_spRecordArray->length()>0;
167  }
168 
169  /** @brief Associate with a specific Scope
170 
171  This will populate the functors in the local
172  RecordView instance.
173  This can be useful for makign Accessor check() calls
174  before accessing individual records. */
175  void bind(sp<Scope>& rspScope)
176  { m_recordView.bind(rspScope); }
177 
178  /// @brief Return the bound Scope
180  { return m_recordView.scope(); }
181 
182  /// @brief Return an Iterator pointing at the first index
183  Iterator begin(void) const
184  { return Iterator(m_spRecordArray,0); }
185 
186  /// @brief Return an invalid Iterator indicating no more
187  Iterator end(void) const
188  { return Iterator(m_spRecordArray,
189  m_spRecordArray->length()); }
190 
191  /** @brief Return a RecordView bound to a Record
192 
193  This Record is obtained from the RecordArray by index.
194  No bounds checking is done.
195 
196  Since this RecordView instance can be reused,
197  it should only be used for immediate access.
198  */
199  T& operator[](U32 a_index)
200  {
201  FEASSERT(m_spRecordArray.isValid());
202  FEASSERT(a_index<m_spRecordArray->length());
203  m_recordView.setWeakRecord(
204  m_spRecordArray->getWeakRecord(a_index));
205  return m_recordView;
206  }
207 
208  /** @brief Return a RecordView potentially bound to a Record
209 
210  This Record is obtained from the RecordArray by index.
211  If the index goes beyond the length of the RecordArray,
212  the RecordView is unbound.
213 
214  Since this RecordView instance can be reused,
215  it should only be used for immediate access.
216  */
217  T& at(U32 a_index)
218  {
219  if(m_spRecordArray.isValid() &&
220  a_index<m_spRecordArray->length())
221  {
222  m_recordView.setWeakRecord(
223  m_spRecordArray->getWeakRecord(a_index));
224  }
225  else
226  {
227  m_recordView.unbind();
228  }
229  return m_recordView;
230  }
231 
232  /** @brief Return the contained RecordView
233 
234  This RecordView is the same RecordView returned by
235  at() and the bracket operator.
236 
237  It is mainly intended to be used for check() calls
238  on its functors.
239  This can be done without being bound to a RecordArray
240  if a Scope has been bound.
241 
242  Since this RecordView instance can be reused,
243  it should only be used for immediate access.
244  */
245  T& recordView(void) { return m_recordView; }
246 
247  private:
248  sp<RecordArray> m_spRecordArray;
249  T m_recordView;
250 };
251 
252 } /* namespace ext */
253 } /* namespace fe */
254 
255 #endif /* __datatool_RecordArrayView_h__ */
void bind(sp< Scope > &rspScope)
Associate with a specific Scope.
Definition: RecordArrayView.h:175
T & recordView(void)
Return the contained RecordView.
Definition: RecordArrayView.h:245
T & at(U32 a_index)
Return a RecordView potentially bound to a Record.
Definition: RecordArrayView.h:217
kernel
Definition: namespace.dox:3
sp< Scope > scope(void)
Return the bound Scope.
Definition: RecordArrayView.h:179
Iterator begin(void) const
Return an Iterator pointing at the first index.
Definition: RecordArrayView.h:183
void bind(const sp< RecordArray > &rspRecordArray)
Associate with a specific RecordArray.
Definition: RecordArrayView.h:154
BWORD operator!=(const DualString &s1, const DualString &s2)
Compare two DualString&#39;s (reverse logic)
Definition: DualString.h:229
RecordView Iteration over a RecordArray.
Definition: RecordArrayView.h:37
BWORD operator==(const DualString &s1, const DualString &s2)
Compare two DualString&#39;s.
Definition: DualString.h:208
BWORD isBound(void) const
Check if bound to a non-zero length RecordArray.
Definition: RecordArrayView.h:163
Iterator end(void) const
Return an invalid Iterator indicating no more.
Definition: RecordArrayView.h:187
Base for all interfacable components.
Definition: Component.h:20
void setWeakRecord(const WeakRecord weakRecord)
Associate with a specific WeakRecord.
Definition: RecordView.h:427
Intrusive Smart Pointer.
Definition: src/core/ptr.h:53
T & operator[](U32 a_index)
Return a RecordView bound to a Record.
Definition: RecordArrayView.h:199
Per-class participation non-RTTI fallback dynamic casting mechanism.
Definition: Castable.h:192