-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurandom.hpp
More file actions
79 lines (58 loc) · 1.51 KB
/
Copy pathurandom.hpp
File metadata and controls
79 lines (58 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#pragma once
#include<unistd.h>
#include<fcntl.h>
#include <type_traits>
/** @brief Describes a RAII wrapper around the `/dev/urandom` file for linux systems that
* reads random bits and constructs type that is passed as the template arguement. */
class Urandom
{
int FileDesc; //!< File descriptor for open
bool OpenFlag; //!< Open Status of the urandom file
public:
/** @brief Constructor -> Sets the read size. */
Urandom(): FileDesc(-1), OpenFlag(false)
{}
/** @brief Destructor -> Closes the file. */
~Urandom()
{
if(OpenFlag)
{
close(FileDesc);
}
}
/** @brief Opens the urandom file. */
bool inline open() __attribute__((always_inline))
{
FileDesc = open("/dev/urandom", O_RDONLY, O_NONBLOCK); //Non-blocking mode implemented
if (FileDesc != -1)
{
Open_flag = true;
return true;
}
return false;
}
/** @brief Returns the open status of the urandom file object. */
bool inline is_open() __attribute__((always_inline))
{
return OpenFlag;
}
/** @brief Close the file without destroying the object. */
void inline close() __attribute__((always_inline))
{
Open_flag = false;
close(FileDesc);
}
/** @brief Reads `sizeof(Type)` number of random bits and return the constructed type. Template specialization */
template <typename Type>
Type inline get()
{
if constexpr (std::is_floating_point<Type>)
{}
else
{
Type read_value; //Explicitly use garbage value
ssize_t rcode = read(FileDesc, &read_value, sizeof(Type));
return read_value;
}
}
};