Skip to content

Commit

Permalink
Revise C++ namespace usage in the runtime component (#734)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Feb 9, 2024
1 parent 4bb3cab commit ebdfe6f
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 128 deletions.
2 changes: 1 addition & 1 deletion src/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ noa_library(NAMESPACE sourcemeta PROJECT jsonbinpack NAME runtime
PRIVATE_HEADERS
decoder.h decoder_basic.h
encoder.h encoder_basic.h encoder_context.h encoder_real.h
encoding.h encoding_tag.h encoding_wrap.h
encoding.h encoding_wrap.h
zigzag.h varint.h numeric.h)

if(JSONBINPACK_INSTALL)
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/include/sourcemeta/jsonbinpack/runtime_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include <sourcemeta/jsonbinpack/runtime_decoder_basic.h>
#include <sourcemeta/jsonbinpack/runtime_encoding.h>
#include <sourcemeta/jsonbinpack/runtime_encoding_tag.h>
#include <sourcemeta/jsonbinpack/runtime_encoding_wrap.h>
#include <sourcemeta/jsonbinpack/runtime_numeric.h>

Expand Down Expand Up @@ -427,7 +426,7 @@ class Decoder : private BasicDecoder<CharT, Traits> {

auto ANY_PACKED_TYPE_TAG_BYTE_PREFIX(const ANY_PACKED_TYPE_TAG_BYTE_PREFIX &)
-> sourcemeta::jsontoolkit::JSON {
using namespace tag::ANY_PACKED_TYPE_TAG_BYTE_PREFIX;
using namespace internal::ANY_PACKED_TYPE_TAG_BYTE_PREFIX;
const std::uint8_t byte{this->get_byte()};
const std::uint8_t type{
static_cast<std::uint8_t>(byte & (0xff >> subtype_size))};
Expand Down
7 changes: 3 additions & 4 deletions src/runtime/include/sourcemeta/jsonbinpack/runtime_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <sourcemeta/jsonbinpack/runtime_encoder_basic.h>
#include <sourcemeta/jsonbinpack/runtime_encoder_real.h>
#include <sourcemeta/jsonbinpack/runtime_encoding.h>
#include <sourcemeta/jsonbinpack/runtime_encoding_tag.h>
#include <sourcemeta/jsonbinpack/runtime_encoding_wrap.h>
#include <sourcemeta/jsonbinpack/runtime_numeric.h>

Expand Down Expand Up @@ -144,7 +143,7 @@ class Encoder : private BasicEncoder<CharT, Traits> {
const auto value{document.to_real()};
std::uint64_t point_position;
const std::int64_t integral{
encoder::real_digits<std::int64_t>(value, &point_position)};
real_digits<std::int64_t>(value, &point_position)};
this->put_varint_zigzag(integral);
this->put_varint(point_position);
}
Expand Down Expand Up @@ -459,7 +458,7 @@ class Encoder : private BasicEncoder<CharT, Traits> {
ANY_PACKED_TYPE_TAG_BYTE_PREFIX(const sourcemeta::jsontoolkit::JSON &document,
const ANY_PACKED_TYPE_TAG_BYTE_PREFIX &)
-> void {
using namespace tag::ANY_PACKED_TYPE_TAG_BYTE_PREFIX;
using namespace internal::ANY_PACKED_TYPE_TAG_BYTE_PREFIX;
if (document.is_null()) {
this->put_byte(TYPE_OTHER | (SUBTYPE_NULL << type_size));
} else if (document.is_boolean()) {
Expand Down Expand Up @@ -576,7 +575,7 @@ class Encoder : private BasicEncoder<CharT, Traits> {
/// @}

private:
using ContextType = typename encoder::Context<CharT>::Type;
using ContextType = typename Context<CharT>::Type;
};

} // namespace sourcemeta::jsonbinpack
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ template <typename CharT, typename Traits> class BasicEncoder {
}
}

inline auto context() -> encoder::Context<CharT> & { return this->context_; }
inline auto context() -> Context<CharT> & { return this->context_; }

private:
std::basic_ostream<CharT, Traits> &stream;
encoder::Context<CharT> context_;
Context<CharT> context_;
};

} // namespace sourcemeta::jsonbinpack
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static constexpr auto MINIMUM_STRING_LENGTH{3};
// document that contains a high number of large strings.
static constexpr auto MAXIMUM_BYTE_SIZE{20971520};

namespace sourcemeta::jsonbinpack::encoder {
namespace sourcemeta::jsonbinpack {

/// @ingroup encoder
template <typename CharT> class Context {
Expand Down Expand Up @@ -98,6 +98,6 @@ template <typename CharT> class Context {
std::uint64_t byte_size = 0;
};

} // namespace sourcemeta::jsonbinpack::encoder
} // namespace sourcemeta::jsonbinpack

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <cmath> // std::modf, std::floor, std::isfinite
#include <concepts> // std::floating_point, std::integral

namespace sourcemeta::jsonbinpack::encoder {
namespace sourcemeta::jsonbinpack {

// IEEE764 floating-point encoding is not precise. Some real numbers
// cannot be represented directly and thus approximations must be
Expand Down Expand Up @@ -47,6 +47,6 @@ constexpr auto real_digits(Real value, std::uint64_t *point_position)
return static_cast<Integer>(std::floor(integral));
}

} // namespace sourcemeta::jsonbinpack::encoder
} // namespace sourcemeta::jsonbinpack

#endif
51 changes: 51 additions & 0 deletions src/runtime/include/sourcemeta/jsonbinpack/runtime_encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/// @see sourcemeta::jsonbinpack::Encoder
/// @see sourcemeta::jsonbinpack::Decoder

#include <sourcemeta/jsonbinpack/runtime_numeric.h>
#include <sourcemeta/jsontoolkit/json.h>

#include <cstdint> // std::int64_t, std::uint64_t
Expand Down Expand Up @@ -962,6 +963,56 @@ struct VARINT_TYPED_ARBITRARY_OBJECT {

// TODO: Write brief description
struct ANY_PACKED_TYPE_TAG_BYTE_PREFIX {};
#ifndef DOXYGEN
namespace internal::ANY_PACKED_TYPE_TAG_BYTE_PREFIX {
constexpr auto type_size = 3;
constexpr std::uint8_t TYPE_SHARED_STRING = 0b00000000;
constexpr std::uint8_t TYPE_STRING = 0b00000001;
constexpr std::uint8_t TYPE_LONG_STRING = 0b00000010;
constexpr std::uint8_t TYPE_OBJECT = 0b00000011;
constexpr std::uint8_t TYPE_ARRAY = 0b00000100;
constexpr std::uint8_t TYPE_POSITIVE_INTEGER_BYTE = 0b00000101;
constexpr std::uint8_t TYPE_NEGATIVE_INTEGER_BYTE = 0b00000110;
constexpr std::uint8_t TYPE_OTHER = 0b00000111;
static_assert(TYPE_SHARED_STRING <= uint_max<type_size>);
static_assert(TYPE_STRING <= uint_max<type_size>);
static_assert(TYPE_LONG_STRING <= uint_max<type_size>);
static_assert(TYPE_OBJECT <= uint_max<type_size>);
static_assert(TYPE_ARRAY <= uint_max<type_size>);
static_assert(TYPE_POSITIVE_INTEGER_BYTE <= uint_max<type_size>);
static_assert(TYPE_NEGATIVE_INTEGER_BYTE <= uint_max<type_size>);
static_assert(TYPE_OTHER <= uint_max<type_size>);

constexpr auto subtype_size = 5;
constexpr std::uint8_t SUBTYPE_FALSE = 0b00000000;
constexpr std::uint8_t SUBTYPE_TRUE = 0b00000001;
constexpr std::uint8_t SUBTYPE_NULL = 0b00000010;
constexpr std::uint8_t SUBTYPE_POSITIVE_INTEGER = 0b00000011;
constexpr std::uint8_t SUBTYPE_NEGATIVE_INTEGER = 0b00000100;
constexpr std::uint8_t SUBTYPE_NUMBER = 0b00000101;
constexpr std::uint8_t SUBTYPE_LONG_STRING_BASE_EXPONENT_7 = 0b00000111;
constexpr std::uint8_t SUBTYPE_LONG_STRING_BASE_EXPONENT_8 = 0b00001000;
constexpr std::uint8_t SUBTYPE_LONG_STRING_BASE_EXPONENT_9 = 0b00001001;
constexpr std::uint8_t SUBTYPE_LONG_STRING_BASE_EXPONENT_10 = 0b00001010;

static_assert(SUBTYPE_FALSE <= uint_max<subtype_size>);
static_assert(SUBTYPE_TRUE <= uint_max<subtype_size>);
static_assert(SUBTYPE_NULL <= uint_max<subtype_size>);
static_assert(SUBTYPE_POSITIVE_INTEGER <= uint_max<subtype_size>);
static_assert(SUBTYPE_NEGATIVE_INTEGER <= uint_max<subtype_size>);
static_assert(SUBTYPE_NUMBER <= uint_max<subtype_size>);
static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_7 <= uint_max<subtype_size>);
static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_8 <= uint_max<subtype_size>);
static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_9 <= uint_max<subtype_size>);
static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_10 <= uint_max<subtype_size>);

