Skip to content

Commit a55a398

Browse files
committed
C++ string constants as std::string_view
Resolves: #203 Signed-off-by: Daniel Kamkha <[email protected]>
1 parent 3328aa6 commit a55a398

File tree

15 files changed

+54
-18
lines changed

15 files changed

+54
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Gluecodium project Release Notes
22

3+
## Unreleased
4+
### Features:
5+
* Constants of `String` type are now generated as `std::string_view` in C++ code.
6+
37
## 13.4.0
48
Release date: 2022-08-29
59
### Features:

functional-tests/functional/input/src/cpp/Constants.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ UseTypeCollectionConstants::get_double_constant( )
5353
std::string
5454
UseTypeCollectionConstants::get_string_constant( )
5555
{
56-
return test::Constants::STRING_CONSTANT;
56+
return std::string{test::Constants::STRING_CONSTANT};
5757
}
5858

5959
test::Constants::StateEnum
@@ -91,7 +91,7 @@ UseInterfaceConstants::get_double_constant( )
9191
std::string
9292
UseInterfaceConstants::get_string_constant( )
9393
{
94-
return ConstantsInterface::STRING_CONSTANT;
94+
return std::string{ConstantsInterface::STRING_CONSTANT};
9595
}
9696

9797
ConstantsInterface::StateEnum

functional-tests/functional/input/src/cpp/StructsWithConstants.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ namespace test
2727
std::string
2828
SimpleRoute::get_default_description( )
2929
{
30-
return SimpleRoute::DEFAULT_DESCRIPTION;
30+
return std::string{SimpleRoute::DEFAULT_DESCRIPTION};
3131
}
3232

3333
std::string
3434
StructsWithConstantsInterface::MultiRoute::get_default_description( )
3535
{
36-
return StructsWithConstantsInterface::MultiRoute::DEFAULT_DESCRIPTION;
36+
return std::string{StructsWithConstantsInterface::MultiRoute::DEFAULT_DESCRIPTION};
3737
}
3838

3939
}

