-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C++: use references for non-trivial types in accessors #1607
Merged
pwrobeldev
merged 3 commits into
master
from
pwrobeldev/fix-struct-accessors-use-const-reference-for-nontrivial-types
Oct 30, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
functional-tests/functional/cpp/tests/AccessorsTest.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (C) 2024 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 | ||
// | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
#include "test/SomeMutableStructWithCppAccessors.h" | ||
|
||
#include <gmock/gmock.h> | ||
|
||
#include <cstdint> | ||
#include <string> | ||
#include <type_traits> | ||
#include <vector> | ||
|
||
namespace test | ||
{ | ||
|
||
using namespace ::testing; | ||
using namespace test; | ||
|
||
namespace { | ||
|
||
using TrivialInt = int32_t; | ||
using TrivialDouble = double; | ||
using NontrivialString = std::string; | ||
using NontrivialList = std::vector<std::string>; | ||
|
||
SomeMutableStructWithCppAccessors get_struct() { | ||
return SomeMutableStructWithCppAccessors { | ||
TrivialInt{32}, | ||
TrivialDouble{27.5}, | ||
NontrivialString{"Some string"}, | ||
NontrivialList{{"S1", "S2", "S3"}} | ||
}; | ||
} | ||
|
||
const SomeMutableStructWithCppAccessors get_const_struct() { | ||
return SomeMutableStructWithCppAccessors { | ||
TrivialInt{64}, | ||
TrivialDouble{52.3}, | ||
NontrivialString{"Another string"}, | ||
NontrivialList{{"A1", "A2", "A3"}} | ||
}; | ||
} | ||
|
||
} // namespace | ||
|
||
TEST( AccessorsOfMutableStruct, GettingFieldsOnLvalue ) | ||
{ | ||
// Given an object of mutable struct with accessors. | ||
SomeMutableStructWithCppAccessors object = get_struct(); | ||
|
||
// When using getters on lvalue. | ||
const auto& int_field = object.get_trivial_int_field(); | ||
const auto& double_field = object.get_trivial_double_field(); | ||
const auto& string_field = object.get_nontrivial_string_field(); | ||
const auto& list_field = object.get_nontrivial_list_field(); | ||
|
||
// Then access can be made. | ||
EXPECT_EQ(TrivialInt{32}, int_field); | ||
EXPECT_DOUBLE_EQ(TrivialDouble{27.5}, double_field); | ||
EXPECT_EQ(NontrivialString{"Some string"}, string_field); | ||
|
||
const NontrivialList expected_list{"S1", "S2", "S3"}; | ||
EXPECT_EQ(expected_list, list_field); | ||
} | ||
|
||
TEST( AccessorsOfMutableStruct, GettingFieldsOnConstLvalue ) | ||
{ | ||
// Given const reference to object of mutable struct with accessors. | ||
const auto& object = get_const_struct(); | ||
|
||
// When using getters on const lvalue. | ||
const auto& int_field = object.get_trivial_int_field(); | ||
const auto& double_field = object.get_trivial_double_field(); | ||
const auto& string_field = object.get_nontrivial_string_field(); | ||
const auto& list_field = object.get_nontrivial_list_field(); | ||
|
||
// Then access can be made. | ||
EXPECT_EQ(TrivialInt{64}, int_field); | ||
EXPECT_DOUBLE_EQ(TrivialDouble{52.3}, double_field); | ||
EXPECT_EQ(NontrivialString{"Another string"}, string_field); | ||
|
||
const NontrivialList expected_list{"A1", "A2", "A3"}; | ||
EXPECT_EQ(expected_list, list_field); | ||
} | ||
|
||
TEST( AccessorsOfMutableStruct, GettingFieldsOnRvalue ) | ||
{ | ||
// When using getter on rvalue. | ||
auto int_field = get_struct().get_trivial_int_field(); | ||
auto double_field = get_struct().get_trivial_double_field(); | ||
auto string_field = get_struct().get_nontrivial_string_field(); | ||
auto list_field = get_struct().get_nontrivial_list_field(); | ||
|
||
// Then access can be made. | ||
EXPECT_EQ(TrivialInt{32}, int_field); | ||
EXPECT_DOUBLE_EQ(TrivialDouble{27.5}, double_field); | ||
EXPECT_EQ(NontrivialString{"Some string"}, string_field); | ||
|
||
const NontrivialList expected_list{"S1", "S2", "S3"}; | ||
EXPECT_EQ(expected_list, list_field); | ||
} | ||
|
||
TEST( AccessorsOfMutableStruct, GettingFieldsOnConstRvalue ) | ||
{ | ||
// When using getters on const rvalue. | ||
auto int_field = get_const_struct().get_trivial_int_field(); | ||
auto double_field = get_const_struct().get_trivial_double_field(); | ||
auto string_field = get_const_struct().get_nontrivial_string_field(); | ||
auto list_field = get_const_struct().get_nontrivial_list_field(); | ||
|
||
// Then access can be made. | ||
EXPECT_EQ(TrivialInt{64}, int_field); | ||
EXPECT_DOUBLE_EQ(TrivialDouble{52.3}, double_field); | ||
EXPECT_EQ(NontrivialString{"Another string"}, string_field); | ||
|
||
const NontrivialList expected_list{"A1", "A2", "A3"}; | ||
EXPECT_EQ(expected_list, list_field); | ||
} | ||
|
||
TEST( AccessorsOfMutableStruct, PreconditionsForNontrivialGetters ) | ||
{ | ||
// String getters. | ||
using LvalueStringGetterRetT = decltype(std::declval<SomeMutableStructWithCppAccessors&>().get_nontrivial_string_field()); | ||
static_assert(std::is_same_v<const std::string&, LvalueStringGetterRetT>, "String getter shall return const ref when called on L-value"); | ||
|
||
using ConstLvalueStringGetterRetT = decltype(std::declval<const SomeMutableStructWithCppAccessors&>().get_nontrivial_string_field()); | ||
static_assert(std::is_same_v<const std::string&, ConstLvalueStringGetterRetT>, "String getter shall return const ref when called on const L-value"); | ||
|
||
using RvalueStringGetterRetT = decltype(std::declval<SomeMutableStructWithCppAccessors&&>().get_nontrivial_string_field()); | ||
static_assert(std::is_same_v<std::string&&, RvalueStringGetterRetT>, "String getter shall return r-ref when called on R-value"); | ||
|
||
using ConstRvalueStringGetterRetT = decltype(std::declval<const SomeMutableStructWithCppAccessors&&>().get_nontrivial_string_field()); | ||
static_assert(std::is_same_v<const std::string&&, ConstRvalueStringGetterRetT>, "String getter shall return const r-ref when called on const R-value"); | ||
|
||
// List getters. | ||
using LvalueListGetterRetT = decltype(std::declval<SomeMutableStructWithCppAccessors&>().get_nontrivial_list_field()); | ||
static_assert(std::is_same_v<const std::vector<std::string>&, LvalueListGetterRetT>, "List getter shall return const l-ref when called on L-value"); | ||
|
||
using ConstLvalueListGetterRetT = decltype(std::declval<const SomeMutableStructWithCppAccessors&>().get_nontrivial_list_field()); | ||
static_assert(std::is_same_v<const std::vector<std::string>&, ConstLvalueListGetterRetT>, "List getter shall return const l-ref when called on const L-value"); | ||
|
||
using RvalueListGetterRetT = decltype(std::declval<SomeMutableStructWithCppAccessors&&>().get_nontrivial_list_field()); | ||
static_assert(std::is_same_v<std::vector<std::string>&&, RvalueListGetterRetT>, "List getter shall return r-ref when called on R-value"); | ||
|
||
using ConstRvalueListGetterRetT = decltype(std::declval<const SomeMutableStructWithCppAccessors&&>().get_nontrivial_list_field()); | ||
static_assert(std::is_same_v<const std::vector<std::string>&&, ConstRvalueListGetterRetT>, "List getter shall return const r-ref when called on const R-value"); | ||
} | ||
|
||
TEST( AccessorsOfMutableStruct, PreconditionsForTrivialGetters ) | ||
{ | ||
// Int getter. | ||
using LvalueIntGetterRetT = decltype(std::declval<SomeMutableStructWithCppAccessors&>().get_trivial_int_field()); | ||
static_assert(std::is_same_v<int, LvalueIntGetterRetT>, "int getter shall always return by-value"); | ||
|
||
using ConstLvalueIntGetterRetT = decltype(std::declval<const SomeMutableStructWithCppAccessors&>().get_trivial_int_field()); | ||
static_assert(std::is_same_v<int, ConstLvalueIntGetterRetT>, "int getter shall always return by-value"); | ||
|
||
using RvalueIntGetterRetT = decltype(std::declval<SomeMutableStructWithCppAccessors&&>().get_trivial_int_field()); | ||
static_assert(std::is_same_v<int, RvalueIntGetterRetT>, "int getter shall always return by-value"); | ||
|
||
using ConstRvalueIntGetterRetT = decltype(std::declval<const SomeMutableStructWithCppAccessors&&>().get_trivial_int_field()); | ||
static_assert(std::is_same_v<int, ConstRvalueIntGetterRetT>, "int getter shall always return by-value"); | ||
|
||
// Double getter. | ||
using LvalueDoubleGetterRetT = decltype(std::declval<SomeMutableStructWithCppAccessors&>().get_trivial_double_field()); | ||
static_assert(std::is_same_v<double, LvalueDoubleGetterRetT>, "double getter shall always return by-value"); | ||
|
||
using ConstLvalueDoubleGetterRetT = decltype(std::declval<const SomeMutableStructWithCppAccessors&>().get_trivial_double_field()); | ||
static_assert(std::is_same_v<double, ConstLvalueDoubleGetterRetT>, "double getter shall always return by-value"); | ||
|
||
using RvalueDoubleGetterRetT = decltype(std::declval<SomeMutableStructWithCppAccessors&&>().get_trivial_double_field()); | ||
static_assert(std::is_same_v<double, RvalueDoubleGetterRetT>, "double getter shall always return by-value"); | ||
|
||
using ConstRvalueDoubleGetterRetT = decltype(std::declval<const SomeMutableStructWithCppAccessors&&>().get_trivial_double_field()); | ||
static_assert(std::is_same_v<double, ConstRvalueDoubleGetterRetT>, "double getter shall always return by-value"); | ||
} | ||
|
||
} // test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Copyright (C) 2024 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 | ||
|
||
package test | ||
|
||
@Cpp(Accessors) | ||
struct SomeMutableStructWithCppAccessors { | ||
trivialIntField: Int | ||
trivialDoubleField: Double | ||
|
||
nontrivialStringField: String | ||
nontrivialListField: List<String> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 11 additions & 1 deletion
12
.../src/test/resources/smoke/equatable/output/cpp/src/smoke/EquatableStructWithAccessors.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,43 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// | ||
|
||
// | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
#include "smoke/EquatableStructWithAccessors.h" | ||
#include <utility> | ||
|
||
namespace smoke { | ||
|
||
EquatableStructWithAccessors::EquatableStructWithAccessors( ) | ||
: foo_field{ } | ||
{ | ||
} | ||
|
||
EquatableStructWithAccessors::EquatableStructWithAccessors( ::std::string foo_field ) | ||
: foo_field( std::move( foo_field ) ) | ||
{ | ||
} | ||
|
||
bool EquatableStructWithAccessors::operator==( const EquatableStructWithAccessors& rhs ) const | ||
{ | ||
return foo_field == rhs.foo_field; | ||
} | ||
|
||
bool EquatableStructWithAccessors::operator!=( const EquatableStructWithAccessors& rhs ) const | ||
{ | ||
return !( *this == rhs ); | ||
} | ||
|
||
|
||
} | ||
|
||
namespace gluecodium { | ||
std::size_t | ||
hash< ::smoke::EquatableStructWithAccessors >::operator( )( const ::smoke::EquatableStructWithAccessors& t ) const | ||
{ | ||
size_t hash_value = 43; | ||
hash_value = (hash_value ^ ::gluecodium::hash< decltype(std::declval<::smoke::EquatableStructWithAccessors>().get_foo_field()) >()(t.get_foo_field())) << 1; | ||
hash_value = (hash_value ^ ::gluecodium::hash< std::remove_cv_t< std::remove_reference_t< decltype(std::declval<::smoke::EquatableStructWithAccessors>().get_foo_field()) > > >()(t.get_foo_field())) << 1; | ||
return hash_value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: if these test cases used
auto&&
, then we would have dangling references.