I'm now stuck in the server I am programming, simply because I do not seem to find a good and realiable way to handle my "single object" classes, I've attempted the Singleton pattern but it is just plain ugly to have to type everything over and over again.
I have this class "TConnectionManager" which basically handles all connection related code to the connections active to my server.
Singleton Pattern - connections.h
Singleton destructor would delete all connections left in "Connections" variable
class TConnectionManager
{
public:
std::list<TConnection*> Connections;
private:
std::mutex ConnectionsLock;
public:
static TConnectionManager& getInstance()
{
static TConnectionManager instance;
return instance;
}
TConnectionManager() = default;
~TConnectionManager();
// non-copyable
TConnectionManager(const TConnectionManager&) = delete;
TConnectionManager& operator=(const TConnectionManager&) = delete;
TConnection* CreateConnection(boost::asio::io_service& IOService);
void ReleaseConnection(TConnection* Connection);
void ProcessConnections();
};
For me to have to type "getInstance" over and over again is getting frustrating as it is, so I tried to search around many places and could not come up with a better solution, and there's also many different point of views.
I have attempted to do this, which seems way cleaner:
C Like - connections.h
Without a real destructor, I'd have to call the "ExitConnections" method at the end of the program to delete all remaining active connections, or set the std::atexit method
extern std::list<TConnection*> Connections;
extern std::mutex ConnectionsLock;
TConnection* CreateConnection(boost::asio::io_service& IOService);
void ReleaseConnection(TConnection* Connection);
void ProcessConnections();
void ExitConnections(); // Destructor alike function which deletes all connections
Also attempted to have a static class "namespace"
Static methods and variables (namespace) - connections.h
Same approach as for the C-Like methods and variables, use ExitConnections method act as a destructor to delete all remaining active connections
class TConnectionManager
{
public:
static std::list<TConnection*> Connections;
private:
static std::mutex ConnectionsLock;
public:
static TConnection* CreateConnection(boost::asio::io_service& IOService);
static void ReleaseConnection(TConnection* Connection);
static void ProcessConnections();
static void ExitConnections();
};
So I don't know which of the three ways later mentioned is the best approach to handle this situation I am stuck in.