Skip to content

Commit

Permalink
Use std::string_view for @Cpp(ToString) on enums (#1479)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kamkha <[email protected]>

Signed-off-by: Daniel Kamkha <[email protected]>
  • Loading branch information
DanielKamkha authored Aug 25, 2022
1 parent 0724189 commit e9af39d
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Include> {
val payloadType = limeFunction.exception?.errorType?.type?.actualType ?: return emptyList()
Expand All @@ -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<Include> =
Expand Down Expand Up @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#pragma once
#include "gluecodium/ExportGluecodiumCpp.h"
#include <cstdint>
#include <string>
#include <string_view>
namespace smoke {
enum class EnumWithToStringHelper {
FIRST,
SECOND
};
std::string
std::string_view
_GLUECODIUM_CPP_EXPORT to_string(EnumWithToStringHelper enumeration);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit e9af39d

Please sign in to comment.