mirror of
https://github.com/QuasarApp/QuasarAppLib.git
synced 2025-04-27 18:24:40 +00:00
add random array fuction into glabal header
This commit is contained in:
parent
d6ee73aff1
commit
9cc9e77bab
28
qaglobalutils.cpp
Normal file
28
qaglobalutils.cpp
Normal 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);
|
||||
}
|
||||
}
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user