From febbf560b53b3c32e541a830cacf367d81a14a72 Mon Sep 17 00:00:00 2001
From: florian <f.spreckelsen@inidscale.com>
Date: Tue, 3 Aug 2021 11:39:08 +0200
Subject: [PATCH] DRAFT: Fill Properties class

---
 include/caosdb/entity.h | 23 ++++++++++++++++++++---
 src/caosdb/entity.cpp   |  9 +++++++++
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/include/caosdb/entity.h b/include/caosdb/entity.h
index 19277cd..cbdf073 100644
--- a/include/caosdb/entity.h
+++ b/include/caosdb/entity.h
@@ -328,7 +328,7 @@ public:
 private:
   static auto CreateProtoProperty() -> ProtoProperty *;
 
-  caosdb::entity::v1alpha1::Property *wrapped;
+  mutable caosdb::entity::v1alpha1::Property *wrapped;
 };
 
 /**
@@ -340,8 +340,18 @@ class Properties {
 public:
   // TODO(fspreck) Implementations needed (basically everything). See Parents
   // container for inspiration.
-  [[nodiscard]] auto At(int index) const -> const Property &;
-  auto Append(const Property &property) -> void;
+  /**
+   * Return the current size of the properties container.
+   *
+   * This is also the number of properties the owningn entity currently has.
+   */
+  [[nodiscard]] inline auto Size() const -> int { return wrapped->size(); }
+  /**
+   * Return the property at the given index.
+   */
+  [[nodiscard]] auto At(int index) const -> const Property {
+    return Property(&(wrapped->at(index)));
+  }
 
   friend class Entity;
 
@@ -352,6 +362,13 @@ private:
       *wrapped)
     : wrapped(wrapped){};
 
+  /**
+   * Append a property
+   *
+   * This increases the Size() by one.
+   */
+  auto Append(const Property &property) -> void;
+
   ::google::protobuf::RepeatedPtrField<caosdb::entity::v1alpha1::Property>
     *wrapped;
 };
diff --git a/src/caosdb/entity.cpp b/src/caosdb/entity.cpp
index 8e032e4..c54a150 100644
--- a/src/caosdb/entity.cpp
+++ b/src/caosdb/entity.cpp
@@ -124,6 +124,15 @@ auto Property::SetDatatype(const std::string &datatype) -> void {
   this->wrapped->set_datatype(datatype);
 }
 
+auto Properties::Append(const Property &property) -> void {
+  auto *destination = this->wrapped->Add();
+  destination->Swap(property.wrapped);
+
+  property.wrapped->Clear();
+
+  property.wrapped = destination;
+}
+
 [[nodiscard]] auto Entity::GetParents() const -> const Parents & {
   return parents;
 }
-- 
GitLab