diff --git a/src/lib/error.hpp b/src/lib/error.hpp index 2c5a14d72..ac089518c 100644 --- a/src/lib/error.hpp +++ b/src/lib/error.hpp @@ -123,6 +123,8 @@ namespace lumiera { /* generic error situations */ LUMIERA_ERROR_DECLARE (WRONG_TYPE); ///< runtime type mismatch + LUMIERA_ERROR_DECLARE (ITER_EXHAUST); ///< end of sequence reached + /** Macro for creating derived exception classes properly * integrated into Lumiera's exception hierarchy. Using diff --git a/src/lib/exception.cpp b/src/lib/exception.cpp index 82b4a93f1..879862982 100644 --- a/src/lib/exception.cpp +++ b/src/lib/exception.cpp @@ -66,6 +66,7 @@ namespace lumiera { /* some further generic error situations */ LUMIERA_ERROR_DEFINE (WRONG_TYPE, "runtime type mismatch"); + LUMIERA_ERROR_DEFINE (ITER_EXHAUST, "end of sequence reached"); } // namespace error diff --git a/src/lib/iter-adaptor.hpp b/src/lib/iter-adaptor.hpp index 5adc27f66..560b03eae 100644 --- a/src/lib/iter-adaptor.hpp +++ b/src/lib/iter-adaptor.hpp @@ -43,32 +43,172 @@ //#include "include/logging.h" -//#include "lib/error.hpp" +#include "lib/error.hpp" +#include "lib/bool-checkable.hpp" //#include "lib/util.hpp" //#include //#include +#include + namespace lib { // using util::for_each; + using boost::remove_pointer; /** + * simple ptr-to-object based implementation of the lumiera forward iterator concept. + * Basically such an PtrIter behaves like the similar concept from STL, but + * - it is not just a disguised pointer (meaning, its more expensive) + * - it checks validity on every operation and may throw + * - it has a distinct back-link to the source container + * - the source container needs to implement iterStart() and iterInc() + * - we need friendship to and from the container class + * - the end-of-iteration can be detected by bool check + * + * @see scoped-ptrvect.hpp usage example + * @see iter-adaptor-test.cpp */ - template - class PtrIter + template + class IterAdapter + : public lib::BoolCheckable > { + const CON* source_; + POS pos_; + + /////////////////////////////////////////////////////////////////////////TODO: implement empty test + /////////////////////////////////////////////////////////////////////////TODO: implement comparisons + + public: + typedef typename POS::pointer pointer; //////////////////TODO: do we really need all those typedefs??? + typedef typename POS::reference reference; + typedef typename POS::value_type value_type; + + IterAdapter (const CON* src, const POS& startpos) + : source_(src) + , pos_(startpos) + { } + + IterAdapter () + : source_(0) + , pos_(0) + { } + + + /* === lumiera forward iterator concept === */ + + reference + operator*() const + { + _maybe_throw(); + return *pos_; + } + + pointer + operator->() const + { + _maybe_throw(); + return pos_; + } + + IterAdapter& + operator++() + { + _maybe_throw(); + CON::iterNext (source_,pos_); + return *this; + } + + IterAdapter + operator++(int) + { + _maybe_throw(); + IterAdapter oldPos(*this); + CON::iterNext (source_,pos_); + return oldPos; + } + + bool + isValid () const + { + return (source_ && CON::iterValid(source_,pos_)); + } + + private: + + void + _maybe_throw() const + { + if (!isValid()) + throw lumiera::error::Invalid ("Can't iterate further", + lumiera::error::LUMIERA_ERROR_ITER_EXHAUST); + } }; - /** + + + + + /** wrapper for an existing Iterator type, + * automatically dereferencing the former's output. + * For this to work, the "source" iterator is expected + * to be declared on \em pointers rather than on values. + * @note bool checkable if and only if source is... */ - template + template class PtrDerefIter + : public lib::BoolCheckable > { + IT i_; + + public: + typedef typename IT::value_type pointer; + typedef typename remove_pointer::type & reference; + + + PtrDerefIter (IT srcIter) + : i_(srcIter) + { } + + + /* === lumiera forward iterator concept === */ + + reference + operator*() const + { + return *(*i_); + } + + pointer + operator->() const + { + return *i_; + } + + PtrDerefIter& + operator++() + { + ++i_; + return *this; + } + + PtrDerefIter + operator++(int) + { + return PtrDerefIter (i_++); + } + + bool + isValid () const + { + return bool(i_); + } + }; diff --git a/src/lib/scoped-ptrvect.hpp b/src/lib/scoped-ptrvect.hpp index ca17b057f..5f68037d7 100644 --- a/src/lib/scoped-ptrvect.hpp +++ b/src/lib/scoped-ptrvect.hpp @@ -45,6 +45,7 @@ #include "include/logging.h" +#include "lib/iter-adaptor.hpp" #include "lib/error.hpp" #include "lib/util.hpp" @@ -101,25 +102,25 @@ namespace lib { T& manage (T* obj) { - if (obj) - try - { - push_back (obj); - return *obj; - } - catch(...) - { - delete obj; - throw; - } } + REQUIRE (obj); + try + { + push_back (obj); + return *obj; + } + catch(...) + { + delete obj; + throw; + } } void clear() { typedef typename _Vec::iterator VIter; - VIter e = this->end(); - for (VIter i = this->begin(); i!=e; ++i) + VIter e = _Vec::end(); + for (VIter i = _Vec::begin(); i!=e; ++i) { if (*i) try @@ -143,8 +144,8 @@ namespace lib { return *get(i); } - typedef PtrDerefIter iterator; - typedef PtrDerefIter const_iterator; + typedef PtrDerefIter iterator; + typedef PtrDerefIter const_iterator; iterator begin() { return iterator (_Vec::begin()); } const_iterator begin() const { return const_iterator (_Vec::begin()); } diff --git a/src/lib/test/test-helper.hpp b/src/lib/test/test-helper.hpp index 91b13da96..268e15cbf 100644 --- a/src/lib/test/test-helper.hpp +++ b/src/lib/test/test-helper.hpp @@ -26,9 +26,11 @@ #include "include/symbol.hpp" +#include "lib/lumitime.hpp" #include #include +#include @@ -36,7 +38,9 @@ namespace lib { namespace test{ using lumiera::Symbol; + using lumiera::Time; using std::string; + using std::rand; @@ -48,7 +52,7 @@ namespace test{ * type identification as implemented by the compiler. */ template - const char* + inline const char* showType (T const& obj, Symbol name=0) { return name? name : typeid(obj).name(); @@ -61,7 +65,7 @@ namespace test{ * type identification as implemented by the compiler. */ template - const char* + inline const char* showType (Symbol name=0) { return name? name : typeid(T).name(); @@ -75,21 +79,21 @@ namespace test{ /** for printing sizeof(), trying to figure out the type name automatically */ template - string + inline string showSizeof(Symbol name=0) { return showSizeof (sizeof (T), showType (name)); } template - string + inline string showSizeof(T const& obj, Symbol name=0) { return showSizeof (sizeof (obj), showType (obj,name)); } template - string + inline string showSizeof(T *obj, Symbol name=0) { return obj? showSizeof (*obj, name) @@ -97,6 +101,16 @@ namespace test{ } + + + /** create a random but not insane Time value */ + inline Time + randTime () + { + return Time (500 * (rand() % 2), (rand() % 600)); + } + + }} // namespace lib::test diff --git a/tests/components/proc/control/argument-tuple-accept-test.cpp b/tests/components/proc/control/argument-tuple-accept-test.cpp index 2155ef13c..7416b1998 100644 --- a/tests/components/proc/control/argument-tuple-accept-test.cpp +++ b/tests/components/proc/control/argument-tuple-accept-test.cpp @@ -23,45 +23,14 @@ #include "lib/test/run.hpp" #include "lib/test/test-helper.hpp" -//#include "proc/asset/media.hpp" -//#include "proc/mobject/session.hpp" -//#include "proc/mobject/session/edl.hpp" -//#include "proc/mobject/session/testclip.hpp" -//#include "proc/mobject/test-dummy-mobject.hpp" -//#include "lib/p.hpp" -//#include "proc/mobject/placement.hpp" -//#include "proc/mobject/placement-index.hpp" -//#include "proc/mobject/explicitplacement.hpp" #include "proc/control/argument-tuple-accept.hpp" #include "lib/meta/function.hpp" #include "lib/meta/tuple.hpp" - -//#include "lib/scoped-ptrvect.hpp" -//#include "lib/lumitime-fmt.hpp" -//#include "lib/meta/typelist.hpp" -//#include "lib/meta/tuple.hpp" -//#include "lib/util.hpp" +#include "lib/lumitime-fmt.hpp" #include -//#include #include -//#include -//#include -//#include -//using std::tr1::bind; -//using std::tr1::placeholders::_1; -//using std::tr1::placeholders::_2; -using std::tr1::function; -//using util::isnil; -//using util::and_all; -//using boost::format; -//using lumiera::Time; -//using util::contains; -//using std::string; -//using std::rand; -//using std::ostream; -//using std::ostrstream; using std::cout; using std::endl; @@ -70,15 +39,12 @@ namespace control { namespace test { using lib::test::showSizeof; + using lib::test::randTime; + using lumiera::Time; + using std::tr1::function; using lumiera::typelist::FunctionSignature; using lumiera::typelist::Tuple; -// using session::test::TestClip; -// using lumiera::P; -// using namespace lumiera::typelist; -// using lumiera::typelist::Tuple; - -// using control::CmdClosure; @@ -98,36 +64,32 @@ namespace test { template class TestClass : public ArgumentTupleAccept< SIG // to derive the desired signature - , TestClass // the target class providing the implementation + , TestClass // the target class providing the implementation , typename Tup::Ty // base class to inherit from > { + typedef typename Tup::Ty ATuple; + + public: + void - bind (typename Tup::Ty const& tuple) + bindArg (ATuple const& tuple) { - *this = tuple; + static_cast (*this) = tuple; } }; - Time - randTime () - { - UNIMPLEMENTED ("create a random but not insane Time value"); - } - - } // test-helper implementation - typedef lib::ScopedPtrVect ArgTuples; - /*************************************************************** - * @test Build test object, which accepts a bind(...) call with - * specifically typed arguments. + /************************************************************* + * @test Build a test object, which accepts a bind(...) call + * with specifically typed arguments. * * @see control::CommandArgumentHolder */ @@ -144,10 +106,10 @@ namespace test { testTime.bind(randTime(),23); cout << showSizeof(testVoid) << endl; - cout << showSizeof(testITim) << endl; + cout << showSizeof(testTime) << endl; - cout << testITim.getHead() << endl; - ASSERT (23 == testITim.getTail().getHead()); + cout << testTime.getHead() << endl; + ASSERT (23 == testTime.getTail().getHead()); } }; diff --git a/tests/components/proc/control/command-argument-test.cpp b/tests/components/proc/control/command-argument-test.cpp index e8e8c485b..3ff10cc23 100644 --- a/tests/components/proc/control/command-argument-test.cpp +++ b/tests/components/proc/control/command-argument-test.cpp @@ -42,7 +42,7 @@ //#include #include #include -#include +#include //#include #include @@ -58,7 +58,7 @@ using lumiera::Time; using std::string; //using std::rand; using std::ostream; -using std::ostrstream; +using std::ostringstream; using std::cout; using std::endl; @@ -67,6 +67,7 @@ namespace control { namespace test { using lib::test::showSizeof; + using lib::test::randTime; // using session::test::TestClip; // using lumiera::P; @@ -82,7 +83,7 @@ namespace test { namespace { // test helpers - ostrstream protocol; ///< used to verify the test function calls + ostringstream protocol; ///< used to verify the test function calls template @@ -103,7 +104,7 @@ namespace test { } TY& - operator* () const + operator* () { return element_; } @@ -111,7 +112,7 @@ namespace test { friend ostream& operator<< (ostream& out, const Tracker& tra) { - return out << element_; + return out << tra.element_; } }; @@ -128,13 +129,13 @@ namespace test { } Tracker - captureState (Tracker