Skip to content

Commit

Permalink
[rd-cpp] Fix array fields serialization and generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mirasrael committed Jan 9, 2024
1 parent 95c5b78 commit 9b23b89
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 64 deletions.
2 changes: 1 addition & 1 deletion rd-cpp/src/rd_core_cpp/src/main/std/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int32_t size(std::vector<T, A> const& value)
template <typename T, typename A>
int32_t size(std::vector<T, A> const& value)
{
return std::size(value);
return static_cast<int32_t>(std::size(value));
}
#endif

Expand Down
12 changes: 6 additions & 6 deletions rd-cpp/src/rd_framework_cpp/src/main/protocol/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ class RD_FRAMEWORK_API Buffer final
}

template <template <class, class> class C, typename T, typename A = allocator<value_or_wrapper<T>>>
C<value_or_wrapper<T>, A> read_array(std::function<value_or_wrapper<T>()> reader)
C<value_or_wrapper<T>, A> read_array(std::function<T()> reader)
{
int32_t len = read_integral<int32_t>();
auto len = read_integral<int32_t>();
C<value_or_wrapper<T>, A> result;
using rd::resize;
resize(result, len);
Expand All @@ -145,15 +145,15 @@ class RD_FRAMEWORK_API Buffer final
{
using rd::size;
const int32_t& len = rd::size(container);
write_integral<int32_t>(static_cast<int32_t>(len));
write_integral<int32_t>(len);
if (len > 0)
{
write(reinterpret_cast<word_t const*>(&container[0]), sizeof(T) * len);
}
}

template <template <class, class> class C, typename T, typename A = allocator<T>>
std::enable_if_t<!rd::util::in_heap_v<T>, void> write_array(C<T, A> const& container, std::function<void(T const&)> writer)
template <template <class, class> class C, typename T, typename A = allocator<value_or_wrapper<T>>>
std::enable_if_t<util::disjunction<util::negation<is_wrapper<value_or_wrapper<T>>>, is_wrapper<T>>::value, void> write_array(C<value_or_wrapper<T>, A> const& container, std::function<void(T const&)> writer)
{
using rd::size;
write_integral<int32_t>(size(container));
Expand All @@ -164,7 +164,7 @@ class RD_FRAMEWORK_API Buffer final
}

template <template <class, class> class C, typename T, typename A = allocator<value_or_wrapper<T>>>
std::enable_if_t<is_wrapper_v<value_or_wrapper<T>>, void> write_array(C<value_or_wrapper<T>, A> const& container, std::function<void(T const&)> writer)
std::enable_if_t<util::conjunction<is_wrapper<value_or_wrapper<T>>, util::negation<is_wrapper<T>>>::value, void> write_array(C<value_or_wrapper<T>, A> const& container, std::function<void(T const&)> writer)
{
using rd::size;
write_integral<int32_t>(size(container));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.jetbrains.rd.generator.nova.util.capitalizeInvariant
import com.jetbrains.rd.generator.nova.util.decapitalizeInvariant
import com.jetbrains.rd.generator.nova.util.joinToOptString
import com.jetbrains.rd.util.Logger
import com.jetbrains.rd.util.PublicApi
import com.jetbrains.rd.util.eol
import com.jetbrains.rd.util.hash.IncrementalHash64
import com.jetbrains.rd.util.string.Eol
Expand Down Expand Up @@ -73,6 +74,13 @@ open class Cpp17Generator(
//region language specific properties
object Namespace : ISetting<String, Declaration>

class SubstitutedType(@PublicApi val originalType: IType, override val name: String) : IType

private fun IType.substituted(scope: Declaration, rawType: Boolean = false, omitNullability: Boolean = false): IType {
val newName = substitutedName(scope, rawType, omitNullability)
return if (newName != name) SubstitutedType(this, newName) else this
}

private val Declaration.namespace: String
get() {
return when (this) {
Expand Down Expand Up @@ -497,7 +505,6 @@ open class Cpp17Generator(
}
}

@Suppress("REDUNDANT_ELSE_IN_WHEN")
protected open fun Member.Reactive.implSimpleName(scope: Declaration): String = "rd::" + when (this) {
is Member.Reactive.Task -> when (actualFlow) {
Sink -> "RdEndpoint"
Expand Down Expand Up @@ -1872,7 +1879,7 @@ open class Cpp17Generator(
is IAttributedType -> itemType.reader()
is IArray, is IImmutableList -> { //awaiting superinterfaces' support in Kotlin
this as IHasItemType
val templateTypes = "${decl.listType.withNamespace()}, ${itemType.templateName(decl)}, ${decl.allocatorType(itemType)}"
val templateTypes = "${decl.listType.withNamespace()}, ${itemType.templateName(decl)}, ${decl.allocatorType(itemType.substituted(decl))}"
if (isPrimitivesArray) {
"buffer.read_array<$templateTypes>()"
} else {
Expand Down Expand Up @@ -2060,7 +2067,7 @@ open class Cpp17Generator(
if (isPrimitivesArray) {
"buffer.write_array($field)"
} else {
val templateTypes = "${decl.listType.withNamespace()}, ${itemType.templateName(decl)}, ${decl.allocatorType(itemType)}"
val templateTypes = "${decl.listType.withNamespace()}, ${itemType.templateName(decl)}, ${decl.allocatorType(itemType.substituted(decl))}"
val lambda = lambda("${itemType.templateName(decl)} const & it", itemType.writer("it"), "void")
"buffer.write_array<$templateTypes>($field, $lambda)"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ object DemoModel : Ext(DemoRoot) {
field("key", PredefinedType.int)
}

private var ClassWithStructArrayField = classdef {
field("arrayField", array(MyScalar))
}

init {
property("boolean_property", PredefinedType.bool)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a RdGen v1.13.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
#include "Baz.Generated.h"


Expand Down Expand Up @@ -57,28 +49,28 @@ Baz Baz::read(rd::SerializationCtx& ctx, rd::Buffer & buffer)
auto z_ = rd::RdProperty<Z, rd::Polymorphic<Z>>::read(ctx, buffer);
auto x_ = buffer.read_integral<int32_t>();
auto sdf_ = rd::RdMap<int32_t, int32_t, rd::Polymorphic<int32_t>, rd::Polymorphic<int32_t>>::read(ctx, buffer);
auto foo_ = buffer.read_array<std::vector, Foo, rd::allocator<Foo>>(
[&ctx, &buffer]() mutable
auto foo_ = buffer.read_array<std::vector, Foo, rd::allocator<rd::Wrapper<Foo>>>(
[&ctx, &buffer]() mutable
{ return ctx.get_serializers().readPolymorphic<Foo>(ctx, buffer); }
);
auto bar_ = buffer.read_array<std::vector, rd::Wrapper<A>, rd::allocator<ANullable>>(
[&ctx, &buffer]() mutable
auto bar_ = buffer.read_array<std::vector, rd::Wrapper<A>, rd::allocator<rd::Wrapper<A>>>(
[&ctx, &buffer]() mutable
{ return buffer.read_nullable<A>(
[&ctx, &buffer]() mutable
[&ctx, &buffer]() mutable
{ return ctx.get_serializers().readPolymorphic<A>(ctx, buffer); }
); }
);
auto nls_field_ = buffer.read_wstring();
auto nls_nullable_field_ = buffer.read_nullable<std::wstring>(
[&ctx, &buffer]() mutable
[&ctx, &buffer]() mutable
{ return buffer.read_wstring(); }
);
auto string_list_field_ = buffer.read_array<std::vector, std::wstring, rd::allocator<String>>(
[&ctx, &buffer]() mutable
auto string_list_field_ = buffer.read_array<std::vector, std::wstring, rd::allocator<rd::Wrapper<std::wstring>>>(
[&ctx, &buffer]() mutable
{ return buffer.read_wstring(); }
);
auto nls_list_field_ = buffer.read_array<std::vector, std::wstring, rd::allocator<String>>(
[&ctx, &buffer]() mutable
auto nls_list_field_ = buffer.read_array<std::vector, std::wstring, rd::allocator<rd::Wrapper<std::wstring>>>(
[&ctx, &buffer]() mutable
{ return buffer.read_wstring(); }
);
auto foo1_ = rd::RdProperty<rd::Wrapper<Foo>, Baz::__FooNullableSerializer>::read(ctx, buffer);
Expand All @@ -103,28 +95,28 @@ void Baz::write(rd::SerializationCtx& ctx, rd::Buffer& buffer) const
z_.write(ctx, buffer);
buffer.write_integral(x_);
sdf_.write(ctx, buffer);
buffer.write_array<std::vector, Foo, rd::allocator<Foo>>(foo_,
[&ctx, &buffer](Foo const & it) mutable -> void
buffer.write_array<std::vector, Foo, rd::allocator<rd::Wrapper<Foo>>>(foo_,
[&ctx, &buffer](Foo const & it) mutable -> void
{ ctx.get_serializers().writePolymorphic<Foo>(ctx, buffer, it); }
);
buffer.write_array<std::vector, rd::Wrapper<A>, rd::allocator<ANullable>>(bar_,
[&ctx, &buffer](rd::Wrapper<A> const & it) mutable -> void
{ buffer.write_nullable<A>(it,
[&ctx, &buffer](rd::Wrapper<A> const & it) mutable -> void
buffer.write_array<std::vector, rd::Wrapper<A>, rd::allocator<rd::Wrapper<A>>>(bar_,
[&ctx, &buffer](rd::Wrapper<A> const & it) mutable -> void
{ buffer.write_nullable<A>(it,
[&ctx, &buffer](rd::Wrapper<A> const & it) mutable -> void
{ ctx.get_serializers().writePolymorphic<A>(ctx, buffer, it); }
); }
);
buffer.write_wstring(nls_field_);
buffer.write_nullable<std::wstring>(nls_nullable_field_,
[&ctx, &buffer](rd::Wrapper<std::wstring> const & it) mutable -> void
buffer.write_nullable<std::wstring>(nls_nullable_field_,
[&ctx, &buffer](rd::Wrapper<std::wstring> const & it) mutable -> void
{ buffer.write_wstring(it); }
);
buffer.write_array<std::vector, std::wstring, rd::allocator<String>>(string_list_field_,
[&ctx, &buffer](std::wstring const & it) mutable -> void
buffer.write_array<std::vector, std::wstring, rd::allocator<rd::Wrapper<std::wstring>>>(string_list_field_,
[&ctx, &buffer](std::wstring const & it) mutable -> void
{ buffer.write_wstring(it); }
);
buffer.write_array<std::vector, std::wstring, rd::allocator<String>>(nls_list_field_,
[&ctx, &buffer](std::wstring const & it) mutable -> void
buffer.write_array<std::vector, std::wstring, rd::allocator<rd::Wrapper<std::wstring>>>(nls_list_field_,
[&ctx, &buffer](std::wstring const & it) mutable -> void
{ buffer.write_wstring(it); }
);
foo1_.write(ctx, buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ Document Document::read(rd::SerializationCtx& ctx, rd::Buffer & buffer)
);
auto andBackAgain_ = rd::RdEndpoint<std::wstring, int32_t, rd::Polymorphic<std::wstring>, rd::Polymorphic<int32_t>>::read(ctx, buffer);
auto completion_ = Completion::read(ctx, buffer);
auto arr1_ = buffer.read_array<std::vector, uint8_t, rd::allocator<Byte>>(
auto arr1_ = buffer.read_array<std::vector, uint8_t, rd::allocator<uint8_t>>(
[&ctx, &buffer]() mutable
{ return buffer.read_integral<uint8_t>(); }
);
auto arr2_ = buffer.read_array<std::vector, std::vector<bool>, rd::allocator<BoolArray>>(
auto arr2_ = buffer.read_array<std::vector, std::vector<bool>, rd::allocator<std::vector<bool>>>(
[&ctx, &buffer]() mutable
{ return buffer.read_array<std::vector, bool, rd::allocator<Bool>>(
{ return buffer.read_array<std::vector, bool, rd::allocator<bool>>(
[&ctx, &buffer]() mutable
{ return buffer.read_bool(); }
); }
Expand All @@ -77,13 +77,13 @@ void Document::write(rd::SerializationCtx& ctx, rd::Buffer& buffer) const
);
andBackAgain_.write(ctx, buffer);
rd::Polymorphic<std::decay_t<decltype(completion_)>>::write(ctx, buffer, completion_);
buffer.write_array<std::vector, uint8_t, rd::allocator<Byte>>(arr1_,
buffer.write_array<std::vector, uint8_t, rd::allocator<uint8_t>>(arr1_,
[&ctx, &buffer](uint8_t const & it) mutable -> void
{ buffer.write_integral(it); }
);
buffer.write_array<std::vector, std::vector<bool>, rd::allocator<BoolArray>>(arr2_,
buffer.write_array<std::vector, std::vector<bool>, rd::allocator<std::vector<bool>>>(arr2_,
[&ctx, &buffer](std::vector<bool> const & it) mutable -> void
{ buffer.write_array<std::vector, bool, rd::allocator<Bool>>(it,
{ buffer.write_array<std::vector, bool, rd::allocator<bool>>(it,
[&ctx, &buffer](bool const & it) mutable -> void
{ buffer.write_bool(it); }
); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Selection Selection::read(rd::SerializationCtx& ctx, rd::Buffer & buffer)
{
auto start_ = buffer.read_integral<int32_t>();
auto end_ = buffer.read_integral<int32_t>();
auto lst_ = buffer.read_array<std::vector, int32_t, rd::allocator<Int>>(
auto lst_ = buffer.read_array<std::vector, int32_t, rd::allocator<int32_t>>(
[&ctx, &buffer]() mutable
{ return buffer.read_integral<int32_t>(); }
);
Expand All @@ -54,7 +54,7 @@ void Selection::write(rd::SerializationCtx& ctx, rd::Buffer& buffer) const
{
buffer.write_integral(start_);
buffer.write_integral(end_);
buffer.write_array<std::vector, int32_t, rd::allocator<Int>>(lst_,
buffer.write_array<std::vector, int32_t, rd::allocator<int32_t>>(lst_,
[&ctx, &buffer](int32_t const & it) mutable -> void
{ buffer.write_integral(it); }
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ Baz Baz::read(rd::SerializationCtx& ctx, rd::Buffer & buffer)
auto z_ = rd::RdProperty<Z, rd::Polymorphic<Z>>::read(ctx, buffer);
auto x_ = buffer.read_integral<int32_t>();
auto sdf_ = rd::RdMap<int32_t, int32_t, rd::Polymorphic<int32_t>, rd::Polymorphic<int32_t>>::read(ctx, buffer);
auto foo_ = buffer.read_array<std::vector, Foo, rd::allocator<Foo>>(
auto foo_ = buffer.read_array<std::vector, Foo, rd::allocator<rd::Wrapper<Foo>>>(
[&ctx, &buffer]() mutable
{ return ctx.get_serializers().readPolymorphic<Foo>(ctx, buffer); }
);
auto bar_ = buffer.read_array<std::vector, rd::Wrapper<A>, rd::allocator<ANullable>>(
auto bar_ = buffer.read_array<std::vector, rd::Wrapper<A>, rd::allocator<rd::Wrapper<A>>>(
[&ctx, &buffer]() mutable
{ return buffer.read_nullable<A>(
[&ctx, &buffer]() mutable
Expand All @@ -73,11 +73,11 @@ Baz Baz::read(rd::SerializationCtx& ctx, rd::Buffer & buffer)
[&ctx, &buffer]() mutable
{ return buffer.read_wstring(); }
);
auto string_list_field_ = buffer.read_array<std::vector, std::wstring, rd::allocator<String>>(
auto string_list_field_ = buffer.read_array<std::vector, std::wstring, rd::allocator<rd::Wrapper<std::wstring>>>(
[&ctx, &buffer]() mutable
{ return buffer.read_wstring(); }
);
auto nls_list_field_ = buffer.read_array<std::vector, std::wstring, rd::allocator<String>>(
auto nls_list_field_ = buffer.read_array<std::vector, std::wstring, rd::allocator<rd::Wrapper<std::wstring>>>(
[&ctx, &buffer]() mutable
{ return buffer.read_wstring(); }
);
Expand All @@ -103,11 +103,11 @@ void Baz::write(rd::SerializationCtx& ctx, rd::Buffer& buffer) const
z_.write(ctx, buffer);
buffer.write_integral(x_);
sdf_.write(ctx, buffer);
buffer.write_array<std::vector, Foo, rd::allocator<Foo>>(foo_,
buffer.write_array<std::vector, Foo, rd::allocator<rd::Wrapper<Foo>>>(foo_,
[&ctx, &buffer](Foo const & it) mutable -> void
{ ctx.get_serializers().writePolymorphic<Foo>(ctx, buffer, it); }
);
buffer.write_array<std::vector, rd::Wrapper<A>, rd::allocator<ANullable>>(bar_,
buffer.write_array<std::vector, rd::Wrapper<A>, rd::allocator<rd::Wrapper<A>>>(bar_,
[&ctx, &buffer](rd::Wrapper<A> const & it) mutable -> void
{ buffer.write_nullable<A>(it,
[&ctx, &buffer](rd::Wrapper<A> const & it) mutable -> void
Expand All @@ -119,11 +119,11 @@ void Baz::write(rd::SerializationCtx& ctx, rd::Buffer& buffer) const
[&ctx, &buffer](rd::Wrapper<std::wstring> const & it) mutable -> void
{ buffer.write_wstring(it); }
);
buffer.write_array<std::vector, std::wstring, rd::allocator<String>>(string_list_field_,
buffer.write_array<std::vector, std::wstring, rd::allocator<rd::Wrapper<std::wstring>>>(string_list_field_,
[&ctx, &buffer](std::wstring const & it) mutable -> void
{ buffer.write_wstring(it); }
);
buffer.write_array<std::vector, std::wstring, rd::allocator<String>>(nls_list_field_,
buffer.write_array<std::vector, std::wstring, rd::allocator<rd::Wrapper<std::wstring>>>(nls_list_field_,
[&ctx, &buffer](std::wstring const & it) mutable -> void
{ buffer.write_wstring(it); }
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ Document Document::read(rd::SerializationCtx& ctx, rd::Buffer & buffer)
);
auto andBackAgain_ = rd::RdCall<std::wstring, int32_t, rd::Polymorphic<std::wstring>, rd::Polymorphic<int32_t>>::read(ctx, buffer);
auto completion_ = Completion::read(ctx, buffer);
auto arr1_ = buffer.read_array<std::vector, uint8_t, rd::allocator<Byte>>(
auto arr1_ = buffer.read_array<std::vector, uint8_t, rd::allocator<uint8_t>>(
[&ctx, &buffer]() mutable
{ return buffer.read_integral<uint8_t>(); }
);
auto arr2_ = buffer.read_array<std::vector, std::vector<bool>, rd::allocator<BoolArray>>(
auto arr2_ = buffer.read_array<std::vector, std::vector<bool>, rd::allocator<std::vector<bool>>>(
[&ctx, &buffer]() mutable
{ return buffer.read_array<std::vector, bool, rd::allocator<Bool>>(
{ return buffer.read_array<std::vector, bool, rd::allocator<bool>>(
[&ctx, &buffer]() mutable
{ return buffer.read_bool(); }
); }
Expand All @@ -77,13 +77,13 @@ void Document::write(rd::SerializationCtx& ctx, rd::Buffer& buffer) const
);
andBackAgain_.write(ctx, buffer);
rd::Polymorphic<std::decay_t<decltype(completion_)>>::write(ctx, buffer, completion_);
buffer.write_array<std::vector, uint8_t, rd::allocator<Byte>>(arr1_,
buffer.write_array<std::vector, uint8_t, rd::allocator<uint8_t>>(arr1_,
[&ctx, &buffer](uint8_t const & it) mutable -> void
{ buffer.write_integral(it); }
);
buffer.write_array<std::vector, std::vector<bool>, rd::allocator<BoolArray>>(arr2_,
buffer.write_array<std::vector, std::vector<bool>, rd::allocator<std::vector<bool>>>(arr2_,
[&ctx, &buffer](std::vector<bool> const & it) mutable -> void
{ buffer.write_array<std::vector, bool, rd::allocator<Bool>>(it,
{ buffer.write_array<std::vector, bool, rd::allocator<bool>>(it,
[&ctx, &buffer](bool const & it) mutable -> void
{ buffer.write_bool(it); }
); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Selection Selection::read(rd::SerializationCtx& ctx, rd::Buffer & buffer)
{
auto start_ = buffer.read_integral<int32_t>();
auto end_ = buffer.read_integral<int32_t>();
auto lst_ = buffer.read_array<std::vector, int32_t, rd::allocator<Int>>(
auto lst_ = buffer.read_array<std::vector, int32_t, rd::allocator<int32_t>>(
[&ctx, &buffer]() mutable
{ return buffer.read_integral<int32_t>(); }
);
Expand All @@ -54,7 +54,7 @@ void Selection::write(rd::SerializationCtx& ctx, rd::Buffer& buffer) const
{
buffer.write_integral(start_);
buffer.write_integral(end_);
buffer.write_array<std::vector, int32_t, rd::allocator<Int>>(lst_,
buffer.write_array<std::vector, int32_t, rd::allocator<int32_t>>(lst_,
[&ctx, &buffer](int32_t const & it) mutable -> void
{ buffer.write_integral(it); }
);
Expand Down

0 comments on commit 9b23b89

Please sign in to comment.