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_INTEGRATION_TESTS "Integration Tests" OFF)
option(SLS_USE_SANITIZER "Sanitizers for debugging" OFF) option(SLS_USE_SANITIZER "Sanitizers for debugging" OFF)
option(SLS_USE_PYTHON "Python bindings" OFF) option(SLS_USE_PYTHON "Python bindings" OFF)
option(SLS_BUILD_DOCS "Documentations" OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
@ -182,10 +183,11 @@ configure_file( .clang-tidy
) )
add_subdirectory(sample) #add_subdirectory(sample)
add_subdirectory(docs)
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; using const_iterator = typename std::array<T, Capacity>::const_iterator;
FixedCapacityContainer() = default; 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()); std::copy(l.begin(), l.end(), data_.begin());
} }
/** Copy construct from another container */
template <typename V, template <typename V,
typename = typename std::enable_if< typename = typename std::enable_if<
is_light_container<V>::value && is_container<V>::value &&
std::is_same<T, typename V::value_type>::value>::type> std::is_same<T, typename V::value_type>::value>::type>
explicit FixedCapacityContainer(const V &v) { FixedCapacityContainer(const V &v) : current_size(v.size()) {
if (v.size() > Capacity) { size_check(v.size());
throw std::runtime_error(
"Capacity needs to be same size or larger than vector");
}
current_size = v.size();
std::copy(v.begin(), v.end(), data_.begin()); std::copy(v.begin(), v.end(), data_.begin());
} }
template <size_t OtherCapacity> /** copy assignment from another container */
explicit FixedCapacityContainer( template <typename V>
const FixedCapacityContainer<T, OtherCapacity> &other) noexcept { typename std::enable_if<is_container<V>::value, FixedCapacityContainer &>::type
static_assert(Capacity >= OtherCapacity, operator=(const V &other) {
"Container needs to be same size or larger"); size_check(other.size());
current_size = other.size();
std::copy(other.cbegin(), other.cend(), data_.begin());
}
FixedCapacityContainer &operator=(const std::vector<T> &other) {
std::copy(other.begin(), other.end(), data_.begin()); std::copy(other.begin(), other.end(), data_.begin());
current_size = other.size(); current_size = other.size();
return *this; return *this;
@ -119,6 +113,13 @@ template <typename T, size_t Capacity> class FixedCapacityContainer {
private: private:
size_type current_size{}; size_type current_size{};
std::array<T, Capacity> data_; 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)); } __attribute__((packed));
/* Free function concerning FixedCapacityContainer */ /* Free function concerning FixedCapacityContainer */

View File

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