fixed_typemap: implement need<> method

It may be used in constructors of other objects to assert a dependency.
It also helps to ensure reverse destruction order of that dependency.
This commit is contained in:
Nekotekina 2020-02-26 20:43:27 +03:00
parent b35a5982e8
commit 5e59f18720
2 changed files with 12 additions and 5 deletions

View File

@ -29,11 +29,8 @@ struct vfs_manager
bool vfs::mount(std::string_view vpath, std::string_view path)
{
if (!g_fxo->get<vfs_manager>())
{
// Init (TODO)
g_fxo->init<vfs_manager>();
}
// Workaround
g_fxo->need<vfs_manager>();
const auto table = g_fxo->get<vfs_manager>();

View File

@ -148,6 +148,16 @@ namespace stx
}
}
// Check if object is not initialized but shall be initialized first (to use in initializing other objects)
template <typename T>
void need() noexcept
{
if (!get<T>())
{
init<T>();
}
}
// Explicitly (re)initialize object of type T possibly with dynamic type As and arguments
template <typename T, typename As = T, typename... Args>
As* init(Args&&... args) noexcept