-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram_runner.hpp
More file actions
139 lines (102 loc) · 4.17 KB
/
program_runner.hpp
File metadata and controls
139 lines (102 loc) · 4.17 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#ifndef PROGRAM_RUNNER_H
#define PROGRAM_RUNNER_H
#include <cstdint>
#include <iostream>
#include <cstdlib>
#include <optional>
#include <variant>
#include <map>
#include <memory>
#include "prng.hpp"
class ProgramRunner {
public:
ProgramRunner(int argc, char **argv, uint64_t warmup_iterations = 0);
struct ProgramStatus {
std::optional<std::string> stdout_message;
std::optional<std::string> stderr_message;
std::optional<int> exit_code;
};
ProgramStatus iterate();
// will return the ProgramStatus of the last iteration
ProgramStatus iterate(uint64_t iterations);
// runs until the program runner finishes, will return the ProgramStatus of the last iteration
ProgramStatus run();
bool is_finished();
const std::string version = "0.1";
const std::string program_name = "randomizer";
private:
enum class Algorithm {
XORShift,
LinearCongruentialGenerator,
MersenneTwister
// Add other algorithms here
};
const std::map<std::string, Algorithm> algorithm_choices {
{"xorshift", Algorithm::XORShift},
{"xor", Algorithm::XORShift},
{"linear-congruential-generator", Algorithm::LinearCongruentialGenerator},
{"lcg", Algorithm::LinearCongruentialGenerator},
{"mersenne", Algorithm::MersenneTwister},
{"mersenne-twister", Algorithm::MersenneTwister},
{"mt", Algorithm::MersenneTwister}
};
enum class ProgramBehaviour {
Error,
Help,
Version,
GenerateUnitNormal,
GenerateFloating,
GenerateInteger
};
const std::map<std::string, ProgramBehaviour> generation_types {
{"unit", ProgramBehaviour::GenerateUnitNormal},
{"normal", ProgramBehaviour::GenerateUnitNormal},
{"unit-normal", ProgramBehaviour::GenerateUnitNormal},
{"normalized", ProgramBehaviour::GenerateUnitNormal},
{"float", ProgramBehaviour::GenerateFloating},
{"floating", ProgramBehaviour::GenerateFloating},
{"decimal", ProgramBehaviour::GenerateFloating},
{"floating-point", ProgramBehaviour::GenerateFloating},
{"int", ProgramBehaviour::GenerateInteger},
{"integer", ProgramBehaviour::GenerateInteger}
};
struct RawArguments {
// what special strings should we show?
bool error;
bool show_help;
bool show_version;
// how do we want to generate the random numbers?
std::optional<std::string> type;
std::optional<std::string> algorithm_str;
std::optional<std::string> min_str;
std::optional<std::string> max_str;
std::optional<std::string> count_str;
};
// Defaults
static constexpr uint32_t DefaultCount = 1;
static constexpr ProgramBehaviour DefaultGenerationType = ProgramBehaviour::GenerateInteger;
static RawArguments parse_args(int argc, char **argv);
void determine_program_configuration(const RawArguments &raw_args);
void determine_user_message_configuration(const bool error, const bool show_version, const bool show_help);
void determine_generation_range_configuration(const std::optional<std::string> &min_str, const std::optional<std::string> &max_str);
void determine_generation_type_configuration(const std::optional<std::string> &generation_type);
void determine_count_configuration(const std::optional<std::string> &count_str);
void determine_algorithm_configuration(const std::optional<std::string> &alg_str);
void create_prng();
void warmup(uint64_t iterations);
std::string error_string();
std::string help_string();
std::string version_string();
std::optional<ProgramBehaviour> behaviour = std::nullopt;
std::optional<Algorithm> algorithm = std::nullopt;
std::optional<std::variant<int32_t, float>> min = std::nullopt;
std::optional<std::variant<int32_t, float>> max = std::nullopt;
std::optional<uint32_t> count = std::nullopt;
uint32_t iteration = 0;
bool finished = false;
std::unique_ptr<PseudoRandomNumberGenerator> prng = nullptr;
// Exit codes
static constexpr int ExitCodeSuccess = 0;
static constexpr int ExitCodeError = 1;
};
#endif