1 /*
2  *  Created by Phil on 28/01/2011.
3  *  Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
4  *
5  *  Distributed under the Boost Software License, Version 1.0. (See accompanying
6  *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7  */
8 #ifndef TWOBLUECUBES_CATCH_GENERATORS_IMPL_HPP_INCLUDED
9 #define TWOBLUECUBES_CATCH_GENERATORS_IMPL_HPP_INCLUDED
10 
11 #include "catch_interfaces_generators.h"
12 
13 #include "catch_common.h"
14 
15 #include <vector>
16 #include <string>
17 #include <map>
18 
19 namespace Catch {
20 
21     struct GeneratorInfo : IGeneratorInfo {
22 
GeneratorInfoCatch::GeneratorInfo23         GeneratorInfo( std::size_t size )
24         :   m_size( size ),
25             m_currentIndex( 0 )
26         {}
27 
moveNextCatch::GeneratorInfo28         bool moveNext() {
29             if( ++m_currentIndex == m_size ) {
30                 m_currentIndex = 0;
31                 return false;
32             }
33             return true;
34         }
35 
getCurrentIndexCatch::GeneratorInfo36         std::size_t getCurrentIndex() const {
37             return m_currentIndex;
38         }
39 
40         std::size_t m_size;
41         std::size_t m_currentIndex;
42     };
43 
44     ///////////////////////////////////////////////////////////////////////////
45 
46     class GeneratorsForTest : public IGeneratorsForTest {
47 
48     public:
~GeneratorsForTest()49         ~GeneratorsForTest() {
50             deleteAll( m_generatorsInOrder );
51         }
52 
getGeneratorInfo(std::string const & fileInfo,std::size_t size)53         IGeneratorInfo& getGeneratorInfo( std::string const& fileInfo, std::size_t size ) {
54             std::map<std::string, IGeneratorInfo*>::const_iterator it = m_generatorsByName.find( fileInfo );
55             if( it == m_generatorsByName.end() ) {
56                 IGeneratorInfo* info = new GeneratorInfo( size );
57                 m_generatorsByName.insert( std::make_pair( fileInfo, info ) );
58                 m_generatorsInOrder.push_back( info );
59                 return *info;
60             }
61             return *it->second;
62         }
63 
moveNext()64         bool moveNext() {
65             std::vector<IGeneratorInfo*>::const_iterator it = m_generatorsInOrder.begin();
66             std::vector<IGeneratorInfo*>::const_iterator itEnd = m_generatorsInOrder.end();
67             for(; it != itEnd; ++it ) {
68                 if( (*it)->moveNext() )
69                     return true;
70             }
71             return false;
72         }
73 
74     private:
75         std::map<std::string, IGeneratorInfo*> m_generatorsByName;
76         std::vector<IGeneratorInfo*> m_generatorsInOrder;
77     };
78 
createGeneratorsForTest()79     IGeneratorsForTest* createGeneratorsForTest()
80     {
81         return new GeneratorsForTest();
82     }
83 
84 } // end namespace Catch
85 
86 #endif // TWOBLUECUBES_CATCH_GENERATORS_IMPL_HPP_INCLUDED
87