-
Notifications
You must be signed in to change notification settings - Fork 0
/
Generator.h
107 lines (75 loc) · 2.52 KB
/
Generator.h
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
#pragma once
#include "Parameters.h"
#include <vector>
#include <iostream>
#include <exception>
#include <string>
#include <chrono>
#include <array>
#include <fstream>
class GeneratorMode{
public:
using id_size = short;
// ----------------------------------------------------
class InvalidId{
};
// ----------------------------------------------------
virtual int generateValue() = 0;
virtual void generateSequence(CommunicationPipe& comm) = 0;
virtual bool canChangeParameters() const = 0;
virtual void stopGenerating() = 0;
bool setModeId(id_size id);
id_size getModeId() { return id; };
protected:
GeneratorMode() {};
virtual ~GeneratorMode() {};
private:
static std::vector<id_size> ids;
id_size id;
};
class RandomMode : public GeneratorMode {
public:
RandomMode(id_size id, SharedParameters* ptr_params);
~RandomMode();
int generateValue() override;
void generateSequence(CommunicationPipe& comm) override;
void stopGenerating() override;
bool canChangeParameters() const override { return true; };
private:
void updateParameters();
private:
std::chrono::milliseconds max_wait_duration;
SharedParameters* ptr_parameters;
bool isGenerating;
};
class ReadingMode : public GeneratorMode {
public:
ReadingMode(id_size id, SharedParameters* ptr_params, const std::string& filename = "Generator_StartParameters.txt");
~ReadingMode();
int generateValue() override;
void generateSequence(CommunicationPipe& comm) override;
void stopGenerating() override;
bool canChangeParameters() const override { return false; };
private:
std::chrono::milliseconds max_wait_duration;
SharedParameters* ptr_parameters;
Parameters parameters_state;
std::string readFilename;
bool isGenerating;
};
class Generator {
public:
Generator(GeneratorMode& gm, SharedParameters* ptr_params);
~Generator();
void addMode(GeneratorMode& gm);
void switchMode();
void run(CommunicationPipe& pipe);
bool canChangeParameters();
private:
SharedParameters* ptr_parameters;
std::vector<GeneratorMode*> generator_modes{ nullptr, nullptr };
size_t curr_pos;
size_t num_modes;
std::condition_variable cond_generate;
bool isSwitched;
};