// Note that the binary values actually match the declared exponents
static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_7 == 7);
static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_8 == 8);
static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_9 == 9);
static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_10 == 10);
} // namespace internal::ANY_PACKED_TYPE_TAG_BYTE_PREFIX
#endif

/// @}
// clang-format on
Expand Down
60 changes: 0 additions & 60 deletions src/runtime/include/sourcemeta/jsonbinpack/runtime_encoding_tag.h

This file was deleted.

44 changes: 22 additions & 22 deletions test/runtime/encode_context_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,47 @@
#include <sourcemeta/jsonbinpack/runtime_encoder_context.h>

TEST(Encoder, context_record_string) {
sourcemeta::jsonbinpack::encoder::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::encoder::Context<char>::Type;
sourcemeta::jsonbinpack::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::Context<char>::Type;
EXPECT_FALSE(context.has("foo", ContextType::Standalone));
context.record("foo", 2, ContextType::Standalone);
EXPECT_TRUE(context.has("foo", ContextType::Standalone));
EXPECT_EQ(context.offset("foo", ContextType::Standalone), 2);
}

TEST(Encoder, context_record_string_too_short) {
sourcemeta::jsonbinpack::encoder::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::encoder::Context<char>::Type;
sourcemeta::jsonbinpack::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::Context<char>::Type;
EXPECT_FALSE(context.has("fo", ContextType::Standalone));
context.record("fo", 2, ContextType::Standalone);
EXPECT_FALSE(context.has("fo", ContextType::Standalone));
}

