Skip to content
Snippets Groups Projects

Copy constructor for result set and entity

Files

+ 48
20
@@ -33,6 +33,7 @@
#include "caosdb/entity/v1alpha1/main.pb.h" // for RepeatedPtrField
#include "caosdb/logging.h" // for CAOSDB_LOG_WARN
#include "caosdb/message_code.h" // for get_message_code
#include "caosdb/protobuf_helper.h" // for get_arena
#include "caosdb/status_code.h" // for StatusCode
#include "caosdb/value.h" // for Value
#include <boost/filesystem/operations.hpp> // for exists, is_di...
@@ -67,6 +68,7 @@ using ProtoImportance = caosdb::entity::v1alpha1::Importance;
using caosdb::StatusCode;
using caosdb::entity::v1alpha1::EntityResponse;
using caosdb::entity::v1alpha1::FileTransmissionId;
using caosdb::utility::get_arena;
using ::google::protobuf::RepeatedPtrField;
using google::protobuf::RepeatedPtrField;
@@ -202,6 +204,8 @@ protected:
}
}
inline auto Clear() noexcept -> void { this->wrapped->Clear(); }
::google::protobuf::RepeatedPtrField<P> *wrapped;
mutable std::map<int, T> cache;
@@ -323,7 +327,6 @@ public:
// friend class Parent;
// friend class Property;
private:
inline Messages() : RepeatedPtrFieldWrapper(){};
};
@@ -588,25 +591,30 @@ private:
};
/**
* @brief Wrapper for the Protobuf entity.
* Entity is the central and basic data object of CaosDB.
*
* This class is a wrapper of the Entity class auto-generated by protobuf
* (henceforth "ProtoEntity").
*
* Overview of the Constructors:
*
* <li> Entity() - Calls Entity(ProtoEntity *) with a fresh ProtoEntity
* <li> Entity(Entity) - Copy constructor, calls Entity(ProtoEntity *) after copying wrapped
* ProtoEntity of the original, then also copies all Messages. <li> Entity(ProtoEntity *) - The
* workhorse of the constructors. Initializes everything and does not call other Entity
* constructors. <li> Entity(EntityResponse *) - Constructor which is used by the Transaction class
* to create an Entity from the server's response, calls Entity(ProtoEntity). <li> Entity(IdResponse
* *) - Constructor which is used by the Transaction class to create an Entity from the servers's
* response. calls Entity(), then moves the data to the wrapped ProtoEntity.
*
*/
class Entity {
public:
Entity();
inline Entity(const Entity &original)
: wrapped(original.wrapped), value(Value(original.wrapped->mutable_value())),
data_type(DataType(original.wrapped->mutable_data_type())) {
this->wrapped->CopyFrom(*original.wrapped);
data_type.wrapped = this->wrapped->mutable_data_type();
value.wrapped = this->wrapped->mutable_value();
properties.wrapped = this->wrapped->mutable_properties();
parents.wrapped = this->wrapped->mutable_parents();
// FIXME(dh) copy messages?
errors.wrapped = CreateMessagesField();
warnings.wrapped = CreateMessagesField();
infos.wrapped = CreateMessagesField();
inline Entity(const Entity &original) : Entity(Copy(*original.wrapped)) {
this->errors.wrapped->CopyFrom(*original.errors.wrapped);
this->warnings.wrapped->CopyFrom(*original.warnings.wrapped);
this->infos.wrapped->CopyFrom(*original.infos.wrapped);
};
explicit Entity(IdResponse *id_response);
explicit Entity(ProtoEntity *other)
: wrapped(other), value(Value(other->mutable_value())),
data_type(DataType(other->mutable_data_type())) {
@@ -614,17 +622,26 @@ public:
value.wrapped = this->wrapped->mutable_value();
properties.wrapped = this->wrapped->mutable_properties();
parents.wrapped = this->wrapped->mutable_parents();
// FIXME(dh) copy messages?
errors.wrapped = CreateMessagesField();
warnings.wrapped = CreateMessagesField();
infos.wrapped = CreateMessagesField();
};
explicit inline Entity(EntityResponse *response) : Entity(response->release_entity()) {
errors.wrapped->Swap(response->mutable_errors());
warnings.wrapped->Swap(response->mutable_warnings());
infos.wrapped->Swap(response->mutable_infos());
this->errors.wrapped->Swap(response->mutable_errors());
this->warnings.wrapped->Swap(response->mutable_warnings());
this->infos.wrapped->Swap(response->mutable_infos());
};
explicit inline Entity(IdResponse *id_response) : Entity() {
this->wrapped->set_id(id_response->id());
this->wrapped->mutable_version()->Swap(id_response->mutable_version());
this->errors.wrapped->Swap(id_response->mutable_errors());
this->warnings.wrapped->Swap(id_response->mutable_warnings());
this->infos.wrapped->Swap(id_response->mutable_infos());
};
explicit inline Entity() : Entity(Entity::CreateProtoEntity()){};
[[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 & {
@@ -734,7 +751,18 @@ public:
return StatusCode::SUCCESS;
}
inline auto ClearMessages() noexcept -> void {
errors.Clear();
warnings.Clear();
infos.Clear();
}
private:
static inline auto Copy(const ProtoEntity &from) -> ProtoEntity * {
auto to = from.New();
to->CopyFrom(from);
return to;
}
inline auto GetNextFileId() -> std::string {
std::string str = "0123456789abcdef";
std::mt19937 generator(std::random_device{}());
Loading