Imported Upstream version 2.81
[platform/upstream/libbullet.git] / UnitTests / cppunit / src / cppunit / ProtectorChain.cpp
1 #include "ProtectorChain.h"
2
3 CPPUNIT_NS_BEGIN
4
5
6 class ProtectorChain::ProtectFunctor : public Functor
7 {
8 public:
9   ProtectFunctor( Protector *protector,
10                   const Functor &functor,
11                   const ProtectorContext &context )
12       : m_protector( protector )
13       , m_functor( functor )
14       , m_context( context )
15   {
16   }
17
18   bool operator()() const
19   {
20     return m_protector->protect( m_functor, m_context );
21   }
22
23 private:
24   Protector *m_protector;
25   const Functor &m_functor;
26   const ProtectorContext &m_context;
27 };
28
29
30 ProtectorChain::~ProtectorChain()
31 {
32   while ( count() > 0 )
33     pop();
34 }
35
36
37 void 
38 ProtectorChain::push( Protector *protector )
39 {
40   m_protectors.push_back( protector );
41 }
42
43
44 void 
45 ProtectorChain::pop()
46 {
47   delete m_protectors.back();
48   m_protectors.pop_back();
49 }
50
51 int 
52 ProtectorChain::count() const
53 {
54   return m_protectors.size();
55 }
56
57
58 bool 
59 ProtectorChain::protect( const Functor &functor,
60                          const ProtectorContext &context )
61 {
62   if ( m_protectors.empty() )
63     return functor();
64
65   Functors functors;
66   for ( int index = m_protectors.size()-1; index >= 0; --index )
67   {
68     const Functor &protectedFunctor = 
69               functors.empty() ? functor : *functors.back();
70
71     functors.push_back( new ProtectFunctor( m_protectors[index],
72                                             protectedFunctor, 
73                                             context ) );
74   }
75
76   const Functor &outermostFunctor = *functors.back();
77   bool succeed = outermostFunctor();
78
79   for ( unsigned int deletingIndex = 0; deletingIndex < m_protectors.size(); ++deletingIndex )
80     delete functors[deletingIndex];
81
82   return succeed;
83 }
84
85
86 CPPUNIT_NS_END