add random array fuction into glabal header

This commit is contained in:
Andrei Yankovich 2022-02-10 20:02:48 +03:00
parent d6ee73aff1
commit 9cc9e77bab
2 changed files with 36 additions and 0 deletions

28
qaglobalutils.cpp Normal file
View File

@ -0,0 +1,28 @@
/*
* Copyright (C) 2018-2021 QuasarApp.
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#include "qaglobalutils.h"
template<class NUM>
void gen(int size, QByteArray &result) {
for (; size >= 0; size -= sizeof(NUM)) {
NUM random = (rand() * rand()) % std::numeric_limits<NUM>::max();
result.insert(0, reinterpret_cast<char*>(&random), sizeof(random));
}
}
void randomArray(int size, QByteArray &result) {
if (size % sizeof(unsigned long long) == 0) {
gen<unsigned long long>(size, result);
} else if (size % sizeof(unsigned int) == 0) {
gen<unsigned int>(size, result);
} else if (size % sizeof(unsigned short) == 0) {
gen<unsigned short>(size, result);
} else {
gen<unsigned char>(size, result);
}
}

View File

@ -12,6 +12,7 @@
#include <type_traits>
#include <string>
#include <typeinfo>
#include <QByteArray>
#include "QtGlobal"
@ -182,4 +183,11 @@ uint8_t static_type_hash_8(T& object) noexcept {
#define debug_assert(condition, msg) assert(condition && msg)
#endif
/**
* @brief randomArray This function return random arrat with size @a size
* @param size This is size of needed array.
* @param result This is result value of generated array.
*/
void randomArray(int size, QByteArray &result);
#endif // GLOBAL_H