Singleton.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*!
  2. * \file Singleton.h
  3. * \brief modèle de singleton applicable à n'importe quelle classe.
  4. * \author The VLE Development Team
  5. * See the AUTHORS or Authors.txt file
  6. * \version 2.0
  7. * \date 6 juin 2013
  8. */
  9. /*
  10. * Copyright (C) 2012-2013 ULCO http://www.univ-littoral.fr
  11. * Copyright (C) 2012-2013 INRA http://www.inra.fr
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. */
  26. #ifndef SINGLETON_H_
  27. #define SINGLETON_H_
  28. #include <iostream>
  29. #include "Constantes.h"
  30. /*! \class Singleton
  31. *
  32. * \brief est un design pattern dont l'objet est de restreindre l'instanciation d'une classe à un seul objet.
  33. */
  34. template <typename T>
  35. class Singleton
  36. {
  37. protected:
  38. /*!
  39. * \brief Constructeur
  40. *
  41. * Constructeur de la classe Singleton.
  42. * \param void
  43. * \return void
  44. *
  45. */
  46. Singleton () { }
  47. /*!
  48. * \brief Destructeur
  49. *
  50. * Destructeur de la classe Singleton.
  51. */
  52. ~Singleton ()
  53. {
  54. #ifdef DEBUG
  55. std::cout << "destroying singleton." << std::endl;
  56. #endif
  57. }
  58. public:
  59. /*!
  60. * \brief Retourner instance.
  61. *
  62. * Méthode qui crée l'unique instance de la classe si elle n'existe pas encore puis la retourne.
  63. *
  64. * \param void
  65. * \return Singleton
  66. */
  67. static T * getInstance ()
  68. {
  69. if (NULL == _instance)
  70. {
  71. #ifdef DEBUG
  72. std::cout << "creating singleton." << std::endl;
  73. #endif
  74. _instance = new T;
  75. }
  76. else
  77. {
  78. #ifdef DEBUG
  79. std::cout << "singleton already created!" << std::endl;
  80. #endif
  81. }
  82. return (static_cast<T*> (_instance));
  83. }
  84. /*!
  85. * \brief Supprimer instance.
  86. *
  87. * Méthode qui supprime l'unique instance de la classe si elle existe.
  88. *
  89. * \param void
  90. * \return void
  91. */
  92. static void kill ()
  93. {
  94. if (_instance != NULL)
  95. {
  96. delete _instance;
  97. _instance = NULL;
  98. }
  99. }
  100. private:
  101. static T *_instance; /*!< Unique instance */
  102. };
  103. template <typename T>
  104. T *Singleton<T>::_instance = NULL;
  105. #endif /* SINGLETON_H_ */