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