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 08087cb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 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

0 comments on commit 08087cb

Please sign in to comment.