-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatastructs.hpp
More file actions
90 lines (77 loc) · 2.33 KB
/
Copy pathDatastructs.hpp
File metadata and controls
90 lines (77 loc) · 2.33 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
#ifndef DATASTRUCTS_HPP
#define DATASTRUCTS_HPP
#include "MessageType.hpp"
#include "Settings.hpp"
#include <fcntl.h>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <cstdlib>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef DEBUG
#define BUMP std::cout << "LINE: " << __LINE__ << std::endl;
#else
#define BUMP ;
#endif
struct Message {
MessageType::MessageType messageType;
int messageId;
char message[::MESSAGE_LENGTH];
template <typename T = char> void store(const T &elem, size_t index) {
if (index >= length<T>())
throw std::runtime_error("Index out of range");
((T *)message)[index] = elem;
}
template <typename T = char> T load(size_t index) {
if (index >= length<T>())
throw std::runtime_error("Index out of range");
return ((T *)message)[index];
}
template <typename T = char> T *ptr(size_t index) {
if (index >= length<T>())
throw std::runtime_error("Index out of range");
return ((T *)message) + index;
}
template <typename T = char> size_t length() {
return ::MESSAGE_LENGTH / sizeof(T);
}
};
template <typename T> void writeStruct(const T &t, const std::string &path) {
int fd = ::open(path.c_str(), O_WRONLY | O_CREAT);
if (fd == -1)
throw std::runtime_error("Can't store settings.");
int length = static_cast<int>(sizeof(T));
if (::write(fd, (void *)&t, length) != length)
throw std::runtime_error("Error while storing settings.");
::close(fd);
}
template <typename T> T loadStruct(const std::string &path) {
T t;
int fd = ::open(path.c_str(), O_RDONLY);
if (fd == -1)
throw std::runtime_error("Can't load settings.");
size_t length = sizeof(T);
if (::read(fd, (void *)&t, length) != length)
throw std::runtime_error("Error while reading settings.");
::close(fd);
return t;
}
struct ChannelSettings {
pid_t parent_pid;
int channel_id;
};
template <typename MessageFormat, char separator = ' ',
size_t Length = MESSAGE_LENGTH / sizeof(MessageFormat)>
void writeMessage(std::ostream &os, const Message &msg) {
os << "Message:\n\tType: " << msg.messageType << "\n\tText: ";
MessageFormat *begin = (MessageFormat *)msg.message;
MessageFormat *end = begin + Length;
while (begin < end) {
os << *begin++ << separator;
}
os << '\n';
}
#endif // DATASTRUCTS_HPP