Skip to content

Commit

Permalink
Merge branch 'jl/LH-26/singleton-transformations'
Browse files Browse the repository at this point in the history
* jl/LH-26/singleton-transformations:
  FileManager: Make a real singleton.
  WcharMbcsConvertor: fix the singleton.
  NppParameters: make it a true singleton.

Conflicts:
	PowerEditor/src/Parameters.cpp

[#26]

Signed-off-by: Thell Fowler <git@tbfowler.name>
  • Loading branch information
Thell Fowler committed Jan 18, 2010
2 parents ba8ebd7 + 55d585a commit b26cbf7
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 49 deletions.
16 changes: 15 additions & 1 deletion PowerEditor/src/MISC/Common/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include "npp_winver.h"

WcharMbcsConvertor * WcharMbcsConvertor::_pSelf = new WcharMbcsConvertor;
WcharMbcsConvertor * WcharMbcsConvertor::_pSelf = NULL;

// Set a call back with the handle after init to set the path.
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/callbackfunctions/browsecallbackproc.asp
Expand Down Expand Up @@ -236,6 +236,20 @@ generic_string purgeMenuItemString(const TCHAR * menuItemStr, bool keepAmpersand

#define initSize 1024

WcharMbcsConvertor * WcharMbcsConvertor::getInstance()
{
if (!_pSelf)
{
_pSelf = new WcharMbcsConvertor;
}
return _pSelf;
}
void WcharMbcsConvertor::destroyInstance()
{
delete _pSelf;
_pSelf = NULL;
}

WcharMbcsConvertor::WcharMbcsConvertor() :
_multiByteStr(NULL),
_multiByteAllocLen(0),
Expand Down
4 changes: 2 additions & 2 deletions PowerEditor/src/MISC/Common/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ BOOL PathCanonicalize(generic_string& path, generic_string& output);
class WcharMbcsConvertor
{
public:
static WcharMbcsConvertor * getInstance() {return _pSelf;};
static void destroyInstance() {delete _pSelf;};
static WcharMbcsConvertor * getInstance();
static void destroyInstance();

const wchar_t * char2wchar(const char *mbStr, UINT codepage);
const wchar_t * char2wchar(const char *mbcs2Convert, UINT codepage, int *mstart, int *mend);
Expand Down
2 changes: 1 addition & 1 deletion PowerEditor/src/Notepad_plus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ Notepad_plus::Notepad_plus():
// its children's windows handle will be destroyed automatically!
Notepad_plus::~Notepad_plus()
{
(NppParameters::getInstance())->destroyInstance();
NppParameters::destroyInstance();
FileManager::destroyInstance();
WcharMbcsConvertor::destroyInstance();
if (_pTrayIco)
Expand Down
83 changes: 45 additions & 38 deletions PowerEditor/src/Parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ generic_string ThemeSwitcher::getThemeFromXmlFileName(const TCHAR *xmlFullPath)
return themeName;
}

NppParameters * NppParameters::_pSelf = new NppParameters;
NppParameters * NppParameters::_pSelf = NULL;
int FileDialog::_dialogFileBoxId = getWinVersion() < WV_W2K?edt1:cmb13;

NppParameters::NppParameters() :
Expand Down Expand Up @@ -538,6 +538,40 @@ NppParameters::NppParameters() :

NppParameters::~NppParameters()
{
if (_pXmlDoc != NULL)
{
if (_pXmlDoc->isDirty())
_pXmlDoc->SaveFile();
delete _pXmlDoc;
}

if (_pXmlUserDoc != NULL)
{
_pXmlUserDoc->SaveFile();
delete _pXmlUserDoc;
}
if (_pXmlUserStylerDoc)
delete _pXmlUserStylerDoc;

if (_pXmlUserLangDoc)
{
delete _pXmlUserLangDoc;
}
if (_pXmlNativeLangDocA)
delete _pXmlNativeLangDocA;

if (_pXmlToolIconsDoc)
delete _pXmlToolIconsDoc;

if (_pXmlShortcutDoc)
delete _pXmlShortcutDoc;

if (_pXmlContextMenuDoc)
delete _pXmlContextMenuDoc;

if (_pXmlSessionDoc)
delete _pXmlSessionDoc;

for( std::vector<Lang *>::const_iterator it = _langList.begin(), end = _langList.end();
it != end;
++it)
Expand Down Expand Up @@ -971,44 +1005,8 @@ bool NppParameters::load()

void NppParameters::destroyInstance()
{
if (_pXmlDoc != NULL)
{
if (_pXmlDoc->isDirty())
_pXmlDoc->SaveFile();
delete _pXmlDoc;
}

if (_pXmlUserDoc != NULL)
{
_pXmlUserDoc->SaveFile();
delete _pXmlUserDoc;
}
if (_pXmlUserStylerDoc)
delete _pXmlUserStylerDoc;

if (_pXmlUserLangDoc)
{
delete _pXmlUserLangDoc;
}
if (_pXmlNativeLangDocA)
delete _pXmlNativeLangDocA;

if (_pXmlToolIconsDoc)
delete _pXmlToolIconsDoc;

if (_pXmlShortcutDoc)
delete _pXmlShortcutDoc;

if (_pXmlContextMenuDoc)
delete _pXmlContextMenuDoc;

if (_pXmlSessionDoc)
delete _pXmlSessionDoc;

if (_pXmlBlacklistDoc)
delete _pXmlBlacklistDoc;

delete _pSelf;
_pSelf = NULL;
}

void NppParameters::setFontList(HWND hWnd)
Expand Down Expand Up @@ -2468,6 +2466,15 @@ TiXmlNode * NppParameters::getChildElementByAttribut(TiXmlNode *pere, const TCHA
return NULL;
}

NppParameters* NppParameters::getInstance()
{
if(!_pSelf)
{
_pSelf = new NppParameters;
}
return _pSelf;
}

// 2 restes : L_H, L_USER
LangType NppParameters::getLangIDFromStr(const TCHAR *langName)
{
Expand Down
4 changes: 2 additions & 2 deletions PowerEditor/src/Parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -661,12 +661,12 @@ private :
class NppParameters
{
public:
static NppParameters * getInstance() {return _pSelf;};
static NppParameters * getInstance();
static LangType getLangIDFromStr(const TCHAR *langName);
static void destroyInstance();
bool load();
bool reloadLang();
bool reloadStylers(TCHAR *stylePath = NULL);
void destroyInstance();

bool _isTaskListRBUTTONUP_Active;
int L_END;
Expand Down
18 changes: 17 additions & 1 deletion PowerEditor/src/ScintillaComponent/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

const TCHAR UNTITLED_STR[] = TEXT("new ");

FileManager * FileManager::_pSelf = new FileManager();
FileManager * FileManager::_pSelf = NULL;

const int blockSize = 128 * 1024 + 4;

Expand Down Expand Up @@ -348,6 +348,22 @@ void Buffer::setLineUndoState(size_t currentLine, size_t undoLevel, bool isSaved

//filemanager

FileManager* FileManager::getInstance()
{
if (!_pSelf)
{
_pSelf = new FileManager();
}
return _pSelf;
}

void FileManager::destroyInstance()
{
delete _pSelf;
_pSelf = NULL;
}


FileManager::~FileManager()
{
for (std::vector<Buffer *>::iterator it = _buffers.begin(), end = _buffers.end(); it != end; ++ it)
Expand Down
5 changes: 2 additions & 3 deletions PowerEditor/src/ScintillaComponent/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ class FileManager {

bool createEmptyFile(const TCHAR * path);

// JOCE: Get Instance / destroy instance not symmetric. Maybe _pSelf shouldn't be a pointer...
static FileManager * getInstance() {return _pSelf;}
static void destroyInstance() { delete _pSelf; }
static FileManager * getInstance();
static void destroyInstance();

void increaseDocNr() {_nextNewNumber++;};

Expand Down
2 changes: 1 addition & 1 deletion PowerEditor/src/winmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR /*cmdLineAnsi*/, int /*
if (hNotepad_plus)
{
// First of all, destroy static object NppParameters
pNppParameters->destroyInstance();
NppParameters::destroyInstance();
FileManager::destroyInstance();

int sw;
Expand Down

0 comments on commit b26cbf7

Please sign in to comment.