📄 resourcemanagement.cpp
字号:
// ResourceManagement.cpp
//
// Copyright (c) 1999-2007 Symbian Software Ltd. All rights reserved.
//
// $Change: 1034634 $
// SYSTEM HEADERS
#include <e32cons.h>
// All messages written to this
LOCAL_D CConsoleBase* console;
// CSimple (definition)
class CSimple : public CBase
{
public :
CSimple(TInt aVal);
void Display();
public:
TInt iVal;
};
// CCompound (definition)
class CCompound : public CBase
{
public :
virtual ~CCompound();
void Display();
static CCompound* NewL(TInt aRoot,TInt aChild);
CSimple* Child();
TInt Root();
private:
CCompound(TInt aRoot);
void ConstructL(TInt aChild);
private:
TInt iRoot;
CSimple* iChild;
};
// CComplex (definition)
class CComplex : public CBase
{
public :
virtual ~CComplex();
void Display();
static CComplex* NewL(TInt aRoot,TInt aChild);
static CComplex* NewLC(TInt aRoot,TInt aChild);
private:
CComplex(TInt aRoot);
void ConstructL(TInt aChild);
private:
TInt iRoot;
CCompound* iChild;
};
// CSimple (implementation)
CSimple::CSimple(TInt aVal)
: iVal(aVal)
{
_LIT(KMsgConCSimple, "CSimple contructor\n");
console->Printf(KMsgConCSimple);
}
void CSimple::Display()
{
// Display class data member on the console.
_LIT(KFormat1, "Value=%d.\n");
console->Printf(KFormat1, iVal);
}
// CCompound (implementation)
CCompound::CCompound(TInt aRoot)
:iRoot(aRoot)
{
}
// Use two stage construction technique to
// prevent memory leak if OOM during construction
CCompound* CCompound::NewL(TInt /* aRoot */, TInt /* aChild */)
{
_LIT(KMsgNewLCCompound, "CCompound NewL\n");
console->Printf(KMsgNewLCCompound);
//TODO Declare CCompound pointer self and assign to it, using
// new ELeave, a CCompound object that takes parameter aRoot,
//TODO Call Cleanup Stack's PushL function with self
//TODO Call self's ConstructL function with parameter aChild
//TODO Call Cleanup Stack's Pop function
//TODO return self
return NULL;
}
// NB. function may leave, as new(ELeave) may leave
void CCompound::ConstructL(TInt /* aChild */)
{
_LIT(KMsg2ConCCompound, "CCompound ConstructL\n");
console->Printf(KMsg2ConCCompound);
//TODO assign to iChild by calling new(ELeave) to
// a CSimple taking aChild as a parameter
}
void CCompound::Display()
{
// Display class member data on the console
_LIT(KFormat4, "Parent=%d. Child=%d.\n");
console->Printf(KFormat4, iRoot, iChild->iVal);
}
CSimple* CCompound::Child()
{
return iChild;
}
TInt CCompound::Root()
{
return iRoot;
}
CCompound::~CCompound()
{
_LIT(KMsgDestCCompound, "CCompound destructor\n");
console->Printf(KMsgDestCCompound);
//TODO Delete member data Child
}
//TODO Comment in implementation for CComplex
/*
// CComplex (implementation)
CComplex::CComplex(TInt aRoot)
:iRoot(aRoot)
{
}
// Use two stage construction technique to
// prevent memory leak if OOM during construction
// NB. due to use of cleanup stack, NewLC is
// now the primitive, rather than NewL
// NewLC with two stage construct
CComplex* CComplex::NewLC(TInt aRoot, TInt aChild)
{
_LIT(KMsgNewLCCComplex, "CComplex NewLC\n");
console->Printf(KMsgNewLCCComplex);
//TODO Declare CComplex pointer self and assign to it,
// using new ELeave, a CComplex object
//TODO Call Cleanup Stack's PushL function with self
//TODO Call self's ConstructL function with parameter aChild
//TODO return self
}
// version of NewLC which leaves nothing on the cleanup stack
CComplex* CComplex::NewL(TInt aRoot, TInt aChild)
{
//TODO Declare CComplex pointer self, assign to it
// using NewLC function with parameters aRoot and aChild
//TODO Call Cleanup Stack's Pop function
return self;
}
// NB. function may leave, as new(ELeave) may leave
void CComplex::ConstructL(TInt aChild)
{
_LIT(KMsg2ConCComplex, "CComplex ConstructL\n");
console->Printf(KMsg2ConCComplex);
//TODO assign to iChild by calling
// CCompound's NewL function with parameters
// aChild and aChild+2
}
// Perhaps a previous implementation of this function leaved
// and the name has been kept for compatibility reasons
void CComplex::Display()
{
// Display class member data on the console
_LIT(KFormat4, "Parent=%d. Child=%d. Grandchild=%d.\n");
console->Printf(KFormat4, iRoot, iChild->Root(), iChild->Child()->iVal);
}
CComplex::~CComplex()
{
_LIT(KMsgDestCComplex, "CComplex destructor\n");
console->Printf(KMsgDestCComplex);
//TODO Delete member data Child
delete iChild;
}
*/
// Definition of the CTestOne class
class CTestOne
{
public:
~CTestOne();
void CreateL(const TDesC& aData);
private :
HBufC* iText;
};
// Implementation of the CTestOne class
_LIT(KTxtInsideDestructor, "Running CTestOne destructor\n");
CTestOne::~CTestOne()
{
delete iText;
console->Printf(KTxtInsideDestructor);
}
void CTestOne::CreateL(const TDesC& aData)
{
iText = aData.AllocL();
}
// Definition of the RTestTwo class
class RTestTwo
{
public:
RTestTwo(TInt aValue);
void Close();
private :
TInt iX;
};
// Implementation of the RTestTwo class
RTestTwo::RTestTwo(TInt aValue)
: iX(aValue)
{
}
_LIT(KTxtCloseRTestTwo, "Closing RTestTwo\n");
void RTestTwo::Close()
{
console->Printf(KTxtCloseRTestTwo);
}
// Definition of the RTestThree class
class RTestThree
{
public:
RTestThree(TInt aValue);
void Release();
private :
TInt iY;
};
// Implementation of the RTestThree class
RTestThree::RTestThree(TInt aValue)
: iY(aValue)
{
}
_LIT(KTxtReleaseRTestThree, "Releasing RTestThree\n");
void RTestThree::Release()
{
console->Printf(KTxtReleaseRTestThree);
}
_LIT(KTxtBlankLine, "\n");
// Do the example
//
// Example to check robustness of class on OOM and attempt to provoke
// memory leaks (orphans).
void doExampleL()
{
//TODO Declare a pointer to a CCompound object and initialise
// it from CCompound's NewL function, passing in 4 and 5
CCompound* myCompoundExample = NULL;
//TODO Comment in Display function
// myCompoundExample->Display();
//TODO Call delete on the object
console->Printf(KTxtBlankLine);
//TODO Call UHEAP SETFAIL macro with RHeap's EDeterministic and value 2
//TODO Declare a pointer to a CComplex object and initialise
// it from CComplex's NewLC function, passing in 2 and 3
//TODO Comment in Display function
// myComplexExample->Display();
//TODO Call CleanupStack's Pop And Destroy function
//TODO Call UHEAP RESET macro to turn off OOM simulator
console->Printf(KTxtBlankLine);
//TODO Comment in rest of function
/*
//TODO Declare a TInt pointer, assign to it a new TInt
//TODO Push this on to the cleanup stack
_LIT(KMsgTintPush, "TInt pushed\n");
console->Printf(KMsgTintPush);
*myTClass = 2;
//TODO Pop and Destroy this off the Cleanup Stack
_LIT(KMsgTintPop, "TInt popped\n");
console->Printf(KMsgTintPop);
console->Printf(KTxtBlankLine);
//TODO Declare a CTestOne object with new ELeave
// and assign to a pointer
//TODO Call the Cleanup Delete PushL function
// This to puts a TCleanUpItem on the cleanup stack
// Exercise the CTestOne object (just to show it doing something
_LIT(KTxtHelloWorld, "Hello World!");
one->CreateL(KTxtHelloWorld);
console->Printf(KTxtBlankLine);
// Construct a RTestTwo object on the program stack.
// The value passed is of no significance; it is just
// to show that the class is not trivial.
//TODO Comment in RTestTwo
//RTestTwo two(2);
//TODO Call the Cleanup Close PushL function with two
// This puts a TCleanUpItem on the cleanup stack
console->Printf(KTxtBlankLine);
// Construct a RTestThree object on the program stack.
// The value passed is of no significance; it is just
// to show that the class is not trivial.
//TODO Comment in RTestThree
//RTestThree three(3);
//TODO Call the Cleanup Release PushL function with three
// This puts a TCleanUpItem on the cleanup stack
//TODO Call the Cleanup Stack's Pop And Destroy function
// cleaning up the top three objects
*/
}
_LIT(KTxtEEMemMan, "EE MemMan");
_LIT(KFormatFailed, "failed: leave code=%d");
_LIT(KTxtOK, "Ok");
_LIT(KTxtPressAnyKey, " [press any key]");
LOCAL_C void callExampleL() // initialize and call example code under cleanup stack
{
console = Console::NewL(KTxtEEMemMan, TSize(KConsFullScreen, KConsFullScreen));
CleanupStack::PushL(console);
TRAPD(error, doExampleL()); // perform example function
if (error)
{
console->Printf(KFormatFailed, error);
}
else
{
console->Printf(KTxtOK);
}
console->Printf(KTxtPressAnyKey);
console->Getch(); // get and ignore character
CleanupStack::PopAndDestroy(console); // close console
}
//////////////////////////////////////////////////////////////////////////////
//
// Main function called by E32
//
//////////////////////////////////////////////////////////////////////////////
GLDEF_C TInt E32Main() // main function called by E32
{
//TODO Call the UHEAP MARK macro
//TODO Declare a CTrapCleanup pointer named cleanup
// assign it the result of calling CTrapCleanup's New() function
CTrapCleanup* cleanup = NULL;
TRAPD(error, callExampleL()); // more initialization, then do example
_LIT(KTxtMemMan, "MemMan");
__ASSERT_ALWAYS(!error, User::Panic(KTxtMemMan, error));
delete cleanup; // destroy clean-up stack
//TODO Call the UHEAP MARKEND macro
return 0; // and return
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -