~blep/cppunit2/trunk

« back to all changes in this revision

Viewing changes to cppunit2/src/cpput/resource.cpp

  • Committer: Baptiste Lepilleur
  • Date: 2009-07-14 10:59:37 UTC
  • Revision ID: blep@users.sourceforge.net-20090714105937-3d38wpx1b2q6v6y0
- Made sure all cpp sources have Unix EOL
- Made sure all python sources have no tabs and use 4 spaces indent

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <cpput/resource.h>
2
 
 
3
 
namespace CppUT
4
 
{
5
 
 
6
 
 
7
 
// //////////////////////////////////////////////////////////////////
8
 
// //////////////////////////////////////////////////////////////////
9
 
// class ResourceHandlerRegistry
10
 
// //////////////////////////////////////////////////////////////////
11
 
// //////////////////////////////////////////////////////////////////
12
 
 
13
 
   ResourceHandlerRegistry &
14
 
   ResourceHandlerRegistry::instance()
15
 
   {
16
 
      static ResourceHandlerRegistry registry;
17
 
      return registry;
18
 
   }
19
 
 
20
 
   void 
21
 
   ResourceHandlerRegistry::addConstant( const ResourceName &name,
22
 
                                         const Resource &constant )
23
 
   {
24
 
      ResourceHandlerPtr handler( new ConstantResourceHandler( constant ) );
25
 
      add( name, handler );
26
 
   }
27
 
 
28
 
 
29
 
   ResourceId 
30
 
   ResourceHandlerRegistry::add( const ResourceName &name, 
31
 
                                 const ResourceHandlerPtr &handler )
32
 
   {
33
 
      CppTL::Mutex::ScopedLockGuard guard( lock_ );
34
 
      ++nextId_;
35
 
      ResourceData data;
36
 
      data.name_ = name;
37
 
      data.lazyCount_ = 0;
38
 
      data.handler_ = handler;
39
 
      data.id_ = nextId_;
40
 
      resourceById_[nextId_] = data;
41
 
      idsByName_[name] = nextId_;
42
 
      return nextId_;
43
 
   }
44
 
 
45
 
 
46
 
   void 
47
 
   ResourceHandlerRegistry::remove( ResourceId id )
48
 
   {
49
 
      CppTL::Mutex::ScopedLockGuard guard( lock_ );
50
 
 
51
 
      ResourceById::iterator itResource = resourceById_.find( id );
52
 
      if ( itResource == resourceById_.end() )
53
 
      {
54
 
         return;
55
 
      }
56
 
 
57
 
      ResourceData &data = itResource->second;
58
 
      idsByName_.erase( data.name_ );
59
 
      resourceById_.erase( itResource );
60
 
   }
61
 
 
62
 
 
63
 
   ResourceLazyPtr 
64
 
   ResourceHandlerRegistry::getResource( const ResourceName &name )
65
 
   {
66
 
      CppTL::Mutex::ScopedLockGuard guard( lock_ );
67
 
      
68
 
      IdsByName::iterator itId = idsByName_.find( name );
69
 
      if ( itId == idsByName_.end() )
70
 
      {
71
 
         return ResourceLazyPtr();
72
 
      }
73
 
 
74
 
      ResourceId id = itId->second;
75
 
      return ResourceLazyPtr( id );
76
 
   }
77
 
 
78
 
 
79
 
   AcquiredResourceHandlePtr 
80
 
   ResourceHandlerRegistry::obtainResource( ResourceId id )
81
 
   {
82
 
      if ( id == 0 )
83
 
      {
84
 
         return AcquiredResourceHandlePtr();
85
 
      }
86
 
 
87
 
      ResourceInstancePtr instance;
88
 
      ResourceHandler::AccessPolicy access;
89
 
      // Scoped lock to ensure that the retrieval/creation of a resource does
90
 
      // prevent the retrieval/creation of another resource.
91
 
      // Otherwise, dead-lock would occur if one thread held the lock on a resource
92
 
      // while trying to obtain a second one, and the second one would be locked by
93
 
      // another thread trying to obtain the first resource.
94
 
      {
95
 
         CppTL::Mutex::ScopedLockGuard guard( lock_ );
96
 
         ResourceById::iterator itData = resourceById_.find( id );
97
 
         if ( itData == resourceById_.end() )
98
 
         {
99
 
            return AcquiredResourceHandlePtr();   // Resource was removed
100
 
         }
101
 
         ResourceData &data = itData->second;
102
 
         access = data.handler_->accessPolicy();
103
 
         if ( !data.instance_ )  // Resource not instantiated, create it
104
 
         {
105
 
            data.instance_.reset( new ResourceInstance( data.name_,
106
 
                                                        data.handler_ ) );
107
 
         }
108
 
         instance = data.instance_;
109
 
      }
110
 
      // Now that the global lock has been required, we can safely acquire the
111
 
      // resource (which may lock until another thread using the resource is done
112
 
      // with it if the resource does not support concurrent access).
113
 
      return instance->acquire( access );
114
 
   }
115
 
 
116
 
 
117
 
   void 
118
 
   ResourceHandlerRegistry::increaseLazyRef( ResourceId id )
119
 
   {
120
 
      if ( id == 0 )
121
 
      {
122
 
         return;
123
 
      }
124
 
 
125
 
      CppTL::Mutex::ScopedLockGuard guard( lock_ );
126
 
      
127
 
      ResourceById::iterator itResource = resourceById_.find( id );
128
 
      if ( itResource != resourceById_.end() )  // checks that the resource was not removed
129
 
      {
130
 
         ResourceData &data = itResource->second;
131
 
         ++(data.lazyCount_);
132
 
      }
133
 
   }
134
 
 
135
 
 
136
 
   void 
137
 
   ResourceHandlerRegistry::decreaseLazyRef( ResourceId id )
138
 
   {
139
 
      if ( id == 0 )
140
 
      {
141
 
         return;
142
 
      }
143
 
 
144
 
      CppTL::Mutex::ScopedLockGuard guard( lock_ );
145
 
      
146
 
      ResourceById::iterator itResource = resourceById_.find( id );
147
 
      if ( itResource != resourceById_.end() )  // checks that the resource was not removed
148
 
      {
149
 
         ResourceData &data = itResource->second;
150
 
         if ( --(data.lazyCount_) == 0 )
151
 
         {
152
 
            // Last lazy reference discarded, remove the ResourceInstancePtr
153
 
            // that was stored to prevent it from being destroyed.
154
 
            // Only other existing AcquiredResourceHandlePtr will prevent it from being 
155
 
            // destroyed.
156
 
            data.instance_.reset();
157
 
         }
158
 
      }
159
 
   }
160
 
 
161
 
 
162
 
 
163
 
// //////////////////////////////////////////////////////////////////
164
 
// //////////////////////////////////////////////////////////////////
165
 
// class ResourceInstance
166
 
// //////////////////////////////////////////////////////////////////
167
 
// //////////////////////////////////////////////////////////////////
168
 
 
169
 
 
170
 
   ResourceInstance::ResourceInstance( const ResourceName &name,
171
 
                                       const ResourceHandlerPtr &handler )
172
 
      : handler_( handler )
173
 
      , name_( name )
174
 
   {
175
 
      resource_ = handler_->create( name );
176
 
   }
177
 
 
178
 
 
179
 
   ResourceInstance::~ResourceInstance()
180
 
   {
181
 
      handler_->destroy( name_, resource_ );
182
 
   }
183
 
 
184
 
 
185
 
   void 
186
 
   ResourceInstance::setUp()
187
 
   {
188
 
      handler_->setUp( name_, resource_ );
189
 
   }
190
 
 
191
 
 
192
 
   void 
193
 
   ResourceInstance::tearDown()
194
 
   {
195
 
      handler_->setUp( name_, resource_ );
196
 
   }
197
 
 
198
 
 
199
 
   AcquiredResourceHandlePtr
200
 
   ResourceInstance::acquire( ResourceHandler::AccessPolicy access )
201
 
   {
202
 
      AcquiredResourceHandlePtr handle( 
203
 
         new AcquiredResourceHandle( lock_, this, access ) );
204
 
      return handle;
205
 
   }
206
 
 
207
 
 
208
 
// //////////////////////////////////////////////////////////////////
209
 
// //////////////////////////////////////////////////////////////////
210
 
// class AcquiredResourceHandle
211
 
// //////////////////////////////////////////////////////////////////
212
 
// //////////////////////////////////////////////////////////////////
213
 
 
214
 
   AcquiredResourceHandle::AcquiredResourceHandle( 
215
 
                              CppTL::Mutex &lock,
216
 
                              const ResourceInstancePtr &instance,
217
 
                              ResourceHandler::AccessPolicy access )
218
 
      : lock_( lock )
219
 
      , instance_( instance )
220
 
      , access_( access )
221
 
   {  
222
 
      if ( access_ != ResourceHandler::concurrent )
223
 
      {
224
 
         lock_.lock();
225
 
      }
226
 
   }
227
 
 
228
 
   AcquiredResourceHandle::~AcquiredResourceHandle()
229
 
   {
230
 
      if ( access_ != ResourceHandler::concurrent )
231
 
      {
232
 
         lock_.unlock();
233
 
      }
234
 
   }
235
 
 
236
 
 
237
 
   void 
238
 
   AcquiredResourceHandle::setUp()
239
 
   {
240
 
      instance_->setUp();
241
 
   }
242
 
 
243
 
   
244
 
   void 
245
 
   AcquiredResourceHandle::tearDown()
246
 
   {
247
 
      instance_->tearDown();
248
 
   }
249
 
 
250
 
 
251
 
 
252
 
// //////////////////////////////////////////////////////////////////
253
 
// //////////////////////////////////////////////////////////////////
254
 
// class ResourceLazyPtr
255
 
// //////////////////////////////////////////////////////////////////
256
 
// //////////////////////////////////////////////////////////////////
257
 
 
258
 
 
259
 
ResourceLazyPtr &
260
 
ResourceLazyPtr::operator =( const ResourceLazyPtr &other )
261
 
{
262
 
   ResourceId oldId = id_;
263
 
   id_ = other.id_;
264
 
   ResourceHandlerRegistry &registry = ResourceHandlerRegistry::instance();
265
 
   registry.increaseLazyRef( id_ );
266
 
   registry.decreaseLazyRef( oldId );
267
 
 
268
 
   return *this;
269
 
}
270
 
 
271
 
 
272
 
AcquiredResourceHandlePtr 
273
 
ResourceLazyPtr::acquire()
274
 
{
275
 
   if ( isValid() )
276
 
   {
277
 
      ResourceHandlerRegistry::instance().obtainResource( id_ );
278
 
   }
279
 
   return AcquiredResourceHandlePtr();
280
 
}
281
 
 
282
 
 
283
 
} // namespace CppUT
 
