Implemented singleton.

This commit is contained in:
hiker
2017-04-23 23:36:02 +10:00
parent 1d7aa7a2f5
commit bff3989bfc
3 changed files with 44 additions and 8 deletions

View File

@@ -1516,12 +1516,12 @@ int main(int argc, char *argv[] )
{
CommandLine::init(argc, argv);
Steam *steam = new Steam();
bool steam_avail = steam->isSteamAvailable();
std::string id = steam->getSteamID();
std::string name = steam->getUserName();
int n = steam->saveAvatarAs("test.png");
delete steam;
Steam::create();
bool steam_avail = Steam::get()->isSteamAvailable();
std::string id = Steam::get()->getSteamID();
std::string name = Steam::get()->getUserName();
int n = Steam::get()->saveAvatarAs("test.png");
Steam::destroy();
CrashReporting::installHandlers();

View File

@@ -24,6 +24,9 @@
# include <windows.h>
#endif
Steam * Steam::m_steam = NULL;
// ----------------------------------------------------------------------------
Steam::Steam()
{
m_steam_available = false;

View File

@@ -22,6 +22,7 @@
# include <windows.h>
#endif
#include <assert.h>
#include <string>
#include <vector>
@@ -34,6 +35,9 @@
class Steam
{
private:
/** Singleton pointer. */
static Steam *m_steam;
/** True if a connection to steam was made successfully. */
bool m_steam_available;
@@ -42,7 +46,7 @@ private:
/** Unique steam id. */
std::string m_steam_id;
#ifdef WIN32
// Various handles for the window pipes
HANDLE m_child_stdin_read;
@@ -56,9 +60,38 @@ private:
std::string decodeString(const std::string &s);
std::string sendCommand(const std::string &command);
std::string getLine();
public:
Steam();
~Steam();
public:
/** Creates a singleton. */
static void create()
{
assert(!m_steam);
m_steam = new Steam();
} // create;
// ------------------------------------------------------------------------
/** Returns the singleton pf the Steam class. */
static Steam *get()
{
assert(m_steam);
return m_steam;
} // get
// ------------------------------------------------------------------------
/** Destroys the singleton of the Steam class. */
static void destroy()
{
assert(m_steam);
delete m_steam;
m_steam = NULL;
} // destroy
// ------------------------------------------------------------------------
const std::string& getUserName();
const std::string& getSteamID();
int saveAvatarAs(const std::string &filename);