Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleSingleton.cpp

Example for Singleton usage

Author
woelk
/* This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/**
@example ExampleSingleton.cpp
@relates Singleton
@brief Example for Singleton usage
@ingroup g_examples
@author woelk
*/
#include <Base/Common/Singleton.hh>
using namespace BIAS;
using namespace std;
/** \cond POORLY_NAMED_EXAMPLE_CLASSES **/
/* myclass */
class MyClass:public Singleton<MyClass> {
public:
double data;
/// make sure that class MyClass cannot be used as non-singleton by
/// declaring constructors and copy operator private
MyClass() : data(0) {}
MyClass(const MyClass &a) : data(a.data) {}
MyClass& operator=(const MyClass& a) { data = a.data; return *this; }
};
typedef Singleton<MyClass> MyClassSingleton;
void function()
{
// set the singleton with 5
MyClassSingleton::GetInstance()->data = 5;
}
/** \endcond */
/** @author woelk 10/2007 (c) www.vision-n.de */
int main(int argc, char *argv[])
{
cout << "initial data: " << MyClassSingleton::GetInstance()->data << endl;
function();
cout << "after function: " << MyClassSingleton::GetInstance()->data << endl;
cout << "accessing Singleton<MyClass> is identical to MyClassSingleton: "
// does not compile, because constructor of MyClass is private
//MyClass a;
return 0;
}