QtIotaBlock 0.4.1
Library with block types on IOTA
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Properties Friends Pages
carray.hpp
1 #pragma once
2 
3 #include "qbigint.hpp"
4 #include <QByteArray>
5 #include <QCryptographicHash>
6 #include <QDataStream>
7 #include <QIODevice>
8 #include <QJsonArray>
9 #include <QJsonObject>
10 #include <QJsonValue>
11 #include <set>
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 {
23 namespace qblocks
24 {
29 class c_array : public QByteArray
30 {
31  public:
36  c_array(const QByteArray &var) : QByteArray(var.constData(), var.size()){};
40  c_array(const QJsonValue &val);
46  QString toHexString(void) const;
55  friend QDataStream &operator<<(QDataStream &out, const c_array &obj);
59  friend QDataStream &operator>>(QDataStream &in, c_array &obj);
64  template <class obj_type> void append(const obj_type &obj)
65  {
66  auto buffer = QDataStream(this, QIODevice::WriteOnly | QIODevice::Append);
67  buffer.setByteOrder(QDataStream::LittleEndian);
68  buffer << obj;
69  }
73  template <QCryptographicHash::Algorithm hashty> c_array hash(void) const
74  {
75  return c_array(QCryptographicHash::hash(*this, hashty));
76  }
81  template <class obj_type> void from_object(const obj_type &obj)
82  {
83  auto buffer = QDataStream(this, QIODevice::WriteOnly | QIODevice::Append);
84  buffer.setByteOrder(QDataStream::LittleEndian);
85  obj.serialize(buffer);
86  }
92  template <class obj_type> obj_type to_object(void)
93  {
94  auto buffer = QDataStream(this, QIODevice::ReadOnly);
95  buffer.setByteOrder(QDataStream::LittleEndian);
96  return obj_type(buffer);
97  }
98 };
99 
103 template <typename max_lenght> class fl_array : public c_array
104 {
105  public:
110  friend QDataStream &operator<<(QDataStream &out, const fl_array &obj)
111  {
112 
113  out << static_cast<max_lenght>(obj.size());
114  out << static_cast<c_array>(obj);
115  return out;
116  }
121  {
122  max_lenght size;
123  in >> size;
124  obj.resize(size);
125  auto var = static_cast<c_array *>(&obj);
126  in >> (*var);
127 
128  return in;
129  }
130 };
131 
140 
143 
147 using NFT_ID = c_array;
160 
166 
172 
178 
182 template <class T> using pvector = std::vector<std::shared_ptr<T>>;
183 
187 template <class T> struct ptrLess
188 {
189  bool operator()(const std::shared_ptr<T> &a, const std::shared_ptr<T> &b) const
190  {
191  return *a < *b;
192  }
193 };
194 
195 template <class T> using pset = std::set<std::shared_ptr<T>, ptrLess<T>>;
196 
201 template <class T> pset<const T> get_T(const QJsonArray &val)
202 {
203  pset<const T> var;
204  for (const auto &v : val)
205  var.insert(T::from_(v));
206  return var;
207 }
212 template <class T> pvector<const T> get_Tvec(const QJsonArray &val)
213 {
214  pvector<const T> var;
215  for (const auto &v : val)
216  var.push_back(T::from_(v));
217  return var;
218 }
222 template <class type_type> type_type get_type(const QJsonValue &val)
223 {
224  return ((type_type)val.toObject()["type"].toInt());
225 }
229 template <class type_type> type_type get_type(QDataStream &val)
230 {
231  type_type type_;
232  val >> type_;
233  return type_;
234 }
235 
239 template <class size_type, class obj_type> void serializeList(QDataStream &out, const pset<const obj_type> &ptrSet)
240 {
241  out << static_cast<size_type>(ptrSet.size());
242  for (const auto &v : ptrSet)
243  v->serialize(out);
244 }
248 template <class size_type, class obj_type> pset<const obj_type> deserializeList(QDataStream &in)
249 {
250  pset<const obj_type> ptrSet;
251  size_type length_;
252  in >> length_;
253  for (auto i = 0; i < length_; i++)
254  {
255  ptrSet.insert(obj_type::template from_<QDataStream>(in));
256  }
257  return ptrSet;
258 }
263 template <class obj_type> void orderList(pvector<obj_type> &ptrVector)
264 {
265  std::sort(ptrVector.begin(), ptrVector.end(), [](auto a, auto b) { return *a < *b; });
266 }
267 
268 }; // namespace qblocks
269 
270 }; // namespace qiota
T begin(T... args)
Byte Array that takes care of storing the objects in serialized form.
Definition: carray.hpp:30
friend QDataStream & operator>>(QDataStream &in, c_array &obj)
Read from datastream to the obj.
Definition: carray.cpp:23
friend QDataStream & operator<<(QDataStream &out, const c_array &obj)
Write to the data stream the obj data.
Definition: carray.cpp:18
c_array(const QByteArray &var)
Copy constructor from ByteArray.
Definition: carray.hpp:36
void append(const obj_type &obj)
Append obj data to the end. For objects that implement QDataStream & operator <<.
Definition: carray.hpp:64
static c_array fromHexString(QString hash)
The data from an "0x..." hex encoded string.
Definition: carray.cpp:8
c_array hash(void) const
Get the hash of the data.
Definition: carray.hpp:73
obj_type to_object(void)
Return Object from serialized data. Objects that implement the constructor(QDataStream &in)
Definition: carray.hpp:92
void from_object(const obj_type &obj)
Append obj data. Objects that have the serialize(QDataStream) function.
Definition: carray.hpp:81
QString toHexString(void) const
The "0x..." hex encoded string of the data.
Definition: carray.cpp:12
Byte Array with fixed length.
Definition: carray.hpp:104
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:110
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:120
T end(T... args)
T insert(T... args)
void orderList(pvector< obj_type > &ptrVector)
Order a Container of shared pointer to objects Normally ordered by type.
Definition: carray.hpp:263
pset< const obj_type > deserializeList(QDataStream &in)
Definition: carray.hpp:248
type_type get_type(const QJsonValue &val)
Definition: carray.hpp:222
void serializeList(QDataStream &out, const pset< const obj_type > &ptrSet)
append to the datastream the serialized form of objects in a container
Definition: carray.hpp:239
pset< const T > get_T(const QJsonArray &val)
Definition: carray.hpp:201
pvector< const T > get_Tvec(const QJsonArray &val)
Definition: carray.hpp:212
Definition: carray.hpp:22
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)
Container of ordered shared pointers.
Definition: carray.hpp:188
bool operator()(const std::shared_ptr< T > &a, const std::shared_ptr< T > &b) const
Definition: carray.hpp:189