From cdfa0e71060c7ac0709f471694deaaf648bee230 Mon Sep 17 00:00:00 2001 From: Kruno Tomola Fabro Date: Sun, 28 Aug 2016 16:31:36 +0100 Subject: [PATCH] Works but with ptr instead of unique_ptr. --- include/barrier/common.hpp | 91 +++++++++++++++----------------------- src/barrier/barrier.cpp | 86 ++++++++++++++++------------------- 2 files changed, 72 insertions(+), 105 deletions(-) diff --git a/include/barrier/common.hpp b/include/barrier/common.hpp index 3fc31bd04..538f00db6 100644 --- a/include/barrier/common.hpp +++ b/include/barrier/common.hpp @@ -51,21 +51,21 @@ TO const *ptr_as(FROM const *ref) } template -TO value_as(FROM &&ref) +TO &&value_as(FROM &&ref) { - static_assert(sizeof(FROM) == sizeof(FROM), "Border class size mismatch"); + static_assert(sizeof(TO) == sizeof(FROM), "Border class size mismatch"); static_assert(alignof(TO) == alignof(FROM), "Border class aligment mismatch"); - return std::move((*reinterpret_cast(&ref))); + return (reinterpret_cast(std::move(ref))); } template -const TO value_as(const FROM &&ref) +const TO &&value_as(const FROM &&ref) { - static_assert(sizeof(FROM) == sizeof(FROM), "Border class size mismatch"); + static_assert(sizeof(TO) == sizeof(FROM), "Border class size mismatch"); static_assert(alignof(TO) == alignof(FROM), "Border class aligment mismatch"); - return std::move((*reinterpret_cast(&ref))); + return (reinterpret_cast(std::move(ref))); } // Barrier classes which will be used only through reference/pointer should @@ -96,57 +96,36 @@ protected: // This way side outside the barrier can't "accidentaly" create this/derived // type because that would be erroneous. Sized() = delete; - // - // // This constructor serves as a check for correctness of size_B and - // // alignment_B template parametars. Derived class MUST call this - // constructor - // // with T which it holds where T is original class from memgraph. - // template - // Sized(T &&d) - // // : data(value_as< - // // typename std::aligned_storage::type>( - // // std::move(d))) - // { - // new (&ref_as(data)) T(std::move(d)); - // static_assert(size_B == sizeof(T), "Border class size mismatch"); - // static_assert(alignment_B == alignof(T), - // "Border class aligment mismatch"); - // } - // - // // This constructor serves as a check for correctness of size_B and - // // alignment_B template parametars. Derived class MUST call this - // constructor - // // with T which it holds where T is original class from memgraph. - // template - // Sized(const T &&d) - // // : data(value_as< - // // const typename std::aligned_storage::type>( - // // std::move(d))) - // { - // new (&ref_as(data)) T(std::move(d)); - // static_assert(size_B == sizeof(T), "Border class size mismatch"); - // static_assert(alignment_B == alignof(T), - // "Border class aligment mismatch"); - // } - // // This constructor serves as a check for correctness of size_B and - // // alignment_B template parametars. Derived class MUST call this - // constructor - // // with T which it holds where T is original class from memgraph. - // template - // Sized(std::unique_ptr &&d) - // : data(value_as< - // const typename std::aligned_storage::type>( - // std::unique_ptr(nullptr))) - // { - // ref_as>(data) = std::move(d); - // - // static_assert(size_B == sizeof(T), "Border class size mismatch"); - // static_assert(alignment_B == alignof(T), - // "Border class aligment mismatch"); - // } + // This constructor serves as a check for correctness of size_B and + // alignment_B template parametars. Derived class MUST call this constructor + // with T which it holds where T is original class from memgraph. + template + Sized(T &&d) + : data(value_as< + typename std::aligned_storage::type>( + std::move(d))) + { + + static_assert(size_B == sizeof(T), "Border class size mismatch"); + static_assert(alignment_B == alignof(T), + "Border class aligment mismatch"); + } + + // This constructor serves as a check for correctness of size_B and + // alignment_B template parametars. Derived class MUST call this constructor + // with T which it holds where T is original class from memgraph. + template + Sized(const T &&d) + : data(value_as< + const typename std::aligned_storage::type>( + std::move(d))) + { + + static_assert(size_B == sizeof(T), "Border class size mismatch"); + static_assert(alignment_B == alignof(T), + "Border class aligment mismatch"); + } private: // Here is the aligned storage which imitates size and aligment of object of diff --git a/src/barrier/barrier.cpp b/src/barrier/barrier.cpp index 30457f2ed..6f38804f1 100644 --- a/src/barrier/barrier.cpp +++ b/src/barrier/barrier.cpp @@ -24,13 +24,13 @@ #define TRANSFORM_REF(x, y) \ x &trans(y &l) { return ref_as(l); } \ - const x &trans(const y &l) { return ref_as(l); } \ + x const &trans(y const &l) { return ref_as(l); } \ y &trans(x &l) { return ref_as(l); } \ - const y &trans(const x &l) { return ref_as(l); } \ + y const &trans(x const &l) { return ref_as(l); } \ x *trans(y *l) { return ptr_as(l); } \ - const x *trans(const y *l) { return ptr_as(l); } \ + x const *trans(y const *l) { return ptr_as(l); } \ y *trans(x *l) { return ptr_as(l); } \ - const y *trans(const x *l) { return ptr_as(l); } + y const *trans(x const *l) { return ptr_as(l); } #define TRANSFORM_REF_TEMPLATED(x, y) \ x &trans(y &l) { return ref_as(l); } \ @@ -70,17 +70,17 @@ return ptr_as(l); \ } -// #define VALID_CONSTRUCTION(x, y) \ -// template <> \ -// barrier::x::x(y &&d) : Sized(std::move(d)) \ -// { \ -// } -// -// #define VALID_CONSTRUCTION_CONST(x, y) \ -// template <> \ -// barrier::x::x(y const &&d) : Sized(std::move(d)) \ -// { \ -// } +#define VALID_CONSTRUCTION(x, y) \ + template <> \ + barrier::x::x(y &&d) : Sized(std::move(d)) \ + { \ + } + +#define VALID_CONSTRUCTION_CONST(x, y) \ + template <> \ + barrier::x::x(y const &&d) : Sized(std::move(d)) \ + { \ + } // Generates transformation function from original class to border class. #define TRANSFORM_VALUE_ONE_RAW(x, y) \ @@ -88,7 +88,8 @@ // Generates transformation function from original class to border class. #define TRANSFORM_VALUE_ONE(x, y) \ - x trans(y &&d) { return value_as(std::move(d)); } + VALID_CONSTRUCTION(x, y) \ + x trans(y &&d) { return x(std::move(d)); } // Generates transformation functions between border class x and original class // y by value. Only mutable values. @@ -101,7 +102,7 @@ #define TRANSFORM_VALUE(x, y) \ TRANSFORM_VALUE_MUT(x, y) \ VALID_CONSTRUCTION_CONST(x, y) \ - const x trans(const y &&d) { return value_as(std::move(d)); } \ + const x trans(const y &&d) { return x(std::move(d)); } \ const y trans(const x &&d) { return value_as(std::move(d)); } // Duplicates given first name to call second given with name and ::name @@ -151,8 +152,7 @@ TRANSFORM_REF(VertexPropertyKey, ::VertexPropertyFamily::PropertyType::PropertyFamilyKey); TRANSFORM_REF(EdgePropertyKey, ::EdgePropertyFamily::PropertyType::PropertyFamilyKey); -TRANSFORM_REF(VertexIterator, - std::unique_ptr>); +TRANSFORM_REF(VertexIterator, IteratorBase *); TRANSFORM_REF(EdgeIterator, std::unique_ptr>); TRANSFORM_REF(VertexAccessIterator, vertex_access_iterator_t); @@ -187,8 +187,7 @@ TRANSFORM_VALUE(VertexPropertyKey, TRANSFORM_VALUE_ONE(VertexAccessIterator, vertex_access_iterator_t); TRANSFORM_VALUE_ONE(OutEdgesIterator, out_edge_iterator_t); TRANSFORM_VALUE_ONE(InEdgesIterator, in_edge_iterator_t); -TRANSFORM_VALUE_ONE(VertexIterator, - std::unique_ptr>); +TRANSFORM_VALUE_ONE(VertexIterator, IteratorBase *); template TRANSFORM_VALUE_ONE_RAW( @@ -263,9 +262,8 @@ template VertexIterator VertexIndex::for_range(DbAccessor &t, Border from, Border to) { - auto ret = THIS->for_range(trans(t), std::move(from), std::move(to)); - VertexIterator it(std::move(ret)); - return it; + // auto ret = THIS->for_range(trans(t), std::move(from), std::move(to)); + return CALL(for_range(trans(t), std::move(from), std::move(to)).release()); } template @@ -399,12 +397,11 @@ VertexAccessor::VertexAccessor(const VertexAccessor &other) : Sized(::VertexAccessor(trans(other))) { } -VertexAccessor::VertexAccessor(VertexAccessor &&other) - : Sized(trans(std::move(other))) +VertexAccessor::VertexAccessor(VertexAccessor &&other) : Sized(std::move(other)) { } VertexAccessor::VertexAccessor(VertexAccessor const &&other) - : Sized(trans(std::move(other))) + : Sized(std::move(other)) { } VertexAccessor::~VertexAccessor() { HALF_CALL(~VertexAccessor()); } @@ -530,12 +527,8 @@ EdgeAccessor::EdgeAccessor(const EdgeAccessor &other) : Sized(::EdgeAccessor(trans(other))) { } -EdgeAccessor::EdgeAccessor(EdgeAccessor &&other) - : Sized(trans(std::move(other))) -{ -} -EdgeAccessor::EdgeAccessor(EdgeAccessor const &&other) - : Sized(trans(std::move(other))) +EdgeAccessor::EdgeAccessor(EdgeAccessor &&other) : Sized(std::move(other)) {} +EdgeAccessor::EdgeAccessor(EdgeAccessor const &&other) : Sized(std::move(other)) { } EdgeAccessor::~EdgeAccessor() { HALF_CALL(~EdgeAccessor()); } @@ -630,28 +623,23 @@ bool operator!=(const EdgeAccessor &a, const EdgeAccessor &b) } // ************************* VertexIterator -VertexIterator::VertexIterator(VertexIterator &&other) - : Sized(std::unique_ptr>(nullptr)) +VertexIterator::VertexIterator(VertexIterator &&other) : Sized(std::move(other)) { - *THIS = value_as>>( - std::move(other)); + trans(other) = nullptr; } -VertexIterator::~VertexIterator() { HALF_CALL(~unique_ptr()); } +VertexIterator::~VertexIterator() +{ + if (*THIS != nullptr) delete (*THIS); +} Option VertexIterator::next() { - return HALF_CALL(get()->next()).map(); + return ((*THIS)->next()).map(); } // ************************* EdgeIterator -// TODO : change -EdgeIterator::EdgeIterator(EdgeIterator &&other) - : Sized(std::unique_ptr>(nullptr)) -{ - *THIS = value_as>>( - std::move(other)); -} +EdgeIterator::EdgeIterator(EdgeIterator &&other) : Sized(std::move(other)) {} EdgeIterator::~EdgeIterator() { HALF_CALL(~unique_ptr()); } @@ -662,7 +650,7 @@ Option EdgeIterator::next() // ************************* OutEdgesIterator OutEdgesIterator::OutEdgesIterator(OutEdgesIterator &&other) - : Sized(trans(std::move(other))) + : Sized(std::move(other)) { } @@ -675,7 +663,7 @@ Option OutEdgesIterator::next() // ************************* InEdgesIterator InEdgesIterator::InEdgesIterator(InEdgesIterator &&other) - : Sized(trans(std::move(other))) + : Sized(std::move(other)) { } @@ -688,7 +676,7 @@ Option InEdgesIterator::next() // ************************* VertexAccessIterator VertexAccessIterator::VertexAccessIterator(VertexAccessIterator &&other) - : Sized(trans(std::move(other))) + : Sized(std::move(other)) { }