Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phaser/compiler/gen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ Generator::Generator(const google::protobuf::FileDescriptor* file,
void Generator::GenerateHeaders(std::ostream& os, std::string* error) {
os << "#pragma once\n";
os << "#include \"phaser/runtime/runtime.h\"\n";
os << "#include <new>\n";
if (frontend_style_ == FrontendStyle::kRos && UsesRos1Intrinsic(file_)) {
os << "#include \"phaser/runtime/ros.h\"\n";
}
Expand Down
28 changes: 28 additions & 0 deletions phaser/compiler/message_gen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,8 @@ absl::Status MessageGenerator::GenerateHeader(std::ostream& os) {
if (IsRosFrontend()) {
GenerateRosOwnerCopyMove(os, true);
GeneratePublicFieldDeclarations(os);
} else {
GenerateProtobufCopyMove(os, true);
}

// Generate protobuf accessors.
Expand Down Expand Up @@ -1030,6 +1032,30 @@ void MessageGenerator::GenerateRosOwnerCopyMove(std::ostream& os, bool decl) {
os << "}\n\n";
}

void MessageGenerator::GenerateProtobufCopyMove(std::ostream& os, bool decl) {
if (IsRosFrontend()) {
return;
}
const std::string name = MessageName(message_);
if (decl) {
os << " " << name << "(const " << name << "& other) = default;\n";
os << " " << name << "& operator=(const " << name
<< "& other) = default;\n";
os << " " << name << "(" << name << "&& other) noexcept = default;\n";
os << " " << name << "& operator=(" << name << "&& other) noexcept;\n\n";
return;
}

os << name << "& " << name << "::operator=(" << name
<< "&& other) noexcept {\n";
os << " if (this != &other) {\n";
os << " this->~" << name << "();\n";
os << " new (this) " << name << "(std::move(other));\n";
os << " }\n";
os << " return *this;\n";
os << "}\n\n";
}

void MessageGenerator::GenerateSource(std::ostream& os) {
for (const auto& nested : nested_message_gens_) {
nested->GenerateSource(os);
Expand All @@ -1038,6 +1064,8 @@ void MessageGenerator::GenerateSource(std::ostream& os) {
GenerateConstructors(os, false);
if (IsRosFrontend()) {
GenerateRosOwnerCopyMove(os, false);
} else {
GenerateProtobufCopyMove(os, false);
}

// Generate creators.
Expand Down
1 change: 1 addition & 0 deletions phaser/compiler/message_gen.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class MessageGenerator {
void GeneratePublicFieldDeclarations(std::ostream& os);
void GenerateRosOneofTypes(std::ostream& os);
void GenerateRosOwnerCopyMove(std::ostream& os, bool decl);
void GenerateProtobufCopyMove(std::ostream& os, bool decl);
void GenerateRosSyncToPayload(std::ostream& os);
void GenerateProtobufAccessors(std::ostream& os);
void GenerateFieldProtobufAccessors(std::ostream& os);
Expand Down
16 changes: 16 additions & 0 deletions phaser/stress_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ TEST(StressTest, DynamicExplicitBufferExpansion) {
EXPECT_EQ("expand-me", msg.s());
}

TEST(StressTest, MoveAssignmentReplacesMessageStorage) {
TestMessage destination;
destination.set_x(1);
destination.set_s("destination");

TestMessage source = TestMessage::CreateDynamicMutable(512);
source.set_x(42);
source.set_s("source");

destination = std::move(source);

EXPECT_EQ(destination.x(), 42);
EXPECT_EQ(destination.s(), "source");
EXPECT_FALSE(source.IsBound());
}

TEST(StressTest, AllocFailsAtStart) {
auto status = ::phaser::NewDynamicBuffer(
1024, ::phaser::test::AllocUntilLimit(0),
Expand Down
Loading