TypedValue getters return references

Reviewers: buda

Reviewed By: buda

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D113
This commit is contained in:
Mislav Bradac 2017-03-13 15:55:48 +01:00
parent 3e0b12f646
commit a5a15673de
2 changed files with 346 additions and 284 deletions

View File

@ -1,13 +1,13 @@
#include "query/backend/cpp/typed_value.hpp"
#include <fmt/format.h>
#include <cmath>
#include <fmt/format.h>
#include <iostream>
#include <memory>
#include "utils/assert.hpp"
TypedValue::TypedValue(const PropertyValue& value) {
TypedValue::TypedValue(const PropertyValue &value) {
switch (value.type()) {
case PropertyValue::Type::Null:
type_ = Type::Null;
@ -61,32 +61,28 @@ TypedValue::operator PropertyValue() const {
// Other solution would be to add additional overloads for references, for
// example Value<string&>.
// Value extraction template instantiations
template <>
bool TypedValue::Value<bool>() const {
template <> const bool &TypedValue::Value<bool>() const {
if (type_ != TypedValue::Type::Bool) {
throw TypedValueException("Incompatible template param and type");
}
return bool_v;
}
template <>
int64_t TypedValue::Value<int64_t>() const {
template <> const int64_t &TypedValue::Value<int64_t>() const {
if (type_ != TypedValue::Type::Int) {
throw TypedValueException("Incompatible template param and type");
}
return int_v;
}
template <>
double TypedValue::Value<double>() const {
template <> const double &TypedValue::Value<double>() const {
if (type_ != TypedValue::Type::Double) {
throw TypedValueException("Incompatible template param and type");
}
return double_v;
}
template <>
std::string TypedValue::Value<std::string>() const {
template <> const std::string &TypedValue::Value<std::string>() const {
if (type_ != TypedValue::Type::String) {
throw TypedValueException("Incompatible template param and type");
}
@ -94,7 +90,8 @@ std::string TypedValue::Value<std::string>() const {
}
template <>
std::vector<TypedValue> TypedValue::Value<std::vector<TypedValue>>() const {
const std::vector<TypedValue> &
TypedValue::Value<std::vector<TypedValue>>() const {
if (type_ != TypedValue::Type::List) {
throw TypedValueException("Incompatible template param and type");
}
@ -102,7 +99,7 @@ std::vector<TypedValue> TypedValue::Value<std::vector<TypedValue>>() const {
}
template <>
std::map<std::string, TypedValue>
std::map<std::string, TypedValue> const &
TypedValue::Value<std::map<std::string, TypedValue>>() const {
if (type_ != TypedValue::Type::Map) {
throw TypedValueException("Incompatible template param and type");
@ -110,31 +107,94 @@ TypedValue::Value<std::map<std::string, TypedValue>>() const {
return map_v;
}
template <>
VertexAccessor TypedValue::Value<VertexAccessor>() const {
template <> const VertexAccessor &TypedValue::Value<VertexAccessor>() const {
if (type_ != TypedValue::Type::Vertex) {
throw TypedValueException("Incompatible template param and type");
}
return vertex_v;
}
template <>
EdgeAccessor TypedValue::Value<EdgeAccessor>() const {
template <> const EdgeAccessor &TypedValue::Value<EdgeAccessor>() const {
if (type_ != TypedValue::Type::Edge) {
throw TypedValueException("Incompatible template param and type");
}
return edge_v;
}
template <>
Path TypedValue::Value<Path>() const {
template <> const Path &TypedValue::Value<Path>() const {
if (type_ != TypedValue::Type::Path) {
throw TypedValueException("Incompatible template param and type");
}
return path_v;
}
TypedValue::TypedValue(const TypedValue& other) : type_(other.type_) {
template <> bool &TypedValue::Value<bool>() {
if (type_ != TypedValue::Type::Bool) {
throw TypedValueException("Incompatible template param and type");
}
return bool_v;
}
template <> int64_t &TypedValue::Value<int64_t>() {
if (type_ != TypedValue::Type::Int) {
throw TypedValueException("Incompatible template param and type");
}
return int_v;
}
template <> double &TypedValue::Value<double>() {
if (type_ != TypedValue::Type::Double) {
throw TypedValueException("Incompatible template param and type");
}
return double_v;
}
template <> std::string &TypedValue::Value<std::string>() {
if (type_ != TypedValue::Type::String) {
throw TypedValueException("Incompatible template param and type");
}
return string_v;
}
template <>
std::vector<TypedValue> &TypedValue::Value<std::vector<TypedValue>>() {
if (type_ != TypedValue::Type::List) {
throw TypedValueException("Incompatible template param and type");
}
return list_v;
}
template <>
std::map<std::string, TypedValue> &
TypedValue::Value<std::map<std::string, TypedValue>>() {
if (type_ != TypedValue::Type::Map) {
throw TypedValueException("Incompatible template param and type");
}
return map_v;
}
template <> VertexAccessor &TypedValue::Value<VertexAccessor>() {
if (type_ != TypedValue::Type::Vertex) {
throw TypedValueException("Incompatible template param and type");
}
return vertex_v;
}
template <> EdgeAccessor &TypedValue::Value<EdgeAccessor>() {
if (type_ != TypedValue::Type::Edge) {
throw TypedValueException("Incompatible template param and type");
}
return edge_v;
}
template <> Path &TypedValue::Value<Path>() {
if (type_ != TypedValue::Type::Path) {
throw TypedValueException("Incompatible template param and type");
}
return path_v;
}
TypedValue::TypedValue(const TypedValue &other) : type_(other.type_) {
switch (other.type_) {
case TypedValue::Type::Null:
return;
@ -168,7 +228,7 @@ TypedValue::TypedValue(const TypedValue& other) : type_(other.type_) {
permanent_fail("Unsupported TypedValue::Type");
}
std::ostream& operator<<(std::ostream& os, const TypedValue::Type type) {
std::ostream &operator<<(std::ostream &os, const TypedValue::Type type) {
switch (type) {
case TypedValue::Type::Null:
return os << "null";
@ -194,7 +254,7 @@ std::ostream& operator<<(std::ostream& os, const TypedValue::Type type) {
permanent_fail("Unsupported TypedValue::Type");
}
std::ostream& operator<<(std::ostream& os, const TypedValue& value) {
std::ostream &operator<<(std::ostream &os, const TypedValue &value) {
switch (value.type_) {
case TypedValue::Type::Null:
return os << "Null";
@ -208,13 +268,13 @@ std::ostream& operator<<(std::ostream& os, const TypedValue& value) {
return os << value.Value<std::string>();
case TypedValue::Type::List:
os << "[";
for (const auto& x : value.Value<std::vector<TypedValue>>()) {
for (const auto &x : value.Value<std::vector<TypedValue>>()) {
os << x << ",";
}
return os << "]";
case TypedValue::Type::Map:
os << "{";
for (const auto& x : value.Value<std::map<std::string, TypedValue>>()) {
for (const auto &x : value.Value<std::map<std::string, TypedValue>>()) {
os << x.first << ": " << x.second << ",";
}
return os << "}";
@ -228,7 +288,7 @@ std::ostream& operator<<(std::ostream& os, const TypedValue& value) {
permanent_fail("Unsupported PropertyValue::Type");
}
TypedValue& TypedValue::operator=(const TypedValue& other) {
TypedValue &TypedValue::operator=(const TypedValue &other) {
// set the type of this
this->~TypedValue();
type_ = other.type_;
@ -315,7 +375,7 @@ TypedValue::~TypedValue() {
* @param value
* @return
*/
double ToDouble(const TypedValue& value) {
double ToDouble(const TypedValue &value) {
switch (value.type()) {
case TypedValue::Type::Int:
return (double)value.Value<int64_t>();
@ -327,7 +387,7 @@ double ToDouble(const TypedValue& value) {
}
}
TypedValue operator<(const TypedValue& a, const TypedValue& b) {
TypedValue operator<(const TypedValue &a, const TypedValue &b) {
if (a.type() == TypedValue::Type::Bool || b.type() == TypedValue::Type::Bool)
throw TypedValueException("Invalid 'less' operand types({} + {})", a.type(),
b.type());
@ -356,7 +416,7 @@ TypedValue operator<(const TypedValue& a, const TypedValue& b) {
// TODO: 2 = "2" -> false, I don't think this is handled correctly at the
// moment.
TypedValue operator==(const TypedValue& a, const TypedValue& b) {
TypedValue operator==(const TypedValue &a, const TypedValue &b) {
if (a.type() == TypedValue::Type::Null || b.type() == TypedValue::Type::Null)
return TypedValue::Null;
@ -370,7 +430,8 @@ TypedValue operator==(const TypedValue& a, const TypedValue& b) {
// can compare list_v-s directly.
auto list1 = a.Value<std::vector<TypedValue>>();
auto list2 = b.Value<std::vector<TypedValue>>();
if (list1.size() != list2.size()) return false;
if (list1.size() != list2.size())
return false;
for (int i = 0; i < (int)list1.size(); ++i) {
if (!(list1[i] == list2[i]).Value<bool>()) {
return false;
@ -417,7 +478,7 @@ TypedValue operator==(const TypedValue& a, const TypedValue& b) {
}
}
TypedValue operator!(const TypedValue& a) {
TypedValue operator!(const TypedValue &a) {
switch (a.type()) {
case TypedValue::Type::Null:
return TypedValue::Null;
@ -435,7 +496,7 @@ TypedValue operator!(const TypedValue& a) {
* @param value a value.
* @return A string.
*/
std::string ValueToString(const TypedValue& value) {
std::string ValueToString(const TypedValue &value) {
switch (value.type()) {
case TypedValue::Type::String:
return value.Value<std::string>();
@ -450,7 +511,7 @@ std::string ValueToString(const TypedValue& value) {
}
}
TypedValue operator-(const TypedValue& a) {
TypedValue operator-(const TypedValue &a) {
switch (a.type()) {
case TypedValue::Type::Null:
return TypedValue::Null;
@ -475,13 +536,14 @@ TypedValue operator-(const TypedValue& a) {
* @param op_name Name of the operation, used only for exception description,
* if raised.
*/
inline void EnsureArithmeticallyOk(const TypedValue& a, const TypedValue& b,
bool string_ok, const std::string& op_name) {
inline void EnsureArithmeticallyOk(const TypedValue &a, const TypedValue &b,
bool string_ok, const std::string &op_name) {
if (a.type() == TypedValue::Type::Bool || b.type() == TypedValue::Type::Bool)
throw TypedValueException("Invalid {} operand types {}, {}", op_name,
a.type(), b.type());
if (string_ok) return;
if (string_ok)
return;
if (a.type() == TypedValue::Type::String ||
b.type() == TypedValue::Type::String)
@ -489,14 +551,14 @@ inline void EnsureArithmeticallyOk(const TypedValue& a, const TypedValue& b,
a.type(), b.type());
}
TypedValue operator+(const TypedValue& a, const TypedValue& b) {
TypedValue operator+(const TypedValue &a, const TypedValue &b) {
if (a.type() == TypedValue::Type::Null || b.type() == TypedValue::Type::Null)
return TypedValue::Null;
if (a.type() == TypedValue::Type::List ||
b.type() == TypedValue::Type::List) {
std::vector<TypedValue> list;
auto append_list = [&list](const TypedValue& v) {
auto append_list = [&list](const TypedValue &v) {
if (v.type() == TypedValue::Type::List) {
auto list2 = v.Value<std::vector<TypedValue>>();
list.insert(list.end(), list2.begin(), list2.end());
@ -525,7 +587,7 @@ TypedValue operator+(const TypedValue& a, const TypedValue& b) {
}
}
TypedValue operator-(const TypedValue& a, const TypedValue& b) {
TypedValue operator-(const TypedValue &a, const TypedValue &b) {
EnsureArithmeticallyOk(a, b, false, "subtraction");
if (a.type() == TypedValue::Type::Null || b.type() == TypedValue::Type::Null)
@ -540,7 +602,7 @@ TypedValue operator-(const TypedValue& a, const TypedValue& b) {
}
}
TypedValue operator/(const TypedValue& a, const TypedValue& b) {
TypedValue operator/(const TypedValue &a, const TypedValue &b) {
EnsureArithmeticallyOk(a, b, false, "division");
if (a.type() == TypedValue::Type::Null || b.type() == TypedValue::Type::Null)
@ -555,7 +617,7 @@ TypedValue operator/(const TypedValue& a, const TypedValue& b) {
}
}
TypedValue operator*(const TypedValue& a, const TypedValue& b) {
TypedValue operator*(const TypedValue &a, const TypedValue &b) {
EnsureArithmeticallyOk(a, b, false, "multiplication");
if (a.type() == TypedValue::Type::Null || b.type() == TypedValue::Type::Null)
@ -570,7 +632,7 @@ TypedValue operator*(const TypedValue& a, const TypedValue& b) {
}
}
TypedValue operator%(const TypedValue& a, const TypedValue& b) {
TypedValue operator%(const TypedValue &a, const TypedValue &b) {
EnsureArithmeticallyOk(a, b, false, "modulo");
if (a.type() == TypedValue::Type::Null || b.type() == TypedValue::Type::Null)
@ -585,13 +647,13 @@ TypedValue operator%(const TypedValue& a, const TypedValue& b) {
}
}
inline bool IsLogicallyOk(const TypedValue& a) {
inline bool IsLogicallyOk(const TypedValue &a) {
return a.type() == TypedValue::Type::Bool ||
a.type() == TypedValue::Type::Null;
}
// TODO: Fix bugs in && and ||. null or true -> true; false and null -> false
TypedValue operator&&(const TypedValue& a, const TypedValue& b) {
TypedValue operator&&(const TypedValue &a, const TypedValue &b) {
if (IsLogicallyOk(a) && IsLogicallyOk(b)) {
if (a.type() == TypedValue::Type::Null ||
b.type() == TypedValue::Type::Null) {
@ -605,7 +667,7 @@ TypedValue operator&&(const TypedValue& a, const TypedValue& b) {
}
}
TypedValue operator||(const TypedValue& a, const TypedValue& b) {
TypedValue operator||(const TypedValue &a, const TypedValue &b) {
if (IsLogicallyOk(a) && IsLogicallyOk(b)) {
if (a.type() == TypedValue::Type::Null ||
b.type() == TypedValue::Type::Null) {
@ -619,7 +681,7 @@ TypedValue operator||(const TypedValue& a, const TypedValue& b) {
}
}
TypedValue operator^(const TypedValue& a, const TypedValue& b) {
TypedValue operator^(const TypedValue &a, const TypedValue &b) {
if (IsLogicallyOk(a) && IsLogicallyOk(b)) {
if (a.type() == TypedValue::Type::Null ||
b.type() == TypedValue::Type::Null) {

View File

@ -1,19 +1,19 @@
#pragma once
#include <cstdint>
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <map>
#include <cstdint>
#include "storage/edge_accessor.hpp"
#include "storage/property_value.hpp"
#include "storage/vertex_accessor.hpp"
#include "traversal/path.hpp"
#include "utils/exceptions/stacktrace_exception.hpp"
#include "utils/total_ordering.hpp"
#include "utils/underlying_cast.hpp"
#include "storage/property_value.hpp"
#include "storage/edge_accessor.hpp"
#include "storage/vertex_accessor.hpp"
#include "traversal/path.hpp"
typedef traversal_template::Path<VertexAccessor, EdgeAccessor> Path;
@ -26,11 +26,11 @@ typedef traversal_template::Path<VertexAccessor, EdgeAccessor> Path;
* TypedValue::Type. Each such type corresponds to exactly one C++ type.
*/
class TypedValue : public TotalOrdering<TypedValue, TypedValue, TypedValue> {
public:
public:
/** Private default constructor, makes Null */
TypedValue() : type_(Type::Null) {}
public:
public:
/** A value type. Each type corresponds to exactly one C++ type */
enum class Type : unsigned {
Null,
@ -58,32 +58,32 @@ class TypedValue : public TotalOrdering<TypedValue, TypedValue, TypedValue> {
operator PropertyValue() const;
/// constructors for non-primitive types
TypedValue(const std::string& value) : type_(Type::String) {
TypedValue(const std::string &value) : type_(Type::String) {
new (&string_v) std::string(value);
}
TypedValue(const char* value) : type_(Type::String) {
TypedValue(const char *value) : type_(Type::String) {
new (&string_v) std::string(value);
}
TypedValue(const std::vector<TypedValue>& value) : type_(Type::List) {
TypedValue(const std::vector<TypedValue> &value) : type_(Type::List) {
new (&list_v) std::vector<TypedValue>(value);
}
TypedValue(const std::map<std::string, TypedValue>& value)
TypedValue(const std::map<std::string, TypedValue> &value)
: type_(Type::Map) {
new (&map_v) std::map<std::string, TypedValue>(value);
}
TypedValue(const VertexAccessor& vertex) : type_(Type::Vertex) {
TypedValue(const VertexAccessor &vertex) : type_(Type::Vertex) {
new (&vertex_v) VertexAccessor(vertex);
}
TypedValue(const EdgeAccessor& edge) : type_(Type::Edge) {
TypedValue(const EdgeAccessor &edge) : type_(Type::Edge) {
new (&edge_v) EdgeAccessor(edge);
}
TypedValue(const Path& path) : type_(Type::Path) { new (&path_v) Path(path); }
TypedValue(const PropertyValue& value);
TypedValue(const Path &path) : type_(Type::Path) { new (&path_v) Path(path); }
TypedValue(const PropertyValue &value);
// assignment ops
TypedValue& operator=(const TypedValue& other);
TypedValue &operator=(const TypedValue &other);
TypedValue(const TypedValue& other);
TypedValue(const TypedValue &other);
~TypedValue();
Type type() const { return type_; }
@ -95,12 +95,12 @@ class TypedValue : public TotalOrdering<TypedValue, TypedValue, TypedValue> {
* @tparam T Type to interpret the value as.
* @return The value as type T.
*/
template <typename T>
T Value() const;
template <typename T> T &Value();
template <typename T> const T &Value() const;
friend std::ostream& operator<<(std::ostream& stream, const TypedValue&prop);
friend std::ostream &operator<<(std::ostream &stream, const TypedValue &prop);
private:
private:
// storage for the value of the property
union {
bool bool_v;
@ -133,31 +133,31 @@ class TypedValue : public TotalOrdering<TypedValue, TypedValue, TypedValue> {
* of incompatible Types.
*/
class TypedValueException : public StacktraceException {
public:
public:
using ::StacktraceException::StacktraceException;
};
// comparison operators
// they return TypedValue because Null can be returned
TypedValue operator==(const TypedValue& a, const TypedValue& b);
TypedValue operator<(const TypedValue& a, const TypedValue& b);
TypedValue operator!(const TypedValue& a);
TypedValue operator==(const TypedValue &a, const TypedValue &b);
TypedValue operator<(const TypedValue &a, const TypedValue &b);
TypedValue operator!(const TypedValue &a);
// arithmetic operators
TypedValue operator-(const TypedValue& a);
TypedValue operator+(const TypedValue& a, const TypedValue& b);
TypedValue operator-(const TypedValue& a, const TypedValue& b);
TypedValue operator/(const TypedValue& a, const TypedValue& b);
TypedValue operator*(const TypedValue& a, const TypedValue& b);
TypedValue operator%(const TypedValue& a, const TypedValue& b);
TypedValue operator-(const TypedValue &a);
TypedValue operator+(const TypedValue &a, const TypedValue &b);
TypedValue operator-(const TypedValue &a, const TypedValue &b);
TypedValue operator/(const TypedValue &a, const TypedValue &b);
TypedValue operator*(const TypedValue &a, const TypedValue &b);
TypedValue operator%(const TypedValue &a, const TypedValue &b);
// binary bool operators
TypedValue operator&&(const TypedValue& a, const TypedValue& b);
TypedValue operator||(const TypedValue& a, const TypedValue& b);
TypedValue operator&&(const TypedValue &a, const TypedValue &b);
TypedValue operator||(const TypedValue &a, const TypedValue &b);
// binary bool xor, not power operator
// Be careful: since ^ is binary operator and || and && are logical operators
// they have different priority in c++.
TypedValue operator^(const TypedValue& a, const TypedValue& b);
TypedValue operator^(const TypedValue &a, const TypedValue &b);
// stream output
std::ostream& operator<<(std::ostream& os, const TypedValue::Type type);
std::ostream &operator<<(std::ostream &os, const TypedValue::Type type);