diff options
author | Jacek Antonelli | 2009-04-30 13:04:20 -0500 |
---|---|---|
committer | Jacek Antonelli | 2009-04-30 13:07:16 -0500 |
commit | ca8149ca6d157eb4b5fc8ba0e5ba3a6e56f72e7e (patch) | |
tree | 8348301d0ac44a524f1819b777686bf086907d76 /linden/indra/llcommon/llmemory.h | |
parent | Second Life viewer sources 1.22.11 (diff) | |
download | meta-impy-ca8149ca6d157eb4b5fc8ba0e5ba3a6e56f72e7e.zip meta-impy-ca8149ca6d157eb4b5fc8ba0e5ba3a6e56f72e7e.tar.gz meta-impy-ca8149ca6d157eb4b5fc8ba0e5ba3a6e56f72e7e.tar.bz2 meta-impy-ca8149ca6d157eb4b5fc8ba0e5ba3a6e56f72e7e.tar.xz |
Second Life viewer sources 1.23.0-RC
Diffstat (limited to '')
-rw-r--r-- | linden/indra/llcommon/llmemory.h | 62 |
1 files changed, 34 insertions, 28 deletions
diff --git a/linden/indra/llcommon/llmemory.h b/linden/indra/llcommon/llmemory.h index 7b3426f..b5c0711 100644 --- a/linden/indra/llcommon/llmemory.h +++ b/linden/indra/llcommon/llmemory.h | |||
@@ -17,7 +17,8 @@ | |||
17 | * There are special exceptions to the terms and conditions of the GPL as | 17 | * There are special exceptions to the terms and conditions of the GPL as |
18 | * it is applied to this Source Code. View the full text of the exception | 18 | * it is applied to this Source Code. View the full text of the exception |
19 | * in the file doc/FLOSS-exception.txt in this software distribution, or | 19 | * in the file doc/FLOSS-exception.txt in this software distribution, or |
20 | * online at http://secondlifegrid.net/programs/open_source/licensing/flossexception | 20 | * online at |
21 | * http://secondlifegrid.net/programs/open_source/licensing/flossexception | ||
21 | * | 22 | * |
22 | * By copying, modifying or distributing this software, you acknowledge | 23 | * By copying, modifying or distributing this software, you acknowledge |
23 | * that you have read and understood your obligations described above, | 24 | * that you have read and understood your obligations described above, |
@@ -409,53 +410,58 @@ protected: | |||
409 | // | 410 | // |
410 | // class Foo: public LLSingleton<Foo>{}; | 411 | // class Foo: public LLSingleton<Foo>{}; |
411 | // | 412 | // |
412 | // Foo* instance = Foo::getInstance(); | 413 | // Foo& instance = Foo::instance(); |
413 | // | 414 | // |
414 | // The second way is to define a seperate class that exposes the singleton | 415 | // The second way is to use the singleton class directly, without inheritance: |
415 | // interface: | ||
416 | // | 416 | // |
417 | // class FooSingleton: public LLSingleton<Foo>{}; | 417 | // typedef LLSingleton<Foo> FooSingleton; |
418 | // | 418 | // |
419 | // Foo* instance = FooSingleton::getInstance(); | 419 | // Foo& instance = FooSingleton::instance(); |
420 | // | ||
421 | // In this case, the class being managed as a singleton needs to provide an | ||
422 | // initSingleton() method since the LLSingleton virtual method won't be | ||
423 | // available | ||
420 | // | 424 | // |
421 | // As currently written, it is not thread-safe. | 425 | // As currently written, it is not thread-safe. |
422 | #if LL_WINDOWS && _MSC_VER < 1400 // this is Visual C++ 2003 or earlier | ||
423 | // workaround for VC7 compiler bug | ||
424 | // adapted from http://www.codeproject.com/KB/tips/VC2003MeyersSingletonBug.aspx | ||
425 | // our version doesn't introduce a nested struct so that you can still declare LLSingleton<MyClass> | ||
426 | // a friend and hide your constructor | ||
427 | |||
428 | template <typename T> | 426 | template <typename T> |
429 | class LLSingleton | 427 | class LLSingleton |
430 | { | 428 | { |
431 | public: | 429 | public: |
432 | static T* getInstance() | 430 | virtual ~LLSingleton() {} |
431 | #ifdef LL_MSVC7 | ||
432 | // workaround for VC7 compiler bug | ||
433 | // adapted from http://www.codeproject.com/KB/tips/VC2003MeyersSingletonBug.aspx | ||
434 | // our version doesn't introduce a nested struct so that you can still declare LLSingleton<MyClass> | ||
435 | // a friend and hide your constructor | ||
436 | static T* getInstance() | ||
433 | { | 437 | { |
434 | LLSingleton<T> singleton; | 438 | LLSingleton<T> singleton; |
435 | return singleton.get(); | 439 | return singleton.vsHack(); |
436 | } | ||
437 | private: | ||
438 | T* get() | ||
439 | { | ||
440 | static T instance; | ||
441 | return &instance; | ||
442 | } | 440 | } |
443 | 441 | ||
444 | }; | 442 | T* vsHack() |
445 | #else | 443 | #else |
446 | |||
447 | template <typename T> | ||
448 | class LLSingleton | ||
449 | { | ||
450 | public: | ||
451 | static T* getInstance() | 444 | static T* getInstance() |
445 | #endif | ||
452 | { | 446 | { |
453 | static T instance; | 447 | static T instance; |
448 | static bool needs_init = true; | ||
449 | if (needs_init) | ||
450 | { | ||
451 | needs_init = false; | ||
452 | instance.initSingleton(); | ||
453 | } | ||
454 | return &instance; | 454 | return &instance; |
455 | } | 455 | } |
456 | }; | ||
457 | 456 | ||
458 | #endif | 457 | static T& instance() |
458 | { | ||
459 | return *getInstance(); | ||
460 | } | ||
461 | |||
462 | private: | ||
463 | virtual void initSingleton() {} | ||
464 | }; | ||
459 | 465 | ||
460 | //---------------------------------------------------------------------------- | 466 | //---------------------------------------------------------------------------- |
461 | 467 | ||