Skip to content

Commit

Permalink
Merge pull request #190 from tiffany1618/issue-186-array-constructors
Browse files Browse the repository at this point in the history
Fix duplicate array constructors in generated C++
  • Loading branch information
bocchino authored Nov 8, 2022
2 parents 27286f5 + aebc0c6 commit 9f36e14
Show file tree
Hide file tree
Showing 8 changed files with 399 additions and 43 deletions.
95 changes: 52 additions & 43 deletions compiler/lib/src/main/scala/codegen/CppWriter/ArrayCppWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,26 @@ case class ArrayCppWriter (

private def getConstructorMembers: List[CppDoc.Class.Member] = {
val defaultValues = getDefaultValues
// Only write this constructor if the array has more than one element
val singleElementConstructor =
if arraySize == 1 then Nil
else List(
CppDoc.Class.Member.Constructor(
CppDoc.Class.Constructor(
Some("Constructor (single element)"),
List(
CppDoc.Function.Param(
CppDoc.Type("const ElementType&"),
"e",
Some("The element"),
)
),
List("Serializable()"),
indexIterator(lines("this->elements[index] = e;")),
)
)
)

List(
CppDoc.Class.Member.Lines(
CppDoc.Lines(
Expand Down Expand Up @@ -212,50 +232,39 @@ case class ArrayCppWriter (
List("Serializable()"),
indexIterator(lines("this->elements[index] = a[index];")),
)
),
CppDoc.Class.Member.Constructor(
CppDoc.Class.Constructor(
Some("Constructor (single element)"),
List(
CppDoc.Function.Param(
)
) ++
singleElementConstructor ++
List(
CppDoc.Class.Member.Constructor(
CppDoc.Class.Constructor(
Some("Constructor (multiple elements)"),
List.range(1, arraySize + 1).map(i => CppDoc.Function.Param(
CppDoc.Type("const ElementType&"),
"e",
Some("The element"),
)
),
List("Serializable()"),
indexIterator(lines("this->elements[index] = e;")),
)
),
CppDoc.Class.Member.Constructor(
CppDoc.Class.Constructor(
Some("Constructor (multiple elements)"),
List.range(1, arraySize + 1).map(i => CppDoc.Function.Param(
CppDoc.Type("const ElementType&"),
s"e$i",
Some(s"Element $i"),
)),
List("Serializable()"),
List.range(1, arraySize + 1).map(i => line(
s"this->elements[${i - 1}] = e$i;"
)),
)
),
CppDoc.Class.Member.Constructor(
CppDoc.Class.Constructor(
Some("Copy Constructor"),
List(
CppDoc.Function.Param(
CppDoc.Type(s"const $name&"),
"obj",
Some("The source object"),
)
),
List("Serializable()"),
indexIterator(lines("this->elements[index] = obj.elements[index];")),
)
),
)
s"e$i",
Some(s"Element $i"),
)),
List("Serializable()"),
List.range(1, arraySize + 1).map(i => line(
s"this->elements[${i - 1}] = e$i;"
)),
)
),
CppDoc.Class.Member.Constructor(
CppDoc.Class.Constructor(
Some("Copy Constructor"),
List(
CppDoc.Function.Param(
CppDoc.Type(s"const $name&"),
"obj",
Some("The source object"),
)
),
List("Serializable()"),
indexIterator(lines("this->elements[index] = obj.elements[index];")),
)
),
)
}

private def getOperatorMembers: List[CppDoc.Class.Member] =
Expand Down
180 changes: 180 additions & 0 deletions compiler/tools/fpp-to-cpp/test/array/SingleElementArrayAc.ref.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// ======================================================================
// \title SingleElementArrayAc.cpp
// \author Generated by fpp-to-cpp
// \brief cpp file for SingleElement array
// ======================================================================

#include <cstdio>
#include <cstring>

