Free Electron
WeakRecordAV.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 __data_WeakRecordAV_h__
8 #define __data_WeakRecordAV_h__
9 
10 namespace fe
11 {
12 
13 /** @brief Non-persistent reference to an instance of a Layout
14 
15  @ingroup data
16 */
17 class FE_DL_EXPORT WeakRecordAV
18 {
19  friend class Scope;
20  friend class RecordArrayAV;
21  public:
22  WeakRecordAV(I32 ignored=0);
23  WeakRecordAV(const RecordAV &other);
24  WeakRecordAV(const WeakRecordAV &other);
25 virtual ~WeakRecordAV(void);
26 
27  operator RecordAV(void);
28 
29  WeakRecordAV &operator=(const WeakRecordAV &other);
30 
31  bool operator==(const WeakRecordAV &other) const;
32  bool operator!=(const WeakRecordAV &other) const;
33 
34  /** @brief Return true if the record points to the
35  original valid state block
36 
37  A record without the ":SN" field can never
38  be confirmed as valid.
39  You may be able access it if you are sure it
40  still exists by other means. */
41  bool isValid(void) const;
42  FE_UWORD idr(void) const;
43  /** Return the Layout. */
44  const sp<LayoutAV> &layout(void) const;
45 
46 
47  /** @brief throw if the record doesn't have
48  a serial number */
49  void demandSerialNumber(void) const;
50 
51  template <class T>
52  T &accessAttribute(FE_UWORD aLocator) const;
53 
54  void *rawAttribute(FE_UWORD aLocator) const;
55 
56  private:
57  /** Return a raw pointer to the Layout.
58  Mainly intended for Accessor for speed. */
59  LayoutAV *rawLayout(void) const;
60  void set(FE_UWORD aArrayIndex, sp<LayoutAV> &aLayout);
61 
62  private:
63 
64  void cacheSerialNumber(void);
65  I32 readSerialNumber(void) const;
66 
67  sp<LayoutAV> m_hpLayout;
68  FE_UWORD m_arrayIndex;
69  I32 m_serialNumber;
70  U32 m_serialLocator;
71 };
72 
73 class WeakRecordAVInfo : public BaseType::Info
74 {
75  public:
76 virtual String print(void *instance);
77 virtual IWORD output(std::ostream &ostrm, void *instance, t_serialMode mode);
78 virtual void input(std::istream &istrm, void *instance, t_serialMode mode);
79 virtual IWORD iosize(void);
80 virtual bool getConstruct(void);
81 virtual void construct(void *instance);
82 virtual void destruct(void *instance);
83 };
84 
85 inline WeakRecordAV::WeakRecordAV(I32):
86  m_arrayIndex(RecordAV::ms_invalidIndex),
87  m_serialNumber(-1),
88  m_serialLocator(0)
89 {
90 }
91 
92 inline WeakRecordAV::WeakRecordAV(const RecordAV& other)
93 {
94  if(other.isValid())
95  {
96  m_hpLayout = other.layout();
97  m_arrayIndex = other.m_arrayIndex;
98 
99  cacheSerialNumber();
100  }
101  else
102  {
103  m_arrayIndex = RecordAV::ms_invalidIndex;
104  m_serialNumber= -1;
105  }
106 }
107 
108 inline WeakRecordAV::WeakRecordAV(const WeakRecordAV &other)
109 {
110  if(other.isValid())
111  {
112  m_hpLayout = other.m_hpLayout;
113  m_arrayIndex = other.m_arrayIndex;
114  m_serialLocator = other.m_serialLocator;
115  m_serialNumber = other.m_serialNumber;
116  }
117  else
118  {
119  m_arrayIndex = RecordAV::ms_invalidIndex;
120  m_serialNumber= -1;
121  }
122 }
123 
124 inline WeakRecordAV::~WeakRecordAV(void)
125 {
126 }
127 
128 inline WeakRecordAV::operator RecordAV(void)
129 {
130  RecordAV record;
131  if(isValid())
132  {
133  record.set(m_arrayIndex, m_hpLayout);
134  }
135  return record;
136 }
137 
138 inline I32 WeakRecordAV::readSerialNumber(void) const
139 {
140 #ifdef FE_AV_FASTITER_ENABLE
141  if(!m_hpLayout->existenceCheck(m_serialLocator, m_arrayIndex))
142  {
143  feLog("WeakRecordAV::readSerialNumber failed %d %d\n",
144  m_serialLocator, m_arrayIndex);
145  return -1;
146  }
147 #endif
148  return accessAttribute<int>(m_serialLocator);
149 }
150 
152 {
153  return m_hpLayout.raw();
154 }
155 
156 inline WeakRecordAV &WeakRecordAV::operator=(const WeakRecordAV &other)
157 {
158  if(this != &other)
159  {
160  m_hpLayout = other.m_hpLayout;
161  m_arrayIndex = other.m_arrayIndex;
162  m_serialLocator = other.m_serialLocator;
163  m_serialNumber = other.m_serialNumber;
164  }
165  return *this;
166 }
167 
168 inline bool WeakRecordAV::operator==(const WeakRecordAV &other) const
169 {
170  if (m_hpLayout != other.m_hpLayout) { return false; }
171  return (m_arrayIndex == other.m_arrayIndex);
172 }
173 
174 inline bool WeakRecordAV::operator!=(const WeakRecordAV &other) const
175 {
176  if (m_hpLayout != other.m_hpLayout) { return true; }
177  return (m_arrayIndex != other.m_arrayIndex);
178 }
179 
180 inline bool WeakRecordAV::isValid(void) const
181 {
182  if(m_arrayIndex != RecordAV::ms_invalidIndex)
183  {
184  if(m_serialNumber<0 || m_serialNumber==readSerialNumber())
185  {
186  return TRUE;
187  }
188 
189  const_cast<WeakRecordAV*>(this)->m_arrayIndex=RecordAV::ms_invalidIndex;
190  const_cast<WeakRecordAV*>(this)->m_serialNumber= -1;
191  }
192 
193  return FALSE;
194 }
195 
196 inline void WeakRecordAV::set(FE_UWORD aArrayIndex, sp<LayoutAV> &aLayout)
197 {
198  m_arrayIndex = aArrayIndex;
199  m_hpLayout = aLayout;
200 
201  // ???? Why were we making this record?
202 #ifdef FE_MAKE_RECORD_FOR_WEAKRECORD
203  RecordAV record;
204  record.set(aArrayIndex, aLayout);
205 #endif
206  cacheSerialNumber();
207 
208  //* reset if there is no serial number
209 #if TRUE
210  if(isValid() && m_serialNumber<0)
211  {
212  feLog("WeakRecordAV::set layout \"%s\" SN %d"
213  " doesn't support \":SN\" attribute -> refusing assignment\n",
214  m_hpLayout->name().c_str(),m_serialNumber);
215  m_hpLayout=NULL;
216  m_arrayIndex=RecordAV::ms_invalidIndex;
217  FEASSERT(0);
218  }
219 #endif
220 }
221 
222 template <class T>
223 inline T &WeakRecordAV::accessAttribute(FE_UWORD aLocator) const
224 {
225 #if 0
226  const FE_UWORD &avIndex = layout()->locatorTable()[aLocator];
227  // TODO: do this without the dynamic cast if it is too slow with (hard cast like the void * below) -- RecordAV too
228  TypeVector<T> &typeVector = dynamic_cast< TypeVector<T>& >(*(layout()->attributeVector()[avIndex]));
229  return typeVector.at(m_arrayIndex);
230 #endif
231  return *(T *)(m_hpLayout->voidAccess(aLocator, m_arrayIndex));
232 }
233 
234 inline void *WeakRecordAV::rawAttribute(FE_UWORD aLocator) const
235 {
236 #if 0
237  const FE_UWORD &avIndex = layout()->locatorTable()[aLocator];
238  sp<BaseTypeVector> &baseVector = layout()->attributeVector()[avIndex];
239  return baseVector->raw_at(m_arrayIndex);
240 #endif
241  return (m_hpLayout->voidAccess(aLocator, m_arrayIndex));
242 }
243 
244 inline FE_UWORD WeakRecordAV::idr(void) const
245 {
246  return m_hpLayout->idr(m_arrayIndex);
247 }
248 
249 inline const sp<LayoutAV> &WeakRecordAV::layout(void) const
250 {
251  return m_hpLayout;
252 }
253 
254 
255 inline void WeakRecordAV::cacheSerialNumber(void)
256 {
257  U32 index = m_hpLayout->serialIndex();
258  m_serialLocator = index;
259  m_serialNumber = readSerialNumber();
260 
261 #if FE_CODEGEN <= FE_DEBUG
262  demandSerialNumber();
263 #endif
264 }
265 
266 } /* namespace */
267 
268 #endif /* __data_WeakRecordAV_h__ */
269 
Homogeneous collection of Records.
Definition: RecordArrayAV.h:32
sp< Layout > layout(void) const
Return the Layout.
Definition: RecordAV.h:174
kernel
Definition: namespace.dox:3
BWORD operator!=(const DualString &s1, const DualString &s2)
Compare two DualString&#39;s (reverse logic)
Definition: DualString.h:229
BWORD operator==(const DualString &s1, const DualString &s2)
Compare two DualString&#39;s.
Definition: DualString.h:208
Automatically reference-counted string container.
Definition: String.h:128
const sp< LayoutAV > & layout(void) const
Return the Layout.
Definition: WeakRecordAV.h:249
bool isValid(void) const
Return true if the record points to the original valid state block.
Definition: WeakRecordAV.h:180
Reference to an instance of a Layout.
Definition: RecordAV.h:25
Intrusive Smart Pointer.
Definition: src/core/ptr.h:53
Layout namespace.
Definition: Scope.h:71
bool isValid(void) const
Return true if the Record points to a valid state block.
Definition: RecordAV.h:195
Record "type" definition.
Definition: LayoutAV.h:30
Non-persistent reference to an instance of a Layout.
Definition: WeakRecordAV.h:17
LayoutAV * rawLayout(void) const
Return a raw pointer to the Layout.
Definition: WeakRecordAV.h:151