ctkSingleton.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Library:   CTK
00004  
00005   Copyright (c) 2010  Kitware Inc.
00006 
00007   Licensed under the Apache License, Version 2.0 (the "License");
00008   you may not use this file except in compliance with the License.
00009   You may obtain a copy of the License at
00010 
00011       http://www.commontk.org/LICENSE
00012 
00013   Unless required by applicable law or agreed to in writing, software
00014   distributed under the License is distributed on an "AS IS" BASIS,
00015   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   See the License for the specific language governing permissions and
00017   limitations under the License.
00018  
00019 =========================================================================*/
00020 
00021 #ifndef __ctkSingleton_h
00022 #define __ctkSingleton_h
00023 
00024 //
00026 //
00029 //
00031 //
00032 
00033 //-----------------------------------------------------------------------------
00035 //
00036 #define CTK_SINGLETON_DECLARE(NAME)      \
00037 static NAME* Instance;                    \
00038 static void classInitialize();            \
00039 static void classFinalize();              \
00040 friend class NAME##Initialize;
00041   
00042 //-----------------------------------------------------------------------------
00045 //
00047 //
00051 #define CTK_SINGLETON_DECLARE_INITIALIZER(EXPORT_DIRECTIVE,NAME)  \
00052 class EXPORT_DIRECTIVE NAME##Initialize                            \
00053 {                                                                  \
00054 public:                                                            \
00055   typedef NAME##Initialize Self;                                   \
00056                                                                    \
00057   NAME##Initialize();                                              \
00058   ~NAME##Initialize();                                             \
00059 private:                                                           \
00060   static unsigned int Count;                                       \
00061 };                                                                 \
00062                                                                    \
00063 static NAME##Initialize NAME##Initializer;
00064 
00065 
00066 //-----------------------------------------------------------------------------
00067 //
00069 //
00072 //
00073 #define CTK_SINGLETON_DEFINE_INITIALIZER(NAME)      \
00074 NAME##Initialize::NAME##Initialize()                \
00075 {                                                   \
00076   if(++Self::Count == 1)                            \
00077     { NAME::classInitialize(); }                    \
00078 }                                                   \
00079                                                     \
00080 NAME##Initialize::~NAME##Initialize()               \
00081 {                                                   \
00082   if(--Self::Count == 0)                            \
00083     { NAME::classFinalize(); }                      \
00084 }                                                   \
00085                                                     \
00086 unsigned int NAME##Initialize::Count;               \
00087 NAME* NAME::Instance;
00088 
00089 
00090 //----------------------------------------------------------------------------
00091 //
00093 //
00094 #define CTK_SINGLETON_DEFINE(NAME)                  \
00095 void NAME::classInitialize()                        \
00096 {                                                   \
00097   Self::Instance = new NAME;                        \
00098 }                                                   \
00099                                                     \
00100 void NAME::classFinalize()                          \
00101 {                                                   \
00102   delete Self::Instance;                            \
00103 }                                                   \
00104                                                     \
00105 CTK_SINGLETON_DEFINE_INITIALIZER(NAME)
00106 
00107 
00108 //----------------------------------------------------------------------------
00109 //
00111 //
00113 //
00114 #define CTK_SINGLETON_DECLARE_PRIVATE(PUB)       \
00115 PUB(const PUB&);                                 \
00116 void operator=(const PUB&);                      \
00117 CTK_DECLARE_PRIVATE(PUB);
00118 
00119 #endif //__ctkSingleton_h
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines