Skip to content
Snippets Groups Projects

Draft: API: remove UniqueResult, lower-case at, size for ResultSet

Closed Timm Fitschen requested to merge f-remove-unique-result into dev
5 files
+ 140
66
Compare changes
  • Side-by-side
  • Inline
Files
5
+ 99
33
@@ -29,16 +29,63 @@
#ifndef CAOSDB_ENTITY_H
#define CAOSDB_ENTITY_H
#include <string> // for string
#include "caosdb/entity/v1alpha1/main.pb.h" // for RepeatedPtrField, Message
#include "caosdb/entity/v1alpha1/main.pb.h" // for ProtoEntity, ProtoParent...
#include "caosdb/message_code.h" // for get_message_code, Messag...
#include "google/protobuf/util/json_util.h" // for MessageToJsonString, Jso...
#include <google/protobuf/util/json_util.h> // for MessageToJsonString, Jso...
#include <google/protobuf/message.h> // for RepeatedPtrField, Message
#include <map> // for map
#include <string> // for string
namespace caosdb::entity {
using caosdb::entity::v1alpha1::IdResponse;
using ProtoParent = caosdb::entity::v1alpha1::Parent;
using ProtoProperty = caosdb::entity::v1alpha1::Property;
using ProtoEntity = caosdb::entity::v1alpha1::Entity;
using ProtoMessage = caosdb::entity::v1alpha1::Message;
using ::google::protobuf::RepeatedPtrField;
static const std::string logger_name = "caosdb::entity";
template <typename T, typename P> class RepeatedPtrFieldWrapper {
public:
/**
* Return the current size of the properties container.
*
* This is also the number of properties the owning entity currently has.
*/
[[nodiscard]] inline auto Size() const -> int { return wrapped->size(); }
/**
* Return a const reference to the element at the given index.
*/
[[nodiscard]] inline auto At(int index) const -> const T & {
return *mutable_at(index);
}
/**
* Return a mutable pointer to the element at the given index.
*/
[[nodiscard]] inline auto mutable_at(int index) const -> T * {
if (cache.count(index) == 0) {
cache.emplace(index, T(&(wrapped->at(index))));
}
return &(cache.at(index));
}
friend class Entity;
virtual ~RepeatedPtrFieldWrapper(){};
protected:
RepeatedPtrFieldWrapper(){};
explicit inline RepeatedPtrFieldWrapper(
::google::protobuf::RepeatedPtrField<P> *wrapped)
: wrapped(wrapped){};
virtual auto Append(const T &element) -> void = 0;
::google::protobuf::RepeatedPtrField<P> *wrapped;
mutable std::map<int, T> cache;
};
/**
* Messages convey information about the state and result of transactions.
@@ -61,23 +108,24 @@ public:
// friend class Parent;
// friend class Property;
friend class Messages;
friend class RepeatedPtrFieldWrapper<Message, ProtoMessage>;
private:
explicit inline Message(caosdb::entity::v1alpha1::Message *wrapped)
: wrapped(wrapped){};
explicit inline Message(ProtoMessage *wrapped) : wrapped(wrapped){};
caosdb::entity::v1alpha1::Message *wrapped;
ProtoMessage *wrapped;
};
/**
* Container for Messages.
*/
class Messages {
class Messages : public RepeatedPtrFieldWrapper<Message, ProtoMessage> {
public:
[[nodiscard]] inline auto Size() const -> int { return wrapped->size(); }
[[nodiscard]] inline auto At(int index) const -> const Message {
return Message(&(wrapped->at(index)));
}
~Messages();
//[[nodiscard]] inline auto Size() const -> int { return wrapped->size(); }
//[[nodiscard]] inline auto At(int index) const -> const Message {
// return Message(&(wrapped->at(index)));
//}
friend class Entity;
// TODO(fspreck) Same here.
@@ -85,10 +133,11 @@ public:
// friend class Property;
private:
inline Messages() : wrapped(nullptr){};
inline Messages() : RepeatedPtrFieldWrapper(){};
auto Append(const Message &message) -> void override;
::google::protobuf::RepeatedPtrField<caosdb::entity::v1alpha1::Message>
*wrapped;
//::google::protobuf::RepeatedPtrField<caosdb::entity::v1alpha1::Message>
//*wrapped;
};
/**
@@ -168,6 +217,7 @@ public:
friend class Entity;
friend class Parents;
friend class RepeatedPtrFieldWrapper<Parent, ProtoParent>;
private:
/**
@@ -196,42 +246,44 @@ private:
*
* Should only be instantiated and write-accessed by the owning entity.
*/
class Parents {
class Parents : public RepeatedPtrFieldWrapper<Parent, ProtoParent> {
public:
~Parents() = default;
/**
* Return the current size of the parent container.
*
* That is also the number of parents the owning entity currently has.
*/
[[nodiscard]] inline auto Size() const -> int { return wrapped->size(); }
//[[nodiscard]] inline auto Size() const -> int { return wrapped->size(); }
/**
* Return the parent at the given index.
*/
[[nodiscard]] inline auto At(int index) const -> const Parent {
return Parent(&(wrapped->at(index)));
}
//[[nodiscard]] inline auto At(int index) const -> const Parent {
// return Parent(&(wrapped->at(index)));
//}
friend class Entity;
private:
inline Parents(){};
inline Parents() : RepeatedPtrFieldWrapper(){};
explicit inline Parents(
::google::protobuf::RepeatedPtrField<caosdb::entity::v1alpha1::Parent>
*wrapped)
: wrapped(wrapped){};
: RepeatedPtrFieldWrapper(wrapped){};
//: wrapped(wrapped){};
/**
* Append a parent.
*
* This increases the Size() by one.
*/
auto Append(const Parent &parent) -> void;
auto Append(const Parent &parent) -> void override;
/**
* The collection of parent messages which serves as a backend for this
* class.
*/
::google::protobuf::RepeatedPtrField<caosdb::entity::v1alpha1::Parent>
*wrapped;
//::google::protobuf::RepeatedPtrField<caosdb::entity::v1alpha1::Parent>
//*wrapped;
};
/**
@@ -321,6 +373,7 @@ public:
friend class Entity;
friend class Properties;
friend class RepeatedPtrFieldWrapper<Property, ProtoProperty>;
private:
static auto CreateProtoProperty() -> ProtoProperty *;
@@ -333,20 +386,30 @@ private:
*
* Should only be instantiated and write-accessed by the owning entity.
*/
class Properties {
class Properties
: public RepeatedPtrFieldWrapper<Property,
caosdb::entity::v1alpha1::Property> {
public:
~Properties() = default;
/**
* Return the current size of the properties container.
*
* This is also the number of properties the owning entity currently has.
*/
[[nodiscard]] inline auto Size() const -> int { return wrapped->size(); }
//[[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)));
}
//[[nodiscard]] auto At(int index) const -> const Property & {
// return mutable_at(index);
//}
//[[nodiscard]] auto mutable_at(int index) const -> Property & {
// if (cache.count(index) == 0) {
// cache.emplace(index, Property(&(wrapped->at(index))));
//}
// return cache.at(index);
//}
friend class Entity;
@@ -355,17 +418,20 @@ private:
explicit inline Properties(
::google::protobuf::RepeatedPtrField<caosdb::entity::v1alpha1::Property>
*wrapped)
: wrapped(wrapped){};
: RepeatedPtrFieldWrapper<Property, caosdb::entity::v1alpha1::Property>(
wrapped){};
//: wrapped(wrapped){};
/**
* Append a property
*
* This increases the Size() by one.
*/
auto Append(const Property &property) -> void;
auto Append(const Property &property) -> void override;
::google::protobuf::RepeatedPtrField<caosdb::entity::v1alpha1::Property>
*wrapped;
//::google::protobuf::RepeatedPtrField<caosdb::entity::v1alpha1::Property>
//*wrapped;
// mutable std::map<int, Property> cache;
};
/**
Loading