Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ksergey committed Oct 14, 2024
1 parent f6eaa22 commit 1f34b9d
Show file tree
Hide file tree
Showing 6 changed files with 72,895 additions and 78 deletions.
38 changes: 14 additions & 24 deletions app/generation/cppng/templates/generate.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,6 @@ public:
>;
// clang-format on

// Field encoding by field num or field name
template <auto N>
using Field = FieldAt<Fields, N>;

constexpr {{ composite_class_name }}() = default;

constexpr {{ composite_class_name }}(std::byte* buffer, std::size_t offset, std::size_t bufferLength, std::uint16_t actingVersion)
Expand Down Expand Up @@ -295,13 +291,15 @@ public:
}

template <std::size_t N>
[[nodiscard]] constexpr auto field() noexcept -> Field<N> {
return Field<N>(this->buffer(), this->offset(), this->bufferLength(), this->actingVersion());
[[nodiscard]] constexpr auto field() noexcept -> FieldByIndex<Fields, N> {
using F = FieldByIndex<Fields, N>;
return F(this->buffer(), this->offset(), this->bufferLength(), this->actingVersion());
}

template <CtString Name>
[[nodiscard]] constexpr auto field() noexcept -> Field<Name> {
return Field<Name>(this->buffer(), this->offset(), this->bufferLength(), this->actingVersion());
[[nodiscard]] constexpr auto field() noexcept -> FieldByName<Fields, Name> {
using F = FieldByName<Fields, Name>;
return F(this->buffer(), this->offset(), this->bufferLength(), this->actingVersion());
}
};
{%- endmacro %}
Expand Down Expand Up @@ -356,10 +354,6 @@ public:
>;
// clang-format on

// Field encoding by field num or field name
template <auto N>
using Field = FieldAt<Fields, N>;

constexpr {{ group_class_name }}() = default;

constexpr {{ group_class_name }}(std::byte* buffer, std::size_t* pos, std::size_t bufferLength, std::uint16_t actingVersion) {
Expand Down Expand Up @@ -466,8 +460,8 @@ public:
}

template <std::size_t N>
[[nodiscard]] constexpr auto field() noexcept -> Field<N> {
using F = Field<N>;
[[nodiscard]] constexpr auto field() noexcept -> FieldByIndex<Fields, N> {
using F = FieldByIndex<Fields, N>;

if constexpr (std::derived_from<typename F::value_type, SBEType_Group>) {
return F(this->buffer(), this->sbePositionPtr(), this->bufferLength(), this->actingVersion());
Expand All @@ -479,8 +473,8 @@ public:
}

template <CtString Name>
[[nodiscard]] constexpr auto field() noexcept -> Field<Name> {
using F = Field<Name>;
[[nodiscard]] constexpr auto field() noexcept -> FieldByName<Fields, Name> {
using F = FieldByName<Fields, Name>;

if constexpr (std::derived_from<typename F::value_type, SBEType_Group>) {
return F(this->buffer(), this->sbePositionPtr(), this->bufferLength(), this->actingVersion());
Expand Down Expand Up @@ -537,10 +531,6 @@ public:
>;
// clang-format on

// Field encoding by field num or field name
template <auto N>
using Field = FieldAt<Fields, N>;

constexpr {{ message_class_name }}() = default;

constexpr {{ message_class_name }}(std::byte* buffer, std::size_t offset, std::size_t bufferLength,
Expand Down Expand Up @@ -638,8 +628,8 @@ public:
}

template <std::size_t N>
[[nodiscard]] constexpr auto field() noexcept -> Field<N> {
using F = Field<N>;
[[nodiscard]] constexpr auto field() noexcept -> FieldByIndex<Fields, N> {
using F = FieldByIndex<Fields, N>;

if constexpr (std::derived_from<typename F::value_type, SBEType_Group>) {
return F(this->buffer(), this->sbePositionPtr(), this->bufferLength(), this->actingVersion());
Expand All @@ -651,8 +641,8 @@ public:
}

template <CtString Name>
[[nodiscard]] constexpr auto field() noexcept -> Field<Name> {
using F = Field<Name>;
[[nodiscard]] constexpr auto field() noexcept -> FieldByName<Fields, Name> {
using F = FieldByName<Fields, Name>;

if constexpr (std::derived_from<typename F::value_type, SBEType_Group>) {
return F(this->buffer(), this->sbePositionPtr(), this->bufferLength(), this->actingVersion());
Expand Down
20 changes: 11 additions & 9 deletions app/generation/cppng/templates/typing.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,18 @@ struct MP_Field {
using encoding_type = EncodingT;
};

// TODO: MP_Find<List, typename MP_MatchName<"test">::fn
template <CtString Name>
struct MP_MatchName {
template <typename T>
using fn = MP_Bool<(T::first_type::value == Name)>;
};

template <typename Fields, std::size_t N>
using FieldByIndex = typename MP_At<Fields, MP_SizeT<N>>::second_type;

template <typename Fields, CtString N>
using FieldByName = FieldByIndex<Fields, MP_Find<Fields, MP_MatchName<N>::template fn>::value>;

namespace detail {

template <typename Fields, auto>
Expand All @@ -70,8 +75,6 @@ struct FieldAtImpl<Fields, Name> {
template <typename Fields, auto N>
using FieldAt = typename detail::FieldAtImpl<Fields, N>::value_type;

inline namespace v2 {

struct SBEType_Type {};
struct SBEType_Enum {};
struct SBEType_Set {};
Expand Down Expand Up @@ -211,7 +214,7 @@ template <typename T>
concept EncodingType = detail::EncodingTypeImpl<T>::value;

template <typename T>
class Data : public SBEType_Type {
class Data : public SBEType_Data {
private:
std::byte* buffer_ = nullptr;
std::size_t bufferLength_ = 0;
Expand All @@ -220,8 +223,8 @@ class Data : public SBEType_Type {
std::uint16_t actingVersion_ = 0;

public:
using length_type = typename T::template Field<"length">::value_type;
using element_type = typename T::template Field<"varData">::value_type;
using length_type = typename FieldByName<typename T::Fields, "length">::value_type;
using element_type = typename FieldByName<typename T::Fields, "varData">::value_type;
using value_type = typename PlainType<element_type, MP_SizeT<0>, MP_None>::value_type;

constexpr Data() = default;
Expand All @@ -236,14 +239,14 @@ class Data : public SBEType_Type {
}

[[nodiscard]] value_type value() const {
auto const size = this->size();
auto const size = *this->lengthPtr();
auto result = value_type(this->dataPtr(), size);
const_cast<Data&>(*this).sbePosition(initialPosition_ + sizeof(length_type) + size * sizeof(element_type));
return result;
}

void setValue(value_type value) {
auto const size = static_cast<length_type>(value.size());
auto const size = *this->lengthPtr();
*this->lengthPtr() = size;
std::copy(value.begin(), value.end(), this->dataPtr());
this->sbePosition(initialPosition_ + sizeof(length_type) + size * sizeof(element_type));
Expand Down Expand Up @@ -363,5 +366,4 @@ class FieldEncoding<T, PresenceT, OffsetT, ConstValueT> : public T {
using T::T;
};

} // namespace v2
} // namespace sbe_code_gen
58 changes: 29 additions & 29 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
include(FetchContent)

FetchContent_Declare(fmt
URL https://github.com/fmtlib/fmt/archive/refs/tags/11.0.2.tar.gz
DOWNLOAD_EXTRACT_TIMESTAMP ON
)
FetchContent_MakeAvailable(fmt)

FetchContent_Declare(nlohmann_json
URL https://github.com/nlohmann/json/archive/refs/tags/v3.11.3.tar.gz
DOWNLOAD_EXTRACT_TIMESTAMP ON
)
FetchContent_MakeAvailable(nlohmann_json)

SBEMakeCodec(schema
SCHEMA "${CMAKE_CURRENT_SOURCE_DIR}/../resources/b3-market-data-messages-1.3.1.xml"
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/sbe"
GENERATOR "cppng"
)

add_executable(example)
target_compile_features(example PRIVATE cxx_std_20)
target_compile_options(example PRIVATE -Wall -Wextra -Wattributes -Wpedantic -Wstrict-aliasing -Wcast-align -g)
target_link_libraries(example PRIVATE fmt::fmt schema)
target_sources(example PRIVATE example.cpp)

add_executable(example_json)
target_compile_features(example_json PRIVATE cxx_std_20)
target_compile_options(example_json PRIVATE -Wall -Wextra -Wattributes -Wpedantic -Wstrict-aliasing -Wcast-align -g)
target_link_libraries(example_json PRIVATE fmt::fmt nlohmann_json::nlohmann_json schema)
target_sources(example_json PRIVATE example_json.cpp)
# FetchContent_Declare(fmt
# URL https://github.com/fmtlib/fmt/archive/refs/tags/11.0.2.tar.gz
# DOWNLOAD_EXTRACT_TIMESTAMP ON
# )
# FetchContent_MakeAvailable(fmt)
#
# FetchContent_Declare(nlohmann_json
# URL https://github.com/nlohmann/json/archive/refs/tags/v3.11.3.tar.gz
# DOWNLOAD_EXTRACT_TIMESTAMP ON
# )
# FetchContent_MakeAvailable(nlohmann_json)
#
# SBEMakeCodec(schema
# SCHEMA "${CMAKE_CURRENT_SOURCE_DIR}/../resources/b3-market-data-messages-1.3.1.xml"
# OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/sbe"
# GENERATOR "cppng"
# )
#
# add_executable(example)
# target_compile_features(example PRIVATE cxx_std_20)
# target_compile_options(example PRIVATE -Wall -Wextra -Wattributes -Wpedantic -Wstrict-aliasing -Wcast-align -g)
# target_link_libraries(example PRIVATE fmt::fmt schema)
# target_sources(example PRIVATE example.cpp)
#
# add_executable(example_json)
# target_compile_features(example_json PRIVATE cxx_std_20)
# target_compile_options(example_json PRIVATE -Wall -Wextra -Wattributes -Wpedantic -Wstrict-aliasing -Wcast-align -g)
# target_link_libraries(example_json PRIVATE fmt::fmt nlohmann_json::nlohmann_json schema)
# target_sources(example_json PRIVATE example_json.cpp)
Loading

0 comments on commit 1f34b9d

Please sign in to comment.