# Singleton

<a id="singleton"></a>

# Singleton

```cpp
#include <icy/singleton.h>

template<class S>
class Singleton
```

[Defined in src/base/include/icy/singleton.h:24](https://github.com/auscaster/icey/blob/main/src/base/include/icy/singleton.h#L24)

Helper template class for managing singleton objects allocated on the heap.

## Public Methods

| Return | Name | Description |
|--------|------|-------------|
|  | [`Singleton`](#singleton-1)  | Defaulted constructor. |
|  | [`~Singleton`](#singleton-2)  | Defaulted destructor. |
| `S *` | [`get`](#get-11) `inline` | Returns a pointer to the managed singleton, instantiating it on first call. Thread-safe; protected by an internal mutex. |
| `S *` | [`swap`](#swap-7) `inline` | Replaces the managed singleton with newPtr and returns the previous instance. The caller takes ownership of the returned pointer. Thread-safe; protected by an internal mutex. |
| `void` | [`destroy`](#destroy-1) `inline` | Destroys the managed singleton instance and resets the internal pointer to null. Thread-safe; protected by an internal mutex. |

---

<a id="singleton-1"></a>

### Singleton

```cpp
Singleton() = default
```

[Defined in src/base/include/icy/singleton.h:27](https://github.com/auscaster/icey/blob/main/src/base/include/icy/singleton.h#L27)

Defaulted constructor.

---

<a id="singleton-2"></a>

### ~Singleton

```cpp
~Singleton() = default
```

[Defined in src/base/include/icy/singleton.h:29](https://github.com/auscaster/icey/blob/main/src/base/include/icy/singleton.h#L29)

Defaulted destructor.

---

<a id="get-11"></a>

### get

`inline`

```cpp
inline S * get()
```

[Defined in src/base/include/icy/singleton.h:34](https://github.com/auscaster/icey/blob/main/src/base/include/icy/singleton.h#L34)

Returns a pointer to the managed singleton, instantiating it on first call. Thread-safe; protected by an internal mutex. 
#### Returns
Pointer to the singleton instance (never null).

---

<a id="swap-7"></a>

### swap

`inline`

```cpp
inline S * swap(S * newPtr)
```

[Defined in src/base/include/icy/singleton.h:47](https://github.com/auscaster/icey/blob/main/src/base/include/icy/singleton.h#L47)

Replaces the managed singleton with newPtr and returns the previous instance. The caller takes ownership of the returned pointer. Thread-safe; protected by an internal mutex. 
#### Returns
Previously managed pointer (caller must delete if non-null).

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `newPtr` | `S *` | Replacement instance (may be nullptr). |

---

<a id="destroy-1"></a>

### destroy

`inline`

```cpp
inline void destroy()
```

[Defined in src/base/include/icy/singleton.h:57](https://github.com/auscaster/icey/blob/main/src/base/include/icy/singleton.h#L57)

Destroys the managed singleton instance and resets the internal pointer to null. Thread-safe; protected by an internal mutex.

## Private Attributes

| Return | Name | Description |
|--------|------|-------------|
| `std::unique_ptr< S, Deleter >` | [`_ptr`](#_ptr-1)  |  |
| `std::mutex` | [`_m`](#_m)  |  |

---

<a id="_ptr-1"></a>

### _ptr

```cpp
std::unique_ptr< S, Deleter > _ptr
```

[Defined in src/base/include/icy/singleton.h:70](https://github.com/auscaster/icey/blob/main/src/base/include/icy/singleton.h#L70)

---

<a id="_m"></a>

### _m

```cpp
std::mutex _m
```

[Defined in src/base/include/icy/singleton.h:71](https://github.com/auscaster/icey/blob/main/src/base/include/icy/singleton.h#L71)
