optional object link implemented and working
This commit is contained in:
parent
07f7837a7b
commit
f597e7c8b4
4 changed files with 111 additions and 45 deletions
|
|
@ -26,11 +26,6 @@
|
|||
|
||||
#include "lib/error.hpp"
|
||||
#include "lib/bool-checkable.hpp"
|
||||
#include "lib/util.hpp"
|
||||
|
||||
//#include <boost/type_traits/remove_const.hpp>
|
||||
//#include <boost/noncopyable.hpp>
|
||||
//#include <tr1/functional>
|
||||
|
||||
|
||||
namespace lib {
|
||||
|
|
@ -38,26 +33,6 @@ namespace lib {
|
|||
using lumiera::error::LUMIERA_ERROR_BOTTOM_VALUE;
|
||||
|
||||
|
||||
/**
|
||||
* Reference wrapper implemented as constant function,
|
||||
* returning the (fixed) reference on invocation
|
||||
*/
|
||||
template<typename T>
|
||||
class ReturnRef
|
||||
{
|
||||
T& ref_;
|
||||
|
||||
public:
|
||||
ReturnRef(T& target) : ref_(target) { }
|
||||
T& operator() () const { return ref_;}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
ReturnRef<T>
|
||||
refFunction (T& target)
|
||||
{
|
||||
return ReturnRef<T> (target);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
@ -68,20 +43,110 @@ namespace lib {
|
|||
* is managed automatically by ctor and dtor, can be detected
|
||||
* through \c bool check and -- contrary to a \c NULL pointer
|
||||
* -- produces a real exception instead of crashing.
|
||||
*
|
||||
* @note \em not taking ownership of the pointee
|
||||
*
|
||||
* @see OptionalRef_test
|
||||
* @see lib::AutoRegistered usage example
|
||||
* @see SessionInterfaceModules::~SessionInterfaceModules()
|
||||
*
|
||||
*/
|
||||
template<typename T>
|
||||
class OptionalRef
|
||||
: public lib::BoolCheckable<OptionalRef<T> >
|
||||
{
|
||||
T* ref_;
|
||||
|
||||
|
||||
public:
|
||||
OptionalRef() : ref_(0) { }
|
||||
OptionalRef(T& target) : ref_(&target) { }
|
||||
~OptionalRef() { ref_ = 0; }
|
||||
OptionalRef()
|
||||
: ref_(0)
|
||||
{ }
|
||||
|
||||
~OptionalRef()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
// using default copy operations...
|
||||
|
||||
explicit
|
||||
OptionalRef(T& target) ///< ...\em not allowing implicit conversion from T&
|
||||
: ref_(&target)
|
||||
{ }
|
||||
|
||||
operator T& () const ///< ...allowing implicit conversion to T&
|
||||
{
|
||||
if (!isValid())
|
||||
throw lumiera::error::Logic ("access to this object is (not/yet) enabled"
|
||||
, LUMIERA_ERROR_BOTTOM_VALUE);
|
||||
return *ref_;
|
||||
}
|
||||
|
||||
|
||||
/* === mutations ===*/
|
||||
|
||||
void
|
||||
link_to (T& target)
|
||||
{
|
||||
ref_ = ⌖
|
||||
}
|
||||
|
||||
void
|
||||
clear()
|
||||
{
|
||||
ref_ = 0;
|
||||
}
|
||||
|
||||
|
||||
/* === comparison and diagnostics === */
|
||||
|
||||
bool
|
||||
isValid() const
|
||||
{
|
||||
return bool(ref_);
|
||||
}
|
||||
|
||||
bool
|
||||
points_to (T const& target) const
|
||||
{
|
||||
return isValid()
|
||||
&& ref_ == ⌖
|
||||
}
|
||||
|
||||
friend bool
|
||||
operator== (OptionalRef const& r1, OptionalRef const& r2)
|
||||
{
|
||||
return r1.ref_ == r2.ref_;
|
||||
}
|
||||
friend bool
|
||||
operator!= (OptionalRef const& r1, OptionalRef const& r2)
|
||||
{
|
||||
return r1.ref_ != r2.ref_;
|
||||
}
|
||||
|
||||
// mixed comparisons
|
||||
friend bool
|
||||
operator== (OptionalRef const& ref, T const& otherTarget) ///< @note might throw
|
||||
{
|
||||
T const& thisTarget (ref); // might throw
|
||||
return thisTarget == otherTarget;
|
||||
}
|
||||
|
||||
friend bool operator== (T const& otherTarget, OptionalRef const& ref) { return ref == otherTarget; }
|
||||
friend bool operator!= (T const& otherTarget, OptionalRef const& ref) { return !(ref == otherTarget); }
|
||||
friend bool operator!= (OptionalRef const& ref, T const& otherTarget) { return !(ref == otherTarget); }
|
||||
};
|
||||
|
||||
|
||||
template<typename T>
|
||||
OptionalRef<T>
|
||||
optionalRefTo (T& target)
|
||||
{
|
||||
return OptionalRef<T> (target);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace lib
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -393,6 +393,11 @@ out: ItemWrapper: sizeof\( .+ItemWrapper.+ ) =
|
|||
END
|
||||
|
||||
|
||||
TEST "switchable reference" OptionalRef_test <<END
|
||||
return: 0
|
||||
END
|
||||
|
||||
|
||||
TEST "Lumiera Iterator Concept" IterAdapter_test 20 <<END
|
||||
out: ::20::19::18::17::16::15::14::13::12::11::10::9::8::7::6::5::4::3::2::1
|
||||
out: ::0::1::2::3::4::5::6::7::8::9::10::11::12::13::14::15::16::17::18::19
|
||||
|
|
|
|||
|
|
@ -24,16 +24,9 @@
|
|||
|
||||
#include "lib/test/run.hpp"
|
||||
#include "lib/test/test-helper.hpp"
|
||||
//#include "lib/util.hpp"
|
||||
|
||||
#include "lib/optional-ref.hpp"
|
||||
|
||||
//#include <tr1/functional>
|
||||
//#include <iostream>
|
||||
//#include <cstdlib>
|
||||
//#include <string>
|
||||
//#include <vector>
|
||||
|
||||
|
||||
|
||||
namespace lib {
|
||||
|
|
@ -46,15 +39,14 @@ namespace test{
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* @test verify a reference-like wrapper class, used to provide a switchable link
|
||||
* to an already existing object.
|
||||
* @test verify a reference-like wrapper class,
|
||||
* used to provide a switchable link to an already existing object.
|
||||
* - bottom ref can be detected by bool check
|
||||
* - access to bottom ref raises exception
|
||||
* - refs can be compared
|
||||
* - refs can be changed and copied
|
||||
* - access to refs is secure even after destructor invocation
|
||||
*
|
||||
* @see lib::OptionalRef
|
||||
* @see lib::AutoRegistered usage example
|
||||
|
|
@ -137,7 +129,7 @@ namespace test{
|
|||
|
||||
// access is secured even after destruction
|
||||
CHECK (r3);
|
||||
r1.~OptionalRef();
|
||||
r3.~SRef(); // don't try this at home!
|
||||
CHECK (!r3);
|
||||
VERIFY_ERROR (BOTTOM_VALUE, r3 == s1 );
|
||||
CHECK (r3 != r2);
|
||||
|
|
@ -154,4 +146,3 @@ namespace test{
|
|||
|
||||
|
||||
}} // namespace lib::test
|
||||
|
||||
|
|
|
|||
|
|
@ -26,11 +26,15 @@
|
|||
#include "lib/tmpbuf.h"
|
||||
#include <stdint.h>
|
||||
|
||||
typedef const char* lumiera_err;
|
||||
struct lumiera_errorcontext_struct;
|
||||
|
||||
struct lumiera_errorcontext_struct*
|
||||
lumiera_error_get (void);
|
||||
|
||||
lumiera_err
|
||||
lumiera_error_set (lumiera_err, const char*);
|
||||
|
||||
|
||||
int
|
||||
main ()
|
||||
|
|
@ -44,6 +48,7 @@ main ()
|
|||
|
||||
/* lumiera_error_get() mallocs a LumieraErrorcontext for each thread */
|
||||
lumiera_error_get();
|
||||
lumiera_error_set("dummy","dummy");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue