~clint-fewbar/ubuntu/precise/squid3/ignore-sighup-early

« back to all changes in this revision

Viewing changes to lib/cppunit-1.10.0/src/cppunit/TestFactoryRegistry.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Luigi Gangitano
  • Date: 2006-11-11 10:32:06 UTC
  • Revision ID: james.westby@ubuntu.com-20061111103206-f3p0r9g0vq44rp3r
Tags: upstream-3.0.PRE5
ImportĀ upstreamĀ versionĀ 3.0.PRE5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <cppunit/extensions/TestFactoryRegistry.h>
 
2
#include <cppunit/portability/CppUnitMap.h>
 
3
#include <cppunit/TestSuite.h>
 
4
#include <assert.h>
 
5
 
 
6
 
 
7
CPPUNIT_NS_BEGIN
 
8
 
 
9
/*! \brief (INTERNAL) List of all TestFactoryRegistry.
 
10
 */
 
11
class TestFactoryRegistryList
 
12
{
 
13
private:
 
14
  typedef CppUnitMap<std::string, TestFactoryRegistry *, std::less<std::string> > Registries;
 
15
  Registries m_registries;
 
16
 
 
17
  enum State {
 
18
    doNotChange =0,
 
19
    notCreated,
 
20
    exist,
 
21
    destroyed
 
22
  };
 
23
 
 
24
  static State stateFlag( State newState = doNotChange )
 
25
  {
 
26
    static State state = notCreated;
 
27
    if ( newState != doNotChange )
 
28
      state = newState;
 
29
    return state;
 
30
  }
 
31
 
 
32
  static TestFactoryRegistryList *getInstance()
 
33
  {
 
34
    static TestFactoryRegistryList list;
 
35
    return &list;
 
36
  }
 
37
 
 
38
  TestFactoryRegistry *getInternalRegistry( const std::string &name )
 
39
  {
 
40
    Registries::const_iterator foundIt = m_registries.find( name );
 
41
    if ( foundIt == m_registries.end() )
 
42
    {
 
43
      TestFactoryRegistry *factory = new TestFactoryRegistry( name );
 
44
      m_registries.insert( std::pair<const std::string, TestFactoryRegistry*>( name, factory ) );
 
45
      return factory;
 
46
    }
 
47
    return (*foundIt).second;
 
48
  }
 
49
 
 
50
public:
 
51
  TestFactoryRegistryList()
 
52
  {
 
53
    stateFlag( exist );
 
54
  }
 
55
 
 
56
  ~TestFactoryRegistryList()
 
57
  {
 
58
    for ( Registries::iterator it = m_registries.begin(); it != m_registries.end(); ++it )
 
59
      delete (*it).second;
 
60
 
 
61
    stateFlag( destroyed );
 
62
  }
 
63
 
 
64
  static TestFactoryRegistry *getRegistry( const std::string &name )
 
65
  {
 
66
    // If the following assertion failed, then TestFactoryRegistry::getRegistry() 
 
67
    // was called during static variable destruction without checking the registry 
 
68
    // validity beforehand using TestFactoryRegistry::isValid() beforehand.
 
69
    assert( isValid() );
 
70
    if ( !isValid() )         // release mode
 
71
      return NULL;            // => force CRASH
 
72
 
 
73
    return getInstance()->getInternalRegistry( name );
 
74
  }
 
75
 
 
76
  static bool isValid()
 
77
  {
 
78
    return stateFlag() != destroyed;
 
79
  }
 
80
};
 
81
 
 
82
 
 
83
 
 
84
TestFactoryRegistry::TestFactoryRegistry( std::string name ) :
 
85
    m_name( name )
 
86
{
 
87
}
 
88
 
 
89
 
 
90
TestFactoryRegistry::~TestFactoryRegistry()
 
91
{
 
92
}
 
93
 
 
94
 
 
95
TestFactoryRegistry &
 
96
TestFactoryRegistry::getRegistry( const std::string &name )
 
97
{
 
98
  return *TestFactoryRegistryList::getRegistry( name );
 
99
}
 
100
 
 
101
 
 
102
void 
 
103
TestFactoryRegistry::registerFactory( const std::string &name,
 
104
                                      TestFactory *factory )
 
105
{
 
106
  registerFactory( factory );
 
107
}
 
108
 
 
109
 
 
110
void 
 
111
TestFactoryRegistry::registerFactory( TestFactory *factory )
 
112
{
 
113
  m_factories.insert( factory );
 
114
}
 
115
 
 
116
 
 
117
void 
 
118
TestFactoryRegistry::unregisterFactory( TestFactory *factory )
 
119
{
 
120
  m_factories.erase( factory );
 
121
}
 
122
 
 
123
 
 
124
void 
 
125
TestFactoryRegistry::addRegistry( const std::string &name )
 
126
{
 
127
  registerFactory( &getRegistry( name ) );
 
128
}
 
129
 
 
130
 
 
131
Test *
 
132
TestFactoryRegistry::makeTest()
 
133
{
 
134
  TestSuite *suite = new TestSuite( m_name );
 
135
  addTestToSuite( suite );
 
136
  return suite;
 
137
}
 
138
 
 
139
 
 
140
void 
 
141
TestFactoryRegistry::addTestToSuite( TestSuite *suite )
 
142
{
 
143
  for ( Factories::iterator it = m_factories.begin(); 
 
144
        it != m_factories.end(); 
 
145
        ++it )
 
146
  {
 
147
    TestFactory *factory = *it;
 
148
    suite->addTest( factory->makeTest() );
 
149
  }
 
150
}
 
151
 
 
152
 
 
153
bool 
 
154
TestFactoryRegistry::isValid()
 
155
{
 
156
  return TestFactoryRegistryList::isValid();
 
157
}
 
158
 
 
159
 
 
160
CPPUNIT_NS_END