Singleton.hh 1.06 KB
Newer Older
Robert Menzel's avatar
Robert Menzel committed
1
2
3
4
5
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University         //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

6
7
8
9
10
11
12
#ifndef ACGL_BASE_SINGLETON_HH
#define ACGL_BASE_SINGLETON_HH

/*
 * A very simple Singleton as a template.
 * Not thread save etc.
 */
Robert Menzel's avatar
Robert Menzel committed
13

Robert Menzel's avatar
Robert Menzel committed
14
#include <ACGL/ACGL.hh>
Robert Menzel's avatar
Robert Menzel committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <tr1/memory>

namespace ACGL{
namespace Base{

template<typename CLASS>
class Singleton
{
public:
    virtual ~Singleton(void) {}
    
    static std::tr1::shared_ptr<CLASS> the(void)
    {
        if(!spInstance)
            spInstance.reset(new CLASS());
        return(spInstance);
    }
    
protected:
    Singleton(void){}
private:
    Singleton(const Singleton&){}
    
private:
    static std::tr1::shared_ptr<CLASS> spInstance;
    
};

template<typename CLASS>
std::tr1::shared_ptr<CLASS> Singleton<CLASS>::spInstance = std::tr1::shared_ptr<CLASS>();

} // Base
} // ACGL

#endif