1
#include <cpput/resource.h>
 
2
 
 
3
namespace CppUT
 
4
{
 
5
 
 
6
 
 
7
// //////////////////////////////////////////////////////////////////
 
8
// //////////////////////////////////////////////////////////////////
 
9
// class ResourceHandlerRegistry
 
10
// //////////////////////////////////////////////////////////////////
 
11
// //////////////////////////////////////////////////////////////////
 
12
 
 
13
   ResourceHandlerRegistry &
 
14
   ResourceHandlerRegistry::instance()
 
15
   {
 
16
      static ResourceHandlerRegistry registry;
 
17
      return registry;
 
18
   }
 
19
 
 
20
   void 
 
21
   ResourceHandlerRegistry::addConstant( const ResourceName &name,
 
22
                                         const Resource &constant )
 
23
   {
 
24
      ResourceHandlerPtr handler( new ConstantResourceHandler( constant ) );
 
25
      add( name, handler );
 
26
   }
 
27
 
 
28
 
 
29
   ResourceId 
 
30
   ResourceHandlerRegistry::add( const ResourceName &name, 
 
31
                                 const ResourceHandlerPtr &handler )
 
32
   {
 
33
      CppTL::Mutex::ScopedLockGuard guard( lock_ );
 
34
      ++nextId_;
 
35
      ResourceData data;
 
36
      data.name_ = name;
 
37
      data.lazyCount_ = 0;
 
38
      data.handler_ = handler;
 
39
      data.id_ = nextId_;
 
40
      resourceById_[nextId_] = data;
 
41
      idsByName_[name] = nextId_;
 
42
      return nextId_;
 
43
   }
 
44
 
 
45
 
 
46
   void 
 
47
   ResourceHandlerRegistry::remove( ResourceId id )
 
48
   {
 
49
      CppTL::Mutex::ScopedLockGuard guard( lock_ );
 
50
 
 
51
      ResourceById::iterator itResource = resourceById_.find( id );
 
52
      if ( itResource == resourceById_.end() )
 
53
      {
 
54
         return;
 
55
      }
 
56
 
 
57
      ResourceData &data = itResource->second;
 
58
      idsByName_.erase( data.name_ );
 
59
      resourceById_.erase( itResource );
 
60
   }
 
61
 
 
62
 
 
63
   ResourceLazyPtr 
 
64
   ResourceHandlerRegistry::getResource( const ResourceName &name )
 
65
   {
 
66
      CppTL::Mutex::ScopedLockGuard guard( lock_ );
 
67
      
 
68
      IdsByName::iterator itId = idsByName_.find( name );
 
69
      if ( itId == idsByName_.end() )
 
70
      {
 
71
         return ResourceLazyPtr();
 
72
      }
 
73
 
 
74
      ResourceId id = itId->second;
 
75
      return ResourceLazyPtr( id );
 
76
   }
 
77
 
 
78
 
 
79
   AcquiredResourceHandlePtr 
 
80
   ResourceHandlerRegistry::obtainResource( ResourceId id )
 
81
   {
 
82
      if ( id == 0 )
 
83
      {
 
84
         return AcquiredResourceHandlePtr();
 
85
      }
 
86
 
 
87
      ResourceInstancePtr instance;
 
88
      ResourceHandler::AccessPolicy access;
 
89
      // Scoped lock to ensure that the retrieval/creation of a resource does
 
90
      // prevent the retrieval/creation of another resource.
 
91
      // Otherwise, dead-lock would occur if one thread held the lock on a resource
 
92
      // while trying to obtain a second one, and the second one would be locked by
 
93
      // another thread trying to obtain the first resource.
 
94
      {
 
95
         CppTL::Mutex::ScopedLockGuard guard( lock_ );
 
96
         ResourceById::iterator itData = resourceById_.find( id );
 
97
         if ( itData == resourceById_.end() )
 
98
         {
 
99
            return AcquiredResourceHandlePtr();   // Resource was removed
 
100
         }
 
101
         ResourceData &data = itData->second;
 
102
         access = data.handler_->accessPolicy();
 
103
         if ( !data.instance_ )  // Resource not instantiated, create it
 
104
         {
 
105
            data.instance_.reset( new ResourceInstance( data.name_,
 
106
                                                        data.handler_ ) );
 
107
         }
 
108
         instance = data.instance_;
 
109
      }
 
110
      // Now that the global lock has been required, we can safely acquire the
 
111
      // resource (which may lock until another thread using the resource is done
 
112
      // with it if the resource does not support concurrent access).
 
113
      return instance->acquire( access );
 
114
   }
 
115
 
 
116
 
 
117
   void 
 
118
   ResourceHandlerRegistry::increaseLazyRef( ResourceId id )
 
119
   {
 
120
      if ( id == 0 )
 
121
      {
 
122
         return;
 
123
      }
 
124
 
 
125
      CppTL::Mutex::ScopedLockGuard guard( lock_ );
 
126
      
 
127
      ResourceById::iterator itResource = resourceById_.find( id );
 
128
      if ( itResource != resourceById_.end() )  // checks that the resource was not removed
 
129
      {
 
130
         ResourceData &data = itResource->second;
 
131
         ++(data.lazyCount_);
 
132
      }
 
133
   }
 
134
 
 
135
 
 
136
   void 
 
137
   ResourceHandlerRegistry::decreaseLazyRef( ResourceId id )
 
138
   {
 
139
      if ( id == 0 )
 
140
      {
 
141
         return;
 
142
      }
 
143
 
 
144
      CppTL::Mutex::ScopedLockGuard guard( lock_ );
 
145
      
 
146
      ResourceById::iterator itResource = resourceById_.find( id );
 
147
      if ( itResource != resourceById_.end() )  // checks that the resource was not removed
 
148
      {
 
149
         ResourceData &data = itResource->second;
 
150
         if ( --(data.lazyCount_) == 0 )
 
151
         {
 
152
            // Last lazy reference discarded, remove the ResourceInstancePtr
 
153
            // that was stored to prevent it from being destroyed.
 
154
            // Only other existing AcquiredResourceHandlePtr will prevent it from being 
 
155
            // destroyed.
 
156
            data.instance_.reset();
 
157
         }
 
158
      }
 
159
   }
 
160
 
 
161
 
 
162
 
 
163
// //////////////////////////////////////////////////////////////////
 
164
// //////////////////////////////////////////////////////////////////
 
165
// class ResourceInstance
 
166
// //////////////////////////////////////////////////////////////////
 
167
// //////////////////////////////////////////////////////////////////
 
168
 
 
169
 
 
170
   ResourceInstance::ResourceInstance( const ResourceName &name,
 
171
                                       const ResourceHandlerPtr &handler )
 
172
      : handler_( handler )
 
173
      , name_( name )
 
174
   {
 
175
      resource_ = handler_->create( name );
 
176
   }
 
177
 
 
178
 
 
179
   ResourceInstance::~ResourceInstance()
 
180
   {
 
181
      handler_->destroy( name_, resource_ );
 
182
   }
 
183
 
 
184
 
 
185
   void 
 
186
   ResourceInstance::setUp()
 
187
   {
 
188
      handler_->setUp( name_, resource_ );
 
189
   }
 
190
 
 
191
 
 
192
   void 
 
193
   ResourceInstance::tearDown()
 
194
   {
 
195
      handler_->setUp( name_, resource_ );
 
196
   }
 
197
 
 
198
 
 
199
   AcquiredResourceHandlePtr
 
200
   ResourceInstance::acquire( ResourceHandler::AccessPolicy access )
 
201
   {
 
202
      AcquiredResourceHandlePtr handle( 
 
203
         new AcquiredResourceHandle( lock_, this, access ) );
 
204
      return handle;
 
205
   }
 
206
 
 
207
 
 
208
// //////////////////////////////////////////////////////////////////
 
209
// //////////////////////////////////////////////////////////////////
 
210
// class AcquiredResourceHandle
 
211
// //////////////////////////////////////////////////////////////////
 
212
// //////////////////////////////////////////////////////////////////
 
213
 
 
214
   AcquiredResourceHandle::AcquiredResourceHandle( 
 
215
                              CppTL::Mutex &lock,
 
216
                              const ResourceInstancePtr &instance,
 
217
                              ResourceHandler::AccessPolicy access )
 
218
      : lock_( lock )
 
219
      , instance_( instance )
 
220
      , access_( access )
 
221
   {  
 
222
      if ( access_ != ResourceHandler::concurrent )
 
223
      {
 
224
         lock_.lock();
 
225
      }
 
226
   }
 
227
 
 
228
   AcquiredResourceHandle::~AcquiredResourceHandle()
 
229
   {
 
230
      if ( access_ != ResourceHandler::concurrent )
 
231
      {
 
232
         lock_.unlock();
 
233
      }
 
234
   }
 
235
 
 
236
 
 
237
   void 
 
238
   AcquiredResourceHandle::setUp()
 
239
   {
 
240
      instance_->setUp();
 
241
   }
 
242
 
 
243
   
 
244
   void 
 
245
   AcquiredResourceHandle::tearDown()
 
246
   {
 
247
      instance_->tearDown();
 
248
   }
 
249
 
 
250
 
 
251
 
 
252
// //////////////////////////////////////////////////////////////////
 
253
// //////////////////////////////////////////////////////////////////
 
254
// class ResourceLazyPtr
 
255
// //////////////////////////////////////////////////////////////////
 
256
// //////////////////////////////////////////////////////////////////
 
257
 
 
258
 
 
259
ResourceLazyPtr &
 
260
ResourceLazyPtr::operator =( const ResourceLazyPtr &other )
 
261
{
 
262
   ResourceId oldId = id_;
 
263
   id_ = other.id_;
 
264
   ResourceHandlerRegistry &registry = ResourceHandlerRegistry::instance();
 
265
   registry.increaseLazyRef( id_ );
 
266
   registry.decreaseLazyRef( oldId );
 
267
 
 
268
   return *this;
 
269
}
 
270
 
 
271
 
 
272
AcquiredResourceHandlePtr 
 
273
ResourceLazyPtr::acquire()
 
274
{
 
275
   if ( isValid() )
 
276
   {
 
277
      ResourceHandlerRegistry::instance().obtainResource( id_ );
 
278
   }
 
279
   return AcquiredResourceHandlePtr();
 
280
}
 
281
 
 
282
 
 
283
} // namespace CppUT