Skip to content

Commit

Permalink
Add emplace_back, move_assignment to BoundedVector (#385)
Browse files Browse the repository at this point in the history
* Add emplace_back, move_assignment to BoundedVector

Signed-off-by: Christopher Ho <christopherj.ho@gmail.com>

* Address reviewer comments:

- Use `auto` for portability
- Add unit tests
- Add documentation

Signed-off-by: Christopher Ho <christopher.ho@apex.ai>
  • Loading branch information
cho3 authored and ivanpauno committed Jun 7, 2019
1 parent f8167cf commit 9ffe214
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,18 @@ class BoundedVector
BoundedVector &
operator=(const BoundedVector & __x)
{
reinterpret_cast<std::vector<_Tp, _Alloc> *>(this)->operator=(
*reinterpret_cast<const std::vector<_Tp, _Alloc> *>(&__x));
(void)_Base::operator=(__x);
return *this;
}

/// %BoundedVector move assignment operator
/**
* \param __x A %BoundedVector of identical element and allocator types.
*/
BoundedVector &
operator=(BoundedVector && __x)
{
(void)_Base::operator=(std::forward<_Base &&>(__x));
return *this;
}

Expand Down Expand Up @@ -454,6 +464,27 @@ class BoundedVector
_Base::push_back(__x);
}

/// Add data to the end of the %BoundedVector.
/**
* This is a typical stack operation.
* The function creates an element at the end of the %BoundedVector
* and assigns the given data to it.
* Due to the nature of a %BoundedVector this operation can be done in
* constant time if the %BoundedVector has preallocated space
* available.
*
* \param args Arguments to be forwarded to the constructor of _Tp
*/
template<typename ... Args>
auto
emplace_back(Args && ... args)
{
if (size() >= _UpperBound) {
throw std::length_error("Exceeded upper bound");
}
return _Base::emplace_back(std::forward<Args>(args)...);
}

/// Insert an object in %BoundedVector before specified iterator.
/**
* This function will insert an object of type T constructed with
Expand Down Expand Up @@ -607,6 +638,13 @@ class BoundedVector
using _Base::erase;
using _Base::pop_back;
using _Base::clear;

private:
/// Cast to base type, to make it easier to dispatch to base implementations
operator _Base &()
{
return *this;
}
};

/// Vector equality comparison.
Expand Down
18 changes: 18 additions & 0 deletions rosidl_generator_cpp/test/test_bounded_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include <gtest/gtest.h>
#include <utility>

#include "rosidl_generator_cpp/bounded_vector.hpp"

Expand All @@ -32,3 +33,20 @@ TEST(rosidl_generator_cpp, bounded_vector) {
auto l = {1, 2, 3};
ASSERT_THROW(v = l, std::length_error);
}

TEST(rosidl_generator_cpp, bounded_vector_rvalue) {
rosidl_generator_cpp::BoundedVector<int, 2> v;
// emplace back
ASSERT_EQ(v.size(), 0u);
ASSERT_EQ(v.max_size(), 2u);
v.emplace_back(1);
v.emplace_back(2);
ASSERT_THROW(v.emplace_back(3), std::length_error);
ASSERT_EQ(v.size(), 2u);
// move assignment
decltype(v) vv;
vv = std::move(v);
ASSERT_EQ(vv.size(), 2u);
ASSERT_EQ(vv[0], 1);
ASSERT_EQ(vv[1], 2);
}

0 comments on commit 9ffe214

Please sign in to comment.