#include "Fw/Types/Assert.hpp"
#include "Fw/Types/StringUtils.hpp"
#include "SingleElementArrayAc.hpp"

// ----------------------------------------------------------------------
// Constructors
// ----------------------------------------------------------------------

SingleElement ::
SingleElement() :
Serializable()
{
// Construct using element-wise constructor
*this = SingleElement(
0
);
}

SingleElement ::
SingleElement(const ElementType (&a)[SIZE]) :
Serializable()
{
for (U32 index = 0; index < SIZE; index++) {
this->elements[index] = a[index];
}
}

SingleElement ::
SingleElement(const ElementType& e1) :
Serializable()
{
this->elements[0] = e1;
}

SingleElement ::
SingleElement(const SingleElement& obj) :
Serializable()
{
for (U32 index = 0; index < SIZE; index++) {
this->elements[index] = obj.elements[index];
}
}

// ----------------------------------------------------------------------
// Operators
// ----------------------------------------------------------------------

SingleElement::ElementType& SingleElement ::
operator[](const U32 i)
{
FW_ASSERT(i < SIZE);
return this->elements[i];
}

const SingleElement::ElementType& SingleElement ::
operator[](const U32 i) const
{
FW_ASSERT(i < SIZE);
return this->elements[i];
}

SingleElement& SingleElement ::
operator=(const SingleElement& obj)
{
if (this == &obj) {
return *this;
}

for (U32 index = 0; index < SIZE; index++) {
this->elements[index] = obj.elements[index];
}
return *this;
}

SingleElement& SingleElement ::
operator=(const ElementType (&a)[SIZE])
{
for (U32 index = 0; index < SIZE; index++) {
this->elements[index] = a[index];
}
return *this;
}

SingleElement& SingleElement ::
operator=(const ElementType& e)
{
for (U32 index = 0; index < SIZE; index++) {
this->elements[index] = e;
}
return *this;
}

bool SingleElement ::
operator==(const SingleElement& obj) const
{
for (U32 index = 0; index < SIZE; index++) {
if (!((*this)[index] == obj[index])) {
return false;
}
}
return true;
}

bool SingleElement ::
operator!=(const SingleElement& obj) const
{
return !(*this == obj);
}

#ifdef BUILD_UT

std::ostream& operator<<(std::ostream& os, const SingleElement& obj) {
Fw::String s;
obj.toString(s);
os << s;
return os;
}

#endif

// ----------------------------------------------------------------------
// Member functions
// ----------------------------------------------------------------------

Fw::SerializeStatus SingleElement ::
serialize(Fw::SerializeBufferBase& buffer) const
{
Fw::SerializeStatus status = Fw::FW_SERIALIZE_OK;
for (U32 index = 0; index < SIZE; index++) {
status = buffer.serialize((*this)[index]);
if (status != Fw::FW_SERIALIZE_OK) {
return status;
}
}
return status;
}

Fw::SerializeStatus SingleElement ::
deserialize(Fw::SerializeBufferBase& buffer)
{
Fw::SerializeStatus status = Fw::FW_SERIALIZE_OK;
for (U32 index = 0; index < SIZE; index++) {
status = buffer.deserialize((*this)[index]);
if (status != Fw::FW_SERIALIZE_OK) {
return status;
}
}
return status;
}

#if FW_ARRAY_TO_STRING || BUILD_UT

void SingleElement ::
toString(Fw::StringBase& sb) const
{
static const char *formatString = "[ "
"%" PRIu32 " ]";

char outputString[FW_ARRAY_TO_STRING_BUFFER_SIZE];
(void) snprintf(
outputString,
FW_ARRAY_TO_STRING_BUFFER_SIZE,
formatString,
this->elements[0]
);

outputString[FW_ARRAY_TO_STRING_BUFFER_SIZE-1] = 0; // NULL terminate
sb = outputString;
}

#endif
Loading

0 comments on commit 9f36e14

Please sign in to comment.