-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharedstreets-tile-parser.cc
37 lines (31 loc) · 1.29 KB
/
sharedstreets-tile-parser.cc
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
#include "sharedstreets-tile-parser.h"
#include <fcntl.h>
#include <stdint.h>
#include "proto/sharedstreets.pb.h"
using google::protobuf::io::CodedInputStream;
using google::protobuf::io::FileInputStream;
template <class T>
SharedStreetsTileParser<T>::SharedStreetsTileParser(const std::filesystem::path& tile_path)
: fd_(open(tile_path.c_str(), O_RDONLY)),
raw_input_(FileInputStream(fd_)),
coded_input_(CodedInputStream(&raw_input_)) {
raw_input_.SetCloseOnDelete(true); // closes the fd_ when raw_input_ is destroyed
}
template <class T>
std::vector<T> SharedStreetsTileParser<T>::GetContents() {
std::vector<T> messages;
uint32_t next_message_length;
while (coded_input_.ReadVarint32(&next_message_length)) {
const auto limit = coded_input_.PushLimit(next_message_length);
T message;
message.ParseFromCodedStream(&coded_input_);
messages.emplace_back(std::move(message));
coded_input_.PopLimit(limit);
}
return messages;
}
// Explicitly instantiate this template for the four SharedStreets message classes.
template class SharedStreetsTileParser<SharedStreetsGeometry>;
template class SharedStreetsTileParser<SharedStreetsIntersection>;
template class SharedStreetsTileParser<SharedStreetsMetadata>;
template class SharedStreetsTileParser<SharedStreetsReference>;