Accelerated Computation Engine
eabstractanalytic_block.h
1 #ifndef EABSTRACTANALYTIC_BLOCK_H
2 #define EABSTRACTANALYTIC_BLOCK_H
3 #include "eabstractanalytic.h"
4 #include "eexception.h"
5 #include "edebug.h"
6 //
7 
8 
9 
17 class EAbstractAnalytic::Block : public QObject
18 {
19  Q_OBJECT
20 public:
21  friend EDebug& operator<<(EDebug&, const EAbstractAnalytic::Block*const);
22  static int extractIndex(const QByteArray& data);
27  explicit Block() = default;
28  explicit Block(int index);
29  int index() const;
30  QByteArray toBytes() const;
31  template<class T> const T* cast() const;
32  void fromBytes(const QByteArray& data);
33  template<class T> T* cast();
34 protected:
35  virtual void write(QDataStream& stream) const;
36  virtual void read(QDataStream& stream);
37 private:
41  int _index {-1};
42 };
43 
44 
45 
46 
47 
48 
57 template<class T> const T* EAbstractAnalytic::Block::cast() const
58 {
59  // Use qt object cast to cast this object to the given template type as read only.
60  // If the cast fails then throw an exception, else return the cast read only
61  // pointer.
62  const T* ret {qobject_cast<const T*>(this)};
63  if ( !ret )
64  {
65  E_MAKE_EXCEPTION(e);
66  e.setTitle(tr("Logic Error"));
67  e.setDetails(tr("Cannot convert abstract analytic block to given type."));
68  throw e;
69  }
70  return ret;
71 }
72 
73 
74 
75 
76 
77 
86 template<class T> T* EAbstractAnalytic::Block::cast()
87 {
88  // Use qt object cast to cast this object to the given template type. If the cast
89  // fails then throw an exception, else return the cast pointer.
90  T* ret {qobject_cast<T*>(this)};
91  if ( !ret )
92  {
93  E_MAKE_EXCEPTION(e);
94  e.setTitle(tr("Logic Error"));
95  e.setDetails(tr("Cannot convert abstract analytic block to given type."));
96  throw e;
97  }
98  return ret;
99 }
100 
101 
102 
103 #endif
virtual void read(QDataStream &stream)
Definition: eabstractanalytic_block.cpp:166
virtual void write(QDataStream &stream) const
Definition: eabstractanalytic_block.cpp:147
QByteArray toBytes() const
Definition: eabstractanalytic_block.cpp:84
void fromBytes(const QByteArray &data)
Definition: eabstractanalytic_block.cpp:117
static int extractIndex(const QByteArray &data)
Definition: eabstractanalytic_block.cpp:21
Definition: edebug.h:38
friend EDebug & operator<<(EDebug &, const EAbstractAnalytic::Block *const)
Definition: common.cpp:268
int index() const
Definition: eabstractanalytic_block.cpp:66
Definition: eabstractanalytic_block.h:17
const T * cast() const
Definition: eabstractanalytic_block.h:57