This commit is contained in:
Erik Frojdh 2019-08-12 17:17:13 +02:00
parent b52a8d2d61
commit 935f7bc960
3 changed files with 52 additions and 22 deletions

View File

@ -42,6 +42,7 @@ option (SLS_USE_TESTS "TESTS" OFF)
option (SLS_USE_INTEGRATION_TESTS "Integration Tests" OFF)
option(SLS_USE_SANITIZER "Sanitizers for debugging" OFF)
option(SLS_USE_PYTHON "Python bindings" OFF)
option(SLS_BUILD_DOCS "Documentations" OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
@ -182,10 +183,11 @@ configure_file( .clang-tidy
)
add_subdirectory(sample)
add_subdirectory(docs)
#add_subdirectory(sample)
if(SLS_BUILD_DOCS)
add_subdirectory(docs)
endif(SLS_BUILD_DOCS)

View File

@ -16,34 +16,28 @@ template <typename T, size_t Capacity> class FixedCapacityContainer {
using const_iterator = typename std::array<T, Capacity>::const_iterator;
FixedCapacityContainer() = default;
explicit FixedCapacityContainer(std::initializer_list<T> l) {
current_size = l.size();
explicit FixedCapacityContainer(std::initializer_list<T> l)
: current_size(l.size()) {
size_check(l.size());
std::copy(l.begin(), l.end(), data_.begin());
}
/** Copy construct from another container */
template <typename V,
typename = typename std::enable_if<
is_light_container<V>::value &&
is_container<V>::value &&
std::is_same<T, typename V::value_type>::value>::type>
explicit FixedCapacityContainer(const V &v) {
if (v.size() > Capacity) {
throw std::runtime_error(
"Capacity needs to be same size or larger than vector");
}
current_size = v.size();
FixedCapacityContainer(const V &v) : current_size(v.size()) {
size_check(v.size());
std::copy(v.begin(), v.end(), data_.begin());
}
template <size_t OtherCapacity>
explicit FixedCapacityContainer(
const FixedCapacityContainer<T, OtherCapacity> &other) noexcept {
static_assert(Capacity >= OtherCapacity,
"Container needs to be same size or larger");
current_size = other.size();
std::copy(other.cbegin(), other.cend(), data_.begin());
}
FixedCapacityContainer &operator=(const std::vector<T> &other) {
/** copy assignment from another container */
template <typename V>
typename std::enable_if<is_container<V>::value, FixedCapacityContainer &>::type
operator=(const V &other) {
size_check(other.size());
std::copy(other.begin(), other.end(), data_.begin());
current_size = other.size();
return *this;
@ -119,6 +113,13 @@ template <typename T, size_t Capacity> class FixedCapacityContainer {
private:
size_type current_size{};
std::array<T, Capacity> data_;
void size_check(size_type s) const {
if (s > Capacity) {
throw std::runtime_error(
"Capacity needs to be same size or larger than vector");
}
}
} __attribute__((packed));
/* Free function concerning FixedCapacityContainer */

View File

@ -17,12 +17,39 @@ TEST_CASE("Construct from vector"){
REQUIRE(fcc == vec);
}
TEST_CASE("Copy construct from vector"){
std::vector<int> vec{1,2,3};
FixedCapacityContainer<int, 5> fcc = vec;
REQUIRE(fcc == vec);
}
TEST_CASE("Copy assignment from vector"){
std::vector<int> vec{1,2,3};
FixedCapacityContainer<int, 5> fcc;
fcc = vec;
REQUIRE(fcc == vec);
}
TEST_CASE("Construct from array"){
std::array<int,3> arr{1,2,3};
FixedCapacityContainer<int, 5> fcc{arr};
REQUIRE(fcc == arr);
}
TEST_CASE("Copy assign from array"){
std::array<int,3> arr{1,2,3};
FixedCapacityContainer<int, 5> fcc;
fcc = arr;
REQUIRE(fcc == arr);
}
TEST_CASE("Copy construct from array"){
std::array<int,3> arr{1,2,3};
FixedCapacityContainer<int, 5> fcc = arr;
REQUIRE(fcc == arr);
}
SCENARIO("FixedCapacityContainers can be sized and resized", "[support]") {
GIVEN("A default constructed container") {