Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constexprify basic_memory_buffer, bit_cast(), make_checked(), fp, bigit #2470

Merged
merged 5 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ jobs:
build_type: Debug
std: 17
os: ubuntu-18.04
- cxx: g++-10
- cxx: g++-11
build_type: Debug
std: 20
os: ubuntu-20.04
install: sudo apt install g++-11
- cxx: clang++-9
build_type: Debug
fuzz: -DFMT_FUZZ=ON -DFMT_FUZZ_LINKMAIN=ON
Expand Down
42 changes: 26 additions & 16 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@
# define FMT_CONSTEXPR_DECL
#endif

#if __cplusplus >= 202002L || \
(__cplusplus >= 201709L && FMT_GCC_VERSION >= 1002)
# define FMT_CONSTEXPR20 constexpr
#else
# define FMT_CONSTEXPR20
#endif

// Check if constexpr std::char_traits<>::compare,length is supported.
#if defined(__GLIBCXX__)
# if __cplusplus >= 201703L && defined(_GLIBCXX_RELEASE) && \
Expand Down Expand Up @@ -773,22 +780,22 @@ template <typename T> class buffer {
FMT_MSC_WARNING(suppress : 26495)
buffer(size_t sz) FMT_NOEXCEPT : size_(sz), capacity_(sz) {}

buffer(T* p = nullptr, size_t sz = 0, size_t cap = 0) FMT_NOEXCEPT
: ptr_(p),
size_(sz),
capacity_(cap) {}
FMT_CONSTEXPR20 buffer(T* p = nullptr, size_t sz = 0,
size_t cap = 0) FMT_NOEXCEPT : ptr_(p),
size_(sz),
capacity_(cap) {}

~buffer() = default;
FMT_CONSTEXPR20 ~buffer() = default;
buffer(buffer&&) = default;

/** Sets the buffer data and capacity. */
void set(T* buf_data, size_t buf_capacity) FMT_NOEXCEPT {
FMT_CONSTEXPR void set(T* buf_data, size_t buf_capacity) FMT_NOEXCEPT {
ptr_ = buf_data;
capacity_ = buf_capacity;
}

/** Increases the buffer capacity to hold at least *capacity* elements. */
virtual void grow(size_t capacity) = 0;
virtual FMT_CONSTEXPR20 void grow(size_t capacity) = 0;

public:
using value_type = T;
Expand All @@ -804,23 +811,23 @@ template <typename T> class buffer {
auto end() const FMT_NOEXCEPT -> const T* { return ptr_ + size_; }

/** Returns the size of this buffer. */
auto size() const FMT_NOEXCEPT -> size_t { return size_; }
constexpr auto size() const FMT_NOEXCEPT -> size_t { return size_; }

/** Returns the capacity of this buffer. */
auto capacity() const FMT_NOEXCEPT -> size_t { return capacity_; }
constexpr auto capacity() const FMT_NOEXCEPT -> size_t { return capacity_; }

/** Returns a pointer to the buffer data. */
auto data() FMT_NOEXCEPT -> T* { return ptr_; }
FMT_CONSTEXPR auto data() FMT_NOEXCEPT -> T* { return ptr_; }

/** Returns a pointer to the buffer data. */
auto data() const FMT_NOEXCEPT -> const T* { return ptr_; }
FMT_CONSTEXPR auto data() const FMT_NOEXCEPT -> const T* { return ptr_; }

/** Clears this buffer. */
void clear() { size_ = 0; }

// Tries resizing the buffer to contain *count* elements. If T is a POD type
// the new elements may not be initialized.
void try_resize(size_t count) {
FMT_CONSTEXPR20 void try_resize(size_t count) {
try_reserve(count);
size_ = count <= capacity_ ? count : capacity_;
}
Expand All @@ -829,20 +836,23 @@ template <typename T> class buffer {
// capacity by a smaller amount than requested but guarantees there is space
// for at least one additional element either by increasing the capacity or by
// flushing the buffer if it is full.
void try_reserve(size_t new_capacity) {
FMT_CONSTEXPR20 void try_reserve(size_t new_capacity) {
if (new_capacity > capacity_) grow(new_capacity);
}

void push_back(const T& value) {
FMT_CONSTEXPR20 void push_back(const T& value) {
try_reserve(size_ + 1);
ptr_[size_++] = value;
}

/** Appends data to the end of the buffer. */
template <typename U> void append(const U* begin, const U* end);

template <typename I> auto operator[](I index) -> T& { return ptr_[index]; }
template <typename I> auto operator[](I index) const -> const T& {
template <typename I> FMT_CONSTEXPR auto operator[](I index) -> T& {
return ptr_[index];
}
template <typename I>
FMT_CONSTEXPR auto operator[](I index) const -> const T& {
return ptr_[index];
}
};
Expand Down
Loading