Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 96 additions & 6 deletions src/lib/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace tigl {
}

namespace {

auto customReplacedType(const std::string& type, const Tables& tables) -> const std::string& {
const auto p = tables.m_customTypes.find(type);
return p ? *p : type;
Expand Down Expand Up @@ -200,7 +201,7 @@ namespace tigl {
return typeName;
case Cardinality::Vector:
{
if (vectorInnerTypeIsUniquePtr(field))
if (vectorInnerTypeIsCPACSClass(field))
return "std::vector<std::unique_ptr<" + typeName + ">>";
else
return "std::vector<" + typeName + ">";
Expand All @@ -220,7 +221,20 @@ namespace tigl {
return customReplacedType(field);
}

auto vectorInnerTypeIsUniquePtr(const Field& field) const -> bool {
auto vectorInnerClass(const Field& field) const -> Class const&
{
if (field.cardinality() != Cardinality::Vector){
throw std::logic_error("Requested vector inner type for non-vector type");
}
auto const it = m_types.classes.find(field.typeName);
if(vectorInnerTypeIsCPACSClass(field) && it != std::end(m_types.classes)) {
return it->second;
} else {
throw std::logic_error("VectorInnerType is not a CPACS class");
}
}

auto vectorInnerTypeIsCPACSClass(const Field& field) const -> bool {
if (field.cardinality() != Cardinality::Vector)
throw std::logic_error("Requested vector inner type for non-vector type");
return m_types.classes.find(field.typeName) != std::end(m_types.classes);
Expand Down Expand Up @@ -264,9 +278,18 @@ namespace tigl {
hpp << "TIGL_EXPORT virtual " << getterSetterType(f) << "& Get" << capitalizeFirstLetter(f.name()) << "();";
hpp << EmptyLine;
hpp << "TIGL_EXPORT virtual size_t Get" << capitalizeFirstLetter(f.cpacsName) << "Count() const;";
if(vectorInnerTypeIsCPACSClass(f) && hasUidField(vectorInnerClass(f))){
hpp << "TIGL_EXPORT virtual size_t Get" << capitalizeFirstLetter(f.cpacsName) << "Index(const std::string& UID) const;";
}
hpp << EmptyLine;
hpp << "TIGL_EXPORT virtual const " << vectorInnerType(f) << "& Get" << capitalizeFirstLetter(f.cpacsName) << "(size_t index) const;";
hpp << "TIGL_EXPORT virtual " << vectorInnerType(f) << "& Get" << capitalizeFirstLetter(f.cpacsName) << "(size_t index);";
} // generate special accessors for uid reference vectors
if(vectorInnerTypeIsCPACSClass(f) && hasUidField(vectorInnerClass(f))){
hpp << EmptyLine;
hpp << "TIGL_EXPORT virtual const " << vectorInnerType(f) << "& Get" << capitalizeFirstLetter(f.cpacsName) << "(const std::string& UID) const;";
hpp << "TIGL_EXPORT virtual " << vectorInnerType(f) << "& Get" << capitalizeFirstLetter(f.cpacsName) << "(const std::string& UID);";
}
} // generate special accessors for uid reference vectors
else if (f.cardinality() == Cardinality::Vector && f.xmlTypeName == c_uidRefType) {
hpp << "TIGL_EXPORT virtual void AddTo" << capitalizeFirstLetter(f.name()) << "(const " << vectorInnerType(f) << "& value);";
hpp << "TIGL_EXPORT virtual bool RemoveFrom" << capitalizeFirstLetter(f.name()) << "(const " << vectorInnerType(f) << "& value);";
Expand Down Expand Up @@ -386,6 +409,40 @@ namespace tigl {
cpp << "}";
cpp << EmptyLine;

// Getter for index by UID
if(vectorInnerTypeIsCPACSClass(f) && hasUidField(vectorInnerClass(f))){
cpp << "size_t " << className << "::Get" << capitalizeFirstLetter(f.cpacsName) << "Index(const std::string& UID) const";
cpp << "{";
{
Scope s(cpp);
cpp << "for (size_t i=0; i < Get" << capitalizeFirstLetter(f.cpacsName) << "Count(); i++) {";
{
Scope s(cpp);
if(vectorInnerTypeIsCPACSClass(f) && hasUidField(vectorInnerClass(f)) && !hasMandatoryUidField(vectorInnerClass(f))) {
cpp << "const boost::optional<std::string> tmpUID(*" << f.fieldName() << "[i]->GetUID());";
cpp << "if (tmpUID == UID) {";
{
Scope s(cpp);
cpp << "return i+1;";
}
cpp << "}";
} else {
cpp << "const std::string tmpUID(" << f.fieldName() << "[i]->GetUID());";
cpp << "if (tmpUID == UID) {";
{
Scope s(cpp);
cpp << "return i+1;";
}
cpp << "}";
}
}
cpp << "}";
cpp << "throw CTiglError(\"Invalid UID in " << className << "::Get" << capitalizeFirstLetter(f.cpacsName) << "Index\", TIGL_UID_ERROR);";
}
cpp << "}";
cpp << EmptyLine;
}

//Getters for elements in vector type by index;
for (auto isConst : { false, true }) {
if(isConst){
Expand All @@ -396,14 +453,15 @@ namespace tigl {
cpp << "{";
{
Scope s(cpp);
cpp << "index--;";
cpp << "if (index < 0 || index >= Get" << capitalizeFirstLetter(f.cpacsName) << "Count()) {";
cpp << "if (index < 1 || index > Get" << capitalizeFirstLetter(f.cpacsName) << "Count()) {";
{
Scope s(cpp);
cpp << "throw CTiglError(\"Invalid index in " << getterSetterType(f) << "::Get" << capitalizeFirstLetter(f.cpacsName) << "\", TIGL_INDEX_ERROR);";
}
cpp << "}";
if (vectorInnerTypeIsUniquePtr(f)) {
cpp << "index--;";
// CPACS classes are stored in smart pointers and we need to derefence
if (vectorInnerTypeIsCPACSClass(f)) {
cpp << "return *" << f.fieldName() << "[index];";
} else {
cpp << "return " << f.fieldName() << "[index];";
Expand All @@ -413,6 +471,38 @@ namespace tigl {
cpp << EmptyLine;
}

//Getters for elements in vector type by uid;
if(vectorInnerTypeIsCPACSClass(f) && hasUidField(vectorInnerClass(f))){
for (auto isConst : { false, true }) {
if(isConst){
cpp << "const " << vectorInnerType(f) << "& " << className << "::Get" << capitalizeFirstLetter(f.cpacsName) << "(const std::string& UID) const";
} else {
cpp << vectorInnerType(f) << "& " << className << "::Get" << capitalizeFirstLetter(f.cpacsName) << "(const std::string& UID)";
}
cpp << "{";
{
Scope s(cpp);
cpp << "for (auto& elem : " << f.fieldName() <<" ) {";
{
Scope s(cpp);
cpp << "if (elem->GetUID() == UID)";
{
Scope s(cpp);
if (vectorInnerTypeIsCPACSClass(f)) {
cpp << "return *elem;";
} else {
cpp << "return elem;";
}

}
cpp << "}";
cpp << "throw CTiglError(\"Invalid UID in " << className << "::Get" << capitalizeFirstLetter(f.cpacsName) << ". \\\"\"+ UID + \"\\\" not found in CPACS file!\" , TIGL_UID_ERROR);";
}
}
cpp << "}";
cpp << EmptyLine;
}
}

}// generate special accessors for uid reference vectors
else if (f.cardinality() == Cardinality::Vector && f.xmlTypeName == c_uidRefType) {
Expand Down
27 changes: 15 additions & 12 deletions test/data/sequence/ref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace generated
TIGL_EXPORT virtual std::vector<int>& GetEs();

TIGL_EXPORT virtual size_t GetECount() const;

TIGL_EXPORT virtual const int& GetE(size_t index) const;
TIGL_EXPORT virtual int& GetE(size_t index);

Expand All @@ -69,6 +70,7 @@ namespace generated
TIGL_EXPORT virtual std::vector<int>& GetGs();

TIGL_EXPORT virtual size_t GetGCount() const;

TIGL_EXPORT virtual const int& GetG(size_t index) const;
TIGL_EXPORT virtual int& GetG(size_t index);

Expand All @@ -79,6 +81,7 @@ namespace generated
TIGL_EXPORT virtual std::vector<int>& GetIs();

TIGL_EXPORT virtual size_t GetICount() const;

TIGL_EXPORT virtual const int& GetI(size_t index) const;
TIGL_EXPORT virtual int& GetI(size_t index);

Expand Down Expand Up @@ -325,19 +328,19 @@ namespace generated

int& CPACSRoot::GetE(size_t index)
{
index--;
if (index < 0 || index >= GetECount()) {
if (index < 1 || index > GetECount()) {
throw CTiglError("Invalid index in std::vector<int>::GetE", TIGL_INDEX_ERROR);
}
index--;
return m_es[index];
}

const int& CPACSRoot::GetE(size_t index) const
{
index--;
if (index < 0 || index >= GetECount()) {
if (index < 1 || index > GetECount()) {
throw CTiglError("Invalid index in std::vector<int>::GetE", TIGL_INDEX_ERROR);
}
index--;
return m_es[index];
}

Expand Down Expand Up @@ -369,19 +372,19 @@ namespace generated

int& CPACSRoot::GetG(size_t index)
{
index--;
if (index < 0 || index >= GetGCount()) {
if (index < 1 || index > GetGCount()) {
throw CTiglError("Invalid index in std::vector<int>::GetG", TIGL_INDEX_ERROR);
}
index--;
return m_gs[index];
}

const int& CPACSRoot::GetG(size_t index) const
{
index--;
if (index < 0 || index >= GetGCount()) {
if (index < 1 || index > GetGCount()) {
throw CTiglError("Invalid index in std::vector<int>::GetG", TIGL_INDEX_ERROR);
}
index--;
return m_gs[index];
}

Expand Down Expand Up @@ -413,19 +416,19 @@ namespace generated

int& CPACSRoot::GetI(size_t index)
{
index--;
if (index < 0 || index >= GetICount()) {
if (index < 1 || index > GetICount()) {
throw CTiglError("Invalid index in std::vector<int>::GetI", TIGL_INDEX_ERROR);
}
index--;
return m_is[index];
}

const int& CPACSRoot::GetI(size_t index) const
{
index--;
if (index < 0 || index >= GetICount()) {
if (index < 1 || index > GetICount()) {
throw CTiglError("Invalid index in std::vector<int>::GetI", TIGL_INDEX_ERROR);
}
index--;
return m_is[index];
}

Expand Down