#ifndef __JLANG__JOBJECTITERATOR__ #define __JLANG__JOBJECTITERATOR__ #include "JLang/JType.hh" #include "JLang/JTypeList.hh" #include "JLang/JNullType.hh" #include "JLang/JPointer.hh" #include "JLang/JSinglePointer.hh" #include "JLang/JRewindable.hh" #include "JLang/JAccessible.hh" /** * \author mdejong */ namespace JLANG {} namespace JPP { using namespace JLANG; } namespace JLANG { /** * Type definition for number of objects to skip. */ typedef unsigned int skip_type; /** * \cond NEVER * Forward declarations for definitions of I/O redirect and pipe operators. * \endcond */ template class JObjectOutput; template class JValve; template class JObjectSelector; class JRegulator; template class JObjectMultiplexer; template class JMultiPipe; /** * Interface of object iteration for a single data type. */ template class JObjectIterator { protected: /** * Default constructor. */ JObjectIterator() {} public: /** * Type definition of pointer_type. */ typedef JPointer pointer_type; /** * Virtual destructor. */ virtual ~JObjectIterator() {} /** * Check availability of next element. * * \return true if the iteration has more elements; else false */ virtual bool hasNext() = 0; /** * Get next element. * * \return pointer to element */ virtual const pointer_type& next() = 0; /** * Skip items. * * \param ns number of items to skip * \return number of items skipped */ virtual skip_type skip(const skip_type ns) { skip_type i = 0; for ( ; i != ns && hasNext(); ++i) { next(); } return i; } /** * Copy to object output. * * \param in object iterator * \param out object output * \return object iterator */ friend inline JObjectIterator& operator>>(JObjectIterator& in, JObjectOutput & out) { while (in.hasNext()) { const T* p = in.next(); if (p != NULL) out.put(*p); else break; } return in; } /** * Pipe terminator. * * \param left object iterator * \param right object output */ friend inline void operator|(JObjectIterator& left, JObjectOutput& right) { left >> right; } /** * Pipe operator. * * \param left object iterator * \param right valve * \return pipe object */ friend inline JMultiPipe& operator|(JObjectIterator& left, const JValve& right) { JMultiPipe::pipe.reset(new JMultiPipe(left, right)); return *JMultiPipe::pipe; } /** * Pipe operator. * * \param left object iterator * \param right object selector * \return pipe object */ friend inline JMultiPipe& operator|(JObjectIterator& left, const JObjectSelector& right) { JMultiPipe::pipe.reset(new JMultiPipe(left, right)); return *JMultiPipe::pipe; } /** * Pipe operator. * * \param left object iterator * \param right regulator * \return pipe object */ friend inline JMultiPipe& operator|(JObjectIterator& left, const JRegulator& right) { JMultiPipe::pipe.reset(new JMultiPipe(left, right)); return *JMultiPipe::pipe; } /** * Pipe operator. * * \param left object iterator * \param right data type * \return object multiplexer */ template friend inline JObjectMultiplexer& operator|(JObjectIterator& left, const JType& right) { JObjectMultiplexer::multiplexer.reset(new JObjectMultiplexer(left)); return *JObjectMultiplexer::multiplexer; } }; /** * Implementation of object iterator for multiple data types. * * This class recursively defines the JLANG::JObjectIterator interface * for all data types by deriving from: * - JObjectIterator; and * - JObjectIterator. */ template class JObjectIterator< JTypeList > : public virtual JObjectIterator, public virtual JObjectIterator { public: typedef JTypeList typelist; /** * Copy to object output. * * Note that all data types of the input are copied to the output. * * \param in object iterator * \param out object output * \return object iterator */ template friend inline JObjectIterator& operator>>(JObjectIterator& in, JOutputIterator_t& out) { static_cast&>(in) >> static_cast&>(out); static_cast&>(in) >> out; return in; } /** * Pipe terminator. * * \param left object iterator * \param right object output */ friend inline void operator|(JObjectIterator& left, JObjectOutput& right) { left >> right; } /** * Pipe operator. * * \param left object iterator * \param right valve * \return pipe object */ friend inline JMultiPipe& operator|(JObjectIterator& left, const JValve& right) { JMultiPipe::pipe.reset(new JMultiPipe(left, right)); return *JMultiPipe::pipe; } /** * Pipe operator. * * \param left object iterator * \param right object selector * \return pipe object */ friend inline JMultiPipe& operator|(JObjectIterator& left, const JObjectSelector& right) { JMultiPipe::pipe.reset(new JMultiPipe(left, right)); return *JMultiPipe::pipe; } /** * Pipe operator. * * \param left object iterator * \param right regulator * \return pipe object */ friend inline JMultiPipe& operator|(JObjectIterator& left, const JRegulator& right) { JMultiPipe::pipe.reset(new JMultiPipe(left, right)); return *JMultiPipe::pipe; } /** * Pipe operator. * * \param left object iterator * \param right data type * \return object multiplexer */ template friend inline JObjectMultiplexer& operator|(JObjectIterator& left, const JType& right) { JObjectMultiplexer::multiplexer.reset(new JObjectMultiplexer(left)); return *JObjectMultiplexer::multiplexer; } }; /** * Terminator class of recursive JObjectIterator class. */ template class JObjectIterator< JTypeList > : public virtual JObjectIterator {}; /** * Implementation for null iteration. */ template struct JNullIterator : public virtual JObjectIterator { typedef typename JObjectIterator::pointer_type pointer_type; /** * Check availability of next element. * * \return false */ virtual bool hasNext() { return false; } /** * Get next element. * * \return NULL */ virtual const pointer_type& next() { return ps; } private: pointer_type ps; }; /** * Interface for object iteration with rewinding. */ template class JRewindableObjectIterator : public virtual JObjectIterator, public virtual JRewindable {}; /** * Interface for object iteration with named access. */ template class JAccessibleObjectIterator : public virtual JObjectIterator, public virtual JAccessible {}; } #endif