Skip to content
Snippets Groups Projects
Verified Commit 54e91c79 authored by Timm Fitschen's avatar Timm Fitschen
Browse files

WIP: move constructor and assignment operator for Entity

parent 7fbe4ad1
No related branches found
No related tags found
1 merge request!27F move entity
Pipeline #13502 failed
......@@ -52,6 +52,7 @@
#include <random> // for mt19937, rand...
#include <stdexcept> // for out_of_range
#include <string> // for string, basic...
#include <utility> // for move
#include <vector> // for vector
namespace caosdb::entity {
......@@ -611,6 +612,9 @@ private:
*/
class Entity {
public:
/**
* Copy constructor.
*/
inline Entity(const Entity &original) : Entity(Copy(*original.wrapped)) {
this->errors.wrapped->CopyFrom(*original.errors.wrapped);
this->warnings.wrapped->CopyFrom(*original.warnings.wrapped);
......@@ -643,6 +647,37 @@ public:
explicit inline Entity() : Entity(Entity::CreateProtoEntity()){};
/**
* Move constructor.
*/
explicit inline Entity(Entity &&original)
: wrapped(std::move(original.wrapped)), value(Value(original.value.wrapped)),
data_type(DataType(original.data_type.wrapped)) {
// TODO(tf) move other things as well
this->properties.wrapped = std::move(original.properties.wrapped);
this->parents.wrapped = std::move(original.parents.wrapped);
this->errors.wrapped = original.errors.wrapped;
this->warnings.wrapped = original.warnings.wrapped;
this->infos.wrapped = original.infos.wrapped;
};
/**
* Move assignment operator.
*/
auto operator=(Entity &&other) -> Entity & {
this->wrapped = std::move(other.wrapped);
this->data_type.wrapped = std::move(other.data_type.wrapped);
this->value.wrapped = std::move(other.value.wrapped);
this->properties.wrapped = std::move(other.properties.wrapped);
this->parents.wrapped = std::move(other.parents.wrapped);
file_descriptor = std::move(other.file_descriptor);
// TODO(tf) add move to messages
errors.wrapped->Swap(other.errors.wrapped);
warnings.wrapped->Swap(other.warnings.wrapped);
infos.wrapped->Swap(other.infos.wrapped);
return *this;
}
[[nodiscard]] inline auto GetId() const noexcept -> const std::string & { return wrapped->id(); };
[[nodiscard]] inline auto HasId() const noexcept -> bool { return !wrapped->id().empty(); }
[[nodiscard]] inline auto GetVersionId() const -> const std::string & {
......@@ -758,6 +793,15 @@ public:
infos.Clear();
}
/**
* Return true if the other entity is equal to to this entity.
*
* This method ignores the errors, warnings and info messages.
*/
inline auto operator==(const Entity &other) const noexcept -> bool {
return this->wrapped->SerializeAsString() == other.wrapped->SerializeAsString();
}
private:
static inline auto Copy(const ProtoEntity &from) -> ProtoEntity * {
auto to = from.New();
......
......@@ -136,7 +136,6 @@ public:
}
[[nodiscard]] inline auto AsString() const noexcept -> const std::string & {
return this->wrapped->scalar_value().string_value();
;
}
[[nodiscard]] inline auto IsDouble() const noexcept -> bool {
......
......@@ -38,7 +38,7 @@
#include <memory> // for allocator, shared_ptr
#include <stdexcept> // for out_of_range
#include <string> // for operator+, to_string
#include <utility> // for move
namespace caosdb::entity {
using caosdb::entity::v1alpha1::IdResponse;
using ProtoEntity = caosdb::entity::v1alpha1::Entity;
......@@ -460,4 +460,33 @@ TEST(test_entity, test_add_file) {
EXPECT_EQ(entity.SetLocalPath(TEST_DATA_DIR + "/test.json"), StatusCode::SUCCESS);
}
TEST(test_entity, test_move_assign) {
Entity entity1;
entity1.SetRole(Role::RECORD_TYPE);
entity1.SetName("E1");
entity1.SetValue("some-string-1");
entity1.SetDataType("some-other-string-1");
Entity entity2;
entity1.SetRole(Role::RECORD);
entity1.SetName("E2");
entity1.SetValue("some-string-2");
entity1.SetDataType("some-other-string-2");
const Entity copy1(entity1); // NOLINT
const Entity copy2(entity2); // NOLINT
EXPECT_EQ(copy1, entity1);
EXPECT_EQ(copy2, entity2);
// Swap
Entity tmp;
tmp = std::move(entity1);
entity1 = std::move(entity2);
entity2 = std::move(tmp);
EXPECT_EQ(copy2, entity1);
EXPECT_EQ(copy1, entity2);
}
} // namespace caosdb::entity
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment