-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfieldmap.cpp
57 lines (55 loc) · 1.63 KB
/
fieldmap.cpp
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
#include "fieldmap.h"
#include <stdexcept>
#include <variant>
#include <stdexcept>
#include <sstream>
const Field Field::EMPTY = Field();
FieldMap& FieldMap::addGroup(uint32_t tag) {
return addGroup(tag,1);
}
FieldMap& FieldMap::addGroup(uint32_t tag,int count) {
auto itr = map.find(tag);
if(itr==nullptr) throw std::runtime_error("group field tag does not exist");
if(!itr->isGroup()) {
FieldMap *obj = (FieldMap*)buffer.allocate(sizeof(FieldMap));
auto fm = new (obj) FieldMap(buffer,msgBytes);
itr->groups = fm;
return *const_cast<FieldMap*>(itr->groups);
}
FieldMap* tail = itr->groups;
while(tail->next!=nullptr) tail = tail->next;
FieldMap *obj = (FieldMap*)buffer.allocate(sizeof(FieldMap));
tail->next = new (obj) FieldMap(buffer,msgBytes);
return *tail->next;
}
void FieldMap::set(uint32_t tag, const Field& field){
map.put(field);
}
const Field& FieldMap::get(uint32_t tag) const {
auto itr = map.find(tag);
if(itr!=nullptr) {
return *itr;
} else {
return Field::EMPTY;
}
}
const FieldMap& FieldMap::getGroup(uint32_t tag,int index) const {
auto itr = map.find(tag);
if(itr==nullptr || !itr->isGroup()) {
std::ostringstream oss;
oss << "tag " << tag << " is not a group";
throw std::runtime_error(oss.str());
}
return *itr->group(index);
}
FieldMap* Field::group(int n) const {
auto ptr = groups;
while(n-->0) { ptr = ptr->next; }
return ptr;
}
int Field::groupCount() const {
auto ptr = groups;
int count = 0;
while(ptr!=nullptr) { count++; ptr = ptr->next; }
return count;
}