summaryrefslogtreecommitdiffstats
blob: f528341eb1f589fb592a8017609dc1c2c080501c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "ProtectorChain.h"

CPPUNIT_NS_BEGIN


class ProtectorChain::ProtectFunctor : public Functor
{
public:
  ProtectFunctor( Protector *protector,
                  const Functor &functor,
                  const ProtectorContext &context )
      : m_protector( protector )
      , m_functor( functor )
      , m_context( context )
  {
  }

  bool operator()() const
  {
    return m_protector->protect( m_functor, m_context );
  }

private:
  Protector *m_protector;
  const Functor &m_functor;
  const ProtectorContext &m_context;
};


ProtectorChain::~ProtectorChain()
{
  while ( count() > 0 )
    pop();
}


void 
ProtectorChain::push( Protector *protector )
{
  m_protectors.push_back( protector );
}


void 
ProtectorChain::pop()
{
  delete m_protectors.back();
  m_protectors.pop_back();
}

int 
ProtectorChain::count() const
{
  return m_protectors.size();
}


bool 
ProtectorChain::protect( const Functor &functor,
                         const ProtectorContext &context )
{
  if ( m_protectors.empty() )
    return functor();

  Functors functors;
  for ( int index = m_protectors.size()-1; index >= 0; --index )
  {
    const Functor &protectedFunctor = 
              functors.empty() ? functor : *functors.back();

    functors.push_back( new ProtectFunctor( m_protectors[index],
                                            protectedFunctor, 
                                            context ) );
  }

  const Functor &outermostFunctor = *functors.back();
  bool succeed = outermostFunctor();

  for ( unsigned int deletingIndex = 0; deletingIndex < m_protectors.size(); ++deletingIndex )
    delete functors[deletingIndex];

  return succeed;
}


CPPUNIT_NS_END