Skip to content

Commit

Permalink
C++ string constants as std::string_view
Browse files Browse the repository at this point in the history
Resolves: #203
Signed-off-by: Daniel Kamkha <[email protected]>
  • Loading branch information
DanielKamkha committed Aug 25, 2022
1 parent e9af39d commit 3ea3291
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions functional-tests/functional/input/src/cpp/Constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LimeContainer>()
.flatMap { it.functions }
.flatMap { includesResolver.resolveElementImports(it) } +
val containers = allTypes.filterIsInstance<LimeContainer>()
return containers.flatMap { it.functions }.flatMap { includesResolver.resolveElementImports(it) } +
allTypeRefs.flatMap { includesResolver.resolveElementImports(it) } +
allValues.flatMap { includesResolver.resolveElementImports(it) } +
allTypes.flatMap { includesResolver.resolveElementImports(it) } +
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ bool
{{>cpp/CppStructImpl}}
{{/structs}}
{{#constants}}
const {{resolveName typeRef}} {{parentName}}::{{resolveName}} = {{resolveName value}};
{{>cpp/CppConstantImpl}}
{{/constants}}
{{#each classes interfaces}}
{{>cpp/CppClassImpl}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}};
Original file line number Diff line number Diff line change
@@ -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}}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
{{>cpp/CppStructImpl}}
{{/structs}}
{{#constants}}
const {{resolveName typeRef}} {{parentName}}::{{resolveName}} = {{resolveName value}};
{{>cpp/CppConstantImpl}}
{{/constants}}
{{#each classes interfaces}}
{{>cpp/CppClassImpl}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
#include "gluecodium/ExportGluecodiumCpp.h"
#include <cstdint>
#include <string>
#include <string_view>
namespace smoke {
struct _GLUECODIUM_CPP_EXPORT Constants {
static const bool BOOL_CONSTANT;
static const int32_t INT_CONSTANT;
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
#include "gluecodium/ExportGluecodiumCpp.h"
#include "smoke/RouteUtils.h"
#include <string>
#include <string_view>
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "gluecodium/VectorHash.h"
#include "smoke/RouteUtils.h"
#include <string>
#include <string_view>
#include <vector>
namespace smoke {
class _GLUECODIUM_CPP_EXPORT StructsWithConstantsInterface {
Expand All @@ -15,15 +16,15 @@ 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;
MultiRoute( );
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";
};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "smoke/StructsWithConstants.h"
#include <utility>
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{ }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{ }
Expand All @@ -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";
}

0 comments on commit 3ea3291

Please sign in to comment.