// SPDX-License-Identifier: GPL-2.0 // Collection of objects that will be deleted on application exit. // This feature is needed because many Qt-objects crash if freed // after the application has exited. #ifndef GLOBALS_H #define GLOBALS_H #include #include template T *make_global(Args &&...args); // construct a global object of type T. template T *register_global(T *); // register an already constructed object. returns input. void free_globals(); // call on application exit. frees all global objects. // Implementation // A class with a virtual destructor that will be used to destruct the objects. struct GlobalObjectBase { virtual ~GlobalObjectBase() { } }; template struct GlobalObject : T, GlobalObjectBase { using T::T; // Inherit constructor from actual object. }; void register_global_internal(GlobalObjectBase *); template T *make_global(Args &&...args) { GlobalObject *res = new GlobalObject(std::forward(args)...); register_global_internal(res); return res; } template T *register_global(T *o) { make_global>(o); return o; } #endif