00001 #ifndef GLW_CONTEXT_H
00002 #define GLW_CONTEXT_H
00003
00004 #include "./noncopyable.h"
00005 #include "./objectdeleter.h"
00006 #include "./buffer.h"
00007 #include "./vertexshader.h"
00008 #include "./geometryshader.h"
00009 #include "./fragmentshader.h"
00010 #include "./program.h"
00011 #include "./renderbuffer.h"
00012 #include "./texture2d.h"
00013 #include "./texturecube.h"
00014 #include "./framebuffer.h"
00015
00016 #include <string>
00017 #include <set>
00018 #include <map>
00019
00020 namespace glw
00021 {
00022
00023 class Context : public detail::NonCopyable
00024 {
00025 friend class detail::ObjectDeleter;
00026
00027 public:
00028
00029 typedef detail::NonCopyable BaseType;
00030 typedef Context ThisType;
00031
00032 Context(void)
00033 : m_acquired (false)
00034 , m_maxUniformBuffers (0)
00035 , m_maxFeedbackBuffers (0)
00036 , m_maxTextureUnits (0)
00037 {
00038 ;
00039 }
00040
00041 virtual ~Context(void)
00042 {
00043 this->release();
00044 }
00045
00046 bool acquire(void)
00047 {
00048
00049 this->release();
00050
00051 this->initializeTargets();
00052
00053 this->m_acquired = true;
00054 (void)glGetError();
00055 return this->m_acquired;
00056 }
00057
00058 void release(void)
00059 {
00060 if (!this->isAcquired()) return;
00061 this->m_acquired = false;
00062 this->terminateTargets();
00063 this->invalidateReferencesToAllObjects();
00064 (void)glGetError();
00065 }
00066
00067 bool isAcquired(void) const
00068 {
00069 return this->m_acquired;
00070 }
00071
00072 bool isValid(void) const
00073 {
00074 return this->isAcquired();
00075 }
00076
00077 BufferHandle createBuffer(const BufferArguments & args)
00078 {
00079 BufferHandle handle = this->createHandle<Buffer>();
00080 handle->object()->create(args);
00081 return handle;
00082 }
00083
00084 BoundVertexBufferHandle bindVertexBuffer(BufferHandle & handle)
00085 {
00086 return this->bind<BoundVertexBuffer>(handle);
00087 }
00088
00089 void unbindVertexBuffer(void)
00090 {
00091 BufferHandle nullHandle;
00092 this->bindVertexBuffer(nullHandle);
00093 }
00094
00095 BoundIndexBufferHandle bindIndexBuffer(BufferHandle & handle)
00096 {
00097 return this->bind<BoundIndexBuffer>(handle);
00098 }
00099
00100 void unbindIndexBuffer(void)
00101 {
00102 BufferHandle nullHandle;
00103 this->bindIndexBuffer(nullHandle);
00104 }
00105
00106 BoundPixelPackBufferHandle bindPixelPackBuffer(BufferHandle & handle)
00107 {
00108 return this->bind<BoundPixelPackBuffer>(handle);
00109 }
00110
00111 void unbindPixelPackBuffer(void)
00112 {
00113 BufferHandle nullHandle;
00114 this->bindPixelPackBuffer(nullHandle);
00115 }
00116
00117 BoundPixelUnpackBufferHandle bindPixelUnpackBuffer(BufferHandle & handle)
00118 {
00119 return this->bind<BoundPixelUnpackBuffer>(handle);
00120 }
00121
00122 void unbindPixelUnpackBuffer(void)
00123 {
00124 BufferHandle nullHandle;
00125 this->bindPixelUnpackBuffer(nullHandle);
00126 }
00127
00128 BoundUniformBufferHandle bindUniformBuffer(BufferHandle & handle, GLuint index, GLintptr offset, GLsizeiptr size)
00129 {
00130 return this->bind<BoundUniformBuffer>(handle, UniformBufferBindingParams(index, offset, size));
00131 }
00132
00133 void unbindUniformBuffer(GLuint index)
00134 {
00135 BufferHandle nullHandle;
00136 this->bindUniformBuffer(nullHandle, index, 0, 0);
00137 }
00138
00139 BoundFeedbackBufferHandle bindFeedbackBuffer(BufferHandle & handle, GLuint index, GLintptr offset, GLsizeiptr size)
00140 {
00141 return this->bind<BoundFeedbackBuffer>(handle, FeedbackBufferBindingParams(index, offset, size));
00142 }
00143
00144 void unbindFeedbackBuffer(GLuint index)
00145 {
00146 BufferHandle nullHandle;
00147 this->bindFeedbackBuffer(nullHandle, index, 0, 0);
00148 }
00149
00150 RenderbufferHandle createRenderbuffer(const RenderbufferArguments & args)
00151 {
00152 RenderbufferHandle handle = this->createHandle<Renderbuffer>();
00153 handle->object()->create(args);
00154 return handle;
00155 }
00156
00157 BoundRenderbufferHandle bindRenderbuffer(RenderbufferHandle & handle)
00158 {
00159 return this->bind<BoundRenderbuffer>(handle);
00160 }
00161
00162 void unbindRenderbuffer(void)
00163 {
00164 RenderbufferHandle nullHandle;
00165 this->bindRenderbuffer(nullHandle);
00166 }
00167
00168 VertexShaderHandle createVertexShader(const VertexShaderArguments & args)
00169 {
00170 VertexShaderHandle handle = this->createHandle<VertexShader>();
00171 handle->object()->create(args);
00172 return handle;
00173 }
00174
00175 BoundVertexShaderHandle bindVertexShader(VertexShaderHandle & handle)
00176 {
00177 return this->bind<BoundVertexShader>(handle, VertexShaderBindingParams());
00178 }
00179
00180 void unbindVertexShader(void)
00181 {
00182 VertexShaderHandle nullHandle;
00183 this->bindVertexShader(nullHandle);
00184 }
00185
00186 GeometryShaderHandle createGeometryShader(const GeometryShaderArguments & args)
00187 {
00188 GeometryShaderHandle handle = this->createHandle<GeometryShader>();
00189 handle->object()->create(args);
00190 return handle;
00191 }
00192
00193 BoundGeometryShaderHandle bindGeometryShader(GeometryShaderHandle & handle)
00194 {
00195 return this->bind<BoundGeometryShader>(handle, GeometryShaderBindingParams());
00196 }
00197
00198 void unbindGeometryShader(void)
00199 {
00200 GeometryShaderHandle nullHandle;
00201 this->bindGeometryShader(nullHandle);
00202 }
00203
00204 FragmentShaderHandle createFragmentShader(const FragmentShaderArguments & args)
00205 {
00206 FragmentShaderHandle handle = this->createHandle<FragmentShader>();
00207 handle->object()->create(args);
00208 return handle;
00209 }
00210
00211 BoundFragmentShaderHandle bindFragmentShader(FragmentShaderHandle & handle)
00212 {
00213 return this->bind<BoundFragmentShader>(handle, FragmentShaderBindingParams());
00214 }
00215
00216 void unbindFragmentShader(void)
00217 {
00218 FragmentShaderHandle nullHandle;
00219 this->bindFragmentShader(nullHandle);
00220 }
00221
00222 ProgramHandle createProgram(const ProgramArguments & args)
00223 {
00224 ProgramHandle handle = this->createHandle<Program>();
00225 handle->object()->create(args);
00226 return handle;
00227 }
00228
00229 BoundProgramHandle bindProgram(ProgramHandle & handle)
00230 {
00231 return this->bind<BoundProgram>(handle, ProgramBindingParams());
00232 }
00233
00234 void unbindProgram(void)
00235 {
00236 ProgramHandle nullHandle;
00237 this->bindProgram(nullHandle);
00238 }
00239
00240 Texture2DHandle createTexture2D(const Texture2DArguments & args)
00241 {
00242 Texture2DHandle handle = this->createHandle<Texture2D>();
00243 handle->object()->create(args);
00244 return handle;
00245 }
00246
00247 BoundTexture2DHandle bindTexture2D(Texture2DHandle & handle, GLint unit)
00248 {
00249 return this->bind<BoundTexture2D>(handle, Texture2DBindingParams(unit));
00250 }
00251
00252 void unbindTexture2D(GLint unit)
00253 {
00254 Texture2DHandle nullHandle;
00255 this->bindTexture2D(nullHandle, unit);
00256 }
00257
00258 TextureCubeHandle createTextureCube(const TextureCubeArguments & args)
00259 {
00260 TextureCubeHandle handle = this->createHandle<TextureCube>();
00261 handle->object()->create(args);
00262 return handle;
00263 }
00264
00265 BoundTextureCubeHandle bindTextureCube(TextureCubeHandle & handle, GLint unit)
00266 {
00267 return this->bind<BoundTextureCube>(handle, TextureCubeBindingParams(unit));
00268 }
00269
00270 void unbindTextureCube(GLint unit)
00271 {
00272 TextureCubeHandle nullHandle;
00273 this->bindTextureCube(nullHandle, unit);
00274 }
00275
00276 FramebufferHandle createFramebuffer(const FramebufferArguments & args)
00277 {
00278 FramebufferHandle handle = this->createHandle<Framebuffer>();
00279 handle->object()->create(args);
00280 return handle;
00281 }
00282
00283 BoundReadFramebufferHandle bindReadFramebuffer(FramebufferHandle & handle)
00284 {
00285 FramebufferHandle nullHandle;
00286 this->bind<BoundReadDrawFramebuffer>(nullHandle, ReadDrawFramebufferBindingParams());
00287 return this->bind<BoundReadFramebuffer>(handle, ReadFramebufferBindingParams());
00288 }
00289
00290 void unbindReadFramebuffer(void)
00291 {
00292 FramebufferHandle nullHandle;
00293 this->bindReadFramebuffer(nullHandle);
00294 }
00295
00296 BoundDrawFramebufferHandle bindDrawFramebuffer(FramebufferHandle & handle)
00297 {
00298 FramebufferHandle nullHandle;
00299 this->bind<BoundReadDrawFramebuffer>(nullHandle, ReadDrawFramebufferBindingParams());
00300 return this->bind<BoundDrawFramebuffer>(handle, DrawFramebufferBindingParams());
00301 }
00302
00303 void unbindDrawFramebuffer(void)
00304 {
00305 FramebufferHandle nullHandle;
00306 this->bindDrawFramebuffer(nullHandle);
00307 }
00308
00309 BoundReadDrawFramebufferHandle bindReadDrawFramebuffer(FramebufferHandle & handle)
00310 {
00311 FramebufferHandle nullHandle;
00312 this->bind<BoundReadFramebuffer>(nullHandle, ReadFramebufferBindingParams());
00313 this->bind<BoundDrawFramebuffer>(nullHandle, DrawFramebufferBindingParams());
00314 return this->bind<BoundReadDrawFramebuffer>(handle, ReadDrawFramebufferBindingParams());
00315 }
00316
00317 void unbindReadDrawFramebuffer(void)
00318 {
00319 FramebufferHandle nullHandle;
00320 this->bindReadDrawFramebuffer(nullHandle);
00321 }
00322
00323 private:
00324
00325 template <typename TObject>
00326 struct ObjectSafeFromObject
00327 {
00328 typedef typename detail::ObjectSafe<TObject>::Type Type;
00329 };
00330
00331 template <typename TObject>
00332 struct ObjectBoundFromObject
00333 {
00334 typedef typename detail::ObjectBound<TObject>::Type Type;
00335 };
00336
00337 template <typename TBinding>
00338 struct ObjectFromBinding
00339 {
00340 typedef typename detail::ObjectBase<TBinding>::Type Type;
00341 };
00342
00343 template <typename TObject>
00344 struct RefCountedPtrFromObject
00345 {
00346 typedef detail::RefCountedObject<TObject, typename detail::DeleterOf<typename detail::RootOf<TObject>::Type>::Type, typename detail::BaseOf<TObject>::Type> Type;
00347 };
00348
00349 template <typename TObject>
00350 struct RefCountedSafeHandleFromObject
00351 {
00352 typedef typename ObjectSafeFromObject<TObject>::Type ObjectSafeType;
00353 typedef detail::RefCountedObject<ObjectSafeType, typename detail::DeleterOf<typename detail::RootOf<ObjectSafeType>::Type>::Type, typename detail::BaseOf<ObjectSafeType>::Type> Type;
00354 };
00355
00356 template <typename TObject>
00357 struct RefCountedBindingHandleFromObject
00358 {
00359 typedef typename ObjectBoundFromObject<TObject>::Type ObjectBoundType;
00360 typedef detail::RefCountedObject<ObjectBoundType, typename detail::DeleterOf<typename detail::RootOf<ObjectBoundType>::Type>::Type, typename detail::BaseOf<ObjectBoundType>::Type> Type;
00361 };
00362
00363 template <typename TBinding>
00364 struct RefCountedBindingHandleFromBinding
00365 {
00366 typedef detail::RefCountedObject<TBinding, typename detail::DeleterOf<typename detail::RootOf<TBinding>::Type>::Type, typename detail::BaseOf<TBinding>::Type> Type;
00367 };
00368
00369 template <typename TObject>
00370 struct PtrFromObject
00371 {
00372 typedef detail::ObjectSharedPointer<TObject, typename detail::DeleterOf<typename detail::RootOf<TObject>::Type>::Type, typename detail::BaseOf<TObject>::Type> Type;
00373 };
00374
00375 template <typename TObject>
00376 struct SafeHandleFromObject
00377 {
00378 typedef typename ObjectSafeFromObject<TObject>::Type ObjectSafeType;
00379 typedef detail::ObjectSharedPointer<ObjectSafeType, typename detail::DeleterOf<typename detail::RootOf<ObjectSafeType>::Type>::Type, typename detail::BaseOf<ObjectSafeType>::Type> Type;
00380 };
00381
00382 template <typename TObject>
00383 struct BindingHandleFromObject
00384 {
00385 typedef typename ObjectBoundFromObject<TObject>::Type ObjectBoundType;
00386 typedef detail::ObjectSharedPointer<ObjectBoundType, typename detail::DeleterOf<typename detail::RootOf<ObjectBoundType>::Type>::Type, typename detail::BaseOf<ObjectBoundType>::Type> Type;
00387 };
00388
00389 template <typename TBinding>
00390 struct SafeHandleFromBinding
00391 {
00392 typedef typename SafeHandleFromObject<typename ObjectFromBinding<TBinding>::Type>::Type Type;
00393 };
00394
00395 template <typename TBinding>
00396 struct BindingHandleFromBinding
00397 {
00398 typedef detail::ObjectSharedPointer<TBinding, typename detail::DeleterOf<typename detail::RootOf<TBinding>::Type>::Type, typename detail::BaseOf<TBinding>::Type> Type;
00399 };
00400
00401 typedef Object ObjectType;
00402 typedef RefCountedPtrFromObject<ObjectType>::Type RefCountedPtrType;
00403 typedef std::map<Object *, RefCountedPtrType *> RefCountedPtrPtrMap;
00404 typedef RefCountedPtrPtrMap::const_iterator RefCountedPtrPtrMapConstIterator;
00405 typedef RefCountedPtrPtrMap::iterator RefCountedPtrPtrMapIterator;
00406 typedef RefCountedPtrPtrMap::value_type RefCountedPtrPtrMapValue;
00407
00408 typedef std::pair<GLenum, GLint> BindingTarget;
00409 typedef BoundObjectHandle::RefCountedObjectType RefCountedBindingType;
00410 typedef std::map<BindingTarget, RefCountedBindingType *> RefCountedBindingPtrMap;
00411 typedef RefCountedBindingPtrMap::const_iterator RefCountedBindingPtrMapConstIterator;
00412 typedef RefCountedBindingPtrMap::iterator RefCountedBindingPtrMapIterator;
00413 typedef RefCountedBindingPtrMap::value_type RefCountedBindingPtrMapValue;
00414
00415 bool m_acquired;
00416 int m_maxUniformBuffers;
00417 int m_maxFeedbackBuffers;
00418 int m_maxTextureUnits;
00419 RefCountedPtrPtrMap m_objects;
00420 RefCountedBindingPtrMap m_bindings;
00421
00422 template <typename TBinding, typename TBindingParams>
00423 void initializeTarget(const TBindingParams & params)
00424 {
00425 typedef TBinding BindingType;
00426 typedef typename RefCountedBindingHandleFromBinding<BindingType>::Type RefCountedBindingHandleType;
00427
00428 const BindingTarget bt = BindingTarget(params.target, params.unit);
00429 RefCountedBindingHandleType * binding = 0;
00430 this->m_bindings.insert(RefCountedBindingPtrMapValue(bt, binding));
00431 }
00432
00433 template <typename TBinding, typename TBindingParams>
00434 void terminateTarget(const TBindingParams & params)
00435 {
00436 typedef TBinding BindingType;
00437 typedef typename SafeHandleFromBinding<BindingType>::Type SafeHandleType;
00438
00439 SafeHandleType nullHandle;
00440 this->bind<BindingType>(nullHandle, params);
00441 }
00442
00443 void initializeTargets(void)
00444 {
00445 this->initializeTarget<BoundVertexBuffer, VertexBufferBindingParams >(VertexBufferBindingParams () );
00446 this->initializeTarget<BoundIndexBuffer, IndexBufferBindingParams >(IndexBufferBindingParams () );
00447 this->initializeTarget<BoundPixelPackBuffer, PixelPackBufferBindingParams >(PixelPackBufferBindingParams () );
00448 this->initializeTarget<BoundPixelUnpackBuffer, PixelUnpackBufferBindingParams >(PixelUnpackBufferBindingParams () );
00449 this->initializeTarget<BoundRenderbuffer, RenderbufferBindingParams >(RenderbufferBindingParams () );
00450 this->initializeTarget<BoundVertexShader, VertexShaderBindingParams >(VertexShaderBindingParams () );
00451 this->initializeTarget<BoundGeometryShader, GeometryShaderBindingParams >(GeometryShaderBindingParams () );
00452 this->initializeTarget<BoundFragmentShader, FragmentShaderBindingParams >(FragmentShaderBindingParams () );
00453 this->initializeTarget<BoundProgram, ProgramBindingParams >(ProgramBindingParams () );
00454 this->initializeTarget<BoundReadFramebuffer, ReadFramebufferBindingParams >(ReadFramebufferBindingParams () );
00455 this->initializeTarget<BoundDrawFramebuffer, DrawFramebufferBindingParams >(DrawFramebufferBindingParams () );
00456 this->initializeTarget<BoundReadDrawFramebuffer, ReadDrawFramebufferBindingParams >(ReadDrawFramebufferBindingParams () );
00457
00458 {
00459 this->m_maxUniformBuffers = 0;
00460 if (GLEW_ARB_uniform_buffer_object)
00461 {
00462 GLint uniformBuffers = 0;
00463 glGetIntegerv(GL_MAX_UNIFORM_BUFFER_BINDINGS, &uniformBuffers);
00464 this->m_maxUniformBuffers = int(uniformBuffers);
00465 for (int i=0; i<this->m_maxUniformBuffers; ++i)
00466 {
00467
00468 this->initializeTarget<BoundUniformBuffer, UniformBufferBindingParams>(UniformBufferBindingParams(GLuint(i), 0, 0));
00469
00470 }
00471 }
00472 }
00473
00474 {
00475 this->m_maxFeedbackBuffers = 0;
00476 if (GLEW_EXT_transform_feedback)
00477 {
00478 GLint feedbackBuffers = 0;
00479 glGetIntegerv(GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS, &feedbackBuffers);
00480 this->m_maxFeedbackBuffers = int(feedbackBuffers);
00481 for (int i=0; i<this->m_maxFeedbackBuffers; ++i)
00482 {
00483 this->initializeTarget<BoundFeedbackBuffer, FeedbackBufferBindingParams>(FeedbackBufferBindingParams(GLuint(i), 0, 0));
00484 }
00485 }
00486 }
00487
00488 {
00489 GLint texUnits = 0;
00490 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &texUnits);
00491 this->m_maxTextureUnits = int(texUnits);
00492 for (int i=0; i<this->m_maxTextureUnits; ++i)
00493 {
00494 this->initializeTarget<BoundTexture2D >(Texture2DBindingParams (GLint(i)));
00495 this->initializeTarget<BoundTextureCube>(TextureCubeBindingParams (GLint(i)));
00496 }
00497 }
00498 }
00499
00500 void terminateTargets(void)
00501 {
00502 this->terminateTarget<BoundVertexBuffer, VertexBufferBindingParams >(VertexBufferBindingParams () );
00503 this->terminateTarget<BoundIndexBuffer, IndexBufferBindingParams >(IndexBufferBindingParams () );
00504 this->terminateTarget<BoundPixelPackBuffer, PixelPackBufferBindingParams >(PixelPackBufferBindingParams () );
00505 this->terminateTarget<BoundPixelUnpackBuffer, PixelUnpackBufferBindingParams >(PixelUnpackBufferBindingParams () );
00506 this->terminateTarget<BoundRenderbuffer, RenderbufferBindingParams >(RenderbufferBindingParams () );
00507 this->terminateTarget<BoundVertexShader, VertexShaderBindingParams >(VertexShaderBindingParams () );
00508 this->terminateTarget<BoundGeometryShader, GeometryShaderBindingParams >(GeometryShaderBindingParams () );
00509 this->terminateTarget<BoundFragmentShader, FragmentShaderBindingParams >(FragmentShaderBindingParams () );
00510 this->terminateTarget<BoundProgram, ProgramBindingParams >(ProgramBindingParams () );
00511 this->terminateTarget<BoundReadFramebuffer, ReadFramebufferBindingParams >(ReadFramebufferBindingParams () );
00512 this->terminateTarget<BoundDrawFramebuffer, DrawFramebufferBindingParams >(DrawFramebufferBindingParams () );
00513 this->terminateTarget<BoundReadDrawFramebuffer, ReadDrawFramebufferBindingParams >(ReadDrawFramebufferBindingParams () );
00514
00515 {
00516 for (int i=0; i<this->m_maxUniformBuffers; ++i)
00517 {
00518 this->terminateTarget<BoundUniformBuffer, UniformBufferBindingParams>(UniformBufferBindingParams(GLuint(i), 0, 0));
00519 }
00520 this->m_maxUniformBuffers = 0;
00521 }
00522
00523 {
00524 for (int i=0; i<this->m_maxFeedbackBuffers; ++i)
00525 {
00526 this->terminateTarget<BoundFeedbackBuffer, FeedbackBufferBindingParams>(FeedbackBufferBindingParams(GLuint(i), 0, 0));
00527 }
00528 this->m_maxFeedbackBuffers = 0;
00529 }
00530
00531 {
00532 for (int i=0; i<this->m_maxTextureUnits; ++i)
00533 {
00534 this->terminateTarget<BoundTexture2D >(Texture2DBindingParams (GLint(i)));
00535 this->terminateTarget<BoundTextureCube>(TextureCubeBindingParams (GLint(i)));
00536 }
00537 this->m_maxTextureUnits = 0;
00538 }
00539 }
00540
00541 template <typename TObject>
00542 TObject * createObject(void)
00543 {
00544 typedef TObject ObjectType;
00545 ObjectType * object = new ObjectType(this);
00546 return object;
00547 }
00548
00549 void destroyObject(Object * object)
00550 {
00551 GLW_ASSERT(object != 0);
00552 object->destroy();
00553 delete object;
00554 }
00555
00556 template <typename TObject>
00557 typename SafeHandleFromObject<TObject>::Type createHandle(void)
00558 {
00559 typedef TObject ObjectType;
00560 typedef typename RefCountedPtrFromObject<ObjectType>::Type RefCountedPtrType;
00561 typedef typename PtrFromObject<ObjectType>::Type PtrType;
00562
00563 typedef typename ObjectSafeFromObject<ObjectType>::Type ObjectSafeType;
00564 typedef typename RefCountedSafeHandleFromObject<ObjectType>::Type RefCountedSafeHandleType;
00565 typedef typename SafeHandleFromObject<TObject>::Type SafeHandleType;
00566
00567 ObjectType * object = this->createObject<ObjectType>();
00568 RefCountedPtrType * refCountedPtr = new RefCountedPtrType(object, typename detail::DeleterOf<Object>::Type());
00569 PtrType ptr = PtrType(refCountedPtr);
00570
00571 ObjectSafeType * objecSafe = new ObjectSafeType(ptr);
00572 RefCountedSafeHandleType * refCountedHandle = new RefCountedSafeHandleType(objecSafe, typename detail::DeleterOf<ObjectSafeType>::Type());
00573 SafeHandleType handle = SafeHandleType(refCountedHandle);
00574
00575 this->m_objects.insert(RefCountedPtrPtrMapValue(object, refCountedPtr));
00576
00577 return handle;
00578 }
00579
00580 void noMoreReferencesTo(Object * object)
00581 {
00582 GLW_ASSERT(object != 0);
00583 RefCountedPtrPtrMapIterator it = this->m_objects.find(object);
00584 GLW_ASSERT(it != this->m_objects.end());
00585 this->m_objects.erase(it);
00586 this->destroyObject(object);
00587 }
00588
00589 void invalidateReferencesToAllObjects(void)
00590 {
00591 for (RefCountedPtrPtrMapIterator it=this->m_objects.begin(); it!=this->m_objects.end(); ++it)
00592 {
00593 Object * object = it->first;
00594 RefCountedPtrType * refPtr = it->second;
00595 refPtr->setNull(false);
00596 this->destroyObject(object);
00597 }
00598 }
00599
00600 template <typename TBinding>
00601 typename BindingHandleFromBinding<TBinding>::Type bind(typename SafeHandleFromBinding<TBinding>::Type & h, const typename detail::ParamsOf<TBinding>::Type & params = typename detail::ParamsOf<TBinding>::Type())
00602 {
00603 typedef TBinding BindingType;
00604 typedef typename detail::ParamsOf<TBinding>::Type BindingParamsType;
00605 typedef typename BindingHandleFromBinding<BindingType>::Type BindingHandleType;
00606 typedef typename RefCountedBindingHandleFromBinding<BindingType>::Type RefCountedBindingHandleType;
00607
00608 const BindingTarget bt = BindingTarget(params.target, params.unit);
00609
00610 RefCountedBindingPtrMapIterator it = this->m_bindings.find(bt);
00611 GLW_ASSERT(it != this->m_bindings.end());
00612
00613 RefCountedBindingHandleType * currentBinding = static_cast<RefCountedBindingHandleType *>(it->second);
00614 if (currentBinding != 0)
00615 {
00616 GLW_ASSERT(!currentBinding->isNull());
00617
00618
00619 if (h.isNull()) currentBinding->object()->unbind();
00620 currentBinding->setNull(true);
00621 currentBinding->unref();
00622 currentBinding = 0;
00623 it->second = 0;
00624 }
00625
00626 if (h.isNull()) return BindingHandleType();
00627
00628 BindingType * binding = new BindingType(h, params);
00629 RefCountedBindingHandleType * newBinding = new RefCountedBindingHandleType(binding, typename detail::DeleterOf<BindingType>::Type());
00630 newBinding->ref();
00631 newBinding->object()->bind();
00632 it->second = newBinding;
00633
00634 return BindingHandleType(newBinding);
00635 }
00636 };
00637
00638 namespace detail
00639 {
00640
00641 inline void ObjectDeleter :: operator () (Object * object) const
00642 {
00643 if (object == 0) return;
00644 object->context()->noMoreReferencesTo(object);
00645 }
00646
00647 };
00648
00649 };
00650
00651 #endif // GLW_CONTEXT_H