diff --git a/CHANGELOG.md b/CHANGELOG.md index f9a64208b6..ae167ff91a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased ### Breaking changes: * C++ enum function `to_string()` generated by `@Cpp(ToString)` attribute returns `std::string_view` now. + * Constants of `String` type are now generated as `std::string_view` in C++ code. ## 13.3.2 Release date: 2022-08-25 @@ -32,6 +33,7 @@ Release date: 2022-08-22 ## 13.2.0 Release date: 2022-08-19 + ### Features: * Added support for per-platform visibility attributes (e.g. `@Java(Internal)`, etc.). * Added a `null` check in Dart for non-nullable class and interface usages. diff --git a/functional-tests/functional/input/src/cpp/Constants.cpp b/functional-tests/functional/input/src/cpp/Constants.cpp index b44e5b1bc5..b6dc9a8b3f 100644 --- a/functional-tests/functional/input/src/cpp/Constants.cpp +++ b/functional-tests/functional/input/src/cpp/Constants.cpp @@ -53,7 +53,7 @@ UseTypeCollectionConstants::get_double_constant( ) std::string UseTypeCollectionConstants::get_string_constant( ) { - return test::Constants::STRING_CONSTANT; + return std::string{test::Constants::STRING_CONSTANT}; } test::Constants::StateEnum @@ -91,7 +91,7 @@ UseInterfaceConstants::get_double_constant( ) std::string UseInterfaceConstants::get_string_constant( ) { - return ConstantsInterface::STRING_CONSTANT; + return std::string{ConstantsInterface::STRING_CONSTANT}; } ConstantsInterface::StateEnum diff --git a/functional-tests/functional/input/src/cpp/StructsWithConstants.cpp b/functional-tests/functional/input/src/cpp/StructsWithConstants.cpp index d464eba3a0..26712f2bf6 100644 --- a/functional-tests/functional/input/src/cpp/StructsWithConstants.cpp +++ b/functional-tests/functional/input/src/cpp/StructsWithConstants.cpp @@ -27,13 +27,13 @@ namespace test std::string SimpleRoute::get_default_description( ) { - return SimpleRoute::DEFAULT_DESCRIPTION; + return std::string{SimpleRoute::DEFAULT_DESCRIPTION}; } std::string StructsWithConstantsInterface::MultiRoute::get_default_description( ) { - return StructsWithConstantsInterface::MultiRoute::DEFAULT_DESCRIPTION; + return std::string{StructsWithConstantsInterface::MultiRoute::DEFAULT_DESCRIPTION}; } } diff --git a/gluecodium/src/main/java/com/here/gluecodium/generator/cpp/CppGeneratorPredicates.kt b/gluecodium/src/main/java/com/here/gluecodium/generator/cpp/CppGeneratorPredicates.kt index 297ccee0a4..7520c1179d 100644 --- a/gluecodium/src/main/java/com/here/gluecodium/generator/cpp/CppGeneratorPredicates.kt +++ b/gluecodium/src/main/java/com/here/gluecodium/generator/cpp/CppGeneratorPredicates.kt @@ -23,6 +23,7 @@ import com.here.gluecodium.generator.common.CommonGeneratorPredicates import com.here.gluecodium.model.lime.LimeAttributeType import com.here.gluecodium.model.lime.LimeAttributeValueType import com.here.gluecodium.model.lime.LimeBasicType +import com.here.gluecodium.model.lime.LimeConstant import com.here.gluecodium.model.lime.LimeContainerWithInheritance import com.here.gluecodium.model.lime.LimeField import com.here.gluecodium.model.lime.LimeFunction @@ -82,6 +83,7 @@ internal object CppGeneratorPredicates { it.attributes.have(LimeAttributeType.CPP, LimeAttributeValueType.CSTRING) } }, + "isStringConstant" to { limeConstant: Any -> limeConstant is LimeConstant && isStringConstant(limeConstant) }, "needsAllFieldsConstructor" to { limeStruct: Any -> when { limeStruct !is LimeStruct -> false @@ -112,6 +114,11 @@ internal object CppGeneratorPredicates { } ) + fun isStringConstant(limeConstant: LimeConstant): Boolean { + val actualType = limeConstant.typeRef.type.actualType + return actualType is LimeBasicType && actualType.typeId == LimeBasicType.TypeId.STRING + } + private fun needsNotNullComment(limeTypeRef: LimeTypeRef) = !limeTypeRef.isNullable && limeTypeRef.type.actualType is LimeContainerWithInheritance diff --git a/gluecodium/src/main/java/com/here/gluecodium/generator/cpp/CppHeaderIncludesCollector.kt b/gluecodium/src/main/java/com/here/gluecodium/generator/cpp/CppHeaderIncludesCollector.kt index 43eb957b30..517f532f1b 100644 --- a/gluecodium/src/main/java/com/here/gluecodium/generator/cpp/CppHeaderIncludesCollector.kt +++ b/gluecodium/src/main/java/com/here/gluecodium/generator/cpp/CppHeaderIncludesCollector.kt @@ -48,9 +48,8 @@ internal class CppHeaderIncludesCollector( val allValues = LimeTypeHelper.getAllValues(limeElement) val equatableTypes = allTypes.filter { it.external?.cpp == null && it.attributes.have(LimeAttributeType.EQUATABLE) } - return allTypes.filterIsInstance() - .flatMap { it.functions } - .flatMap { includesResolver.resolveElementImports(it) } + + val containers = allTypes.filterIsInstance() + return containers.flatMap { it.functions }.flatMap { includesResolver.resolveElementImports(it) } + allTypeRefs.flatMap { includesResolver.resolveElementImports(it) } + allValues.flatMap { includesResolver.resolveElementImports(it) } + allTypes.flatMap { includesResolver.resolveElementImports(it) } + @@ -82,6 +81,9 @@ internal class CppHeaderIncludesCollector( if (typeRegisteredClasses.isNotEmpty()) { additionalIncludes += includesResolver.typeRepositoryInclude } + if (limeElement is LimeContainer && limeElement.constants.any { CppGeneratorPredicates.isStringConstant(it) }) { + additionalIncludes += CppLibraryIncludes.STRING_VIEW + } return additionalIncludes } } diff --git a/gluecodium/src/main/resources/templates/cpp/CppClassImpl.mustache b/gluecodium/src/main/resources/templates/cpp/CppClassImpl.mustache index a0b0f9e442..1928142ca6 100644 --- a/gluecodium/src/main/resources/templates/cpp/CppClassImpl.mustache +++ b/gluecodium/src/main/resources/templates/cpp/CppClassImpl.mustache @@ -45,7 +45,7 @@ bool {{>cpp/CppStructImpl}} {{/structs}} {{#constants}} -const {{resolveName typeRef}} {{parentName}}::{{resolveName}} = {{resolveName value}}; +{{>cpp/CppConstantImpl}} {{/constants}} {{#each classes interfaces}} {{>cpp/CppClassImpl}} diff --git a/gluecodium/src/main/resources/templates/cpp/CppConstant.mustache b/gluecodium/src/main/resources/templates/cpp/CppConstant.mustache index eb1ef7a69b..e1fe5df128 100644 --- a/gluecodium/src/main/resources/templates/cpp/CppConstant.mustache +++ b/gluecodium/src/main/resources/templates/cpp/CppConstant.mustache @@ -19,4 +19,6 @@ ! !}} {{>cpp/CppDocComment}}{{>cpp/CppAttributes}} -{{#if needsExport}}{{>cpp/CppExportMacro}}{{/if}}{{storageQualifier}} const {{resolveName typeRef}} {{resolveName}}; +{{#if needsExport}}{{>cpp/CppExportMacro}}{{/if}}{{storageQualifier}} {{!! +}}{{#ifPredicate "isStringConstant"}}constexpr std::string_view {{resolveName}} = {{resolveName value}}{{/ifPredicate}}{{!! +}}{{#unlessPredicate "isStringConstant"}}const {{resolveName typeRef}} {{resolveName}}{{/unlessPredicate}}; diff --git a/gluecodium/src/main/resources/templates/cpp/CppConstantImpl.mustache b/gluecodium/src/main/resources/templates/cpp/CppConstantImpl.mustache new file mode 100644 index 0000000000..6b3e23406f --- /dev/null +++ b/gluecodium/src/main/resources/templates/cpp/CppConstantImpl.mustache @@ -0,0 +1,22 @@ +{{!! + ! + ! Copyright (C) 2016-2022 HERE Europe B.V. + ! + ! Licensed under the Apache License, Version 2.0 (the "License"); + ! you may not use this file except in compliance with the License. + ! You may obtain a copy of the License at + ! + ! http://www.apache.org/licenses/LICENSE-2.0 + ! + ! Unless required by applicable law or agreed to in writing, software + ! distributed under the License is distributed on an "AS IS" BASIS, + ! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ! See the License for the specific language governing permissions and + ! limitations under the License. + ! + ! SPDX-License-Identifier: Apache-2.0 + ! License-Filename: LICENSE + ! + !}} +{{#unlessPredicate "isStringConstant"}}const {{resolveName typeRef}} {{parentName}}::{{resolveName}} = {{resolveName value}}; +{{/unlessPredicate}} \ No newline at end of file diff --git a/gluecodium/src/main/resources/templates/cpp/CppStructImpl.mustache b/gluecodium/src/main/resources/templates/cpp/CppStructImpl.mustache index 39de3a52eb..93332f1b1b 100644 --- a/gluecodium/src/main/resources/templates/cpp/CppStructImpl.mustache +++ b/gluecodium/src/main/resources/templates/cpp/CppStructImpl.mustache @@ -27,7 +27,7 @@ {{>cpp/CppStructImpl}} {{/structs}} {{#constants}} -const {{resolveName typeRef}} {{parentName}}::{{resolveName}} = {{resolveName value}}; +{{>cpp/CppConstantImpl}} {{/constants}} {{#each classes interfaces}} {{>cpp/CppClassImpl}} diff --git a/gluecodium/src/test/resources/smoke/constants/output/cpp/include/smoke/Constants.h b/gluecodium/src/test/resources/smoke/constants/output/cpp/include/smoke/Constants.h index 2223965f2a..2cc783cb5d 100644 --- a/gluecodium/src/test/resources/smoke/constants/output/cpp/include/smoke/Constants.h +++ b/gluecodium/src/test/resources/smoke/constants/output/cpp/include/smoke/Constants.h @@ -6,6 +6,7 @@ #include "gluecodium/ExportGluecodiumCpp.h" #include #include +#include namespace smoke { struct _GLUECODIUM_CPP_EXPORT Constants { static const bool BOOL_CONSTANT; @@ -13,7 +14,7 @@ struct _GLUECODIUM_CPP_EXPORT Constants { static const uint32_t UINT_CONSTANT; static const float FLOAT_CONSTANT; static const double DOUBLE_CONSTANT; - static const ::std::string STRING_CONSTANT; + static constexpr std::string_view STRING_CONSTANT = "Foo bar"; enum class StateEnum { OFF, ON diff --git a/gluecodium/src/test/resources/smoke/constants/output/cpp/src/smoke/Constants.cpp b/gluecodium/src/test/resources/smoke/constants/output/cpp/src/smoke/Constants.cpp index 517a09b11f..5b348e09c6 100644 --- a/gluecodium/src/test/resources/smoke/constants/output/cpp/src/smoke/Constants.cpp +++ b/gluecodium/src/test/resources/smoke/constants/output/cpp/src/smoke/Constants.cpp @@ -10,6 +10,5 @@ const int32_t Constants::INT_CONSTANT = -11; const uint32_t Constants::UINT_CONSTANT = 4294967295; const float Constants::FLOAT_CONSTANT = 2.71f; const double Constants::DOUBLE_CONSTANT = -3.14; -const ::std::string Constants::STRING_CONSTANT = "Foo bar"; const ::smoke::Constants::StateEnum Constants::ENUM_CONSTANT = ::smoke::Constants::StateEnum::ON; } diff --git a/gluecodium/src/test/resources/smoke/structs/output/cpp/include/smoke/StructsWithConstants.h b/gluecodium/src/test/resources/smoke/structs/output/cpp/include/smoke/StructsWithConstants.h index d0bb755b70..0e5e8c68c7 100644 --- a/gluecodium/src/test/resources/smoke/structs/output/cpp/include/smoke/StructsWithConstants.h +++ b/gluecodium/src/test/resources/smoke/structs/output/cpp/include/smoke/StructsWithConstants.h @@ -6,10 +6,11 @@ #include "gluecodium/ExportGluecodiumCpp.h" #include "smoke/RouteUtils.h" #include +#include namespace smoke { struct _GLUECODIUM_CPP_EXPORT StructsWithConstants { struct _GLUECODIUM_CPP_EXPORT Route { - static const ::std::string DEFAULT_DESCRIPTION; + static constexpr std::string_view DEFAULT_DESCRIPTION = "Nonsense"; static const ::smoke::RouteUtils::RouteType DEFAULT_TYPE; ::std::string description; ::smoke::RouteUtils::RouteType type; diff --git a/gluecodium/src/test/resources/smoke/structs/output/cpp/include/smoke/StructsWithConstantsInterface.h b/gluecodium/src/test/resources/smoke/structs/output/cpp/include/smoke/StructsWithConstantsInterface.h index daaae8283b..2dfdd48a9e 100644 --- a/gluecodium/src/test/resources/smoke/structs/output/cpp/include/smoke/StructsWithConstantsInterface.h +++ b/gluecodium/src/test/resources/smoke/structs/output/cpp/include/smoke/StructsWithConstantsInterface.h @@ -7,6 +7,7 @@ #include "gluecodium/VectorHash.h" #include "smoke/RouteUtils.h" #include +#include #include namespace smoke { class _GLUECODIUM_CPP_EXPORT StructsWithConstantsInterface { @@ -15,7 +16,7 @@ class _GLUECODIUM_CPP_EXPORT StructsWithConstantsInterface { virtual ~StructsWithConstantsInterface() = 0; public: struct _GLUECODIUM_CPP_EXPORT MultiRoute { - static const ::std::string DEFAULT_DESCRIPTION; + static constexpr std::string_view DEFAULT_DESCRIPTION = "Foo"; static const ::smoke::RouteUtils::RouteType DEFAULT_TYPE; ::std::vector< ::std::string > descriptions; ::smoke::RouteUtils::RouteType type; @@ -23,7 +24,7 @@ class _GLUECODIUM_CPP_EXPORT StructsWithConstantsInterface { MultiRoute( ::std::vector< ::std::string > descriptions, ::smoke::RouteUtils::RouteType type ); }; struct _GLUECODIUM_CPP_EXPORT StructWithConstantsOnly { - static const ::std::string DEFAULT_DESCRIPTION; + static constexpr std::string_view DEFAULT_DESCRIPTION = "Foo"; }; }; } diff --git a/gluecodium/src/test/resources/smoke/structs/output/cpp/src/smoke/StructsWithConstants.cpp b/gluecodium/src/test/resources/smoke/structs/output/cpp/src/smoke/StructsWithConstants.cpp index 8153220d88..7b4a4e98a4 100644 --- a/gluecodium/src/test/resources/smoke/structs/output/cpp/src/smoke/StructsWithConstants.cpp +++ b/gluecodium/src/test/resources/smoke/structs/output/cpp/src/smoke/StructsWithConstants.cpp @@ -5,7 +5,6 @@ #include "smoke/StructsWithConstants.h" #include namespace smoke { -const ::std::string StructsWithConstants::Route::DEFAULT_DESCRIPTION = "Nonsense"; const ::smoke::RouteUtils::RouteType StructsWithConstants::Route::DEFAULT_TYPE = ::smoke::RouteUtils::RouteType::EQUESTRIAN; StructsWithConstants::Route::Route( ) : description{ }, type{ } diff --git a/gluecodium/src/test/resources/smoke/structs/output/cpp/src/smoke/StructsWithConstantsInterface.cpp b/gluecodium/src/test/resources/smoke/structs/output/cpp/src/smoke/StructsWithConstantsInterface.cpp index a9ea28d91f..d7e9b2067e 100644 --- a/gluecodium/src/test/resources/smoke/structs/output/cpp/src/smoke/StructsWithConstantsInterface.cpp +++ b/gluecodium/src/test/resources/smoke/structs/output/cpp/src/smoke/StructsWithConstantsInterface.cpp @@ -9,7 +9,6 @@ StructsWithConstantsInterface::StructsWithConstantsInterface() { } StructsWithConstantsInterface::~StructsWithConstantsInterface() { } -const ::std::string StructsWithConstantsInterface::MultiRoute::DEFAULT_DESCRIPTION = "Foo"; const ::smoke::RouteUtils::RouteType StructsWithConstantsInterface::MultiRoute::DEFAULT_TYPE = ::smoke::RouteUtils::RouteType::NONE; StructsWithConstantsInterface::MultiRoute::MultiRoute( ) : descriptions{ }, type{ } @@ -19,5 +18,4 @@ StructsWithConstantsInterface::MultiRoute::MultiRoute( ::std::vector< ::std::str : descriptions( std::move( descriptions ) ), type( std::move( type ) ) { } -const ::std::string StructsWithConstantsInterface::StructWithConstantsOnly::DEFAULT_DESCRIPTION = "Foo"; }