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,7 +1,7 @@
#include "query/backend/cpp/typed_value.hpp"
#include <fmt/format.h>
#include <cmath>
#include <fmt/format.h>
#include <iostream>
#include <memory>
@ -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,24 +107,87 @@ 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 <> const Path &TypedValue::Value<Path>() const {
if (type_ != TypedValue::Type::Path) {
throw TypedValueException("Incompatible template param and type");
}
return path_v;
}
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 <>
Path TypedValue::Value<Path>() const {
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");
}
@ -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;
@ -481,7 +542,8 @@ inline void EnsureArithmeticallyOk(const TypedValue& a, const TypedValue& b,
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)

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;
@ -95,8 +95,8 @@ 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);