Welcome to mirror list, hosted at ThFree Co, Russian Federation.

util_singleton.h « util « src - github.com/doitsujin/dxvk.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ee5fe56d176817578ab7d0aa7aa2d9f454ee50bb (plain)
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
44
#pragma once

#include "rc/util_rc_ptr.h"

#include "thread.h"

namespace dxvk {

/**
 * \brief Singleton helper
 *
 * Class that manages a dynamically created 
 */
template<typename T>
class Singleton {

public:

  template<typename... Args>
  Rc<T> acquire(Args... constantArgs) {
    std::lock_guard lock(m_mutex);

    if (!(m_useCount++))
      m_object = new T(constantArgs...);

    return m_object;
  }

  void release() {
    std::lock_guard lock(m_mutex);

    if (!(--m_useCount))
      m_object = nullptr;
  }

private:

  dxvk::mutex m_mutex;
  size_t      m_useCount  = 0;
  Rc<T>       m_object    = nullptr;;

};

}