You should be able to take most any of the common Singleton implementations, and modify it to be a manager with CreateNew() and SetActive() functions available.
// Session.h
class ActiveSessionManager;
class Session
{
public:
protected:
Session(){};
void MakeActiveSession();
friend class ActiveSessionManager;
};
// ActiveSessionManager.h
class ActiveSessionManager
{
public:
static Session *GetActiveSession()
{
if ( s_active == nullptr )
{
s_active = new Session();
}
return s_active;
}
static void SetActiveSession( Session *session )
{
s_active = session;
}
static Session *CreateNewSession()
{
return new Session();
}
static Session *CreateNewActiveSession()
{
s_active = CreateNewSession();
return s_active;
}
private:
ActiveSessionManager(){};
static Session *s_active;
};
// I would put these in cpps.
Session *ActiveSessionManager::s_active = nullptr;
void Session::MakeActiveSession()
{
ActiveSessionManager::SetActiveSession( this );
}
In my implementation I only allow ActiveSessionManager to instance Sessions, as it would then be able to keep track of all sessions generated ( the tracking is left as an exercise for the reader ).
You could combine the manager and session into a single class as well, but I find the separation easier to follow.