Singleton.hpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00026 //----------------------------------------------------------------------
00027 #ifndef ICL_CORE_SINGLETON_HPP_INCLUDED
00028 #define ICL_CORE_SINGLETON_HPP_INCLUDED
00029 
00030 #include "Singleton.h"
00031 
00032 namespace icl_core {
00033 
00034 template
00035 <class T, template <class> class TCreationPolicy,
00036  template <class> class TLifetimePolicy, template <class> class TThreadingModel>
00037 T& Singleton<T, TCreationPolicy, TLifetimePolicy, TThreadingModel>::instance()
00038 {
00039   T *temp = m_instance;
00040   TThreadingModel<T>::memoryBarrier();
00041   if (temp == NULL)
00042   {
00043     typename TThreadingModel<T>::Guard guard(m_lock);
00044     temp = m_instance;
00045     if (temp == NULL)
00046     {
00047       if (m_destroyed)
00048       {
00049         TLifetimePolicy<T>::onDeadReference();
00050         m_destroyed = false;
00051       }
00052       temp = TCreationPolicy<T>::create();
00053       TThreadingModel<T>::memoryBarrier();
00054       m_instance = temp;
00055       TLifetimePolicy<T>::scheduleDestruction(&destroySingleton);
00056     }
00057   }
00058   return *m_instance;
00059 }
00060 
00061 template
00062 <class T, template <class> class TCreationPolicy,
00063  template <class> class TLifetimePolicy, template <class> class TThreadingModel>
00064 void Singleton<T, TCreationPolicy, TLifetimePolicy, TThreadingModel>::destroySingleton()
00065 {
00066   TCreationPolicy<T>::destroy(m_instance);
00067   m_instance = NULL;
00068   m_destroyed = true;
00069 }
00070 
00071 template
00072 <class T, template <class> class TCreationPolicy,
00073  template <class> class TLifetimePolicy, template <class> class TThreadingModel>
00074 T *Singleton<T, TCreationPolicy, TLifetimePolicy, TThreadingModel>::m_instance = NULL;
00075 
00076 template
00077 <class T, template <class> class TCreationPolicy,
00078  template <class> class TLifetimePolicy, template <class> class TThreadingModel>
00079 bool Singleton<T, TCreationPolicy, TLifetimePolicy, TThreadingModel>::m_destroyed = false;
00080 
00081 template
00082 <class T, template <class> class TCreationPolicy,
00083  template <class> class TLifetimePolicy, template <class> class TThreadingModel>
00084 typename TThreadingModel<T>::Lock Singleton<T, TCreationPolicy, TLifetimePolicy, TThreadingModel>::m_lock;
00085 
00086 }
00087 
00088 #endif


fzi_icl_core
Author(s):
autogenerated on Tue Aug 8 2017 02:28:04