diff --git a/slsSupportLib/include/FixedCapacityContainer.h b/slsSupportLib/include/FixedCapacityContainer.h index 046f5b1f3..4b39d52db 100644 --- a/slsSupportLib/include/FixedCapacityContainer.h +++ b/slsSupportLib/include/FixedCapacityContainer.h @@ -1,4 +1,3 @@ - #pragma once #include #include @@ -12,39 +11,43 @@ template class FixedCapacityContainer { explicit FixedCapacityContainer(std::initializer_list l); explicit FixedCapacityContainer(const std::vector &v); + template + explicit FixedCapacityContainer( + const FixedCapacityContainer &other) noexcept; + FixedCapacityContainer &operator=(const std::vector &other); - bool operator==(const std::vector &other) const; - bool operator!=(const std::vector &other) const; + bool operator==(const std::vector &other) const noexcept; + bool operator!=(const std::vector &other) const noexcept; template - bool - operator==(const FixedCapacityContainer &other) const; + bool operator==(const FixedCapacityContainer &other) const + noexcept; template - bool - operator!=(const FixedCapacityContainer &other) const; + bool operator!=(const FixedCapacityContainer &other) const + noexcept; T &operator[](size_t i) { return data_[i]; } const T &operator[](size_t i) const { return data_[i]; } - size_t size() const { return size_; } - bool empty() const{ return size_ == 0;} - size_t capacity() const { return Capacity; } + constexpr size_t size() const noexcept { return size_; } + bool empty() const noexcept { return size_ == 0; } + constexpr size_t capacity() const noexcept { return Capacity; } void push_back(const T &value); void resize(size_t new_size); void erase(T *ptr); - T &front() { return data_.front(); } - T &back() { return data_[size_ - 1]; } - const T &front() const { return data_.front(); } - const T &back() const { return data_[size_ - 1]; } + T &front() noexcept { return data_.front(); } + T &back() noexcept { return data_[size_ - 1]; } + constexpr const T &front() const noexcept { return data_.front(); } + constexpr const T &back() const noexcept { return data_[size_ - 1]; } // iterators - T *begin() { return &data_[0]; } - T *end() { return &data_[size_]; } - const T *cbegin() const { return &data_[0]; } - const T *cend() const { return &data_[size_]; } + T *begin() noexcept { return &data_[0]; } + T *end() noexcept { return &data_[size_]; } + const T *cbegin() const noexcept { return &data_[0]; } + const T *cend() const noexcept { return &data_[size_]; } private: size_t size_{0}; @@ -62,10 +65,24 @@ FixedCapacityContainer::FixedCapacityContainer( template FixedCapacityContainer::FixedCapacityContainer( const std::vector &v) { + if (v.size() > Capacity) { + throw std::runtime_error( + "Capacity needs to be same size or larger than vector"); + } size_ = v.size(); std::copy(v.begin(), v.end(), data_.begin()); } +template +template +FixedCapacityContainer::FixedCapacityContainer( + const FixedCapacityContainer &other) noexcept { + static_assert(Capacity >= OtherCapacity, + "Container needs to be same size or larger"); + size_ = other.size(); + std::copy(other.cbegin(), other.cend(), data_.begin()); +} + template void FixedCapacityContainer::push_back(const T &value) { if (size_ == Capacity) { @@ -94,7 +111,7 @@ operator=(const std::vector &other) { template bool FixedCapacityContainer:: -operator==(const std::vector &other) const { +operator==(const std::vector &other) const noexcept { if (size_ != other.size()) { return false; } else { @@ -109,14 +126,15 @@ operator==(const std::vector &other) const { template bool FixedCapacityContainer:: -operator!=(const std::vector &other) const { +operator!=(const std::vector &other) const noexcept { return !(*this == other); } template template bool FixedCapacityContainer:: -operator==(const FixedCapacityContainer &other) const { +operator==(const FixedCapacityContainer &other) const + noexcept { if (size_ != other.size()) { return false; } else { @@ -132,7 +150,8 @@ operator==(const FixedCapacityContainer &other) const { template template bool FixedCapacityContainer:: -operator!=(const FixedCapacityContainer &other) const { +operator!=(const FixedCapacityContainer &other) const + noexcept { return !(*this == other); } @@ -146,26 +165,27 @@ void FixedCapacityContainer::erase(T *ptr) { } /* Free function concerning FixedCapacityContainer */ - template -T *begin(FixedCapacityContainer &container) { +constexpr T *begin(FixedCapacityContainer &container) noexcept { return container.begin(); } template -T *end(FixedCapacityContainer &container) { +constexpr T *end(FixedCapacityContainer &container) noexcept { return container.end(); } template -bool operator==(const std::vector &vec, - const FixedCapacityContainer &fixed_container) { +bool operator==( + const std::vector &vec, + const FixedCapacityContainer &fixed_container) noexcept { return fixed_container == vec; } template -bool operator!=(const std::vector &vec, - const FixedCapacityContainer &fixed_container) { +bool operator!=( + const std::vector &vec, + const FixedCapacityContainer &fixed_container) noexcept { return fixed_container != vec; } diff --git a/slsSupportLib/tests/test-FixedCapacityContainer.cpp b/slsSupportLib/tests/test-FixedCapacityContainer.cpp index 78215d336..a5c931a12 100644 --- a/slsSupportLib/tests/test-FixedCapacityContainer.cpp +++ b/slsSupportLib/tests/test-FixedCapacityContainer.cpp @@ -201,5 +201,17 @@ SCENARIO("Assigning containers to each other", "[support]") { REQUIRE(c[2] == 3); } } + WHEN("We create a const FixedCapacityContainer"){ + const FixedCapacityContainer c(a); + THEN("The values are still the same using const operators"){ + REQUIRE(c[0] == 1); + REQUIRE(c[1] == 2); + REQUIRE(c[2] == 3); + REQUIRE(c.front() == 1); + REQUIRE(c.back() == 3); + + } + } } -} \ No newline at end of file +} + diff --git a/slsSupportLib/tests/test-sls_detector_defs.cpp b/slsSupportLib/tests/test-sls_detector_defs.cpp index 9b745a29e..dd5ab366d 100644 --- a/slsSupportLib/tests/test-sls_detector_defs.cpp +++ b/slsSupportLib/tests/test-sls_detector_defs.cpp @@ -1,5 +1,4 @@ #include "catch.hpp" -#include "slsDetector.h" #include "sls_detector_defs.h" using dt = slsDetectorDefs::detectorType; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b8366739e..39607890c 100755 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -12,11 +12,22 @@ target_link_libraries(tests slsProjectOptions slsProjectWarnings slsSupportLib - slsDetectorShared - slsReceiverShared pthread rt ) + +if (SLS_USE_TEXTCLIENT) + target_link_libraries(tests + slsDetectorShared + ) +endif (SLS_USE_TEXTCLIENT) + +if (SLS_USE_RECEIVER) + target_link_libraries(tests + slsReceiverShared + ) +endif (SLS_USE_RECEIVER) + set_target_properties(tests PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )