From 2b7707a2f11bb7ce9aac23f341ad1d040148e570 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20Pintari=C4=87?= <99442742+mpintaric55334@users.noreply.github.com> Date: Fri, 28 Jul 2023 12:26:10 +0200 Subject: [PATCH] Add non-const return value for mgp::Value subtypes (#1099) --- include/mgp.hpp | 106 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/include/mgp.hpp b/include/mgp.hpp index e7b7fc982..144133e4b 100644 --- a/include/mgp.hpp +++ b/include/mgp.hpp @@ -399,6 +399,9 @@ class List { /// @brief Returns the value at the given `index`. const Value operator[](size_t index) const; + ///@brief Same as above, but non const value + Value operator[](size_t index); + class Iterator { private: friend class List; @@ -1068,32 +1071,46 @@ class Value { /// @pre Value type needs to be Type::Bool. bool ValueBool() const; + bool ValueBool(); /// @pre Value type needs to be Type::Int. int64_t ValueInt() const; + int64_t ValueInt(); /// @pre Value type needs to be Type::Double. double ValueDouble() const; + double ValueDouble(); /// @pre Value type needs to be Type::Numeric. double ValueNumeric() const; + double ValueNumeric(); /// @pre Value type needs to be Type::String. std::string_view ValueString() const; + std::string_view ValueString(); /// @pre Value type needs to be Type::List. const List ValueList() const; + List ValueList(); /// @pre Value type needs to be Type::Map. const Map ValueMap() const; + Map ValueMap(); /// @pre Value type needs to be Type::Node. const Node ValueNode() const; + Node ValueNode(); /// @pre Value type needs to be Type::Relationship. const Relationship ValueRelationship() const; + Relationship ValueRelationship(); /// @pre Value type needs to be Type::Path. const Path ValuePath() const; + Path ValuePath(); /// @pre Value type needs to be Type::Date. const Date ValueDate() const; + Date ValueDate(); /// @pre Value type needs to be Type::LocalTime. const LocalTime ValueLocalTime() const; + LocalTime ValueLocalTime(); /// @pre Value type needs to be Type::LocalDateTime. const LocalDateTime ValueLocalDateTime() const; + LocalDateTime ValueLocalDateTime(); /// @pre Value type needs to be Type::Duration. const Duration ValueDuration() const; + Duration ValueDuration(); /// @brief Returns whether the value is null. bool IsNull() const; @@ -2182,6 +2199,8 @@ inline bool List::Empty() const { return Size() == 0; } inline const Value List::operator[](size_t index) const { return Value(mgp::list_at(ptr_, index)); } +inline Value List::operator[](size_t index) { return Value(mgp::list_at(ptr_, index)); } + inline bool List::Iterator::operator==(const Iterator &other) const { return iterable_ == other.iterable_ && index_ == other.index_; } @@ -3130,6 +3149,12 @@ inline bool Value::ValueBool() const { } return mgp::value_get_bool(ptr_); } +inline bool Value::ValueBool() { + if (Type() != Type::Bool) { + throw ValueException("Type of value is wrong: expected Bool."); + } + return mgp::value_get_bool(ptr_); +} inline std::int64_t Value::ValueInt() const { if (Type() != Type::Int) { @@ -3137,6 +3162,12 @@ inline std::int64_t Value::ValueInt() const { } return mgp::value_get_int(ptr_); } +inline std::int64_t Value::ValueInt() { + if (Type() != Type::Int) { + throw ValueException("Type of value is wrong: expected Int."); + } + return mgp::value_get_int(ptr_); +} inline double Value::ValueDouble() const { if (Type() != Type::Double) { @@ -3144,6 +3175,12 @@ inline double Value::ValueDouble() const { } return mgp::value_get_double(ptr_); } +inline double Value::ValueDouble() { + if (Type() != Type::Double) { + throw ValueException("Type of value is wrong: expected Double."); + } + return mgp::value_get_double(ptr_); +} inline double Value::ValueNumeric() const { if (Type() != Type::Int && Type() != Type::Double) { @@ -3154,6 +3191,15 @@ inline double Value::ValueNumeric() const { } return mgp::value_get_double(ptr_); } +inline double Value::ValueNumeric() { + if (Type() != Type::Int && Type() != Type::Double) { + throw ValueException("Type of value is wrong: expected Int or Double."); + } + if (Type() == Type::Int) { + return static_cast(mgp::value_get_int(ptr_)); + } + return mgp::value_get_double(ptr_); +} inline std::string_view Value::ValueString() const { if (Type() != Type::String) { @@ -3161,6 +3207,12 @@ inline std::string_view Value::ValueString() const { } return mgp::value_get_string(ptr_); } +inline std::string_view Value::ValueString() { + if (Type() != Type::String) { + throw ValueException("Type of value is wrong: expected String."); + } + return mgp::value_get_string(ptr_); +} inline const List Value::ValueList() const { if (Type() != Type::List) { @@ -3168,6 +3220,12 @@ inline const List Value::ValueList() const { } return List(mgp::value_get_list(ptr_)); } +inline List Value::ValueList() { + if (Type() != Type::List) { + throw ValueException("Type of value is wrong: expected List."); + } + return List(mgp::value_get_list(ptr_)); +} inline const Map Value::ValueMap() const { if (Type() != Type::Map) { @@ -3175,6 +3233,12 @@ inline const Map Value::ValueMap() const { } return Map(mgp::value_get_map(ptr_)); } +inline Map Value::ValueMap() { + if (Type() != Type::Map) { + throw ValueException("Type of value is wrong: expected Map."); + } + return Map(mgp::value_get_map(ptr_)); +} inline const Node Value::ValueNode() const { if (Type() != Type::Node) { @@ -3182,6 +3246,12 @@ inline const Node Value::ValueNode() const { } return Node(mgp::value_get_vertex(ptr_)); } +inline Node Value::ValueNode() { + if (Type() != Type::Node) { + throw ValueException("Type of value is wrong: expected Node."); + } + return Node(mgp::value_get_vertex(ptr_)); +} inline const Relationship Value::ValueRelationship() const { if (Type() != Type::Relationship) { @@ -3189,6 +3259,12 @@ inline const Relationship Value::ValueRelationship() const { } return Relationship(mgp::value_get_edge(ptr_)); } +inline Relationship Value::ValueRelationship() { + if (Type() != Type::Relationship) { + throw ValueException("Type of value is wrong: expected Relationship."); + } + return Relationship(mgp::value_get_edge(ptr_)); +} inline const Path Value::ValuePath() const { if (Type() != Type::Path) { @@ -3196,6 +3272,12 @@ inline const Path Value::ValuePath() const { } return Path(mgp::value_get_path(ptr_)); } +inline Path Value::ValuePath() { + if (Type() != Type::Path) { + throw ValueException("Type of value is wrong: expected Path."); + } + return Path(mgp::value_get_path(ptr_)); +} inline const Date Value::ValueDate() const { if (Type() != Type::Date) { @@ -3203,6 +3285,12 @@ inline const Date Value::ValueDate() const { } return Date(mgp::value_get_date(ptr_)); } +inline Date Value::ValueDate() { + if (Type() != Type::Date) { + throw ValueException("Type of value is wrong: expected Date."); + } + return Date(mgp::value_get_date(ptr_)); +} inline const LocalTime Value::ValueLocalTime() const { if (Type() != Type::LocalTime) { @@ -3210,6 +3298,12 @@ inline const LocalTime Value::ValueLocalTime() const { } return LocalTime(mgp::value_get_local_time(ptr_)); } +inline LocalTime Value::ValueLocalTime() { + if (Type() != Type::LocalTime) { + throw ValueException("Type of value is wrong: expected LocalTime."); + } + return LocalTime(mgp::value_get_local_time(ptr_)); +} inline const LocalDateTime Value::ValueLocalDateTime() const { if (Type() != Type::LocalDateTime) { @@ -3217,6 +3311,12 @@ inline const LocalDateTime Value::ValueLocalDateTime() const { } return LocalDateTime(mgp::value_get_local_date_time(ptr_)); } +inline LocalDateTime Value::ValueLocalDateTime() { + if (Type() != Type::LocalDateTime) { + throw ValueException("Type of value is wrong: expected LocalDateTime."); + } + return LocalDateTime(mgp::value_get_local_date_time(ptr_)); +} inline const Duration Value::ValueDuration() const { if (Type() != Type::Duration) { @@ -3224,6 +3324,12 @@ inline const Duration Value::ValueDuration() const { } return Duration(mgp::value_get_duration(ptr_)); } +inline Duration Value::ValueDuration() { + if (Type() != Type::Duration) { + throw ValueException("Type of value is wrong: expected Duration."); + } + return Duration(mgp::value_get_duration(ptr_)); +} inline bool Value::IsNull() const { return mgp::value_is_null(ptr_); }