Singleton.hh 967 Bytes
Newer Older
Robert Menzel's avatar
Robert Menzel committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University         //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

#ifndef SINGLETON_HH
#define SINGLETON_HH

#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