TEST(Encoder, context_record_string_empty) {
sourcemeta::jsonbinpack::encoder::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::encoder::Context<char>::Type;
sourcemeta::jsonbinpack::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::Context<char>::Type;
EXPECT_FALSE(context.has("", ContextType::Standalone));
context.record("", 2, ContextType::Standalone);
EXPECT_FALSE(context.has("", ContextType::Standalone));
}

TEST(Encoder, context_has_on_unknown_string) {
sourcemeta::jsonbinpack::encoder::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::encoder::Context<char>::Type;
sourcemeta::jsonbinpack::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::Context<char>::Type;
EXPECT_FALSE(context.has("foobarbaz", ContextType::Standalone));
}

TEST(Encoder, context_increase_offset) {
sourcemeta::jsonbinpack::encoder::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::encoder::Context<char>::Type;
sourcemeta::jsonbinpack::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::Context<char>::Type;
context.record("foo", 2, ContextType::Standalone);
context.record("foo", 4, ContextType::Standalone);
EXPECT_EQ(context.offset("foo", ContextType::Standalone), 4);
}

TEST(Encoder, context_do_not_decrease_offset) {
sourcemeta::jsonbinpack::encoder::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::encoder::Context<char>::Type;
sourcemeta::jsonbinpack::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::Context<char>::Type;
context.record("foo", 4, ContextType::Standalone);
context.record("foo", 2, ContextType::Standalone);
EXPECT_EQ(context.offset("foo", ContextType::Standalone), 4);
Expand All @@ -57,15 +57,15 @@ TEST(Encoder, context_not_record_too_big) {
const auto length{25000000};
const std::string too_big(length, 'x');
EXPECT_EQ(too_big.size(), length);
sourcemeta::jsonbinpack::encoder::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::encoder::Context<char>::Type;
sourcemeta::jsonbinpack::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::Context<char>::Type;
context.record(too_big, 1, ContextType::Standalone);
EXPECT_FALSE(context.has(too_big, ContextType::Standalone));
}

TEST(Encoder, context_remove_oldest) {
sourcemeta::jsonbinpack::encoder::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::encoder::Context<char>::Type;
sourcemeta::jsonbinpack::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::Context<char>::Type;
context.record("foo", 10, ContextType::Standalone);
context.record("bar", 3, ContextType::Standalone);
context.record("baz", 7, ContextType::PrefixLengthVarintPlusOne);
Expand Down Expand Up @@ -109,8 +109,8 @@ TEST(Encoder, context_is_a_circular_buffer) {
EXPECT_EQ(string_5.size(), length);
EXPECT_EQ(string_6.size(), length);

sourcemeta::jsonbinpack::encoder::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::encoder::Context<char>::Type;
sourcemeta::jsonbinpack::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::Context<char>::Type;

context.record(string_1, length * 0, ContextType::Standalone);
context.record(string_2, length * 1, ContextType::Standalone);
Expand Down Expand Up @@ -141,8 +141,8 @@ TEST(Encoder, context_is_a_circular_buffer) {
}

TEST(Encoder, context_same_string_different_type) {
sourcemeta::jsonbinpack::encoder::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::encoder::Context<char>::Type;
sourcemeta::jsonbinpack::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::Context<char>::Type;
context.record("foo", 10, ContextType::Standalone);
context.record("foo", 20, ContextType::PrefixLengthVarintPlusOne);

Expand All @@ -154,8 +154,8 @@ TEST(Encoder, context_same_string_different_type) {
}

TEST(Encoder, context_no_fallback_type) {
sourcemeta::jsonbinpack::encoder::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::encoder::Context<char>::Type;
sourcemeta::jsonbinpack::Context<char> context;
using ContextType = sourcemeta::jsonbinpack::Context<char>::Type;
context.record("foo", 10, ContextType::Standalone);
EXPECT_TRUE(context.has("foo", ContextType::Standalone));
EXPECT_FALSE(context.has("foo", ContextType::PrefixLengthVarintPlusOne));
Expand Down
Loading

0 comments on commit ebdfe6f

Please sign in to comment.