src/core/singleton.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 2834 1aab57845b07
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
mathieu@526
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
mathieu@526
     2
/*
mathieu@526
     3
 * Copyright (c) 2007 INRIA
mathieu@526
     4
 *
mathieu@526
     5
 * This program is free software; you can redistribute it and/or modify
mathieu@526
     6
 * it under the terms of the GNU General Public License version 2 as
mathieu@526
     7
 * published by the Free Software Foundation;
mathieu@526
     8
 *
mathieu@526
     9
 * This program is distributed in the hope that it will be useful,
mathieu@526
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@526
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@526
    12
 * GNU General Public License for more details.
mathieu@526
    13
 *
mathieu@526
    14
 * You should have received a copy of the GNU General Public License
mathieu@526
    15
 * along with this program; if not, write to the Free Software
mathieu@526
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@526
    17
 *
mathieu@526
    18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
mathieu@526
    19
 */
mathieu@526
    20
#ifndef SINGLETON_H
mathieu@526
    21
#define SINGLETON_H
mathieu@526
    22
mathieu@526
    23
namespace ns3 {
mathieu@526
    24
mathieu@3190
    25
/**
mathieu@3190
    26
 * \brief a template singleton
mathieu@3190
    27
 *
mathieu@3190
    28
 * This template class can be used to implement the singleton pattern.
mathieu@3190
    29
 * The underlying object will be destroyed automatically when the process
mathieu@3190
    30
 * exits. Note that, if you call Singleton::Get again after the object has
mathieu@3190
    31
 * been destroyed, the object will be re-created which will result in a
mathieu@3190
    32
 * memory leak as reported by most memory leak checkers. It is up to the
mathieu@3190
    33
 * user to ensure that Singleton::Get is never called from a static variable
mathieu@3190
    34
 * finalizer.
mathieu@3190
    35
 */
mathieu@526
    36
template <typename T>
mathieu@526
    37
class Singleton
mathieu@526
    38
{
mathieu@526
    39
public:
mathieu@526
    40
  static T *Get (void);
mathieu@526
    41
mathieu@526
    42
};
mathieu@526
    43
mathieu@526
    44
} // namespace ns3
mathieu@526
    45
mathieu@526
    46
namespace ns3 {
mathieu@526
    47
mathieu@526
    48
template <typename T>
mathieu@526
    49
T *
mathieu@526
    50
Singleton<T>::Get (void)
mathieu@526
    51
{
mathieu@526
    52
  static T object;
mathieu@526
    53
  return &object;
mathieu@526
    54
}
mathieu@526
    55
mathieu@526
    56
mathieu@526
    57
} // namespace ns3
mathieu@526
    58
mathieu@526
    59
#endif /* SINGLETON_H */