MidlePayQml 0.0.4
MiddlePay QML module
carray.hpp
1 #pragma once
2 
3 #include <QByteArray>
4 #include <QDataStream>
5 #include <QJsonValue>
6 #include <QJsonArray>
7 #include <QJsonObject>
8 #include<QIODevice>
9 #include <QCryptographicHash>
10 #include "qbigint.hpp"
11 
12 
13 #include <QtCore/QtGlobal>
14 
15 #if defined(WINDOWS_QBLOCK)
16 # define QBLOCK_EXPORT Q_DECL_EXPORT
17 #else
18 #define QBLOCK_EXPORT Q_DECL_IMPORT
19 #endif
20 
21 namespace qiota{
22 namespace qblocks{
27 class c_array : public QByteArray
28 {
29 public:
34  c_array(const QByteArray & var):QByteArray(var.constData(),var.size()){};
38  c_array(const QJsonValue& val);
44  QString toHexString(void)const;
53  friend QDataStream & operator << (QDataStream &out, const c_array & obj);
62  template<class obj_type> void append(const obj_type& obj)
63  {
65  buffer.setByteOrder(QDataStream::LittleEndian);
66  buffer<<obj;
67  }
71  template<QCryptographicHash::Algorithm hashty> c_array hash(void)const
72  {
73  return c_array(QCryptographicHash::hash(*this,hashty));
74  }
79  template<class obj_type> void from_object(const obj_type& obj)
80  {
82  buffer.setByteOrder(QDataStream::LittleEndian);
83  obj.serialize(buffer);
84  }
90  template<class obj_type> obj_type to_object(void)
91  {
92  auto buffer=QDataStream(this,QIODevice::ReadOnly);
93  buffer.setByteOrder(QDataStream::LittleEndian);
94  return obj_type(buffer);
95  }
96 
97 
98 
99 };
100 
104 template<typename max_lenght>
105 class fl_array : public c_array
106 {
107 public:
112  friend QDataStream & operator << (QDataStream &out, const fl_array & obj)
113  {
114 
115  out<<static_cast<max_lenght>(obj.size());
116  out<<static_cast<c_array>(obj);
117  return out;
118 
119  }
124  {
125  max_lenght size;
126  in>>size;
127  obj.resize(size);
128  auto var=static_cast<c_array*>(&obj);
129  in>>(*var);
130 
131  return in;
132  }
133 
134 };
135 
144 
147 
148 
165 
170 
175 
180 
184 template<class T> using pvector=std::vector<std::shared_ptr<T>> ;
185 
190 template<class T>
192 {
193  pvector<const T> var;
194  for(const auto& v:val)var.push_back(T::from_(v));
195  return var;
196 }
200 template<class type_type> type_type get_type(const QJsonValue& val)
201 {
202  return ((type_type)val.toObject()["type"].toInt());
203 }
207 template<class type_type> type_type get_type(QDataStream & val)
208 {
209  type_type type_;
210  val>>type_;
211  return type_;
212 }
213 
217 template<class size_type,class obj_type> void serialize_list(QDataStream &out,
218  const pvector<const obj_type> & ptr_vector)
219 {
220  out<<static_cast<size_type>(ptr_vector.size());
221  for(const auto& v: ptr_vector)v->serialize(out);
222 }
226 template<class size_type,class obj_type> pvector<const obj_type> deserialize_list(QDataStream &in)
227 {
228  pvector<const obj_type> ptr_vector;
229  size_type length_;
230  in>>length_;
231  for(auto i=0;i<length_;i++)
232  {
233  ptr_vector.push_back(obj_type::template from_<QDataStream>(in));
234  }
235  return ptr_vector;
236 }
241 template<class obj_type> void order_list(pvector<obj_type> &ptr_vector)
242 {
243  std::sort(ptr_vector.begin(), ptr_vector.end(), [](auto a, auto b)
244  {
245  return *a < *b;
246  });
247 }
248 
249 };
250 
251 };
252 
T begin(T... args)
Byte Array that takes care of storing the objects in serialized form.
Definition: carray.hpp:28
friend QDataStream & operator>>(QDataStream &in, c_array &obj)
Read from datastream to the obj.
static c_array fromHexString(QString hash)
The data from an "0x..." hex encoded string.
friend QDataStream & operator<<(QDataStream &out, const c_array &obj)
Write to the data stream the obj data.
c_array(const QByteArray &var)
Copy constructor from ByteArray.
Definition: carray.hpp:34
void append(const obj_type &obj)
Append obj data to the end. For objects that implement QDataStream & operator <<.
Definition: carray.hpp:62
c_array hash(void) const
Get the hash of the data.
Definition: carray.hpp:71
obj_type to_object(void)
Return Object from serialized data. Objects that implement the constructor(QDataStream &in)
Definition: carray.hpp:90
void from_object(const obj_type &obj)
Append obj data. Objects that have the serialize(QDataStream) function.
Definition: carray.hpp:79
c_array(const QJsonValue &val)
Constructor from a json value hex string.
QString toHexString(void) const
The "0x..." hex encoded string of the data.
Byte Array with fixed length.
Definition: carray.hpp:106
friend QDataStream & operator<<(QDataStream &out, const fl_array &obj)
Write to the data stream the obj data prepending the obj size as max_lenght type.
Definition: carray.hpp:112
friend QDataStream & operator>>(QDataStream &in, fl_array &obj)
Read from datastream to the obj. The size obj the object is read from the datastream.
Definition: carray.hpp:123
T end(T... args)
void order_list(pvector< obj_type > &ptr_vector)
Order a Container of shared pointer to objects Normally ordered by type.
Definition: carray.hpp:241
type_type get_type(const QJsonValue &val)
Definition: carray.hpp:200
pvector< const obj_type > deserialize_list(QDataStream &in)
Definition: carray.hpp:226
pvector< const T > get_T(const QJsonArray &val)
Definition: carray.hpp:191
void serialize_list(QDataStream &out, const pvector< const obj_type > &ptr_vector)
append to the datastream the serialized form of objects in a container
Definition: carray.hpp:217
Definition: qbigint.hpp:7
T push_back(T... args)
const char * constData() const const
void resize(qsizetype newSize, char c)
qsizetype size() const const
QByteArray hash(QByteArrayView data, QCryptographicHash::Algorithm method)
QJsonObject toObject() const const
T size(T... args)
T sort(T... args)