Singleton.h
Go to the documentation of this file.
1
/* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2
3
This file is part of the TerraLib - a Framework for building GIS enabled applications.
4
5
TerraLib is free software: you can redistribute it and/or modify
6
it under the terms of the GNU Lesser General Public License as published by
7
the Free Software Foundation, either version 3 of the License,
8
or (at your option) any later version.
9
10
TerraLib is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU Lesser General Public License for more details.
14
15
You should have received a copy of the GNU Lesser General Public License
16
along with TerraLib. See COPYING. If not, write to
17
TerraLib Team at <terralib-team@terralib.org>.
18
*/
19
20
/*!
21
\file terralib/common/Singleton.h
22
23
\brief Template support for singleton pattern.
24
*/
25
26
#ifndef __TERRALIB_COMMON_INTERNAL_SINGLETON_H
27
#define __TERRALIB_COMMON_INTERNAL_SINGLETON_H
28
29
// TerraLib
30
#include "
Config.h
"
31
32
// Boost
33
#include <boost/noncopyable.hpp>
34
35
namespace
te
36
{
37
namespace
common
38
{
39
/*!
40
\class Singleton
41
42
\brief Template support for singleton pattern.
43
44
In TerraLib, classes that manage resources like DataSourceManager and ProjectManager
45
are singletons. The basic interface of a singleton is defined by
46
this singleton class. If you want to get access to the
47
singleton instance you can write a code like:
48
49
\code
50
DataSourceManager& dsm = DataSourceManager::getInstance();
51
\endcode
52
53
or
54
55
\code
56
std::vector<DataSource*> datasources = DataSourceManager::getInstance().getDataSourceList();
57
\endcode
58
59
If you want to use this template class in order
60
to create your singleton class, the type T must
61
satisfy the following requirements:
62
<ul>
63
<li>It must have an empty constructor;</li>
64
</ul>
65
You can create a singleton class as follow:
66
\code
67
class MySingletonClass : public te::common::Singleton<MySingletonClass>
68
{
69
friend class te::common::Singleton<MySingletonClass>;
70
71
public:
72
73
// Singleton methods (just as an example).
74
int getId()
75
{
76
return id;
77
}
78
79
...
80
81
protected:
82
83
// Empty constructor
84
MySingletonClass()
85
{
86
...
87
}
88
89
private:
90
91
// My private data
92
int id;
93
94
...
95
};
96
\endcode
97
98
\ingroup common
99
*/
100
template
<
class
T>
class
Singleton
:
public
boost::noncopyable
101
{
102
public
:
103
104
/*!
105
\brief It returns a reference to the singleton instance.
106
107
\return A reference to the singleton instance.
108
*/
109
static
T&
getInstance
();
110
111
protected
:
112
113
/*! \brief The singleton constructor is protected. */
114
Singleton
();
115
116
/*! \brief This will avoid clients trying to release pointers to base class. */
117
virtual
~Singleton
();
118
119
private
:
120
121
// No copy allowed
122
Singleton
(
const
Singleton
& other);
123
Singleton
&
operator=
(
const
Singleton
& other);
124
};
125
126
template
<
class
T>
inline
T&
Singleton<T>::getInstance
()
127
{
128
static
T m_singleton;
// The singleton instance.
129
130
return
m_singleton;
131
}
132
133
template
<
class
T>
inline
Singleton<T>::Singleton
()
134
{
135
}
136
137
template
<
class
T>
inline
Singleton<T>::~Singleton
()
138
{
139
}
140
}
// end namespace common
141
}
// end namespace te
142
143
#endif // __TERRALIB_COMMON_INTERNAL_SINGLETON_H
144
te
TerraLib.
Definition:
AddressGeocodingOp.h:52
te::common::Singleton::Singleton
Singleton()
The singleton constructor is protected.
Definition:
Singleton.h:133
te::common::Singleton::Singleton
Singleton(const Singleton &other)
te::common::Singleton
Template support for singleton pattern.
Definition:
Singleton.h:101
te::common::Singleton::getInstance
static T & getInstance()
It returns a reference to the singleton instance.
Definition:
Singleton.h:126
te::common::Singleton::operator=
Singleton & operator=(const Singleton &other)
te::common::Singleton::~Singleton
virtual ~Singleton()
This will avoid clients trying to release pointers to base class.
Definition:
Singleton.h:137
Config.h
Proxy configuration file for TerraView (see terraview_config.h).
git_release
src
terralib
common
Singleton.h
Generated on Fri Dec 4 2020 09:43:34 for TerraLib by
1.8.20