back on route: trying to get the ArgumentHolder to fly...

This commit is contained in:
Fischlurch 2009-07-18 19:36:32 +02:00
parent 3f8d82a13f
commit 6510155e76
4 changed files with 360 additions and 27 deletions

View file

@ -45,6 +45,7 @@
//#include "pre.hpp"
//#include "lib/error.hpp"
#include "proc/control/argument-tuple-accept.hpp"
#include "proc/control/command-closure.hpp"
#include "proc/control/memento-tie.hpp"
@ -104,14 +105,17 @@ namespace control {
* actually allocating storage to hold the command arguments
* and the undo state (memento) for Proc-Layer commands.
* Both the contained components within ArgumentHolder
* can be in \em empty state; there is no distinct
* can be in \em empty state; there are no distinct
* lifecycle limitations. ArgumentHolder is part
* of Proc-Layer command's implementation
* and should not be used standalone.
*/
template<typename SIG, typename MEM>
class ArgumentHolder
: public CmdClosure
: public ArgumentTupleAccept< SIG // to derive the desired bind(..) signature
, ArgumentHolder<SIG,MEM> // target class providing the implementation
, CmdClosure // base class to inherit from
>
{
Closure<SIG> arguments_;
MementoTie<SIG,MEM> memento_;
@ -141,8 +145,8 @@ namespace control {
virtual operator string() const
{
return "Command-State{ arguments="
+ arguments_? arguments_ : "unbound"
+ memento_ ? ", <memento> }" : "<no undo> }"
+ (arguments_? string(arguments_) : "unbound")
+ (memento_ ? ", <memento> }" : "<no undo> }")
;
}
@ -159,14 +163,14 @@ namespace control {
{ }
/** has undo state capturing been invoked? */
bool canUndo () { return bool(memento_); }
bool empty () { return !arguments_; }
bool canUndo () const { return bool(memento_); }
bool empty () const { return !arguments_; }
/** store a new argument tuple within this ArgumentHolder,
* discarding and previously stored arguments */
void
bind (ArgTuple argTup)
bindArg (ArgTuple argTup)
{
this->arguments_ = Closure<SIG> (argTup);
}
@ -185,6 +189,15 @@ namespace control {
return this->memento_ = MementoTie<SIG,MEM> (undoFunc,captureFunc);
}
/** direct "backdoor" access to stored memento value.
* @note you might access a placeholder when called prior to \c tie(..) */
MEM&
memento ()
{
return memento_.getState();
}
};

View file

@ -97,7 +97,7 @@ namespace control {
virtual operator string() const =0;
virtual bool isValid () const { return true; }
virtual bool isValid () const =0;
virtual CmdFunctor bindArguments (CmdFunctor&) =0;
};
@ -212,6 +212,9 @@ namespace control {
else
return dumped+")";
}
bool isValid () const { return true; }
};

View file

@ -51,7 +51,8 @@
//using std::tr1::placeholders::_2;
//using std::tr1::function;
using util::isnil;
using util::and_all;
//using util::and_all;
using util::for_each;
using boost::format;
using lumiera::Time;
//using util::contains;
@ -178,46 +179,60 @@ namespace test {
virtual void
run (Arg)
{
#if false ////////////////////////////////////////////////////////////////////////////TODO.....
ArgTuples testTuples;
Tracker<Time>::instanceCnt = 0;
Tracker<string>::instanceCnt = 0;
createArgTuples (testTuples);
checkArgumentComparison ();
serialiseArgTuples (testTuples);
createTuples (testTuples);
// checkArgumentComparison ();
// serialiseArgTuples (testTuples);
simulateCmdLifecycle();
// simulateCmdLifecycle();
ASSERT (0 == Tracker<Time>::instanceCnt);
ASSERT (0 == Tracker<string>::instanceCnt);
}
typedef struct{ int i[5]; } Sint5;
/** @test create various argument tuples and re-access their contents */
void
createTuples (ArgTuples& tup)
{
tup.manage (new ArgumentHolder<void()>);
tup.manage (new ArgumentHolder<void(int)>);
tup.manage (new ArgumentHolder<void(int,Time)>);
tup.manage (new ArgumentHolder<void(int,Time), Time>);
tup.manage (new ArgumentHolder<void(int,Time), int[5]>);
typedef ArgumentHolder<void(), bool> A1;
typedef ArgumentHolder<void(int), void*> A2;
typedef ArgumentHolder<void(int,Time), int> A3;
typedef ArgumentHolder<void(int,Time), Time> A4;
typedef ArgumentHolder<void(int,Time), Sint5> A5;
ASSERT (and_all (tup, isnil));
A1* arg1 = new A1(); tup.manage (arg1);
A2* arg2 = new A2(); tup.manage (arg2);
A3* arg3 = new A3(); tup.manage (arg3);
A4* arg4 = new A4(); tup.manage (arg4);
A5* arg5 = new A5(); tup.manage (arg5);
ASSERT (isnil (*arg1));
ASSERT (isnil (*arg2));
ASSERT (isnil (*arg3));
ASSERT (isnil (*arg4));
ASSERT (isnil (*arg5));
for_each (tup, showIt);
tup[1].bind (rand() % 20);
tup[2].bind (rand() % 20, randTime());
tup[3].bind (rand() % 20, randTime());
tup[4].bind (rand() % 20, randTime());
arg2->bind (rand() % 20);
arg3->bind (rand() % 20, randTime());
arg4->bind (rand() % 20, randTime());
arg5->bind (rand() % 20, randTime());
tup[1].memento() = 42;
tup[4].memento()[3] = 513;
arg3->memento() = 42;
arg5->memento().i[3] = 513;
for_each (tup, showIt);
}
#if false ////////////////////////////////////////////////////////////////////////////TODO.....
/** @test serialise and de-serialise each tuple and check validity
* @todo unimplemented, waiting on Serialiser
*/
@ -319,8 +334,8 @@ namespace test {
bound_undoFun();
cout << protocol.str() << endl;
#endif ////////////////////////////////////////////////////////////////////////////TODO.....
}
#endif ////////////////////////////////////////////////////////////////////////////TODO.....
};

View file

@ -0,0 +1,302 @@
/*
OpaqueHolder(Test) - check the inline type erasure helper
Copyright (C) Lumiera.org
2009, Hermann Vosseler <Ichthyostega@web.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* *****************************************************/
#include "lib/test/run.hpp"
#include "lib/test/test-helper.hpp"
#include "lib/util.hpp"
#include "lib/opaque-holder.hpp"
#include "lib/bool-checkable.hpp"
#include <iostream>
#include <vector>
namespace lib {
namespace test{
using ::Test;
using util::isnil;
using util::for_each;
using util::isSameObject;
using lumiera::error::LUMIERA_ERROR_INVALID;
using lumiera::error::LUMIERA_ERROR_ASSERTION;
using std::vector;
using std::cout;
using std::endl;
namespace { // test dummy hierarchy
// Note: common storage but no vtable
long _checksum = 0;
uint _create_count = 0;
struct Base
{
uint id_;
Base(uint i=0) : id_(i) { _checksum +=id_; ++_create_count; }
Base(Base const& o) : id_(o.id_) { _checksum +=id_; ++_create_count; }
uint getIt() { return id_; }
};
template<uint ii>
struct DD : Base
{
DD() : Base(ii) { }
~DD() { _checksum -= ii; } // doing the decrement here
}; // verifies the correct dtor is called
struct Special
: DD<7>
, BoolCheckable<Special>
{
ulong myVal_;
Special (uint val)
: myVal_(val)
{ }
bool
isValid () const ///< custom boolean "validity" check
{
return myVal_ % 2;
}
};
/** maximum additional storage maybe wasted
* due to alignment of the contained object
* within OpaqueHolder's buffer
*/
const size_t _ALIGN_ = sizeof(size_t);
}
typedef OpaqueHolder<Base> Opaque;
typedef vector<Opaque> TestList;
/**********************************************************************************
* @test use the OpaqueHolder inline buffer to handle objects of a family of types
* through a common interface, without being forced to use heap storage
* or a custom allocator.
*
* @todo this test doesn't cover automatic conversions and conversions using RTTI
* from the target objects, while \code OpaqueHolder.template get() \endcode
* would allow for such conversions. This is similar to Ticket #141, and
* actually based on the same code as variant.hpp (access-casted.hpp)
*/
class OpaqueHolder_test : public Test
{
virtual void
run (Arg)
{
_checksum = 0;
_create_count = 0;
{
TestList objs = createDummies ();
for_each (objs, reAccess);
checkHandling (objs);
checkSpecialSubclass ();
}
ASSERT (0 == _checksum); // all dead
}
TestList
createDummies ()
{
TestList list;
list.push_back (DD<1>());
list.push_back (DD<3>());
list.push_back (DD<5>());
list.push_back (DD<7>());
return list;
} //note: copy
static void
reAccess (Opaque& elm)
{
cout << elm->getIt() << endl;
}
/** @test cover the basic situations of object handling,
* especially copy operations and re-assignments
*/
void
checkHandling (TestList& objs)
{
Opaque oo;
ASSERT (!oo);
ASSERT (isnil(oo));
oo = objs[1];
ASSERT (oo);
ASSERT (!isnil(oo));
typedef DD<3> D3;
typedef DD<5> D5;
D3 d3 (oo.get<D3>() );
ASSERT (3 == oo->getIt()); // re-access through Base interface
ASSERT (!isSameObject (d3, *oo));
VERIFY_ERROR (WRONG_TYPE, oo.get<D5>() );
// direct assignment of target into Buffer
oo = D5();
ASSERT (oo);
ASSERT (5 == oo->getIt());
VERIFY_ERROR (WRONG_TYPE, oo.get<D3>() );
// can get a direct reference to contained object
D5 &rd5 (oo.get<D5>());
ASSERT (isSameObject (rd5, *oo));
ASSERT (!isnil(oo));
oo = objs[3]; // copy construction also works on non-empty object
ASSERT (7 == oo->getIt());
// WARNING: direct ref has been messed up through the backdoor!
ASSERT (7 == rd5.getIt());
ASSERT (isSameObject (rd5, *oo));
uint cnt_before = _create_count;
oo.clear();
ASSERT (!oo);
oo = D5(); // direct assignment also works on empty object
ASSERT (oo);
ASSERT (5 == oo->getIt());
ASSERT (_create_count == 2 + cnt_before);
// one within buff and one for the anonymous temporary D5()
// verify that self-assignment is properly detected...
cnt_before = _create_count;
oo = oo;
ASSERT (oo);
ASSERT (_create_count == cnt_before);
oo = oo.get<D5>();
ASSERT (_create_count == cnt_before);
oo = *oo;
ASSERT (_create_count == cnt_before);
ASSERT (oo);
oo.clear();
ASSERT (!oo);
ASSERT (isnil(oo));
VERIFY_ERROR (INVALID, oo.get<D5>() );
#if false ////////////////////////////////////////////////////////TODO: restore throwing ASSERT
VERIFY_ERROR (ASSERTION, oo->getIt() );
#endif////////////////////////////////////////////////////////////
// can't access empty holder...
Opaque o1 (oo);
ASSERT (!o1);
Opaque o2 (d3);
ASSERT (!isSameObject (d3, *o2));
ASSERT (3 == o2->getIt());
ASSERT (sizeof(Opaque) <= sizeof(Base) + sizeof(void*) + _ALIGN_);
}
/** @test OpaqueHolder with additional storage for subclass.
* When a subclass requires more storage than the base class or
* Interface, we need to create a custom OpaqueHolder, specifying the
* actually necessary storage. Such a custom OpaqueHolder behaves exactly
* like the standard variant, but there is protection against accidentally
* using a standard variant to hold an instance of the larger subclass.
*
* @test Moreover, if the concrete class has a custom operator bool(), it
* will be invoked automatically from OpaqueHolder's operator bool()
*
*/
void
checkSpecialSubclass ()
{
typedef OpaqueHolder<Base, sizeof(Special)> SpecialOpaque;
cout << showSizeof<Base>() << endl;
cout << showSizeof<Special>() << endl;
cout << showSizeof<Opaque>() << endl;
cout << showSizeof<SpecialOpaque>() << endl;
ASSERT (sizeof(Special) > sizeof(Base));
ASSERT (sizeof(SpecialOpaque) > sizeof(Opaque));
ASSERT (sizeof(SpecialOpaque) <= sizeof(Special) + sizeof(void*) + _ALIGN_);
Special s1 (6);
Special s2 (3);
ASSERT (!s1); // even value
ASSERT (s2); // odd value
ASSERT (7 == s1.getIt()); // indeed subclass of DD<7>
ASSERT (7 == s2.getIt());
SpecialOpaque ospe0;
SpecialOpaque ospe1 (s1);
SpecialOpaque ospe2 (s2);
ASSERT (!ospe0); // note: bool test (isValid)
ASSERT (!ospe1); // also forwarded to contained object (myVal_==6 is even)
ASSERT ( ospe2);
ASSERT ( isnil(ospe0)); // while isnil just checks the empty state
ASSERT (!isnil(ospe1));
ASSERT (!isnil(ospe2));
ASSERT (7 == ospe1->getIt());
ASSERT (6 == ospe1.get<Special>().myVal_);
ASSERT (3 == ospe2.get<Special>().myVal_);
ospe1 = DD<5>(); // but can be reassigned like any normal Opaque
ASSERT (ospe1);
ASSERT (5 == ospe1->getIt());
VERIFY_ERROR (WRONG_TYPE, ospe1.get<Special>() );
Opaque normal = DD<5>();
ASSERT (normal);
ASSERT (5 == normal->getIt());
#if false ////////////////////////////////////////////////////////TODO: restore throwing ASSERT
// Assertion protects against SEGV
VERIFY_ERROR (ASSERTION, normal = s1 );
#endif////////////////////////////////////////////////////////////
}
};
LAUNCHER (OpaqueHolder_test, "unit common");
}} // namespace lib::test