44 lines
1.0 KiB
C++
44 lines
1.0 KiB
C++
/**
|
|
* @file utils.hpp
|
|
* @author Gustice
|
|
* @brief Template
|
|
*
|
|
* @copyright Copyright (c) 2024
|
|
*/
|
|
#pragma once
|
|
|
|
#include <cassert>
|
|
#include <exception>
|
|
#include <cstdint>
|
|
|
|
/**
|
|
* @brief Base for constructs that allow only one concurrently active instance
|
|
*
|
|
* @tparam T
|
|
* @note needs a `T * T::SingleActive<T>::active{};` definition in source
|
|
*/
|
|
template <typename T>
|
|
class SingleActive {
|
|
static bool active;
|
|
|
|
public:
|
|
/// @brief Constructor, memorizes if already on instance was successfully build
|
|
SingleActive() {
|
|
if (active) {
|
|
throw "Instance already active";
|
|
}
|
|
assert(active == false && "Constructor my only be run if no instance active");
|
|
active = true;
|
|
}
|
|
|
|
/// @brief Destructor, clears memory of existing instance
|
|
~SingleActive() {
|
|
assert(active == true && "Destructor my only be run on active instance");
|
|
active = false;
|
|
}
|
|
|
|
// Disable move and assignment
|
|
SingleActive(SingleActive &other) = delete;
|
|
void operator=(const SingleActive &) = delete;
|
|
};
|