Singleton.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 #include "Singleton.h"
00019 #include "debug/Log.h"
00020 #include <string.h>
00021 
00022 namespace oasys {
00023 
00024 #define MAX_SINGLETONS 64
00025 
00026 SingletonBase** SingletonBase::all_singletons_ = 0;
00027 int             SingletonBase::num_singletons_ = 0;
00028 SingletonBase::Fini SingletonBase::fini_;
00029 
00030 //----------------------------------------------------------------------
00031 SingletonBase::SingletonBase()
00032 {
00033     if (all_singletons_ == 0) {
00034         all_singletons_ = (SingletonBase**)malloc(MAX_SINGLETONS * sizeof(SingletonBase*));
00035         memset(all_singletons_, 0, (MAX_SINGLETONS * sizeof(SingletonBase*)));
00036     }
00037 
00038     if (num_singletons_ < MAX_SINGLETONS)
00039         all_singletons_[num_singletons_++] = this;
00040 }
00041 
00042 
00043 //----------------------------------------------------------------------
00044 SingletonBase::~SingletonBase()
00045 {
00046 }
00047 
00048 //----------------------------------------------------------------------
00049 SingletonBase::Fini::~Fini()
00050 {
00051     if (getenv("OASYS_CLEANUP_SINGLETONS"))
00052     {
00053         for (int i = SingletonBase::num_singletons_ - 1; i >= 0; --i)
00054         {
00055             log_debug_p("/debug",
00056                         "deleting singleton %d (%p)",
00057                         i, SingletonBase::all_singletons_[i]);
00058             
00059             delete SingletonBase::all_singletons_[i];
00060         }
00061     }
00062 
00063     oasys::Log::shutdown();
00064 }
00065 
00066 } // namespace oasys

Generated on Sat Sep 8 08:36:18 2007 for DTN Reference Implementation by  doxygen 1.5.3