diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d360c3949..f9a64208b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Gluecodium project Release Notes +## Unreleased +### Breaking changes: + * C++ enum function `to_string()` generated by `@Cpp(ToString)` attribute returns `std::string_view` now. + ## 13.3.2 Release date: 2022-08-25 ### Bug fixes: diff --git a/gluecodium/src/main/java/com/here/gluecodium/generator/cpp/CppIncludeResolver.kt b/gluecodium/src/main/java/com/here/gluecodium/generator/cpp/CppIncludeResolver.kt index 0150fedea3..81349d7731 100644 --- a/gluecodium/src/main/java/com/here/gluecodium/generator/cpp/CppIncludeResolver.kt +++ b/gluecodium/src/main/java/com/here/gluecodium/generator/cpp/CppIncludeResolver.kt @@ -24,7 +24,7 @@ import com.here.gluecodium.generator.common.Include import com.here.gluecodium.model.lime.LimeAttributeType.ASYNC import com.here.gluecodium.model.lime.LimeAttributeType.CPP import com.here.gluecodium.model.lime.LimeAttributeType.OPTIMIZED -import com.here.gluecodium.model.lime.LimeAttributeValueType +import com.here.gluecodium.model.lime.LimeAttributeValueType.TO_STRING import com.here.gluecodium.model.lime.LimeBasicType import com.here.gluecodium.model.lime.LimeBasicType.TypeId import com.here.gluecodium.model.lime.LimeContainerWithInheritance @@ -72,15 +72,13 @@ internal class CppIncludeResolver( is LimeFunction -> resolveFunctionIncludes(limeElement) is LimeLambda -> cppIncludesCache.resolveIncludes(limeElement) + CppLibraryIncludes.FUNCTIONAL is LimeNamedElement -> cppIncludesCache.resolveIncludes(limeElement) + - if (limeElement.attributes.have(CPP, LimeAttributeValueType.TO_STRING)) listOf(CppLibraryIncludes.STRING) - else emptyList() + listOfNotNull(CppLibraryIncludes.STRING_VIEW.takeIf { limeElement.attributes.have(CPP, TO_STRING) }) else -> emptyList() } private fun resolveFunctionIncludes(limeFunction: LimeFunction) = resolveExceptionIncludes(limeFunction) + - if (limeFunction.attributes.have(ASYNC)) listOf(CppLibraryIncludes.FUNCTIONAL) - else emptyList() + listOfNotNull(CppLibraryIncludes.FUNCTIONAL.takeIf { limeFunction.attributes.have(ASYNC) }) private fun resolveExceptionIncludes(limeFunction: LimeFunction): List { val payloadType = limeFunction.exception?.errorType?.type?.actualType ?: return emptyList() @@ -89,10 +87,7 @@ internal class CppIncludeResolver( if (limeFunction.attributes.have(ASYNC)) cppIncludesCache.resolveIncludes(payloadType) else emptyList() is LimeBasicType -> listOf(returnInclude) else -> cppIncludesCache.resolveIncludes(payloadType) + returnInclude - } + when { - limeFunction.returnType.isVoid -> emptyList() - else -> listOf(returnInclude) - } + } + listOfNotNull(returnInclude.takeIf { !limeFunction.returnType.isVoid }) } private fun resolveValueIncludes(limeValue: LimeValue): List = @@ -123,11 +118,7 @@ internal class CppIncludeResolver( TypeId.DATE -> listOf(CppLibraryIncludes.CHRONO, timePointHashInclude) TypeId.DURATION -> listOf(CppLibraryIncludes.CHRONO, durationHashInclude) TypeId.LOCALE -> listOf(localeInclude) - TypeId.BLOB -> listOf( - CppLibraryIncludes.MEMORY, - CppLibraryIncludes.VECTOR, - CppLibraryIncludes.INT_TYPES - ) + TypeId.BLOB -> listOf(CppLibraryIncludes.MEMORY, CppLibraryIncludes.VECTOR, CppLibraryIncludes.INT_TYPES) else -> emptyList() } } diff --git a/gluecodium/src/main/java/com/here/gluecodium/generator/cpp/CppLibraryIncludes.kt b/gluecodium/src/main/java/com/here/gluecodium/generator/cpp/CppLibraryIncludes.kt index f27a234c29..9bb9374545 100644 --- a/gluecodium/src/main/java/com/here/gluecodium/generator/cpp/CppLibraryIncludes.kt +++ b/gluecodium/src/main/java/com/here/gluecodium/generator/cpp/CppLibraryIncludes.kt @@ -29,6 +29,7 @@ object CppLibraryIncludes { val MAP = Include.createSystemInclude("unordered_map") val MEMORY = Include.createSystemInclude("memory") val STRING = Include.createSystemInclude("string") + val STRING_VIEW = Include.createSystemInclude("string_view") val VECTOR = Include.createSystemInclude("vector") val NEW = Include.createSystemInclude("new") val SYSTEM_ERROR = Include.createSystemInclude("system_error") diff --git a/gluecodium/src/main/resources/templates/cpp/CppEnumeration.mustache b/gluecodium/src/main/resources/templates/cpp/CppEnumeration.mustache index 2dadeea97b..96bd1bbf23 100644 --- a/gluecodium/src/main/resources/templates/cpp/CppEnumeration.mustache +++ b/gluecodium/src/main/resources/templates/cpp/CppEnumeration.mustache @@ -28,7 +28,7 @@ enum class {{>cpp/CppAttributesInline}}{{resolveName}} { }; {{#if attributes.cpp.tostring}} -std::string +std::string_view {{>cpp/CppExportMacro}}to_string({{resolveName}} enumeration); {{/if}} {{/unless}} diff --git a/gluecodium/src/main/resources/templates/cpp/CppEnumerationImpl.mustache b/gluecodium/src/main/resources/templates/cpp/CppEnumerationImpl.mustache index 1bda70165f..7e420e08c8 100644 --- a/gluecodium/src/main/resources/templates/cpp/CppEnumerationImpl.mustache +++ b/gluecodium/src/main/resources/templates/cpp/CppEnumerationImpl.mustache @@ -30,7 +30,7 @@ static_assert( {{/if}} {{#if attributes.cpp.tostring}} - std::string + std::string_view to_string({{resolveName}} enumeration) { switch (enumeration) { {{#set enum=this}}{{#enumerators}} diff --git a/gluecodium/src/test/resources/smoke/enums/output/cpp/include/smoke/EnumWithToStringHelper.h b/gluecodium/src/test/resources/smoke/enums/output/cpp/include/smoke/EnumWithToStringHelper.h index 0c0b0b689b..74106ecd05 100644 --- a/gluecodium/src/test/resources/smoke/enums/output/cpp/include/smoke/EnumWithToStringHelper.h +++ b/gluecodium/src/test/resources/smoke/enums/output/cpp/include/smoke/EnumWithToStringHelper.h @@ -5,12 +5,12 @@ #pragma once #include "gluecodium/ExportGluecodiumCpp.h" #include -#include +#include namespace smoke { enum class EnumWithToStringHelper { FIRST, SECOND }; -std::string +std::string_view _GLUECODIUM_CPP_EXPORT to_string(EnumWithToStringHelper enumeration); } diff --git a/gluecodium/src/test/resources/smoke/enums/output/cpp/src/smoke/EnumWithToStringHelper.cpp b/gluecodium/src/test/resources/smoke/enums/output/cpp/src/smoke/EnumWithToStringHelper.cpp index d6d090368c..6fdc31a8df 100644 --- a/gluecodium/src/test/resources/smoke/enums/output/cpp/src/smoke/EnumWithToStringHelper.cpp +++ b/gluecodium/src/test/resources/smoke/enums/output/cpp/src/smoke/EnumWithToStringHelper.cpp @@ -2,9 +2,9 @@ // // // ------------------------------------------------------------------------------------------------- -#include "smoke\EnumWithToStringHelper.h" +#include "smoke/EnumWithToStringHelper.h" namespace smoke { - std::string + std::string_view to_string(EnumWithToStringHelper enumeration) { switch (enumeration) { case EnumWithToStringHelper::FIRST: return "EnumWithToStringHelper::FIRST";