-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currently POSIX-only.
- Loading branch information
leha-bot
committed
Sep 13, 2024
1 parent
4f42715
commit e094458
Showing
7 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
set (HEADER_LIST | ||
${CMAKE_CURRENT_SOURCE_DIR}/Constants.hpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/Library.hpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/Random.hpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/Types.hpp | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef FLAMEIDE_OS_POSIX_RANDOM_HPP | ||
#define FLAMEIDE_OS_POSIX_RANDOM_HPP | ||
|
||
#include <FlameIDE/Common/Traits/Numbers.hpp> | ||
#include <FlameIDE/Common/Traits/Functional.hpp> | ||
|
||
namespace flame_ide | ||
{namespace os | ||
{ | ||
|
||
flame_ide::primitive_types::ssize_t RandomRange(void *buf, flame_ide::primitive_types::size_t size); | ||
|
||
/// @brief Get next random value from POSIX getrandom(2). | ||
/// It fills all bytes of returned value. | ||
/// @tparam T Integral type used for returned value (default is int). | ||
template <typename T = flame_ide::primitive_types::int_t> | ||
typename EnableType<IsIntegralValue<T>, T>::Type Random() | ||
{ | ||
T result{}; | ||
return RandomRange(&result, sizeof(T)) == sizeof(T) ? result : 0; | ||
} | ||
|
||
}} | ||
|
||
#endif // FLAMEIDE_OS_POSIX_RANDOM_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include "FlameIDE/Common/PrimitiveTypes.hpp" | ||
#include <FlameIDE/Os/Random.hpp> | ||
|
||
#include <android/api-level.h> | ||
#define __ANDROID_API__ 34 | ||
#include <sys/random.h> | ||
|
||
namespace flame_ide | ||
{namespace os | ||
{ | ||
|
||
|
||
flame_ide::primitive_types::ssize_t RandomRange(void *buf, flame_ide::primitive_types::size_t size) | ||
{ | ||
return getrandom(buf, size, 0); | ||
} | ||
|
||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
set (SOURCE_LIST | ||
${CMAKE_CURRENT_SOURCE_DIR}/LibraryFunctions.cpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/Random.cpp | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <FlameIDE/../../src/Os/Tests/RandomTest.hpp> | ||
|
||
#include <FlameIDE/Os/Random.hpp> | ||
|
||
namespace flame_ide | ||
{namespace os | ||
{namespace tests | ||
{ | ||
|
||
RandomTest::RandomTest() : ::AbstractTest("Random") | ||
{} | ||
|
||
RandomTest::~RandomTest() = default; | ||
|
||
int RandomTest::vStart() | ||
{ | ||
constexpr size_t BUF_SIZE = 15; | ||
|
||
char buf[BUF_SIZE]; | ||
if (flame_ide::os::RandomRange(buf, BUF_SIZE) != BUF_SIZE) | ||
{ | ||
std::cout << "OS-dependent random failed" << std::endl; | ||
return FAILED; | ||
} | ||
else | ||
{ | ||
std::cout << "First byte of random buf: " << static_cast<int>(buf[0]) << std::endl; | ||
} | ||
|
||
std::cout << "Random() test (int): " << flame_ide::os::Random() << std::endl; | ||
std::cout << "Random() test (short): " << flame_ide::os::Random<short>() << std::endl; | ||
return SUCCESS; | ||
} | ||
|
||
}}} // namespace flame_ide::os::tests | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#ifndef FLAMEIDE_SRC_OS_TESTS_RANDOMTEST_HPP | ||
#define FLAMEIDE_SRC_OS_TESTS_RANDOMTEST_HPP | ||
|
||
#include <tests/Test.hpp> | ||
|
||
namespace flame_ide | ||
{namespace os | ||
{namespace tests | ||
{ | ||
|
||
class RandomTest: public ::AbstractTest | ||
{ | ||
public: | ||
RandomTest(); | ||
virtual ~RandomTest(); | ||
|
||
private: | ||
virtual int vStart(); | ||
}; | ||
|
||
}}} // namespace flame_ide::os::tests | ||
|
||
#endif // FLAMEIDE_SRC_OS_TESTS_RANDOMTEST_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters