Skip to content

Commit 809acb2

Browse files
committed
Make buffer constructors and functions constexpr
1 parent 99d98dd commit 809acb2

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

tests/buffer.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ using BT = int16_t;
1414

1515
TEST_CASE("buffer default ctor", "[buffer]")
1616
{
17-
zmq::mutable_buffer mb;
18-
zmq::const_buffer cb;
17+
constexpr zmq::mutable_buffer mb;
18+
constexpr zmq::const_buffer cb;
1919
CHECK(mb.size() == 0);
2020
CHECK(mb.data() == nullptr);
2121
CHECK(cb.size() == 0);
@@ -36,6 +36,13 @@ TEST_CASE("buffer data ctor", "[buffer]")
3636
CHECK(mb.data() == from_mut.data());
3737
const auto cmb = mb;
3838
static_assert(std::is_same<decltype(cmb.data()), void*>::value, "");
39+
40+
constexpr const void* cp = nullptr;
41+
constexpr void* p = nullptr;
42+
constexpr zmq::const_buffer cecb = zmq::buffer(p, 0);
43+
constexpr zmq::mutable_buffer cemb = zmq::buffer(p, 0);
44+
CHECK(cecb.data() == nullptr);
45+
CHECK(cemb.data() == nullptr);
3946
}
4047

4148
TEST_CASE("const_buffer operator+", "[buffer]")

zmq.hpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -734,14 +734,14 @@ constexpr recv_flags operator~(recv_flags a) noexcept
734734
class mutable_buffer
735735
{
736736
public:
737-
mutable_buffer() noexcept : _data(nullptr), _size(0) {}
738-
mutable_buffer(void *p, size_t n) noexcept : _data(p), _size(n)
737+
constexpr mutable_buffer() noexcept : _data(nullptr), _size(0) {}
738+
constexpr mutable_buffer(void *p, size_t n) noexcept : _data(p), _size(n)
739739
{
740740
assert(p != nullptr || n == 0);
741741
}
742742

743-
void *data() const noexcept { return _data; }
744-
size_t size() const noexcept { return _size; }
743+
constexpr void *data() const noexcept { return _data; }
744+
constexpr size_t size() const noexcept { return _size; }
745745
mutable_buffer &operator+=(size_t n) noexcept
746746
{
747747
// (std::min) is a workaround for when a min macro is defined
@@ -769,16 +769,16 @@ inline mutable_buffer operator+(size_t n, const mutable_buffer &mb) noexcept
769769
class const_buffer
770770
{
771771
public:
772-
const_buffer() noexcept : _data(nullptr), _size(0) {}
773-
const_buffer(const void *p, size_t n) noexcept : _data(p), _size(n) {}
774-
const_buffer(const mutable_buffer &mb) noexcept :
772+
constexpr const_buffer() noexcept : _data(nullptr), _size(0) {}
773+
constexpr const_buffer(const void *p, size_t n) noexcept : _data(p), _size(n) {}
774+
constexpr const_buffer(const mutable_buffer &mb) noexcept :
775775
_data(mb.data()),
776776
_size(mb.size())
777777
{
778778
}
779779

780-
const void *data() const noexcept { return _data; }
781-
size_t size() const noexcept { return _size; }
780+
constexpr const void *data() const noexcept { return _data; }
781+
constexpr size_t size() const noexcept { return _size; }
782782
const_buffer &operator+=(size_t n) noexcept
783783
{
784784
const auto shift = (std::min)(n, _size);
@@ -805,27 +805,27 @@ inline const_buffer operator+(size_t n, const const_buffer &cb) noexcept
805805

806806
// buffer creation
807807

808-
inline mutable_buffer buffer(void* p, size_t n) noexcept
808+
constexpr mutable_buffer buffer(void* p, size_t n) noexcept
809809
{
810810
return mutable_buffer(p, n);
811811
}
812-
inline const_buffer buffer(const void* p, size_t n) noexcept
812+
constexpr const_buffer buffer(const void* p, size_t n) noexcept
813813
{
814814
return const_buffer(p, n);
815815
}
816-
inline mutable_buffer buffer(const mutable_buffer& mb) noexcept
816+
constexpr mutable_buffer buffer(const mutable_buffer& mb) noexcept
817817
{
818818
return mb;
819819
}
820-
inline mutable_buffer buffer(const mutable_buffer& mb, size_t n) noexcept
820+
constexpr mutable_buffer buffer(const mutable_buffer& mb, size_t n) noexcept
821821
{
822822
return mutable_buffer(mb.data(), (std::min)(mb.size(), n));
823823
}
824-
inline const_buffer buffer(const const_buffer& cb) noexcept
824+
constexpr const_buffer buffer(const const_buffer& cb) noexcept
825825
{
826826
return cb;
827827
}
828-
inline const_buffer buffer(const const_buffer& cb, size_t n) noexcept
828+
constexpr const_buffer buffer(const const_buffer& cb, size_t n) noexcept
829829
{
830830
return const_buffer(cb.data(), (std::min)(cb.size(), n));
831831
}

0 commit comments

Comments
 (0)