[Bf-blender-cvs] [d23a5b1d88d] master: BLI: constexpr Span, IndexRange, StringRef(Null/Base)
Ankit Meel
noreply at git.blender.org
Wed Dec 16 08:34:24 CET 2020
Commit: d23a5b1d88df513aff1dc1ba09730221567f2857
Author: Ankit Meel
Date: Wed Dec 16 13:02:28 2020 +0530
Branches: master
https://developer.blender.org/rBd23a5b1d88df513aff1dc1ba09730221567f2857
BLI: constexpr Span, IndexRange, StringRef(Null/Base)
Motivated by `std::string_view` being usable in
const (compile-time) context.
One functional change was needed for StringRef:
`std::char_traits<char>::length(str)` instead of `strlen`.
Reviewed By: JacquesLucke, LazyDodo
Differential Revision: https://developer.blender.org/D9788
===================================================================
M source/blender/blenlib/BLI_index_range.hh
M source/blender/blenlib/BLI_span.hh
M source/blender/blenlib/BLI_string_ref.hh
M source/blender/blenlib/tests/BLI_index_range_test.cc
M source/blender/blenlib/tests/BLI_span_test.cc
M source/blender/blenlib/tests/BLI_string_ref_test.cc
===================================================================
diff --git a/source/blender/blenlib/BLI_index_range.hh b/source/blender/blenlib/BLI_index_range.hh
index 2b060c986cd..4121542c887 100644
--- a/source/blender/blenlib/BLI_index_range.hh
+++ b/source/blender/blenlib/BLI_index_range.hh
@@ -73,21 +73,22 @@ class IndexRange {
int64_t size_ = 0;
public:
- IndexRange() = default;
+ constexpr IndexRange() = default;
- explicit IndexRange(int64_t size) : start_(0), size_(size)
+ constexpr explicit IndexRange(int64_t size) : start_(0), size_(size)
{
BLI_assert(size >= 0);
}
- IndexRange(int64_t start, int64_t size) : start_(start), size_(size)
+ constexpr IndexRange(int64_t start, int64_t size) : start_(start), size_(size)
{
BLI_assert(start >= 0);
BLI_assert(size >= 0);
}
template<typename T>
- IndexRange(const tbb::blocked_range<T> &range) : start_(range.begin()), size_(range.size())
+ constexpr IndexRange(const tbb::blocked_range<T> &range)
+ : start_(range.begin()), size_(range.size())
{
}
@@ -96,33 +97,33 @@ class IndexRange {
int64_t current_;
public:
- Iterator(int64_t current) : current_(current)
+ constexpr Iterator(int64_t current) : current_(current)
{
}
- Iterator &operator++()
+ constexpr Iterator &operator++()
{
current_++;
return *this;
}
- bool operator!=(const Iterator &iterator) const
+ constexpr bool operator!=(const Iterator &iterator) const
{
return current_ != iterator.current_;
}
- int64_t operator*() const
+ constexpr int64_t operator*() const
{
return current_;
}
};
- Iterator begin() const
+ constexpr Iterator begin() const
{
return Iterator(start_);
}
- Iterator end() const
+ constexpr Iterator end() const
{
return Iterator(start_ + size_);
}
@@ -130,7 +131,7 @@ class IndexRange {
/**
* Access an element in the range.
*/
- int64_t operator[](int64_t index) const
+ constexpr int64_t operator[](int64_t index) const
{
BLI_assert(index >= 0);
BLI_assert(index < this->size());
@@ -140,7 +141,7 @@ class IndexRange {
/**
* Two ranges compare equal when they contain the same numbers.
*/
- friend bool operator==(IndexRange a, IndexRange b)
+ constexpr friend bool operator==(IndexRange a, IndexRange b)
{
return (a.size_ == b.size_) && (a.start_ == b.start_ || a.size_ == 0);
}
@@ -148,7 +149,7 @@ class IndexRange {
/**
* Get the amount of numbers in the range.
*/
- int64_t size() const
+ constexpr int64_t size() const
{
return size_;
}
@@ -156,7 +157,7 @@ class IndexRange {
/**
* Create a new range starting at the end of the current one.
*/
- IndexRange after(int64_t n) const
+ constexpr IndexRange after(int64_t n) const
{
BLI_assert(n >= 0);
return IndexRange(start_ + size_, n);
@@ -165,7 +166,7 @@ class IndexRange {
/**
* Create a new range that ends at the start of the current one.
*/
- IndexRange before(int64_t n) const
+ constexpr IndexRange before(int64_t n) const
{
BLI_assert(n >= 0);
return IndexRange(start_ - n, n);
@@ -175,7 +176,7 @@ class IndexRange {
* Get the first element in the range.
* Asserts when the range is empty.
*/
- int64_t first() const
+ constexpr int64_t first() const
{
BLI_assert(this->size() > 0);
return start_;
@@ -185,7 +186,7 @@ class IndexRange {
* Get the last element in the range.
* Asserts when the range is empty.
*/
- int64_t last() const
+ constexpr int64_t last() const
{
BLI_assert(this->size() > 0);
return start_ + size_ - 1;
@@ -194,7 +195,7 @@ class IndexRange {
/**
* Get the element one after the end. The returned value is undefined when the range is empty.
*/
- int64_t one_after_last() const
+ constexpr int64_t one_after_last() const
{
return start_ + size_;
}
@@ -202,7 +203,7 @@ class IndexRange {
/**
* Get the first element in the range. The returned value is undefined when the range is empty.
*/
- int64_t start() const
+ constexpr int64_t start() const
{
return start_;
}
@@ -210,7 +211,7 @@ class IndexRange {
/**
* Returns true when the range contains a certain number, otherwise false.
*/
- bool contains(int64_t value) const
+ constexpr bool contains(int64_t value) const
{
return value >= start_ && value < start_ + size_;
}
@@ -218,7 +219,7 @@ class IndexRange {
/**
* Returns a new range, that contains a sub-interval of the current one.
*/
- IndexRange slice(int64_t start, int64_t size) const
+ constexpr IndexRange slice(int64_t start, int64_t size) const
{
BLI_assert(start >= 0);
BLI_assert(size >= 0);
@@ -226,7 +227,7 @@ class IndexRange {
BLI_assert(new_start + size <= start_ + size_ || size == 0);
return IndexRange(new_start, size);
}
- IndexRange slice(IndexRange range) const
+ constexpr IndexRange slice(IndexRange range) const
{
return this->slice(range.start(), range.size());
}
diff --git a/source/blender/blenlib/BLI_span.hh b/source/blender/blenlib/BLI_span.hh
index 5b4d2769f57..3f410efe908 100644
--- a/source/blender/blenlib/BLI_span.hh
+++ b/source/blender/blenlib/BLI_span.hh
@@ -93,15 +93,15 @@ template<typename T> class Span {
/**
* Create a reference to an empty array.
*/
- Span() = default;
+ constexpr Span() = default;
- Span(const T *start, int64_t size) : data_(start), size_(size)
+ constexpr Span(const T *start, int64_t size) : data_(start), size_(size)
{
BLI_assert(size >= 0);
}
template<typename U, typename std::enable_if_t<is_convertible_pointer_v<U, T>> * = nullptr>
- Span(const U *start, int64_t size) : data_(static_cast<const T *>(start)), size_(size)
+ constexpr Span(const U *start, int64_t size) : data_(static_cast<const T *>(start)), size_(size)
{
BLI_assert(size >= 0);
}
@@ -117,16 +117,17 @@ template<typename T> class Span {
* Span<int> span = {1, 2, 3, 4};
* call_function_with_array(span);
*/
- Span(const std::initializer_list<T> &list)
+ constexpr Span(const std::initializer_list<T> &list)
: Span(list.begin(), static_cast<int64_t>(list.size()))
{
}
- Span(const std::vector<T> &vector) : Span(vector.data(), static_cast<int64_t>(vector.size()))
+ constexpr Span(const std::vector<T> &vector)
+ : Span(vector.data(), static_cast<int64_t>(vector.size()))
{
}
- template<std::size_t N> Span(const std::array<T, N> &array) : Span(array.data(), N)
+ template<std::size_t N> constexpr Span(const std::array<T, N> &array) : Span(array.data(), N)
{
}
@@ -135,7 +136,7 @@ template<typename T> class Span {
* Span<T *> -> Span<const T *>
*/
template<typename U, typename std::enable_if_t<is_convertible_pointer_v<U, T>> * = nullptr>
- Span(Span<U> array) : data_(static_cast<const T *>(array.data())), size_(array.size())
+ constexpr Span(Span<U> array) : data_(static_cast<const T *>(array.data())), size_(array.size())
{
}
@@ -143,7 +144,7 @@ template<typename T> class Span {
* Returns a contiguous part of the array. This invokes undefined behavior when the slice does
* not stay within the bounds of the array.
*/
- Span slice(int64_t start, int64_t size) const
+ constexpr Span slice(int64_t start, int64_t size) const
{
BLI_assert(start >= 0);
BLI_assert(size >= 0);
@@ -151,7 +152,7 @@ template<typename T> class Span {
return Span(data_ + start, size);
}
- Span slice(IndexRange range) const
+ constexpr Span slice(IndexRange range) const
{
return this->slice(range.start(), range.size());
}
@@ -160,7 +161,7 @@ template<typename T> class Span {
* Returns a new Span with n elements removed from the beginning. This invokes undefined
* behavior when the array is too small.
*/
- Span drop_front(int64_t n) const
+ constexpr Span drop_front(int64_t n) const
{
BLI_assert(n >= 0);
BLI_assert(n <= this->size());
@@ -171,7 +172,7 @@ template<typename T> class Span {
* Returns a new Span with n elements removed from the beginning. This invokes undefined
* behavior when the array is too small.
*/
- Span drop_back(int64_t n) const
+ constexpr Span drop_back(int64_t n) const
{
BLI_assert(n >= 0);
BLI_assert(n <= this->size());
@@ -182,7 +183,7 @@ template<typename T> class Span {
* Returns a new Span that only contains the first n elements. This invokes undefined
* behavior when the array is too small.
*/
- Span take_front(int64_t n) const
+ constexpr Span take_front(int64_t n) const
{
BLI_assert(n >= 0);
BLI_assert(n <= this->size());
@@ -193,7 +194,7 @@ template<typename T> class Span {
* Returns a new Span that only contains the last n elements. This invokes undefined
* behavior when the array is too small.
*/
- Span take_back(int64_t n) const
+ constexpr Span take_back(int64_t n) const
{
BLI_assert(n >= 0);
BLI_assert(n <= this->size());
@@ -204,25 +205,25 @@ template<typename T> class Span {
* Returns the pointer to the beginning of the referenced array. This may be nullptr when the
* size is zero.
*/
- const T *data() const
+ constexpr const T *data() const
{
return data_;
}
- const T *begin() const
+ constexpr const T *begin() const
{
return data_;
}
- const T *end() const
+ constexpr const T *end() const
{
return data_ + size_;
}
- std::reverse_iterator<const T *> rbegin() const
+ constexpr std::reverse_iterator<const T *> rbegin() const
{
return std::reverse_iterator<const T *>(this->end());
}
- std::reverse_iterator<const T *> rend() const
+ constexpr std::reverse_iterator<const T *> rend() const
{
return std::reverse_iterator<const T *>(this->begin());
}
@@ -231,7 +232,7 @@ template<typename T> class Span {
* Access an element in the array. This invokes undefined behavior when the index is out of
* bounds.
*/
- const T &operator[](int64_t index) const
+ constexpr const T &operator[](int64_t index) const
{
BLI_assert(index >= 0);
BLI_assert(index < size_);
@@
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list