| 2519 | |
| 2520 | template<typename T> |
| 2521 | class CompositeGenerator { |
| 2522 | public: |
| 2523 | CompositeGenerator() : m_totalSize( 0 ) {} |
| 2524 | |
| 2525 | // *** Move semantics, similar to auto_ptr *** |
| 2526 | CompositeGenerator( CompositeGenerator& other ) |
| 2527 | : m_fileInfo( other.m_fileInfo ), |
| 2528 | m_totalSize( 0 ) |
| 2529 | { |
| 2530 | move( other ); |
| 2531 | } |
| 2532 | |
| 2533 | CompositeGenerator& setFileInfo( const char* fileInfo ) { |
| 2534 | m_fileInfo = fileInfo; |
| 2535 | return *this; |
| 2536 | } |
| 2537 | |
| 2538 | ~CompositeGenerator() { |
| 2539 | deleteAll( m_composed ); |
| 2540 | } |
| 2541 | |
| 2542 | operator T () const { |
| 2543 | size_t overallIndex = getCurrentContext().getGeneratorIndex( m_fileInfo, m_totalSize ); |
| 2544 | |
| 2545 | typename std::vector<const IGenerator<T>*>::const_iterator it = m_composed.begin(); |
| 2546 | typename std::vector<const IGenerator<T>*>::const_iterator itEnd = m_composed.end(); |
| 2547 | for( size_t index = 0; it != itEnd; ++it ) |
| 2548 | { |
| 2549 | const IGenerator<T>* generator = *it; |
| 2550 | if( overallIndex >= index && overallIndex < index + generator->size() ) |
| 2551 | { |
| 2552 | return generator->getValue( overallIndex-index ); |
| 2553 | } |
| 2554 | index += generator->size(); |
| 2555 | } |
| 2556 | CATCH_INTERNAL_ERROR( "Indexed past end of generated range" ); |
| 2557 | return T(); // Suppress spurious "not all control paths return a value" warning in Visual Studio - if you know how to fix this please do so |
| 2558 | } |
| 2559 | |
| 2560 | void add( const IGenerator<T>* generator ) { |
| 2561 | m_totalSize += generator->size(); |
| 2562 | m_composed.push_back( generator ); |
| 2563 | } |
| 2564 | |
| 2565 | CompositeGenerator& then( CompositeGenerator& other ) { |
| 2566 | move( other ); |
| 2567 | return *this; |
| 2568 | } |
| 2569 | |
| 2570 | CompositeGenerator& then( T value ) { |
| 2571 | ValuesGenerator<T>* valuesGen = new ValuesGenerator<T>(); |
| 2572 | valuesGen->add( value ); |
| 2573 | add( valuesGen ); |
| 2574 | return *this; |
| 2575 | } |
| 2576 | |
| 2577 | private: |
| 2578 | |