base implementation fo the ItemWrapper
This commit is contained in:
parent
83eb6976cd
commit
3db676fa32
5 changed files with 104 additions and 27 deletions
|
|
@ -66,7 +66,9 @@ namespace lumiera {
|
|||
|
||||
/* some further generic error situations */
|
||||
LUMIERA_ERROR_DEFINE (WRONG_TYPE, "runtime type mismatch");
|
||||
LUMIERA_ERROR_DEFINE (ITER_EXHAUST, "end of sequence reached");
|
||||
LUMIERA_ERROR_DEFINE (ITER_EXHAUST, "end of sequence reached");
|
||||
LUMIERA_ERROR_DEFINE (BOTTOM_VALUE, "invalid or NIL value");
|
||||
|
||||
|
||||
} // namespace error
|
||||
|
||||
|
|
|
|||
|
|
@ -26,12 +26,11 @@
|
|||
|
||||
|
||||
#include "lib/meta/util.hpp"
|
||||
#include "lib/wrapper.hpp"
|
||||
#include "lib/wrapper.hpp" ////////////////////////TODO only because of AssignableRefWrapper -- can we get rid of this import?
|
||||
|
||||
#include <boost/type_traits/is_convertible.hpp>
|
||||
#include <boost/type_traits/is_arithmetic.hpp>
|
||||
#include <boost/utility/enable_if.hpp>
|
||||
#include <boost/ref.hpp>
|
||||
#include <string>
|
||||
|
||||
|
||||
|
|
@ -101,7 +100,7 @@ namespace typelist {
|
|||
typedef TY* pointer;
|
||||
typedef TY& reference;
|
||||
typedef TY value_type;
|
||||
typedef lib::wrap::AssignableRefWrapper<TY> member_type;
|
||||
typedef lib::wrapper::AssignableRefWrapper<TY> member_type;
|
||||
};
|
||||
//////////////////////////////////////////TODO: not needed 12/09 -- obsolete? useful? keep it?
|
||||
|
||||
|
|
|
|||
|
|
@ -34,16 +34,22 @@
|
|||
#ifndef LIB_WRAPPER_H
|
||||
#define LIB_WRAPPER_H
|
||||
|
||||
//#include "lib/bool-checkable.hpp"
|
||||
#include "lib/error.hpp"
|
||||
#include "lib/bool-checkable.hpp"
|
||||
#include "lib/util.hpp"
|
||||
|
||||
//#include <tr1/memory>
|
||||
#include <tr1/functional> ////////////////////////TODO only because of tr1::ref_wrapper, used by AssignableRefWrapper -- can we get rid of this import?
|
||||
|
||||
|
||||
namespace lib {
|
||||
namespace wrap {
|
||||
namespace wrapper {
|
||||
|
||||
//using std::tr1::shared_ptr;
|
||||
//using std::tr1::weak_ptr;
|
||||
using util::unConst;
|
||||
using util::isSameObject;
|
||||
using lumiera::error::LUMIERA_ERROR_BOTTOM_VALUE;
|
||||
|
||||
|
||||
|
||||
|
|
@ -56,9 +62,9 @@ namespace wrap {
|
|||
*/
|
||||
template<typename TY>
|
||||
class AssignableRefWrapper
|
||||
: public boost::reference_wrapper<TY>
|
||||
: public std::tr1::reference_wrapper<TY>
|
||||
{
|
||||
typedef boost::reference_wrapper<TY> RefWrapper;
|
||||
typedef std::tr1::reference_wrapper<TY> RefWrapper;
|
||||
public:
|
||||
|
||||
explicit AssignableRefWrapper(TY& ref)
|
||||
|
|
@ -111,46 +117,103 @@ namespace wrap {
|
|||
class ItemWrapper
|
||||
: public BoolCheckable<ItemWrapper<TY> >
|
||||
{
|
||||
typedef typename impl::ItemWrapperStorage<TY> Item;
|
||||
mutable
|
||||
char content_[sizeof(TY)];
|
||||
bool created_;
|
||||
|
||||
void
|
||||
build (TY const& ref)
|
||||
{
|
||||
new(&content_) TY(ref);
|
||||
created_ = true;
|
||||
}
|
||||
|
||||
void
|
||||
discard ()
|
||||
{
|
||||
if (created_) access().~TY();
|
||||
created_ = false;
|
||||
}
|
||||
|
||||
TY&
|
||||
access () const
|
||||
{
|
||||
return reinterpret_cast<TY&> (content_);
|
||||
}
|
||||
|
||||
Item item_;
|
||||
|
||||
public:
|
||||
ItemWrapper()
|
||||
: item_()
|
||||
: created_(false)
|
||||
{ }
|
||||
|
||||
explicit
|
||||
ItemWrapper(TY& o)
|
||||
: item_(o)
|
||||
{ }
|
||||
ItemWrapper(TY const& o)
|
||||
{
|
||||
build (o);
|
||||
}
|
||||
|
||||
//note: using default copy ctor and assignment operator!
|
||||
~ItemWrapper()
|
||||
{
|
||||
discard();
|
||||
}
|
||||
|
||||
|
||||
/* == copy and assignment == */
|
||||
|
||||
ItemWrapper (ItemWrapper const& ref)
|
||||
{
|
||||
if (ref.isValid())
|
||||
build (*ref);
|
||||
}
|
||||
|
||||
ItemWrapper&
|
||||
operator= (ItemWrapper const& ref)
|
||||
{
|
||||
if (!ref.isValid())
|
||||
discard();
|
||||
else
|
||||
(*this) = (*ref);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename X>
|
||||
ItemWrapper&
|
||||
operator= (X const& something) ///< accept anything assignable to TY
|
||||
{
|
||||
item_ = something;
|
||||
if (isSameObject (something, access() ))
|
||||
{
|
||||
if (created_)
|
||||
access() = something;
|
||||
else
|
||||
build (something);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
/* == value access == */
|
||||
TY&
|
||||
operator* ()
|
||||
operator* () const
|
||||
{
|
||||
return item_;
|
||||
if (!created_)
|
||||
throw lumiera::error::State ("accessing uninitialised value/ref wrapper"
|
||||
, LUMIERA_ERROR_BOTTOM_VALUE);
|
||||
return access();
|
||||
}
|
||||
|
||||
bool
|
||||
isValid () const
|
||||
{
|
||||
return item_.isValid();
|
||||
return created_;
|
||||
}
|
||||
|
||||
void
|
||||
reset ()
|
||||
{
|
||||
item_.clear();
|
||||
discard();
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -180,8 +180,9 @@ namespace session {
|
|||
{
|
||||
REQUIRE (contains (id));
|
||||
ScopeContents contents = scopeTab_.equal_range (id);
|
||||
return iterator (ScopeRangeIter(contents.first, contents.second)
|
||||
,rangeIndexElementsResolver() );
|
||||
UNIMPLEMENTED ("WIP-WIP-WIP");
|
||||
// return iterator (ScopeRangeIter(contents.first, contents.second)
|
||||
// ,scopeIndexElementsResolver() );
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -327,7 +328,7 @@ namespace session {
|
|||
* resolve it through the main index table (placementTab_).
|
||||
*/
|
||||
PlacementMO&
|
||||
resolveScopeIndexElement(pair<PID,PID> const& entry)
|
||||
resolveScopeIndexElement(pair<PID,PID> const& entry) const
|
||||
{
|
||||
ID elemID (entry.second);
|
||||
ASSERT (contains (elemID));
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@
|
|||
|
||||
|
||||
namespace lib {
|
||||
namespace wrapper {
|
||||
namespace test{
|
||||
|
||||
using ::Test;
|
||||
|
|
@ -46,6 +47,7 @@ namespace test{
|
|||
// using util::isnil;
|
||||
// using std::vector;
|
||||
using std::string;
|
||||
using lib::test::randStr;
|
||||
// using std::cout;
|
||||
// using std::endl;
|
||||
|
||||
|
|
@ -82,9 +84,10 @@ namespace test{
|
|||
ulong ll (rand() % 1000);
|
||||
string ss (randStr(50));
|
||||
const char* cp (ss.c_str());
|
||||
Tracker tt;
|
||||
// Tracker tt;
|
||||
|
||||
verifyWrapper (ItemWrapper<ulong> (ll), ll, -123);
|
||||
verifyWrapper<ulong> (ItemWrapper<ulong> (ll), ll, -123);
|
||||
#if false //////////////////////////////////////////////////////////////////////////////////////////////////////////TICKET 475 !!!!!!!!!
|
||||
verifyWrapper (ItemWrapper<ulong&> (ll), ll, 45678 );
|
||||
verifyWrapper (ItemWrapper<ulong const&> (ll), ll, 0 );
|
||||
verifyWrapper (ItemWrapper<ulong*> (&ll), &ll, (ulong*) 0 );
|
||||
|
|
@ -98,6 +101,7 @@ namespace test{
|
|||
verifyWrapper (ItemWrapper<Tracker*> (&tt), &tt, (Tracker*) 0 );
|
||||
|
||||
verifyWrapper (ItemWrapper<const char*> (cp), cp, "Lumiera");
|
||||
#endif //////////////////////////////////////////////////////////////////////////////////////TODO using yet undefined facilities.....!!!!!
|
||||
|
||||
verifyWrappedRef ();
|
||||
}
|
||||
|
|
@ -117,13 +121,17 @@ namespace test{
|
|||
ASSERT (!copy2);
|
||||
ASSERT (false == bool(empty));
|
||||
|
||||
#if false //////////////////////////////////////////////////////////////////////////////////////////////////////////TICKET 475 !!!!!!!!!
|
||||
ASSERT (wrap == copy1);
|
||||
#endif //////////////////////////////////////////////////////////////////////////////////////TODO using yet undefined facilities.....!!!!!
|
||||
ASSERT (wrap != copy2);
|
||||
ASSERT (wrap != empty);
|
||||
|
||||
copy2 = copy1;
|
||||
ASSERT (copy2);
|
||||
#if false //////////////////////////////////////////////////////////////////////////////////////////////////////////TICKET 475 !!!!!!!!!
|
||||
ASSERT (wrap == copy2);
|
||||
#endif //////////////////////////////////////////////////////////////////////////////////////TODO using yet undefined facilities.....!!!!!
|
||||
ASSERT (wrap != empty);
|
||||
|
||||
copy2 = otherVal;
|
||||
|
|
@ -143,9 +151,11 @@ namespace test{
|
|||
ASSERT (wrap != copy1);
|
||||
ASSERT (wrap != copy2);
|
||||
|
||||
copy1.clear();
|
||||
copy1.reset();
|
||||
ASSERT (!copy1);
|
||||
#if false //////////////////////////////////////////////////////////////////////////////////////////////////////////TICKET 475 !!!!!!!!!
|
||||
ASSERT (empty == copy1);
|
||||
#endif //////////////////////////////////////////////////////////////////////////////////////TODO using yet undefined facilities.....!!!!!
|
||||
ASSERT (copy2 != copy1);
|
||||
VERIFY_ERROR (BOTTOM_VALUE, *copy1 );
|
||||
};
|
||||
|
|
@ -154,6 +164,7 @@ namespace test{
|
|||
void
|
||||
verifyWrappedRef ()
|
||||
{
|
||||
#if false //////////////////////////////////////////////////////////////////////////////////////////////////////////TICKET 475 !!!!!!!!!
|
||||
int x = 5;
|
||||
ItemWrapper<int&> refWrap;
|
||||
ASSERT (!refWrap);
|
||||
|
|
@ -170,6 +181,7 @@ namespace test{
|
|||
ASSERT (isSameObject (*ptrWrap, x));
|
||||
**ptrWrap += 13;
|
||||
ASSERT (x == 23);
|
||||
#endif //////////////////////////////////////////////////////////////////////////////////////TODO using yet undefined facilities.....!!!!!
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -179,5 +191,5 @@ namespace test{
|
|||
LAUNCHER (ItemWrapper_test, "unit common");
|
||||
|
||||
|
||||
}} // namespace lib::test
|
||||
}}} // namespace lib::wrapper::test
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue