♻️ overwork byte_container_with_subtype

This commit is contained in:
Niels Lohmann 2021-08-01 22:00:57 +02:00
parent eb488bb4d9
commit 7c19aa2210
No known key found for this signature in database
GPG Key ID: 7F3CEA63AE251B69
3 changed files with 115 additions and 14 deletions

View File

@ -26,6 +26,8 @@ class byte_container_with_subtype : public BinaryType
public:
/// the type of the underlying container
using container_type = BinaryType;
/// the type of the subtype
using subtype_type = std::uint8_t;
byte_container_with_subtype() noexcept(noexcept(container_type()))
: container_type()
@ -39,13 +41,13 @@ class byte_container_with_subtype : public BinaryType
: container_type(std::move(b))
{}
byte_container_with_subtype(const container_type& b, std::uint8_t subtype_) noexcept(noexcept(container_type(b)))
byte_container_with_subtype(const container_type& b, subtype_type subtype_) noexcept(noexcept(container_type(b)))
: container_type(b)
, m_subtype(subtype_)
, m_has_subtype(true)
{}
byte_container_with_subtype(container_type&& b, std::uint8_t subtype_) noexcept(noexcept(container_type(std::move(b))))
byte_container_with_subtype(container_type&& b, subtype_type subtype_) noexcept(noexcept(container_type(std::move(b))))
: container_type(std::move(b))
, m_subtype(subtype_)
, m_has_subtype(true)
@ -80,7 +82,7 @@ class byte_container_with_subtype : public BinaryType
@since version 3.8.0
*/
void set_subtype(std::uint8_t subtype_) noexcept
void set_subtype(subtype_type subtype_) noexcept
{
m_subtype = subtype_;
m_has_subtype = true;
@ -90,7 +92,7 @@ class byte_container_with_subtype : public BinaryType
@brief return the binary subtype
Returns the numerical subtype of the value if it has a subtype. If it does
not have a subtype, this function will return size_t(-1) as a sentinel
not have a subtype, this function will return subtype_type(-1) as a sentinel
value.
@return the numerical subtype of the binary value
@ -107,9 +109,9 @@ class byte_container_with_subtype : public BinaryType
@since version 3.8.0
*/
constexpr std::uint8_t subtype() const noexcept
constexpr subtype_type subtype() const noexcept
{
return m_subtype;
return m_has_subtype ? m_subtype : subtype_type(-1);
}
/*!
@ -159,7 +161,7 @@ class byte_container_with_subtype : public BinaryType
}
private:
std::uint8_t m_subtype = 0;
subtype_type m_subtype = 0;
bool m_has_subtype = false;
};

View File

@ -4982,6 +4982,8 @@ class byte_container_with_subtype : public BinaryType
public:
/// the type of the underlying container
using container_type = BinaryType;
/// the type of the subtype
using subtype_type = std::uint8_t;
byte_container_with_subtype() noexcept(noexcept(container_type()))
: container_type()
@ -4995,13 +4997,13 @@ class byte_container_with_subtype : public BinaryType
: container_type(std::move(b))
{}
byte_container_with_subtype(const container_type& b, std::uint8_t subtype_) noexcept(noexcept(container_type(b)))
byte_container_with_subtype(const container_type& b, subtype_type subtype_) noexcept(noexcept(container_type(b)))
: container_type(b)
, m_subtype(subtype_)
, m_has_subtype(true)
{}
byte_container_with_subtype(container_type&& b, std::uint8_t subtype_) noexcept(noexcept(container_type(std::move(b))))
byte_container_with_subtype(container_type&& b, subtype_type subtype_) noexcept(noexcept(container_type(std::move(b))))
: container_type(std::move(b))
, m_subtype(subtype_)
, m_has_subtype(true)
@ -5036,7 +5038,7 @@ class byte_container_with_subtype : public BinaryType
@since version 3.8.0
*/
void set_subtype(std::uint8_t subtype_) noexcept
void set_subtype(subtype_type subtype_) noexcept
{
m_subtype = subtype_;
m_has_subtype = true;
@ -5046,7 +5048,7 @@ class byte_container_with_subtype : public BinaryType
@brief return the binary subtype
Returns the numerical subtype of the value if it has a subtype. If it does
not have a subtype, this function will return size_t(-1) as a sentinel
not have a subtype, this function will return subtype_type(-1) as a sentinel
value.
@return the numerical subtype of the binary value
@ -5063,9 +5065,9 @@ class byte_container_with_subtype : public BinaryType
@since version 3.8.0
*/
constexpr std::uint8_t subtype() const noexcept
constexpr subtype_type subtype() const noexcept
{
return m_subtype;
return m_has_subtype ? m_subtype : subtype_type(-1);
}
/*!
@ -5115,7 +5117,7 @@ class byte_container_with_subtype : public BinaryType
}
private:
std::uint8_t m_subtype = 0;
subtype_type m_subtype = 0;
bool m_has_subtype = false;
};

View File

@ -0,0 +1,97 @@
/*
__ _____ _____ _____
__| | __| | | | JSON for Modern C++ (test suite)
| | |__ | | | | | | version 3.9.1
|_____|_____|_____|_|___| https://github.com/nlohmann/json
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
SPDX-License-Identifier: MIT
Copyright (c) 2013-2019 Niels Lohmann <http://nlohmann.me>.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "doctest_compatibility.h"
#include <nlohmann/json.hpp>
using nlohmann::json;
TEST_CASE("byte_container_with_subtype")
{
using subtype_type = nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>>::subtype_type;
SECTION("empty container")
{
nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>> container;
CHECK(!container.has_subtype());
CHECK(container.subtype() == subtype_type(-1));
container.clear_subtype();
CHECK(!container.has_subtype());
CHECK(container.subtype() == subtype_type(-1));
container.set_subtype(42);
CHECK(container.has_subtype());
CHECK(container.subtype() == 42);
}
SECTION("subtyped container")
{
nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>> container({}, 42);
CHECK(container.has_subtype());
CHECK(container.subtype() == 42);
container.clear_subtype();
CHECK(!container.has_subtype());
CHECK(container.subtype() == subtype_type(-1));
}
SECTION("comparisons")
{
std::vector<std::uint8_t> bytes = {{0xCA, 0xFE, 0xBA, 0xBE}};
nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>> container1;
nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>> container2({}, 42);
nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>> container3(bytes);
nlohmann::byte_container_with_subtype<std::vector<std::uint8_t>> container4(bytes, 42);
CHECK(container1 == container1);
CHECK(container1 != container2);
CHECK(container1 != container3);
CHECK(container1 != container4);
CHECK(container2 != container1);
CHECK(container2 == container2);
CHECK(container2 != container3);
CHECK(container2 != container4);
CHECK(container3 != container1);
CHECK(container3 != container2);
CHECK(container3 == container3);
CHECK(container3 != container4);
CHECK(container4 != container1);
CHECK(container4 != container2);
CHECK(container4 != container3);
CHECK(container4 == container4);
container3.clear();
container4.clear();
CHECK(container1 == container3);
CHECK(container2 == container4);
}
}