Skip to content

Commit

Permalink
Rename Plan to Encoding (#842)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Oct 8, 2024
1 parent 59258d2 commit 3dc921a
Show file tree
Hide file tree
Showing 34 changed files with 140 additions and 163 deletions.
6 changes: 4 additions & 2 deletions src/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
noa_library(NAMESPACE sourcemeta PROJECT jsonbinpack NAME runtime
FOLDER "JSON BinPack/Runtime"
PRIVATE_HEADERS
decoder.h encoder.h
decoder.h
encoder.h
input_stream.h
output_stream.h
encoder_context.h
plan.h plan_wrap.h
encoding.h
encoding_wrap.h
SOURCES
input_stream.cc
output_stream.cc
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/decoder_any.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <sourcemeta/jsonbinpack/numeric.h>
#include <sourcemeta/jsonbinpack/runtime_decoder.h>

#include <sourcemeta/jsonbinpack/runtime_plan_wrap.h>
#include <sourcemeta/jsonbinpack/runtime_encoding_wrap.h>

#include "unreachable.h"

Expand Down
6 changes: 3 additions & 3 deletions src/runtime/decoder_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ auto Decoder::FIXED_TYPED_ARRAY(const struct FIXED_TYPED_ARRAY &options)
sourcemeta::jsontoolkit::JSON result =
sourcemeta::jsontoolkit::JSON::make_array();
for (std::size_t index = 0; index < options.size; index++) {
const Plan &encoding{prefix_encodings > index
? options.prefix_encodings[index].value
: options.encoding->value};
const Encoding &encoding{prefix_encodings > index
? options.prefix_encodings[index].value
: options.encoding->value};
result.push_back(this->read(encoding));
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/decoder_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace sourcemeta::jsonbinpack {

Decoder::Decoder(Stream &input) : InputStream{input} {}

auto Decoder::read(const Plan &encoding) -> sourcemeta::jsontoolkit::JSON {
auto Decoder::read(const Encoding &encoding) -> sourcemeta::jsontoolkit::JSON {
switch (encoding.index()) {
#define HANDLE_DECODING(index, name) \
case (index): \
Expand Down
9 changes: 5 additions & 4 deletions src/runtime/encoder_any.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <sourcemeta/jsonbinpack/numeric.h>
#include <sourcemeta/jsonbinpack/runtime_encoder.h>
#include <sourcemeta/jsonbinpack/runtime_plan_wrap.h>
#include <sourcemeta/jsonbinpack/runtime_encoding_wrap.h>

#include "unreachable.h"

Expand Down Expand Up @@ -159,7 +159,8 @@ auto Encoder::ANY_PACKED_TYPE_TAG_BYTE_PREFIX(
static_cast<std::uint8_t>(TYPE_ARRAY | ((size + 1) << type_size)));
}

Plan encoding{sourcemeta::jsonbinpack::ANY_PACKED_TYPE_TAG_BYTE_PREFIX{}};
Encoding encoding{
sourcemeta::jsonbinpack::ANY_PACKED_TYPE_TAG_BYTE_PREFIX{}};
this->FIXED_TYPED_ARRAY(document, {size, wrap(std::move(encoding)), {}});
} else if (document.is_object()) {
const auto size{document.size()};
Expand All @@ -171,9 +172,9 @@ auto Encoder::ANY_PACKED_TYPE_TAG_BYTE_PREFIX(
static_cast<std::uint8_t>(TYPE_OBJECT | ((size + 1) << type_size)));
}

Plan key_encoding{
Encoding key_encoding{
sourcemeta::jsonbinpack::PREFIX_VARINT_LENGTH_STRING_SHARED{}};
Plan value_encoding{
Encoding value_encoding{
sourcemeta::jsonbinpack::ANY_PACKED_TYPE_TAG_BYTE_PREFIX{}};
this->FIXED_TYPED_ARBITRARY_OBJECT(
document,
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/encoder_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ auto Encoder::FIXED_TYPED_ARRAY(const sourcemeta::jsontoolkit::JSON &document,
const auto prefix_encodings{options.prefix_encodings.size()};
assert(prefix_encodings <= document.size());
for (std::size_t index = 0; index < options.size; index++) {
const Plan &encoding{prefix_encodings > index
? options.prefix_encodings[index].value
: options.encoding->value};
const Encoding &encoding{prefix_encodings > index
? options.prefix_encodings[index].value
: options.encoding->value};
this->write(document.at(index), encoding);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/encoder_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace sourcemeta::jsonbinpack {
Encoder::Encoder(Stream &output) : OutputStream{output} {}

auto Encoder::write(const sourcemeta::jsontoolkit::JSON &document,
const Plan &encoding) -> void {
const Encoding &encoding) -> void {
switch (encoding.index()) {
#define HANDLE_ENCODING(index, name) \
case (index): \
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/include/sourcemeta/jsonbinpack/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

#include <sourcemeta/jsonbinpack/runtime_decoder.h>
#include <sourcemeta/jsonbinpack/runtime_encoder.h>
#include <sourcemeta/jsonbinpack/runtime_plan.h>
#include <sourcemeta/jsonbinpack/runtime_encoding.h>

namespace sourcemeta::jsonbinpack {

/// @ingroup runtime
SOURCEMETA_JSONBINPACK_RUNTIME_EXPORT
auto load(const sourcemeta::jsontoolkit::JSON &input) -> Plan;
auto load(const sourcemeta::jsontoolkit::JSON &input) -> Encoding;

} // namespace sourcemeta::jsonbinpack

Expand Down
4 changes: 2 additions & 2 deletions src/runtime/include/sourcemeta/jsonbinpack/runtime_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include "runtime_export.h"

#include <sourcemeta/jsonbinpack/runtime_encoding.h>
#include <sourcemeta/jsonbinpack/runtime_input_stream.h>
#include <sourcemeta/jsonbinpack/runtime_plan.h>

#include <sourcemeta/jsontoolkit/json.h>

Expand All @@ -14,7 +14,7 @@ namespace sourcemeta::jsonbinpack {
class SOURCEMETA_JSONBINPACK_RUNTIME_EXPORT Decoder : private InputStream {
public:
Decoder(Stream &input);
auto read(const Plan &encoding) -> sourcemeta::jsontoolkit::JSON;
auto read(const Encoding &encoding) -> sourcemeta::jsontoolkit::JSON;

// The methods that implement individual encodings as considered private
#ifndef DOXYGEN
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/include/sourcemeta/jsonbinpack/runtime_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include "runtime_export.h"

#include <sourcemeta/jsonbinpack/runtime_encoder_context.h>
#include <sourcemeta/jsonbinpack/runtime_encoding.h>
#include <sourcemeta/jsonbinpack/runtime_output_stream.h>
#include <sourcemeta/jsonbinpack/runtime_plan.h>

#include <sourcemeta/jsontoolkit/json.h>

Expand All @@ -16,7 +16,7 @@ class SOURCEMETA_JSONBINPACK_RUNTIME_EXPORT Encoder : private OutputStream {
public:
Encoder(Stream &output);
auto write(const sourcemeta::jsontoolkit::JSON &document,
const Plan &encoding) -> void;
const Encoding &encoding) -> void;

// The methods that implement individual encodings as considered private
#ifndef DOXYGEN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@

namespace sourcemeta::jsonbinpack {

// We cannot directly create an Plan variant type whose values potentially
// include other Plan instances. As a workaround, we have a helper
// We cannot directly create an Encoding variant type whose values potentially
// include other Encoding instances. As a workaround, we have a helper
// encoding wrapper that we can use as an incomplete type
struct __internal_encoding_wrapper;
// Use these alias types. Never use the internal wrapper type directly
using SinglePlan = std::shared_ptr<__internal_encoding_wrapper>;
using MultiplePlans = std::vector<__internal_encoding_wrapper>;
using SingleEncoding = std::shared_ptr<__internal_encoding_wrapper>;
using MultipleEncodings = std::vector<__internal_encoding_wrapper>;

/// @ingroup runtime
/// @defgroup plan_integer Integer Encodings
/// @defgroup encoding_integer Integer Encodings
/// @{

// clang-format off
Expand Down Expand Up @@ -179,7 +179,7 @@ struct ARBITRARY_MULTIPLE_ZIGZAG_VARINT {
/// @}

/// @ingroup runtime
/// @defgroup plan_number Number Encodings
/// @defgroup encoding_number Number Encodings
/// @{

// clang-format off
Expand Down Expand Up @@ -226,7 +226,7 @@ struct DOUBLE_VARINT_TUPLE {};
/// @}

/// @ingroup runtime
/// @defgroup plan_enum Enumeration Encodings
/// @defgroup encoding_enum Enumeration Encodings
/// @{

// clang-format off
Expand Down Expand Up @@ -370,7 +370,7 @@ struct CONST_NONE {
/// @}

/// @ingroup runtime
/// @defgroup plan_string String Encodings
/// @defgroup encoding_string String Encodings
/// @{

// clang-format off
Expand Down Expand Up @@ -661,7 +661,7 @@ struct PREFIX_VARINT_LENGTH_STRING_SHARED {};
/// @}

/// @ingroup runtime
/// @defgroup plan_array Array Encodings
/// @defgroup encoding_array Array Encodings
/// @{

// clang-format off
Expand Down Expand Up @@ -703,9 +703,9 @@ struct FIXED_TYPED_ARRAY {
/// The array length
const std::uint64_t size;
/// Element encoding
const SinglePlan encoding;
const SingleEncoding encoding;
/// Positional encodings
const MultiplePlans prefix_encodings;
const MultipleEncodings prefix_encodings;
};

// clang-format off
Expand Down Expand Up @@ -754,9 +754,9 @@ struct BOUNDED_8BITS_TYPED_ARRAY {
/// The maximum length of the array
const std::uint64_t maximum;
/// Element encoding
const SinglePlan encoding;
const SingleEncoding encoding;
/// Positional encodings
const MultiplePlans prefix_encodings;
const MultipleEncodings prefix_encodings;
};

// clang-format off
Expand Down Expand Up @@ -800,9 +800,9 @@ struct FLOOR_TYPED_ARRAY {
/// The minimum length of the array
const std::uint64_t minimum;
/// Element encoding
const SinglePlan encoding;
const SingleEncoding encoding;
/// Positional encodings
const MultiplePlans prefix_encodings;
const MultipleEncodings prefix_encodings;
};

// clang-format off
Expand Down Expand Up @@ -845,15 +845,15 @@ struct ROOF_TYPED_ARRAY {
/// The maximum length of the array
const std::uint64_t maximum;
/// Element encoding
const SinglePlan encoding;
const SingleEncoding encoding;
/// Positional encodings
const MultiplePlans prefix_encodings;
const MultipleEncodings prefix_encodings;
};

/// @}

/// @ingroup runtime
/// @defgroup plan_object Object Encodings
/// @defgroup encoding_object Object Encodings
/// @{

// clang-format off
Expand Down Expand Up @@ -902,9 +902,9 @@ struct FIXED_TYPED_ARBITRARY_OBJECT {
/// The object size
const std::uint64_t size;
/// Key encoding
const SinglePlan key_encoding;
const SingleEncoding key_encoding;
/// Value encoding
const SinglePlan encoding;
const SingleEncoding encoding;
};

// clang-format off
Expand Down Expand Up @@ -946,15 +946,15 @@ struct FIXED_TYPED_ARBITRARY_OBJECT {
// clang-format on
struct VARINT_TYPED_ARBITRARY_OBJECT {
/// Key encoding
const SinglePlan key_encoding;
const SingleEncoding key_encoding;
/// Value encoding
const SinglePlan encoding;
const SingleEncoding encoding;
};

/// @}

/// @ingroup runtime
/// @defgroup plan_any Any Encodings
/// @defgroup encoding_any Any Encodings
/// @{

// TODO: Write brief description
Expand Down Expand Up @@ -1014,8 +1014,8 @@ static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_10 == 10);
// clang-format on

/// @ingroup runtime
/// Represents an encoding plan
using Plan = std::variant<
/// Represents an encoding
using Encoding = std::variant<
BOUNDED_MULTIPLE_8BITS_ENUM_FIXED, FLOOR_MULTIPLE_ENUM_VARINT,
ROOF_MULTIPLE_MIRROR_ENUM_VARINT, ARBITRARY_MULTIPLE_ZIGZAG_VARINT,
DOUBLE_VARINT_TUPLE, BYTE_CHOICE_INDEX, LARGE_CHOICE_INDEX,
Expand All @@ -1028,11 +1028,11 @@ using Plan = std::variant<
FIXED_TYPED_ARBITRARY_OBJECT, VARINT_TYPED_ARBITRARY_OBJECT,
ANY_PACKED_TYPE_TAG_BYTE_PREFIX>;

// Helper definitions that rely on the Plan data type
// Helper definitions that rely on the Encoding data type
#ifndef DOXYGEN
// Ignore this definition on the documentation
struct __internal_encoding_wrapper {
const Plan value;
const Encoding value;
};
#endif

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef SOURCEMETA_JSONBINPACK_RUNTIME_ENCODING_WRAP_H_
#define SOURCEMETA_JSONBINPACK_RUNTIME_ENCODING_WRAP_H_

#include <sourcemeta/jsonbinpack/runtime_plan.h>
#include <sourcemeta/jsonbinpack/runtime_encoding.h>

#include <algorithm> // std::transform
#include <initializer_list> // std::initializer_list
Expand All @@ -27,7 +27,7 @@ namespace sourcemeta::jsonbinpack {
// as we want to avoid including their definitions in if the user
// doesn't import this header.

inline auto wrap(Plan &&encoding) -> SinglePlan {
inline auto wrap(Encoding &&encoding) -> SingleEncoding {
return std::make_shared<aggregate_adapter<__internal_encoding_wrapper>>(
std::move(encoding));
}
Expand All @@ -36,15 +36,16 @@ inline auto wrap(Plan &&encoding) -> SinglePlan {
template <typename Iterator>
requires std::forward_iterator<Iterator>
// clang-format on
inline auto wrap(Iterator begin, Iterator end) -> MultiplePlans {
MultiplePlans result;
std::transform(begin, end, std::back_inserter(result), [](Plan encoding) {
inline auto wrap(Iterator begin, Iterator end) -> MultipleEncodings {
MultipleEncodings result;
std::transform(begin, end, std::back_inserter(result), [](Encoding encoding) {
return __internal_encoding_wrapper{std::move(encoding)};
});
return result;
}

inline auto wrap(std::initializer_list<Plan> encodings) -> MultiplePlans {
inline auto wrap(std::initializer_list<Encoding> encodings)
-> MultipleEncodings {
return wrap(encodings.begin(), encodings.end());
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace sourcemeta::jsonbinpack {

auto load(const sourcemeta::jsontoolkit::JSON &input) -> Plan {
auto load(const sourcemeta::jsontoolkit::JSON &input) -> Encoding {
assert(input.defines("binpackEncoding"));
assert(input.defines("binpackOptions"));
const auto encoding{input.at("binpackEncoding").to_string()};
Expand Down
Loading

0 comments on commit 3dc921a

Please sign in to comment.