J
Jim Langston
I understand the rule of three, that if I have a custom constructor, copy or
destructor I probably need the other 2. My class object definately has a
custom constructor and destructor, but I'm fuzzy on copy.
Could someone help me out with this class? Shown are the variables,
constructors and destructor only (rest snipped out to preserve space). Yes,
I realize this is fairly ugly code, any suggestions/critiques on that would
be appreciated also. I know some won't like the fact it's called
CGUIElement and not GUIElement.
I guess my main question is what to do about the TEXTURE* (all caps is from
library I'm using, not my idea) and how to handle them if it's copied. In
theory I guess I would only like to accept a shallow copy with the
confirment that the original is not used anymore.
Currently they are being created like this:
Client.GUIElements[Client.GUI_BITMAPS] =
new CGUIElement( ClosedFileName, jColor(255, 0, 240, 255), GUI_List,
726, 674,
FileNames, 85, 44 );
and I can't think of anywhere they would actually be copied, but am not
sure.
How would I go about disabling copies so that this class could never be
copied since I don't have a copy override?
Any advice is appreciated.
// Header //
class CGUIElement
{
private:
TEXTURE* OpenBitmap; // Bitmap to show when GUI is "Open"
TEXTURE* ClosedBitmap; // Show when GUI is not "Open" (or ExtraBitmaps)
std::vector<TEXTURE*> ExtraBitmaps; // Used when more than 2 bitmaps are
needed
GUIType Type; // Kind of element this is
int ListIndex; // If type List what bitmap to show when open?
int NotifyMsg; // Some Observer has requested we send this info
// on Notifies
unsigned long BitmapX, // Where to display this bitmap
BitmapY;
unsigned int Width, // Size of bitmap
Height;
unsigned int X1, Y1, X2, Y2; // Used for clickable selection
unsigned int TextX, // Just to store/return text x/y
TextY,
TextX2,
TextY2,
TextWidth, // Window Max Text Length
TextHeight; // Window Max Text Lines
bool IsLoaded; // Will normall be true, unless error loading
bool IsOpen; // Is the attached window, or this window "Open"?
public:
CGUIElement( std::string OpenFileName, std::string ClosedFileName,
JCOLOR ColorKey,
GUIType WhatType,
unsigned long X, unsigned long Y,
unsigned int ThisX1 = 0, unsigned int ThisY1 = 0,
unsigned int ThisX2 = 0, unsigned int ThisY2 = 0,
unsigned int textX = 0, unsigned int textY = 0,
unsigned int textX2 = 0, unsigned int textY2 = 0);
virtual ~CGUIElement();
};
// CPP file //
CGUIElement::CGUIElement( std::string OpenFileName, std::string
ClosedFileName,
JCOLOR ColorKey,
GUIType WhatType,
unsigned long X, unsigned long Y,
unsigned int inX1, unsigned int inY1,
unsigned int inX2, unsigned int inY2,
unsigned int textX, unsigned int textY,
unsigned int textX2, unsigned int textY2) : OpenNotifier(this),
OpenObserver(this),
CloseNotifier(this), CloseObserver(this),
CloseOnOpenNotifier(this), CloseOnOpenObserver(this),
IsOpen(false), IsLoaded(false),
NotifyMsg(0),
Type(WhatType),
BitmapX(X), BitmapY(Y),
X1(inX1), Y1(inY1),
X2(inX2), Y2(inY2),
TextX(textX), TextY(textY),
TextX2(textX2), TextY2(textY2),
TextWidth(0), TextHeight(0)
{
OpenBitmap = (TEXTURE*) NULL;
ClosedBitmap = (TEXTURE*) NULL;
OpenBitmap = new TEXTURE;
jLoad_Texture(OpenBitmap, const_cast<char*>( OpenFileName.c_str() ),
false, ColorKey, 0);
if ( ! OpenBitmap->Success )
{
IsLoaded = false;
delete OpenBitmap;
OpenBitmap = (TEXTURE*) NULL;
ClosedBitmap = (TEXTURE*) NULL;
}
else if ( ClosedFileName.length() > 0)
{
ClosedBitmap = new TEXTURE;
jLoad_Texture(ClosedBitmap, const_cast<char*>(
ClosedFileName.c_str() ), false, ColorKey, 0);
if ( ! ClosedBitmap->Success )
{
// Couldn't load 2nd bitmap, unload 1st
jRelease_Texture(OpenBitmap);
delete OpenBitmap;
delete ClosedBitmap;
ClosedBitmap = (TEXTURE*) NULL;
OpenBitmap = (TEXTURE*) NULL;
IsLoaded = false;
}
else
IsLoaded = true;
}
else
{
ClosedBitmap = (TEXTURE*) NULL;
IsLoaded = true;
};
if ( IsLoaded )
{
Width = OpenBitmap->Width;
Height = OpenBitmap->Height;
}
};
CGUIElement::~CGUIElement( )
{
if ( OpenBitmap != NULL )
{
jRelease_Texture(OpenBitmap);
delete OpenBitmap;
}
if ( ClosedBitmap != NULL )
{
jRelease_Texture(ClosedBitmap);
delete ClosedBitmap;
}
std::vector<TEXTURE*>::iterator it;
for ( it = ExtraBitmaps.begin(); it != ExtraBitmaps.end(); ++it )
{
TEXTURE* Bitmap = (*it);
jRelease_Texture(Bitmap);
delete Bitmap;
}
};
destructor I probably need the other 2. My class object definately has a
custom constructor and destructor, but I'm fuzzy on copy.
Could someone help me out with this class? Shown are the variables,
constructors and destructor only (rest snipped out to preserve space). Yes,
I realize this is fairly ugly code, any suggestions/critiques on that would
be appreciated also. I know some won't like the fact it's called
CGUIElement and not GUIElement.
I guess my main question is what to do about the TEXTURE* (all caps is from
library I'm using, not my idea) and how to handle them if it's copied. In
theory I guess I would only like to accept a shallow copy with the
confirment that the original is not used anymore.
Currently they are being created like this:
Client.GUIElements[Client.GUI_BITMAPS] =
new CGUIElement( ClosedFileName, jColor(255, 0, 240, 255), GUI_List,
726, 674,
FileNames, 85, 44 );
and I can't think of anywhere they would actually be copied, but am not
sure.
How would I go about disabling copies so that this class could never be
copied since I don't have a copy override?
Any advice is appreciated.
// Header //
class CGUIElement
{
private:
TEXTURE* OpenBitmap; // Bitmap to show when GUI is "Open"
TEXTURE* ClosedBitmap; // Show when GUI is not "Open" (or ExtraBitmaps)
std::vector<TEXTURE*> ExtraBitmaps; // Used when more than 2 bitmaps are
needed
GUIType Type; // Kind of element this is
int ListIndex; // If type List what bitmap to show when open?
int NotifyMsg; // Some Observer has requested we send this info
// on Notifies
unsigned long BitmapX, // Where to display this bitmap
BitmapY;
unsigned int Width, // Size of bitmap
Height;
unsigned int X1, Y1, X2, Y2; // Used for clickable selection
unsigned int TextX, // Just to store/return text x/y
TextY,
TextX2,
TextY2,
TextWidth, // Window Max Text Length
TextHeight; // Window Max Text Lines
bool IsLoaded; // Will normall be true, unless error loading
bool IsOpen; // Is the attached window, or this window "Open"?
public:
CGUIElement( std::string OpenFileName, std::string ClosedFileName,
JCOLOR ColorKey,
GUIType WhatType,
unsigned long X, unsigned long Y,
unsigned int ThisX1 = 0, unsigned int ThisY1 = 0,
unsigned int ThisX2 = 0, unsigned int ThisY2 = 0,
unsigned int textX = 0, unsigned int textY = 0,
unsigned int textX2 = 0, unsigned int textY2 = 0);
virtual ~CGUIElement();
};
// CPP file //
CGUIElement::CGUIElement( std::string OpenFileName, std::string
ClosedFileName,
JCOLOR ColorKey,
GUIType WhatType,
unsigned long X, unsigned long Y,
unsigned int inX1, unsigned int inY1,
unsigned int inX2, unsigned int inY2,
unsigned int textX, unsigned int textY,
unsigned int textX2, unsigned int textY2) : OpenNotifier(this),
OpenObserver(this),
CloseNotifier(this), CloseObserver(this),
CloseOnOpenNotifier(this), CloseOnOpenObserver(this),
IsOpen(false), IsLoaded(false),
NotifyMsg(0),
Type(WhatType),
BitmapX(X), BitmapY(Y),
X1(inX1), Y1(inY1),
X2(inX2), Y2(inY2),
TextX(textX), TextY(textY),
TextX2(textX2), TextY2(textY2),
TextWidth(0), TextHeight(0)
{
OpenBitmap = (TEXTURE*) NULL;
ClosedBitmap = (TEXTURE*) NULL;
OpenBitmap = new TEXTURE;
jLoad_Texture(OpenBitmap, const_cast<char*>( OpenFileName.c_str() ),
false, ColorKey, 0);
if ( ! OpenBitmap->Success )
{
IsLoaded = false;
delete OpenBitmap;
OpenBitmap = (TEXTURE*) NULL;
ClosedBitmap = (TEXTURE*) NULL;
}
else if ( ClosedFileName.length() > 0)
{
ClosedBitmap = new TEXTURE;
jLoad_Texture(ClosedBitmap, const_cast<char*>(
ClosedFileName.c_str() ), false, ColorKey, 0);
if ( ! ClosedBitmap->Success )
{
// Couldn't load 2nd bitmap, unload 1st
jRelease_Texture(OpenBitmap);
delete OpenBitmap;
delete ClosedBitmap;
ClosedBitmap = (TEXTURE*) NULL;
OpenBitmap = (TEXTURE*) NULL;
IsLoaded = false;
}
else
IsLoaded = true;
}
else
{
ClosedBitmap = (TEXTURE*) NULL;
IsLoaded = true;
};
if ( IsLoaded )
{
Width = OpenBitmap->Width;
Height = OpenBitmap->Height;
}
};
CGUIElement::~CGUIElement( )
{
if ( OpenBitmap != NULL )
{
jRelease_Texture(OpenBitmap);
delete OpenBitmap;
}
if ( ClosedBitmap != NULL )
{
jRelease_Texture(ClosedBitmap);
delete ClosedBitmap;
}
std::vector<TEXTURE*>::iterator it;
for ( it = ExtraBitmaps.begin(); it != ExtraBitmaps.end(); ++it )
{
TEXTURE* Bitmap = (*it);
jRelease_Texture(Bitmap);
delete Bitmap;
}
};