Simple refactoring of Singleton. Make it non-copyable.
Reviewed By: wraitii Tested By: Stan Differential Revision: https://code.wildfiregames.com/D1564 This was SVN commit r22050.
This commit is contained in:
parent
43758bcb92
commit
43a291a071
@ -1,4 +1,4 @@
|
||||
/* Copyright (C) 2018 Wildfire Games.
|
||||
/* Copyright (C) 2019 Wildfire Games.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
@ -20,8 +20,8 @@
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef L10N_H
|
||||
#define L10N_H
|
||||
#ifndef INCLUDED_L10N
|
||||
#define INCLUDED_L10N
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@ -41,16 +41,7 @@
|
||||
*/
|
||||
class L10n : public Singleton<L10n>
|
||||
{
|
||||
/**
|
||||
* Marks the L10n class as ‘noncopyable’.
|
||||
*
|
||||
* This is required, as the class works as a singleton.
|
||||
*
|
||||
* @sa #NONCOPYABLE(className)
|
||||
*/
|
||||
NONCOPYABLE(L10n);
|
||||
public:
|
||||
|
||||
/**
|
||||
* Creates an instance of L10n.
|
||||
*
|
||||
@ -590,4 +581,4 @@ private:
|
||||
icu::DateFormat* CreateDateTimeInstance(const DateTimeType& type, const icu::DateFormat::EStyle& style, const icu::Locale& locale) const;
|
||||
};
|
||||
|
||||
#endif // L10N_H
|
||||
#endif // INCLUDED_L10N
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2017 Wildfire Games.
|
||||
/* Copyright (c) 2019 Wildfire Games.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
@ -216,7 +216,7 @@ switch(x % 2)
|
||||
*/
|
||||
#define NONCOPYABLE(className) \
|
||||
className(const className&) = delete; \
|
||||
const className& operator=(const className&) = delete
|
||||
className& operator=(const className&) = delete
|
||||
|
||||
#if ICC_VERSION
|
||||
# define ASSUME_ALIGNED(ptr, multiple) __assume_aligned(ptr, multiple)
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* Copyright (C) 2018 Wildfire Games.
|
||||
/* Copyright (C) 2019 Wildfire Games.
|
||||
* This file is part of 0 A.D.
|
||||
*
|
||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
||||
@ -50,12 +50,8 @@ typedef std::vector<CStr> CConfigValueSet;
|
||||
|
||||
#define g_ConfigDB CConfigDB::GetSingleton()
|
||||
|
||||
class CConfigDB: public Singleton<CConfigDB>
|
||||
class CConfigDB : public Singleton<CConfigDB>
|
||||
{
|
||||
static std::map<CStr, CConfigValueSet> m_Map[];
|
||||
static VfsPath m_ConfigFile[];
|
||||
static bool m_HasChanges[];
|
||||
|
||||
public:
|
||||
CConfigDB();
|
||||
|
||||
@ -165,6 +161,11 @@ public:
|
||||
bool WriteValueToFile(EConfigNamespace ns, const CStr& name, const CStr& value, const VfsPath& path);
|
||||
|
||||
bool WriteValueToFile(EConfigNamespace ns, const CStr& name, const CStr& value);
|
||||
|
||||
private:
|
||||
static std::map<CStr, CConfigValueSet> m_Map[];
|
||||
static VfsPath m_ConfigFile[];
|
||||
static bool m_HasChanges[];
|
||||
};
|
||||
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* Copyright (C) 2013 Wildfire Games.
|
||||
/* Copyright (C) 2019 Wildfire Games.
|
||||
* This file is part of 0 A.D.
|
||||
*
|
||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
||||
@ -65,36 +65,6 @@ typedef boost::flyweight<
|
||||
class CProfileNode
|
||||
{
|
||||
NONCOPYABLE(CProfileNode);
|
||||
|
||||
friend class CProfileManager;
|
||||
friend class CProfileNodeTable;
|
||||
|
||||
const char* name;
|
||||
|
||||
int calls_frame_current;
|
||||
int calls_turn_current;
|
||||
RingBuf<int, PROFILE_AMORTIZE_FRAMES> calls_per_frame;
|
||||
RingBuf<int, PROFILE_AMORTIZE_TURNS> calls_per_turn;
|
||||
|
||||
double time_frame_current;
|
||||
double time_turn_current;
|
||||
RingBuf<double, PROFILE_AMORTIZE_FRAMES> time_per_frame;
|
||||
RingBuf<double, PROFILE_AMORTIZE_TURNS> time_per_turn;
|
||||
|
||||
long mallocs_frame_current;
|
||||
long mallocs_turn_current;
|
||||
RingBuf<long, PROFILE_AMORTIZE_FRAMES> mallocs_per_frame;
|
||||
RingBuf<long, PROFILE_AMORTIZE_TURNS> mallocs_per_turn;
|
||||
|
||||
double start;
|
||||
long start_mallocs;
|
||||
int recursion;
|
||||
|
||||
CProfileNode* parent;
|
||||
std::vector<CProfileNode*> children;
|
||||
std::vector<CProfileNode*> script_children;
|
||||
CProfileNodeTable* display_table;
|
||||
|
||||
public:
|
||||
typedef std::vector<CProfileNode*>::iterator profile_iterator;
|
||||
typedef std::vector<CProfileNode*>::const_iterator const_profile_iterator;
|
||||
@ -132,17 +102,40 @@ public:
|
||||
void Call();
|
||||
// Leaves the node. Returns true if the node has actually been left
|
||||
bool Return();
|
||||
|
||||
private:
|
||||
friend class CProfileManager;
|
||||
friend class CProfileNodeTable;
|
||||
|
||||
const char* name;
|
||||
|
||||
int calls_frame_current;
|
||||
int calls_turn_current;
|
||||
RingBuf<int, PROFILE_AMORTIZE_FRAMES> calls_per_frame;
|
||||
RingBuf<int, PROFILE_AMORTIZE_TURNS> calls_per_turn;
|
||||
|
||||
double time_frame_current;
|
||||
double time_turn_current;
|
||||
RingBuf<double, PROFILE_AMORTIZE_FRAMES> time_per_frame;
|
||||
RingBuf<double, PROFILE_AMORTIZE_TURNS> time_per_turn;
|
||||
|
||||
long mallocs_frame_current;
|
||||
long mallocs_turn_current;
|
||||
RingBuf<long, PROFILE_AMORTIZE_FRAMES> mallocs_per_frame;
|
||||
RingBuf<long, PROFILE_AMORTIZE_TURNS> mallocs_per_turn;
|
||||
|
||||
double start;
|
||||
long start_mallocs;
|
||||
int recursion;
|
||||
|
||||
CProfileNode* parent;
|
||||
std::vector<CProfileNode*> children;
|
||||
std::vector<CProfileNode*> script_children;
|
||||
CProfileNodeTable* display_table;
|
||||
};
|
||||
|
||||
class CProfileManager : public Singleton<CProfileManager>
|
||||
{
|
||||
CProfileNode* root;
|
||||
CProfileNode* current;
|
||||
|
||||
bool needs_structural_reset;
|
||||
|
||||
void PerformStructuralReset();
|
||||
|
||||
public:
|
||||
CProfileManager();
|
||||
~CProfileManager();
|
||||
@ -166,6 +159,14 @@ public:
|
||||
|
||||
inline const CProfileNode* GetCurrent() { return( current ); }
|
||||
inline const CProfileNode* GetRoot() { return( root ); }
|
||||
|
||||
private:
|
||||
CProfileNode* root;
|
||||
CProfileNode* current;
|
||||
|
||||
bool needs_structural_reset;
|
||||
|
||||
void PerformStructuralReset();
|
||||
};
|
||||
|
||||
#define g_Profiler CProfileManager::GetSingleton()
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* Copyright (C) 2009 Wildfire Games.
|
||||
/* Copyright (C) 2019 Wildfire Games.
|
||||
* This file is part of 0 A.D.
|
||||
*
|
||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
||||
@ -15,58 +15,59 @@
|
||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* template base class for Singletons
|
||||
*/
|
||||
|
||||
/*
|
||||
USAGE: class myClass : Singleton<myClass>{};
|
||||
|
||||
Modified from http://gamedev.net/reference/articles/article1954.asp
|
||||
*/
|
||||
|
||||
#ifndef INCLUDED_SINGLETON
|
||||
#define INCLUDED_SINGLETON
|
||||
|
||||
#include "lib/debug.h"
|
||||
|
||||
/**
|
||||
* Template base class for singletons.
|
||||
*
|
||||
* Usage:
|
||||
* class MyClass : public Singleton<MyClass> {};
|
||||
* MyClass::GetSingleton().MyMethod();
|
||||
*
|
||||
* Modified from http://gamedev.net/reference/articles/article1954.asp
|
||||
*/
|
||||
template<typename T>
|
||||
class Singleton
|
||||
{
|
||||
static T* ms_singleton;
|
||||
|
||||
NONCOPYABLE(Singleton);
|
||||
public:
|
||||
Singleton()
|
||||
{
|
||||
ENSURE( !ms_singleton );
|
||||
ENSURE(!ms_singleton);
|
||||
ms_singleton = static_cast<T*>(this);
|
||||
}
|
||||
|
||||
~Singleton()
|
||||
{
|
||||
ENSURE( ms_singleton );
|
||||
ms_singleton = 0;
|
||||
ENSURE(ms_singleton);
|
||||
ms_singleton = nullptr;
|
||||
}
|
||||
|
||||
static T& GetSingleton()
|
||||
{
|
||||
ENSURE( ms_singleton );
|
||||
ENSURE(ms_singleton);
|
||||
return *ms_singleton;
|
||||
}
|
||||
|
||||
static T* GetSingletonPtr()
|
||||
{
|
||||
ENSURE( ms_singleton );
|
||||
ENSURE(ms_singleton);
|
||||
return ms_singleton;
|
||||
}
|
||||
|
||||
static bool IsInitialised()
|
||||
{
|
||||
return (ms_singleton != 0);
|
||||
return ms_singleton != nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
static T* ms_singleton;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
T* Singleton<T>::ms_singleton = 0;
|
||||
T* Singleton<T>::ms_singleton = nullptr;
|
||||
|
||||
#endif
|
||||
#endif // INCLUDED_SINGLETON
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* Copyright (C) 2016 Wildfire Games.
|
||||
/* Copyright (C) 2019 Wildfire Games.
|
||||
* This file is part of 0 A.D.
|
||||
*
|
||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
||||
@ -34,13 +34,13 @@ class ScriptEngine : public Singleton<ScriptEngine>
|
||||
public:
|
||||
ScriptEngine()
|
||||
{
|
||||
ENSURE(m_Runtimes.size() == 0 && "JS_Init must be called before any runtimes are created!");
|
||||
ENSURE(m_Runtimes.empty() && "JS_Init must be called before any runtimes are created!");
|
||||
JS_Init();
|
||||
}
|
||||
|
||||
~ScriptEngine()
|
||||
{
|
||||
ENSURE(m_Runtimes.size() == 0 && "All runtimes must be destroyed before calling JS_ShutDown!");
|
||||
ENSURE(m_Runtimes.empty() && "All runtimes must be destroyed before calling JS_ShutDown!");
|
||||
JS_ShutDown();
|
||||
}
|
||||
|
||||
@ -48,7 +48,6 @@ public:
|
||||
void UnRegisterRuntime(const JSRuntime* rt) { m_Runtimes.remove(rt); }
|
||||
|
||||
private:
|
||||
|
||||
std::list<const JSRuntime*> m_Runtimes;
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user