From e60abf66c0e95103e5b8c1ebf8d6e4f0971a2d60 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Thu, 12 Jan 2017 06:27:31 +0100 Subject: [PATCH] get this wrapper basically to compile the simple case of an embedded pointer actualy works already --- src/lib/replaceable-item.hpp | 39 +++++++++++++++++++++--- tests/library/replaceable-item-test.cpp | 40 ++++++++++++++----------- 2 files changed, 57 insertions(+), 22 deletions(-) diff --git a/src/lib/replaceable-item.hpp b/src/lib/replaceable-item.hpp index fec47db51..d1da820bd 100644 --- a/src/lib/replaceable-item.hpp +++ b/src/lib/replaceable-item.hpp @@ -52,6 +52,7 @@ namespace wrapper { // using util::unConst; using util::isSameObject; + using std::forward; // using lumiera::error::LUMIERA_ERROR_BOTTOM_VALUE; @@ -99,6 +100,9 @@ namespace wrapper { return access(); } + X& get() { return access(); } + + private: X& access() const @@ -131,11 +135,38 @@ namespace wrapper { } }; + template - class ReplaceableItem>> - : public X - { - using X::X; + struct is_assignable_value + : std::__and_, std::__not_>> + { }; + + + template + class ReplaceableItem>> + { + X val_; + + public: + ReplaceableItem() : val_() { } + + ReplaceableItem(X const& val) : val_(val) { } + ReplaceableItem(X && val) : val_(forward(val)) { } + + ReplaceableItem& operator= (X const& val) { val_=val; return *this; } + ReplaceableItem& operator= (X && val) { val_=forward(val); return *this; } + + operator X&() { return val_; } + operator X const&() const { return val_; } + + X& get() { return val_; } + }; + + + template + class ReplaceableItem>> + { + static_assert( not sizeof(X), "ReplaceableItem for references is pointless"); }; diff --git a/tests/library/replaceable-item-test.cpp b/tests/library/replaceable-item-test.cpp index 40ae2b915..31e4ac596 100644 --- a/tests/library/replaceable-item-test.cpp +++ b/tests/library/replaceable-item-test.cpp @@ -25,6 +25,7 @@ #include "lib/test/run.hpp" #include "lib/test/test-helper.hpp" #include "lib/util.hpp" +#include "lib/format-cout.hpp"////////////TODO #include "lib/replaceable-item.hpp" @@ -49,8 +50,6 @@ namespace test{ using std::vector; using std::string; using std::rand; - using std::cout; - using std::endl; @@ -102,6 +101,13 @@ namespace test{ string s2 (randStr(50)); const char* cp (s1.c_str()); + cout << std::boolalpha << is_assignable_value::value <::value <::value <::value <::value < (l1, l2); verifyWrapper (l1, l2); verifyWrapper (&l1, &l2); @@ -117,9 +123,11 @@ namespace test{ verifySaneInstanceHandling(); - verifyWrappedRef (); +#endif /////////////////////////////////////////////////////////////////////////////////////////////////////////////UNIMPLEMENTED :: TICKET #888 + verifyWrappedPtr (); } +#if false /////////////////////////////////////////////////////////////////////////////////////////////////////////////UNIMPLEMENTED :: TICKET #888 template void @@ -188,30 +196,26 @@ namespace test{ } CHECK (0 == cntTracker); } +#endif /////////////////////////////////////////////////////////////////////////////////////////////////////////////UNIMPLEMENTED :: TICKET #888 - /** @test verify especially that we can handle - * and re-"assign" an embedded reference - */ + /** @test verify especially that we can handle and re-"assign" an embedded pointer */ void - verifyWrappedRef () + verifyWrappedPtr () { int x = 5; - ReplaceableItem refWrap; - CHECK (refWrap == 0); + ReplaceableItem ptrWrap; + CHECK (ptrWrap.get() == NULL); - refWrap = x; - CHECK (5 == refWrap); - CHECK (x == refWrap); + ptrWrap = &x; + CHECK (5 == *ptrWrap.get()); + CHECK (&x == ptrWrap.get()); - refWrap += 5; + *ptrWrap.get() += 5; CHECK (x == 10); - ItemWrapper ptrWrap (& static_cast(refWrap)); - CHECK ( isSameObject (*static_cast(ptrWrap), x)); - CHECK (!isSameObject ( static_cast(ptrWrap), &x)); - *static_cast(ptrWrap) += 13; - CHECK (x == 23); + CHECK ( isSameObject (*ptrWrap.get(), x)); + CHECK (!isSameObject ( ptrWrap.get(), x)); } };