-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFrameContainer.h
129 lines (112 loc) · 3.97 KB
/
FrameContainer.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* @file FrameContainer.h
* @brief Frame Structure Container
* @date Created : 2024-05-04 12:27:57 JST
* Last Modified : 2024-06-30 18:56:56 JST
*
* @author Shinsuke OTA <[email protected]>
*
*/
#ifndef NESTDAQ_FRAMECONTAINEW_H
#define NESTDAQ_FRAMECONTAINEW_H
#include "SubTimeFrameHeader.h"
#include "TimeFrameHeader.h"
#include "FilterHeader.h"
#include "HeartbeatFrameHeader.h"
#include <iostream>
namespace nestdaq {
template <typename frametype> class Header {
public:
void SetHeader(void* data) {
this->fHeader = static_cast<frametype*>(data);
}
frametype* GetHeader() { return this->fHeader; }
template<typename unit>
void CopyHeaderTo(std::vector<unit>* output) {
for (uint32_t i = 0, n = sizeof(frametype)/sizeof(unit); i < n; ++i) {
output->push_back(*((unit*)fHeader+i));
}
}
protected:
frametype *fHeader;
};
template <class frametype, class datatype>
class DataFrame : public Header<frametype> {
public:
void Set(void* data) {
this->SetHeader(data);
fData = reinterpret_cast<datatype*>((char*)data + sizeof(frametype));
auto len = this->GetHeader()->length;
auto hlen = this->GetHeader()->hLength;
fNumData = (len > hlen) ? (len - hlen )/sizeof(datatype) : 0;
}
datatype UncheckedAt(uint64_t idx) { return *(fData+idx); }
auto GetNumData() const { return fNumData; }
auto GetData() { return fData; }
template <typename unit>
void CopyDataTo(std::vector<unit>* output) {
auto nj = sizeof(datatype)/sizeof(unit);
for (uint32_t i =0, n = fNumData; i < n; ++i) {
for (uint32_t j = 0 ; j < nj; ++j) {
output->push_back(*((unit*)(fData+i) + j));
}
}
}
template <typename unit>
void CopyDataTo(std::vector<unit>* output, uint64_t idx) {
auto nj = sizeof(datatype)/sizeof(unit);
for (uint32_t j = 0; j < nj; ++j) {
output->push_back(*((unit*)(fData+idx)+j));
}
}
template <typename unit>
void CopyAllTo(std::vector<unit>* output) {
std::cout << std::hex << this->GetHeader()->magic << " n data = " << this->GetNumData() << std::endl;
this->template CopyHeaderTo<unit>(output);
this->template CopyDataTo<unit>(output);
}
uint64_t GetRealLength() {
return sizeof(frametype) + fNumData * sizeof(datatype);
}
void Clear() {
;
}
protected:
datatype* fData;
uint64_t fNumData;
};
template <class frametype, class child>
class ContainerFrame : public Header<frametype>, public std::vector<child*> {
public:
uint64_t GetRealLength() {
uint64_t length = sizeof(frametype);
for (int i = 0, n = this->size(); i<n; ++i) {
length += this->at(i)->GetRealLength();
}
return length;
}
template <typename unit> void CopyAllTo(std::vector<unit>* output) {
this->template CopyHeaderTo<unit>(output);
std::cout << std::hex << this->GetHeader()->magic << std::endl;
for (int i = 0,n = this->size(); i < n; ++i) {
this->at(i)->template CopyAllTo<unit>(output);
}
}
void Clear() {
std::for_each(this->begin(),this->end(),
[] (child* c) {
c->Clear();
delete c;
});
this->std::vector<child*>::clear();
}
protected:
void clear();
};
using THBF = DataFrame<struct HeartbeatFrame::Header,uint64_t>;
using TSTF = ContainerFrame<struct SubTimeFrame::Header,THBF>;
using TTT = DataFrame<struct Filter::TrgTimeHeader,struct Filter::TrgTime>;
using TLF = ContainerFrame<struct Filter::Header,TTT>;
using TTF = ContainerFrame<struct TimeFrame::Header,TSTF>;
}
#endif // NESTDAQ_FRAMECONTAINEW_H