gluecodium/src/main/java/com/here/gluecodium/generator/cpp/CppGeneratorPredicates.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.here.gluecodium.generator.common.CommonGeneratorPredicates
2323
import com.here.gluecodium.model.lime.LimeAttributeType
2424
import com.here.gluecodium.model.lime.LimeAttributeValueType
2525
import com.here.gluecodium.model.lime.LimeBasicType
26+
import com.here.gluecodium.model.lime.LimeConstant
2627
import com.here.gluecodium.model.lime.LimeContainerWithInheritance
2728
import com.here.gluecodium.model.lime.LimeField
2829
import com.here.gluecodium.model.lime.LimeFunction
@@ -82,6 +83,7 @@ internal object CppGeneratorPredicates {
8283
it.attributes.have(LimeAttributeType.CPP, LimeAttributeValueType.CSTRING)
8384
}
8485
},
86+
"isStringConstant" to { limeConstant: Any -> limeConstant is LimeConstant && isStringConstant(limeConstant) },
8587
"needsAllFieldsConstructor" to { limeStruct: Any ->
8688
when {
8789
limeStruct !is LimeStruct -> false
@@ -112,6 +114,11 @@ internal object CppGeneratorPredicates {
112114
}
113115
)
114116

117+
fun isStringConstant(limeConstant: LimeConstant): Boolean {
118+
val actualType = limeConstant.typeRef.type.actualType
119+
return actualType is LimeBasicType && actualType.typeId == LimeBasicType.TypeId.STRING
120+
}
121+
115122
private fun needsNotNullComment(limeTypeRef: LimeTypeRef) =
116123
!limeTypeRef.isNullable && limeTypeRef.type.actualType is LimeContainerWithInheritance
117124

gluecodium/src/main/java/com/here/gluecodium/generator/cpp/CppHeaderIncludesCollector.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ internal class CppHeaderIncludesCollector(
4848
val allValues = LimeTypeHelper.getAllValues(limeElement)
4949
val equatableTypes =
5050
allTypes.filter { it.external?.cpp == null && it.attributes.have(LimeAttributeType.EQUATABLE) }
51-
return allTypes.filterIsInstance<LimeContainer>()
52-
.flatMap { it.functions }
53-
.flatMap { includesResolver.resolveElementImports(it) } +
51+
val containers = allTypes.filterIsInstance<LimeContainer>()
52+
return containers.flatMap { it.functions }.flatMap { includesResolver.resolveElementImports(it) } +
5453
allTypeRefs.flatMap { includesResolver.resolveElementImports(it) } +
5554
allValues.flatMap { includesResolver.resolveElementImports(it) } +
5655
allTypes.flatMap { includesResolver.resolveElementImports(it) } +
@@ -82,6 +81,9 @@ internal class CppHeaderIncludesCollector(
8281
if (typeRegisteredClasses.isNotEmpty()) {
8382
additionalIncludes += includesResolver.typeRepositoryInclude
8483
}
84+
if (limeElement is LimeContainer && limeElement.constants.any { CppGeneratorPredicates.isStringConstant(it) }) {
85+
additionalIncludes += CppLibraryIncludes.STRING_VIEW
86+
}
8587
return additionalIncludes
8688
}
8789
}

gluecodium/src/main/resources/templates/cpp/CppClassImpl.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ bool
4545
{{>cpp/CppStructImpl}}
4646
{{/structs}}
4747
{{#constants}}
48-
const {{resolveName typeRef}} {{parentName}}::{{resolveName}} = {{resolveName value}};
48+
{{>cpp/CppConstantImpl}}
4949
{{/constants}}
5050
{{#each classes interfaces}}
5151
{{>cpp/CppClassImpl}}

gluecodium/src/main/resources/templates/cpp/CppConstant.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@
1919
!
2020
!}}
2121
{{>cpp/CppDocComment}}{{>cpp/CppAttributes}}
22-
{{#if needsExport}}{{>cpp/CppExportMacro}}{{/if}}{{storageQualifier}} const {{resolveName typeRef}} {{resolveName}};
22+
{{#if needsExport}}{{>cpp/CppExportMacro}}{{/if}}{{storageQualifier}} {{!!
23+
}}{{#ifPredicate "isStringConstant"}}constexpr std::string_view {{resolveName}} = {{resolveName value}}{{/ifPredicate}}{{!!
24+
}}{{#unlessPredicate "isStringConstant"}}const {{resolveName typeRef}} {{resolveName}}{{/unlessPredicate}};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{{!!
2+
!
3+
! Copyright (C) 2016-2022 HERE Europe B.V.
4+
!
5+
! Licensed under the Apache License, Version 2.0 (the "License");
6+
! you may not use this file except in compliance with the License.
7+
! You may obtain a copy of the License at
8+
!
9+
! http://www.apache.org/licenses/LICENSE-2.0
10+
!
11+
! Unless required by applicable law or agreed to in writing, software
12+
! distributed under the License is distributed on an "AS IS" BASIS,
13+
! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
! See the License for the specific language governing permissions and
15+
! limitations under the License.
16+
!
17+
! SPDX-License-Identifier: Apache-2.0
18+
! License-Filename: LICENSE
19+
!
20+
!}}
21+
{{#unlessPredicate "isStringConstant"}}const {{resolveName typeRef}} {{parentName}}::{{resolveName}} = {{resolveName value}};
22+
{{/unlessPredicate}}

gluecodium/src/main/resources/templates/cpp/CppStructImpl.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
{{>cpp/CppStructImpl}}
2828
{{/structs}}
2929
{{#constants}}
30-
const {{resolveName typeRef}} {{parentName}}::{{resolveName}} = {{resolveName value}};
30+
{{>cpp/CppConstantImpl}}
3131
{{/constants}}
3232
{{#each classes interfaces}}
3333
{{>cpp/CppClassImpl}}

gluecodium/src/test/resources/smoke/constants/output/cpp/include/smoke/Constants.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
#include "gluecodium/ExportGluecodiumCpp.h"
77
#include <cstdint>
88
#include <string>
9+
#include <string_view>
910
namespace smoke {
1011
struct _GLUECODIUM_CPP_EXPORT Constants {
1112
static const bool BOOL_CONSTANT;
1213
static const int32_t INT_CONSTANT;
1314
static const uint32_t UINT_CONSTANT;
1415
static const float FLOAT_CONSTANT;
1516
static const double DOUBLE_CONSTANT;
16-
static const ::std::string STRING_CONSTANT;
17+
static constexpr std::string_view STRING_CONSTANT = "Foo bar";
1718
enum class StateEnum {
1819
OFF,
1920
ON

0 commit comments

Comments
 (0)