Added noexcept and constexpr (#21)

* noexcept and constexpr

* check size
This commit is contained in:
Erik Fröjdh 2019-05-14 11:08:25 +02:00 committed by Dhanya Thattil
parent fbada2e81a
commit 0224dccd2e
4 changed files with 75 additions and 33 deletions

View File

@ -1,4 +1,3 @@
#pragma once
#include <array>
#include <cassert>
@ -12,39 +11,43 @@ template <typename T, size_t Capacity> class FixedCapacityContainer {
explicit FixedCapacityContainer(std::initializer_list<T> l);
explicit FixedCapacityContainer(const std::vector<T> &v);
template <size_t OtherCapacity>
explicit FixedCapacityContainer(
const FixedCapacityContainer<T, OtherCapacity> &other) noexcept;
FixedCapacityContainer &operator=(const std::vector<T> &other);
bool operator==(const std::vector<T> &other) const;
bool operator!=(const std::vector<T> &other) const;
bool operator==(const std::vector<T> &other) const noexcept;
bool operator!=(const std::vector<T> &other) const noexcept;
template <size_t OtherCapacity>
bool
operator==(const FixedCapacityContainer<T, OtherCapacity> &other) const;
bool operator==(const FixedCapacityContainer<T, OtherCapacity> &other) const
noexcept;
template <size_t OtherCapacity>
bool
operator!=(const FixedCapacityContainer<T, OtherCapacity> &other) const;
bool operator!=(const FixedCapacityContainer<T, OtherCapacity> &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<T, Capacity>::FixedCapacityContainer(
template <typename T, size_t Capacity>
FixedCapacityContainer<T, Capacity>::FixedCapacityContainer(
const std::vector<T> &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 <typename T, size_t Capacity>
template <size_t OtherCapacity>
FixedCapacityContainer<T, Capacity>::FixedCapacityContainer(
const FixedCapacityContainer<T, OtherCapacity> &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 <typename T, size_t Capacity>
void FixedCapacityContainer<T, Capacity>::push_back(const T &value) {
if (size_ == Capacity) {
@ -94,7 +111,7 @@ operator=(const std::vector<T> &other) {
template <typename T, size_t Capacity>
bool FixedCapacityContainer<T, Capacity>::
operator==(const std::vector<T> &other) const {
operator==(const std::vector<T> &other) const noexcept {
if (size_ != other.size()) {
return false;
} else {
@ -109,14 +126,15 @@ operator==(const std::vector<T> &other) const {
template <typename T, size_t Capacity>
bool FixedCapacityContainer<T, Capacity>::
operator!=(const std::vector<T> &other) const {
operator!=(const std::vector<T> &other) const noexcept {
return !(*this == other);
}
template <typename T, size_t Capacity>
template <size_t OtherCapacity>
bool FixedCapacityContainer<T, Capacity>::
operator==(const FixedCapacityContainer<T, OtherCapacity> &other) const {
operator==(const FixedCapacityContainer<T, OtherCapacity> &other) const
noexcept {
if (size_ != other.size()) {
return false;
} else {
@ -132,7 +150,8 @@ operator==(const FixedCapacityContainer<T, OtherCapacity> &other) const {
template <typename T, size_t Capacity>
template <size_t OtherCapacity>
bool FixedCapacityContainer<T, Capacity>::
operator!=(const FixedCapacityContainer<T, OtherCapacity> &other) const {
operator!=(const FixedCapacityContainer<T, OtherCapacity> &other) const
noexcept {
return !(*this == other);
}
@ -146,26 +165,27 @@ void FixedCapacityContainer<T, Capacity>::erase(T *ptr) {
}
/* Free function concerning FixedCapacityContainer */
template <typename T, size_t Capacity>
T *begin(FixedCapacityContainer<T, Capacity> &container) {
constexpr T *begin(FixedCapacityContainer<T, Capacity> &container) noexcept {
return container.begin();
}
template <typename T, size_t Capacity>
T *end(FixedCapacityContainer<T, Capacity> &container) {
constexpr T *end(FixedCapacityContainer<T, Capacity> &container) noexcept {
return container.end();
}
template <typename T, size_t Capacity>
bool operator==(const std::vector<T> &vec,
const FixedCapacityContainer<T, Capacity> &fixed_container) {
bool operator==(
const std::vector<T> &vec,
const FixedCapacityContainer<T, Capacity> &fixed_container) noexcept {
return fixed_container == vec;
}
template <typename T, size_t Capacity>
bool operator!=(const std::vector<T> &vec,
const FixedCapacityContainer<T, Capacity> &fixed_container) {
bool operator!=(
const std::vector<T> &vec,
const FixedCapacityContainer<T, Capacity> &fixed_container) noexcept {
return fixed_container != vec;
}

View File

@ -201,5 +201,17 @@ SCENARIO("Assigning containers to each other", "[support]") {
REQUIRE(c[2] == 3);
}
}
WHEN("We create a const FixedCapacityContainer"){
const FixedCapacityContainer<int, 5> 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);
}
}
}
}
}

View File

@ -1,5 +1,4 @@
#include "catch.hpp"
#include "slsDetector.h"
#include "sls_detector_defs.h"
using dt = slsDetectorDefs::detectorType;

View File

@ -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
)