Open FFBoard
Open source force feedback firmware
Singleton.h
Go to the documentation of this file.
1
/*
2
* Singleton.h
3
*
4
* Created on: 15.04.2021
5
* Author: Yannick
6
*/
7
8
#ifndef SRC_SINGLETON_H_
9
#define SRC_SINGLETON_H_
10
#include "main.h"
11
#include "memory"
12
/*
13
* Helper class to enforce singleton pattern and prevent creation of multiple instances by the classchooser
14
* This is intended to be used by the classchooser. It will not prevent you from making multiple instances by calling the constructor directly
15
* Subclasses have to inherit this to prevent classchooser from creating multiple instances
16
*/
17
template
<
typename
T>
18
class
Singleton
{
19
20
protected
:
21
static
T*
instance_ptr
;
22
Singleton
() {}
23
24
~Singleton
(){
25
Singleton<T>::instance_ptr
=
nullptr
;
// Reset pointer if destroyed
26
Singleton<T>::singletonRefCount
= 0;
27
}
28
static
int16_t
singletonRefCount
;
29
30
public
:
31
32
static
T*
getInstance
(){
33
singletonRefCount
++;
34
if
(!
Singleton<T>::instance_ptr
){
35
Singleton<T>::instance_ptr
=
new
T();
36
}
37
return
Singleton<T>::instance_ptr
;
38
}
39
40
Singleton
(
const
Singleton
&) =
delete
;
41
Singleton
&
operator=
(
const
Singleton
) =
delete
;
42
static
int16_t
getSingletonRefCount
(){
return
Singleton<T>::singletonRefCount
;}
43
44
};
45
46
#endif
/* SRC_SINGLETON_H_ */
Singleton
Definition:
Singleton.h:18
Singleton::instance_ptr
static T * instance_ptr
Definition:
Singleton.h:21
Singleton::Singleton
Singleton(const Singleton &)=delete
Singleton::getSingletonRefCount
static int16_t getSingletonRefCount()
Definition:
Singleton.h:42
Singleton::Singleton
Singleton()
Definition:
Singleton.h:22
Singleton::singletonRefCount
static int16_t singletonRefCount
Definition:
Singleton.h:28
Singleton::~Singleton
~Singleton()
Definition:
Singleton.h:24
Singleton::getInstance
static T * getInstance()
Definition:
Singleton.h:32
Singleton::operator=
Singleton & operator=(const Singleton)=delete
Firmware
FFBoard
Inc
Singleton.h
Generated by
1.9.2