UDK 3.2.7 C/C++ API Reference
|
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 00002 /************************************************************************* 00003 * 00004 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 00005 * 00006 * Copyright 2000, 2010 Oracle and/or its affiliates. 00007 * 00008 * OpenOffice.org - a multi-platform office productivity suite 00009 * 00010 * This file is part of OpenOffice.org. 00011 * 00012 * OpenOffice.org is free software: you can redistribute it and/or modify 00013 * it under the terms of the GNU Lesser General Public License version 3 00014 * only, as published by the Free Software Foundation. 00015 * 00016 * OpenOffice.org is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Lesser General Public License version 3 for more details 00020 * (a copy is included in the LICENSE file that accompanied this code). 00021 * 00022 * You should have received a copy of the GNU Lesser General Public License 00023 * version 3 along with OpenOffice.org. If not, see 00024 * <http://www.openoffice.org/license.html> 00025 * for a copy of the LGPLv3 License. 00026 * 00027 ************************************************************************/ 00028 00029 #if !defined INCLUDED_RTL_INSTANCE_HXX 00030 #define INCLUDED_RTL_INSTANCE_HXX 00031 00032 #include "osl/doublecheckedlocking.h" 00033 #include "osl/getglobalmutex.hxx" 00034 00035 namespace { 00036 00270 template< typename Inst, typename InstCtor, 00271 typename Guard, typename GuardCtor, 00272 typename Data = int, typename DataCtor = int > 00273 class rtl_Instance 00274 { 00275 public: 00276 static inline Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor) 00277 { 00278 #if defined _MSC_VER 00279 static Inst * m_pInstance = 0; 00280 #endif // _MSC_VER 00281 Inst * p = m_pInstance; 00282 if (!p) 00283 { 00284 Guard aGuard(aGuardCtor()); 00285 p = m_pInstance; 00286 if (!p) 00287 { 00288 p = aInstCtor(); 00289 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER(); 00290 m_pInstance = p; 00291 } 00292 } 00293 else 00294 { 00295 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER(); 00296 } 00297 return p; 00298 } 00299 00300 static inline Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor, 00301 DataCtor aDataCtor) 00302 { 00303 #if defined _MSC_VER 00304 static Inst * m_pInstance = 0; 00305 #endif // _MSC_VER 00306 Inst * p = m_pInstance; 00307 if (!p) 00308 { 00309 Data aData(aDataCtor()); 00310 Guard aGuard(aGuardCtor()); 00311 p = m_pInstance; 00312 if (!p) 00313 { 00314 p = aInstCtor(aData); 00315 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER(); 00316 m_pInstance = p; 00317 } 00318 } 00319 else 00320 { 00321 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER(); 00322 } 00323 return p; 00324 } 00325 00326 static inline Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor, 00327 const Data &rData) 00328 { 00329 #if defined _MSC_VER 00330 static Inst * m_pInstance = 0; 00331 #endif // _MSC_VER 00332 Inst * p = m_pInstance; 00333 if (!p) 00334 { 00335 Guard aGuard(aGuardCtor()); 00336 p = m_pInstance; 00337 if (!p) 00338 { 00339 p = aInstCtor(rData); 00340 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER(); 00341 m_pInstance = p; 00342 } 00343 } 00344 else 00345 { 00346 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER(); 00347 } 00348 return p; 00349 } 00350 00351 private: 00352 #if !defined _MSC_VER 00353 static Inst * m_pInstance; 00354 #endif // _MSC_VER 00355 }; 00356 00357 #if !defined _MSC_VER 00358 template< typename Inst, typename InstCtor, 00359 typename Guard, typename GuardCtor, 00360 typename Data, typename DataCtor > 00361 Inst * 00362 rtl_Instance< Inst, InstCtor, Guard, GuardCtor, Data, DataCtor >::m_pInstance 00363 = 0; 00364 #endif // _MSC_VER 00365 00366 } 00367 00368 namespace rtl { 00369 00389 #if defined HAVE_THREADSAFE_STATICS 00390 template<typename T, typename Unique> 00391 class Static { 00392 public: 00399 static T & get() { 00400 static T instance; 00401 return instance; 00402 } 00403 }; 00404 #else 00405 template<typename T, typename Unique> 00406 class Static { 00407 public: 00414 static T & get() { 00415 return *rtl_Instance< 00416 T, StaticInstance, 00417 ::osl::MutexGuard, ::osl::GetGlobalMutex >::create( 00418 StaticInstance(), ::osl::GetGlobalMutex() ); 00419 } 00420 private: 00421 struct StaticInstance { 00422 T * operator () () { 00423 static T instance; 00424 return &instance; 00425 } 00426 }; 00427 }; 00428 #endif 00429 00449 #if defined HAVE_THREADSAFE_STATICS 00450 template<typename T, typename Data, typename Unique> 00451 class StaticWithArg { 00452 public: 00459 static T & get(const Data& rData) { 00460 static T instance(rData); 00461 return instance; 00462 } 00463 00470 static T & get(Data& rData) { 00471 static T instance(rData); 00472 return instance; 00473 } 00474 }; 00475 #else 00476 template<typename T, typename Data, typename Unique> 00477 class StaticWithArg { 00478 public: 00485 static T & get(const Data& rData) { 00486 return *rtl_Instance< 00487 T, StaticInstanceWithArg, 00488 ::osl::MutexGuard, ::osl::GetGlobalMutex, 00489 Data >::create( StaticInstanceWithArg(), 00490 ::osl::GetGlobalMutex(), 00491 rData ); 00492 } 00493 00500 static T & get(Data& rData) { 00501 return *rtl_Instance< 00502 T, StaticInstanceWithArg, 00503 ::osl::MutexGuard, ::osl::GetGlobalMutex, 00504 Data >::create( StaticInstanceWithArg(), 00505 ::osl::GetGlobalMutex(), 00506 rData ); 00507 } 00508 private: 00509 struct StaticInstanceWithArg { 00510 T * operator () (const Data& rData) { 00511 static T instance(rData); 00512 return &instance; 00513 } 00514 00515 T * operator () (Data& rData) { 00516 static T instance(rData); 00517 return &instance; 00518 } 00519 }; 00520 }; 00521 #endif 00522 00531 #if defined HAVE_THREADSAFE_STATICS 00532 template<typename T, typename InitAggregate> 00533 class StaticAggregate { 00534 public: 00542 static T * get() { 00543 static T *instance = InitAggregate()(); 00544 return instance; 00545 } 00546 }; 00547 #else 00548 template<typename T, typename InitAggregate> 00549 class StaticAggregate { 00550 public: 00557 static T * get() { 00558 return rtl_Instance< 00559 T, InitAggregate, 00560 ::osl::MutexGuard, ::osl::GetGlobalMutex >::create( 00561 InitAggregate(), ::osl::GetGlobalMutex() ); 00562 } 00563 }; 00564 #endif 00565 00596 #if defined HAVE_THREADSAFE_STATICS 00597 template<typename T, typename InitData, 00598 typename Unique = InitData, typename Data = T> 00599 class StaticWithInit { 00600 public: 00607 static T & get() { 00608 static T instance = InitData()(); 00609 return instance; 00610 } 00611 }; 00612 #else 00613 template<typename T, typename InitData, 00614 typename Unique = InitData, typename Data = T> 00615 class StaticWithInit { 00616 public: 00623 static T & get() { 00624 return *rtl_Instance< 00625 T, StaticInstanceWithInit, 00626 ::osl::MutexGuard, ::osl::GetGlobalMutex, 00627 Data, InitData >::create( StaticInstanceWithInit(), 00628 ::osl::GetGlobalMutex(), 00629 InitData() ); 00630 } 00631 private: 00632 struct StaticInstanceWithInit { 00633 T * operator () ( Data d ) { 00634 static T instance(d); 00635 return &instance; 00636 } 00637 }; 00638 }; 00639 #endif 00640 } // namespace rtl 00641 00642 #endif // INCLUDED_RTL_INSTANCE_HXX 00643 00644 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */