Created
August 24, 2023 10:19
-
-
Save ardabbour/b814d0bafa0d50832435268584ebcb60 to your computer and use it in GitHub Desktop.
Defines the ObjectManager class template for managing objects with activation and deactivation capabilities.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file object_manager.h | |
* @brief Defines the ObjectManager class template for managing objects with activation and deactivation capabilities. | |
* @author Abdul Rahman Dabbour | |
* @license MIT | |
*/ | |
#ifndef OBJECT_MANAGER_H | |
#define OBJECT_MANAGER_H | |
#include <memory> | |
#include <tuple> | |
#include <utility> | |
/** | |
* @brief A template class for managing objects with activation and deactivation capabilities. | |
* @tparam T The type of the object to manage. | |
* @tparam Args Variadic template arguments for the constructor of the managed object. | |
*/ | |
template <typename T, typename... Args> class ObjectManager { | |
bool active_{false}; /**< Stores the activation state of the managed object. */ | |
std::unique_ptr<T> object_; /**< Stores the managed object. */ | |
std::tuple<Args...> constructor_args_; /**< Stores the constructor arguments for the managed object. */ | |
public: | |
/** | |
* @brief Constructor for the ObjectManager class. | |
* @param args Arguments to be forwarded to the constructor of the managed object. | |
*/ | |
ObjectManager(Args &&...args) : constructor_args_(std::forward<Args>(args)...) { activate(); } | |
/** | |
* @brief Checks if the managed object is currently active. | |
* @return `true` if the object is active, `false` otherwise. | |
*/ | |
auto isActive() const -> bool { return active_; } | |
/** | |
* @brief Activates the managed object if it is not already active. | |
*/ | |
auto activate() -> void { | |
if (!isActive()) { | |
object_ = createObject(std::index_sequence_for<Args...>{}); | |
active_ = true; | |
} | |
} | |
/** | |
* @brief Deactivates the managed object if it is currently active. | |
*/ | |
auto deactivate() -> void { | |
if (isActive()) { | |
object_.reset(); | |
active_ = false; | |
} | |
} | |
/** | |
* @brief Toggles the activation state of the managed object. | |
*/ | |
auto toggle() -> void { isActive() ? deactivate() : activate(); } | |
private: | |
/** | |
* @brief Helper function to create the managed object using the stored constructor arguments. | |
* @tparam Indices The indices of the constructor arguments. | |
* @param indices Index sequence of the constructor arguments. | |
* @return A unique pointer to the created managed object. | |
*/ | |
template <std::size_t... Indices> | |
auto createObject(std::index_sequence<Indices...> /* indices */) const -> std::unique_ptr<T> { | |
return std::make_unique<T>(std::get<Indices>(constructor_args_)...); | |
} | |
}; | |
/** | |
* @brief Creates an ObjectManager instance for managing objects of type T. | |
* @tparam T The type of the object to manage. | |
* @tparam Args Variadic template arguments for the constructor of the managed object. | |
* @param args Arguments to be forwarded to the constructor of the managed object. | |
* @return An instance of ObjectManager managing objects of type T. | |
*/ | |
template <typename T, typename... Args> auto make_managed(Args &&...args) -> ObjectManager<T, Args...> { | |
return ObjectManager<T, Args...>(std::forward<Args>(args)...); | |
} | |
#endif // OBJECT_MANAGER_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment