ArnLib  4.0.x
Active Registry Network
ArnEvent.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2022 Michael Wiklund.
2 // All rights reserved.
3 // Contact: arnlib@wiklunden.se
4 //
5 // This file is part of the ArnLib - Active Registry Network.
6 // Parts of ArnLib depend on Qt and/or other libraries that have their own
7 // licenses. Usage of these other libraries is subject to their respective
8 // license agreements.
9 //
10 // GNU Lesser General Public License Usage
11 // This file may be used under the terms of the GNU Lesser General Public
12 // License version 2.1 as published by the Free Software Foundation and
13 // appearing in the file LICENSE_LGPL.txt included in the packaging of this
14 // file. In addition, as a special exception, you may use the rights described
15 // in the Nokia Qt LGPL Exception version 1.1, included in the file
16 // LGPL_EXCEPTION.txt in this package.
17 //
18 // GNU General Public License Usage
19 // Alternatively, this file may be used under the terms of the GNU General Public
20 // License version 3.0 as published by the Free Software Foundation and appearing
21 // in the file LICENSE_GPL.txt included in the packaging of this file.
22 //
23 // Other Usage
24 // Alternatively, this file may be used in accordance with the terms and conditions
25 // contained in a signed written agreement between you and Michael Wiklund.
26 //
27 // This program is distributed in the hope that it will be useful, but WITHOUT ANY
28 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
29 // PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
30 //
31 
32 #include <ArnInc/ArnEvent.hpp>
33 #include <ArnLink.hpp>
34 #include <QMutex>
35 
36 
37 ArnEvent::ArnEvent( QEvent::Type type)
38  : QEvent( type)
39  , _target( arnNullptr)
40  , _targetMutex( arnNullptr)
41  , _targetNextPending( arnNullptr)
42  , _targetPendingChain( arnNullptr)
43 {
44  Q_UNUSED(_spare)
45 }
46 
47 
49 {
50  if (_targetMutex) _targetMutex->lock();
51 
52  if (_targetPendingChain) { // If pending chain used, remove this event from it
53  *_targetPendingChain = _targetNextPending;
54  if (_targetNextPending)
55  _targetNextPending->_targetPendingChain = _targetPendingChain;
56  }
57 
58  if (_targetMutex) _targetMutex->unlock();
59 }
60 
61 
62 int ArnEvent::baseType( int setVal)
63 {
64  static int bt = -1;
65 
66  if (bt > 0) return bt;
67 
69  bt = setVal;
70  if (bt < 0)
71  bt = 2022; // Arn default
72  Q_ASSERT_X((bt >= QEvent::User) && (bt + Idx::N <= QEvent::MaxUser), "ArnEvent::baseType()",
73  "Selected base for ArnEvent number is out of boundary for Qt User event");
74 
76  for (int i = 0; i < Idx::N; ++i) {
77  int wantType = bt + i;
78  int gotType = QEvent::registerEventType( wantType);
79  Q_UNUSED(gotType) // Silence warning (in some compilers)
80  Q_ASSERT_X(gotType == wantType, "ArnEvent::baseType()",
81  "Assigning event number for ArnEvent is already taken,"
82  " use ArnEvent::baseType() to set other base event number for ArnEvents.");
83  }
84 
85  return bt;
86 }
87 
88 
89 bool ArnEvent::isArnEvent( int evType)
90 {
91  int bt = baseType();
92 
93  return (evType >= bt) && (evType < bt + Idx::N);
94 }
95 
96 
97 #define TO_IDX_RETVAL(evType) \
98  int retVal = (evType) - baseType(); \
99  retVal = ((retVal >= 0) && (retVal < Idx::N)) ? retVal : Idx::QtEvent;
100 
101 
102 int ArnEvent::toIdx( QEvent::Type type)
103 {
104  TO_IDX_RETVAL(type)
105  return retVal;
106 }
107 
108 
109 int ArnEvent::toIdx() const
110 {
111  TO_IDX_RETVAL(type())
112  return retVal;
113 }
114 
115 
116 QString ArnEvent::toString( QEvent::Type type)
117 {
118  return Idx::txt().getTxtString( toIdx( type)) +
119  "(" + QString::number( type) + ")";
120 }
121 
122 
123 QString ArnEvent::toString() const
124 {
125  return toString( type());
126 }
127 
128 
130 {
131  _target = other->_target;
132  _targetMutex = other->_targetMutex;
133 
134  return this;
135 }
136 
137 
138 void ArnEvent::setTarget( void* target)
139 {
140  _target = target;
141 }
142 
143 
144 void ArnEvent::setTargetPendingChain( ArnEvent** targetPendingChain)
145 {
146  if (_targetMutex) _targetMutex->lock();
147 
148  _targetPendingChain = targetPendingChain;
149 
150  if (_targetPendingChain) { // If pending chain used, link this event to it
151  _targetNextPending = *_targetPendingChain;
152  *_targetPendingChain = this;
153  if (_targetNextPending)
154  _targetNextPending->_targetPendingChain = &_targetNextPending;
155  }
156 
157  if (_targetMutex) _targetMutex->unlock();
158 }
159 
160 
161 void ArnEvent::setTargetMutex( QMutex* targetMutex)
162 {
163  _targetMutex = targetMutex;
164 }
165 
166 
168 {
169  QMutex* targetMutex = _targetMutex;
170  if (targetMutex) targetMutex->lock();
171 
172  if (_targetPendingChain) { // If pending chain used, inhibit all events in it
173  ArnEvent* ev = this;
174  do {
175  ArnEvent* evNext = ev->_targetNextPending;
176  ev->_target = arnNullptr; // Inhibit event, i.e. it will be dropped when delivered
177  ev->_targetMutex = arnNullptr;
178  *ev->_targetPendingChain = arnNullptr;
179  ev->_targetNextPending = arnNullptr;
180  ev = evNext;
181  } while (ev);
182  }
183 
184  if (targetMutex) targetMutex->unlock();
185 }
186 
187 
188 
189 ArnEvValueChange::ArnEvValueChange( int sendId, const QByteArray* valueData, const ArnLinkHandle& handleData)
190  : ArnEvent( type())
191  , _sendId( sendId)
192  , _valueData( valueData)
193  , _handleData( &handleData)
194 {
195  if (_valueData)
196  _valueData = new QByteArray( *valueData);
197  if (_handleData && !_handleData->isNull())
198  _handleData = new ArnLinkHandle( *_handleData);
199  else
200  _handleData = arnNullptr;
201 }
202 
203 
205 {
206  if (_valueData)
207  delete _valueData;
208  if (_handleData)
209  delete _handleData;
210 }
211 
212 
214 {
215  static int evType = baseType() + Idx::ValueChange;
216 
217  return Type( evType);
218 }
219 
220 
222 {
223  return (new ArnEvValueChange( _sendId, _valueData, *_handleData))->copyOpt( this);
224 }
225 
226 
227 ArnEvAtomicOp::ArnEvAtomicOp( int op, const QVariant& arg1, const QVariant& arg2)
228  : ArnEvent( type())
229  , _op( Op::fromInt( op))
230  , _arg1( arg1)
231  , _arg2( arg2)
232 {
233 }
234 
235 
237 {
238 }
239 
240 
241 QEvent::Type ArnEvAtomicOp::type()
242 {
243  static int evType = baseType() + Idx::AtomicOp;
244 
245  return Type( evType);
246 }
247 
248 
250 {
251  return (new ArnEvAtomicOp( _op, _arg1, _arg2))->copyOpt( this);
252 }
253 
254 
255 ArnEvLinkCreate::ArnEvLinkCreate( const QString& path, ArnLink* arnLink, bool isLastLink)
256  : ArnEvent( type())
257  , _path( path)
258  , _arnLink( arnLink)
259  , _isLastLink( isLastLink)
260 {
261 }
262 
263 
264 QEvent::Type ArnEvLinkCreate::type()
265 {
266  static int evType = baseType() + Idx::LinkCreate;
267 
268  return Type( evType);
269 }
270 
271 
273 {
274  return (new ArnEvLinkCreate( _path, _arnLink, _isLastLink))->copyOpt( this);
275 }
276 
277 
278 
279 ArnEvModeChange::ArnEvModeChange( const QString& path, uint linkId, Arn::ObjectMode mode)
280  : ArnEvent( type())
281  , _path( path)
282  , _linkId( linkId)
283  , _mode( mode)
284 {
285 }
286 
287 
288 QEvent::Type ArnEvModeChange::type()
289 {
290  static int evType = baseType() + Idx::ModeChange;
291 
292  return Type( evType);
293 }
294 
295 
297 {
298  return (new ArnEvModeChange( _path, _linkId, _mode))->copyOpt( this);
299 }
300 
301 
302 
303 ArnEvMonitor::ArnEvMonitor( int monEvType, const QByteArray& data, bool isLocal, void* sessionHandler)
304  : ArnEvent( type())
305  , _monEvType( monEvType)
306  , _data( data)
307  , _isLocal( isLocal)
308  , _sessionHandler( sessionHandler)
309 {
310 }
311 
312 
313 QEvent::Type ArnEvMonitor::type()
314 {
315  static int evType = baseType() + Idx::Monitor;
316 
317  return Type( evType);
318 }
319 
320 
322 {
323  return (new ArnEvMonitor( _monEvType, _data, _isLocal, _sessionHandler))->copyOpt( this);
324 }
325 
326 
327 
328 ArnEvRetired::ArnEvRetired( ArnLink* startLink, bool isBelow, bool isGlobal)
329  : ArnEvent( type())
330  , _startLink( startLink)
331  , _isBelow( isBelow)
332  , _isGlobal( isGlobal)
333 {
334 }
335 
336 
337 QEvent::Type ArnEvRetired::type()
338 {
339  static int evType = baseType() + Idx::Retired;
340 
341  return Type( evType);
342 }
343 
344 
346 {
347  return (new ArnEvRetired( _startLink, _isBelow, _isGlobal))->copyOpt( this);
348 }
349 
350 
351 
352 ArnEvZeroRef::ArnEvZeroRef( ArnLink* arnLink)
353  : ArnEvent( type())
354  , _arnLink (arnLink)
355 {
356 }
357 
358 
359 QEvent::Type ArnEvZeroRef::type()
360 {
361  static int evType = baseType() + Idx::ZeroRef;
362 
363  return Type( evType);
364 }
365 
366 
368 {
369  return (new ArnEvZeroRef( _arnLink))->copyOpt( this);
370 }
371 
372 
373 
375  : ArnEvent( type())
376  , _refStep( refStep)
377 {
378 }
379 
380 
382 {
383 }
384 
385 
386 QEvent::Type ArnEvRefChange::type()
387 {
388  static int evType = baseType() + Idx::RefChange;
389 
390  return Type( evType);
391 }
392 
393 
395 {
396  return (new ArnEvRefChange( _refStep))->copyOpt( this);
397 }
virtual ~ArnEvRefChange()
Definition: ArnEvent.cpp:381
static int baseType(int setVal=-1)
Definition: ArnEvent.cpp:62
static QEvent::Type type()
Definition: ArnEvent.cpp:241
const QByteArray * valueData() const
Definition: ArnEvent.hpp:250
static QEvent::Type type()
Definition: ArnEvent.cpp:313
static QEvent::Type type()
Definition: ArnEvent.cpp:359
QString toString() const
Definition: ArnEvent.cpp:123
virtual ArnEvent * makeHeapClone()
Definition: ArnEvent.cpp:367
virtual ArnEvent * makeHeapClone()
Definition: ArnEvent.cpp:221
static QEvent::Type type()
Definition: ArnEvent.cpp:213
ArnEvMonitor(int monEvType, const QByteArray &data, bool isLocal, void *sessionHandler)
Definition: ArnEvent.cpp:303
ArnEvZeroRef(ArnLink *arnLink)
Definition: ArnEvent.cpp:352
Max index.
Definition: ArnEvent.hpp:62
ArnEvValueChange(int sendId, const QByteArray *valueData, const ArnLinkHandle &handleData)
Definition: ArnEvent.cpp:189
virtual ~ArnEvAtomicOp()
Definition: ArnEvent.cpp:236
virtual ~ArnEvValueChange()
Definition: ArnEvent.cpp:204
static bool isArnEvent(int evType)
Definition: ArnEvent.cpp:89
static QEvent::Type type()
Definition: ArnEvent.cpp:288
void setTargetPendingChain(ArnEvent **targetPendingChain=arnNullptr)
Definition: ArnEvent.cpp:144
ArnEvRefChange(int refStep)
Definition: ArnEvent.cpp:374
static QEvent::Type type()
Definition: ArnEvent.cpp:386
void inhibitPendingChain()
Definition: ArnEvent.cpp:167
virtual ArnEvent * makeHeapClone()
Definition: ArnEvent.cpp:321
virtual ArnEvent * makeHeapClone()
Definition: ArnEvent.cpp:345
virtual ~ArnEvent()
Definition: ArnEvent.cpp:48
#define TO_IDX_RETVAL(evType)
Definition: ArnEvent.cpp:97
static QEvent::Type type()
Definition: ArnEvent.cpp:337
int toIdx() const
Definition: ArnEvent.cpp:109
virtual ArnEvent * makeHeapClone()
Definition: ArnEvent.cpp:394
ArnEvRetired(ArnLink *startLink, bool isBelow, bool isGlobal)
Definition: ArnEvent.cpp:328
virtual ArnEvent * makeHeapClone()
Definition: ArnEvent.cpp:249
ArnEvent * copyOpt(const ArnEvent *other)
Definition: ArnEvent.cpp:129
void setTargetMutex(QMutex *targetMutex)
Definition: ArnEvent.cpp:161
void setTarget(void *target)
Definition: ArnEvent.cpp:138
ArnEvAtomicOp(int op, const QVariant &arg1, const QVariant &arg2)
Definition: ArnEvent.cpp:227
ArnEvent(QEvent::Type type)
Definition: ArnEvent.cpp:37
void * target() const
Definition: ArnEvent.hpp:116
ArnEvModeChange(const QString &path, uint linkId, Arn::ObjectMode mode)
Definition: ArnEvent.cpp:279
virtual ArnEvent * makeHeapClone()
Definition: ArnEvent.cpp:296