add front and back (#269)

* add front and back

i'll get the last message in some situations,but have to do like this :
```
multimsg msgs;
auto msg = msgs[msgs.size() - 1];
func(msg.size());
auto *msg = msgs.end() - 1;
```
but, std::queue have some method 'front' and 'back'. so maybe i can simply do this ? :
```
multimsg msgs;
auto msg = msgs.back();
```

* add test for front and back
This commit is contained in:
tangfu 2018-10-17 15:41:21 +08:00 committed by Luca Boccassi
parent 1547b6ec22
commit 5af24314c3
2 changed files with 20 additions and 0 deletions

View File

@ -75,6 +75,14 @@ TEST(multipart, legacy_test)
multipart.pushmem("Frame0", 6);
assert(multipart.size() == 10);
const message_t& front_msg = multipart.front();
assert(multipart.size() == 10);
assert(std::string(front_msg.data<char>(), front_msg.size()) == "Frame0");
const message_t& back_msg = multipart.back();
assert(multipart.size() == 10);
assert(std::string(back_msg.data<char>(), back_msg.size()) == "Frame9");
msg = multipart.remove();
assert(multipart.size() == 9);
assert(std::string(msg.data<char>(), msg.size()) == "Frame9");

View File

@ -246,6 +246,18 @@ class multipart_t
return message;
}
// get message part from front
const message_t &front()
{
return m_parts.front();
}
// get message part from back
const message_t &back()
{
return m_parts.back();
}
// Get pointer to a specific message part
const message_t *peek(size_t index) const { return &m_parts[index]; }