Some DeletedAtShutdown and Singleton usages


#1

Here’s some code I’m using for my NewLookAndFeel class and also UserSettings class. This is very handy, as its created only once, taking less memory compared to a multi-memory-variable code. All my PNG files get added to the NewLookAndFeel class. So, when the Plugin loads, I just check if it was loaded already, otherwise, I fill up the Image* variables. (Background = new Image(…)

Header:

[code]//-----------------------------------------------------------------------------------------------------------
class JUCE_API NewLookAndFeel : public LookAndFeel, DeletedAtShutdown
{
public:
NewLookAndFeel();
~NewLookAndFeel();

juce_DeclareSingleton (NewLookAndFeel,false)

(…)

static void drawImageSquare(Graphics& g,Image** images,int x,int y,int width,int height,float opacity);

// Extra Variables //
Image* Background[9];
//
bool didLoadSkin;

};[/code]

CPP:

[code]//-----------------------------------------------------------------------------------------------------------
NewLookAndFeel::NewLookAndFeel()
{
didLoadSkin = false;
}

//-----------------------------------------------------------------------------------------------------------
NewLookAndFeel::~NewLookAndFeel()
{
setDefaultLookAndFeel (0);

   for (int x=0; x<9; x++) deleteAndZero(Background[x]);

didLoadSkin = false;

   clearSingletonInstance();

}

//-----------------------------------------------------------------------------------------------------------
void NewLookAndFeel::drawImageSquare(Graphics& g,Image** images,int x,int y,int width,int height,float opacity)
{
g.setTiledImageFill(*images[4],x+images[3]->getWidth(),y+images[1]->getHeight(),opacity);
g.fillRect(x+images[3]->getWidth(),y+images[1]->getHeight(),width-images[3]->getWidth()-images[5]->getWidth(), height-images[1]->getHeight()-images[7]->getHeight());

g.setTiledImageFill(*images[1],x+images[0]->getWidth(),y,opacity);
g.fillRect(x+images[0]->getWidth(),y,width-images[0]->getWidth()-images[2]->getWidth(),images[1]->getHeight());

g.setTiledImageFill(*images[7],x+images[6]->getWidth(),y+height-images[7]->getHeight(),opacity);
g.fillRect(x+images[6]->getWidth(),y+height-images[7]->getHeight(),width-images[6]->getWidth()-images[8]->getWidth(),images[7]->getHeight());

g.setTiledImageFill(*images[3],x,y+images[0]->getHeight(),opacity);
g.fillRect(x,y+images[0]->getHeight(),images[3]->getWidth(),height-images[0]->getHeight()-images[6]->getHeight());

g.setTiledImageFill(*images[5],x+width-images[5]->getWidth(),y,opacity);
g.fillRect(x+width-images[5]->getWidth(),y+images[2]->getHeight(),images[5]->getWidth(),height-images[2]->getHeight()-images[8]->getHeight());

g.setTiledImageFill(*images[0],x,y,opacity);
g.fillRect(x,y,images[0]->getWidth(),images[0]->getHeight());

g.setTiledImageFill(*images[2],x+width-images[2]->getWidth(),y,opacity);
g.fillRect(x+width-images[2]->getWidth(),y,images[2]->getWidth(),images[2]->getHeight());

g.setTiledImageFill(*images[6],x,y+height-images[6]->getHeight(),opacity);
g.fillRect(x,y+height-images[6]->getHeight(),images[6]->getWidth(),images[6]->getHeight());

g.setTiledImageFill(*images[8],x+width-images[8]->getWidth(),y+height-images[8]->getHeight(),opacity);
g.fillRect(x+width-images[8]->getWidth(),y+height-images[8]->getHeight(),images[8]->getWidth(),images[8]->getHeight());

}

juce_ImplementSingleton (NewLookAndFeel)[/code]


#2

Then over the FilterEditor I just do:

newLookAndFeel = NewLookAndFeel::getInstance(); newLookAndFeel->userSettings = userSettings; getLookAndFeel().setDefaultLookAndFeel (newLookAndFeel);

And I don’t worry about deletion, as the DeleteAtShutdown does the job. 8)

Wk


#3

And here’s my FilterSettings (UserSettings) class:

Header:

[code]class JUCE_API FilterSettings : public DeletedAtShutdown
{
public:
FilterSettings();
~FilterSettings();

juce_DeclareSingleton (FilterSettings,false)

PropertySet* defaultSettings;
PropertiesFile* userSettings;

};[/code]

CPP file:

[code]//-----------------------------------------------------------------------------------------------------------
FilterSettings::FilterSettings()
{
userSettings = 0;
defaultSettings = 0;

File checkPath(T("C:"));

if (checkPath.exists())
{
	String dataPath = checkPath.getFullPathName();

	File userFile(dataPath + T("\\Configurations\\TreeView.xml"));
	userSettings = new PropertiesFile(userFile,1000,PropertiesFile::storeAsXML);
	defaultSettings = new PropertySet();

	// Add Default Settings Here //
	defaultSettings->setValue(T("TreeViewShowExt"),false);
	defaultSettings->setValue(T("TreeViewShowPlusMinus"),false);
	
	userSettings->setFallbackPropertySet(defaultSettings);

	if (!userFile.existsAsFile())
	{
		File fPath = File(File::getSpecialLocation (File::userHomeDirectory));
		while (fPath.getParentDirectory() != fPath)  fPath = fPath.getParentDirectory();

		userSettings->setValue(T("TreeViewBookName0"),fPath.getFullPathName());
		userSettings->setValue(T("TreeViewBook0"),fPath.getFullPathName());
		
		userSettings->setValue(T("TreeViewBookName1"),T("Presets"));
		userSettings->setValue(T("TreeViewBook1"),dataPath + T("\\Presets"));
	}
}

}

//-----------------------------------------------------------------------------------------------------------
FilterSettings::~FilterSettings()
{
if (userSettings != 0)
{
userSettings->saveIfNeeded();
userSettings->setFallbackPropertySet(0);
}
//
deleteAndZero(userSettings);
deleteAndZero(defaultSettings);

    clearSingletonInstance();

}

juce_ImplementSingleton (FilterSettings)[/code]


#4

And on my Filter construction class, I just do:

Again, no need to worry about deleting userSettings, as I declared with DeleteAtShutdown.

Wk


#5

Hi,

I guess, the problem I have in storing some plugin settings has something to do with this.
I am using:

copyXmlToBinary (xmlState, destData);

to store settings. I assume (please correct me if i am wrong) that those settings will be stored somewhere in the host project file i.e. in Cubase within the Cubase project file.

If I load a Cubase project file where two instances of my plugin are inserted, it then will mess up some settings i.e. file paths of their file browser or let’s say, they getting
similar.

Should I use singleton classes here?

Thank you!
Joerg


#6

I forgot to add “clearSingletonInstance();” at the destructor of each singleton. :oops:

I just updated the examples above with the correction. 8)


#7