version 1.1.2.2, 2006/06/30 21:11:03
|
version 1.1.2.3, 2006/06/30 22:37:52
|
|
|
|
|
//%2006//////////////////////////////////////////////////////////////////////// | //%2006//////////////////////////////////////////////////////////////////////// |
// | // |
// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development | // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development |
|
|
| |
PEGASUS_NAMESPACE_BEGIN | PEGASUS_NAMESPACE_BEGIN |
| |
/** Implements a simple magic number that is set on construction and cleared |
/** The Magic class implements a convenient way to use magic numbers in |
on destruction. |
user-defined classes. Magic numbers help detect use of uninitialized, |
|
destructed, or corrupted objects. |
|
|
|
To instrument a class to use magic numbers simply add a class member |
|
of type Magic<> as shown in thsi example: |
|
|
|
\code |
|
class MyClass |
|
{ |
|
public: |
|
|
|
MyClass(); |
|
|
|
~MyClass(); |
|
|
|
void foo(); |
|
|
|
private: |
|
Magic<0xC531B144> _magic; |
|
}; |
|
\endcode |
|
|
|
Choose whatever number you like for a magic number. The number above was |
|
generated by the Linux uuidgen utility (Windows has a utility with the |
|
same name). |
|
|
|
To test magic number, add the following expression wherever necessary |
|
(usually as the first line of every member function). |
|
|
|
\code |
|
PEGASUS_ASSERT_DEBUG(_magic); |
|
\endcode |
|
|
|
Here's a typical example: |
|
|
|
\code |
|
MyClass::~MyClass() |
|
{ |
|
PEGASUS_ASSERT_DEBUG(_magic); |
|
} |
|
\endcode |
|
|
|
This tests whether the magic number is 0xC531B144 and asserts if it is |
|
not. |
|
|
|
Note that using a magic number makes the user-defined class 4 bytes larger |
|
but there is no run-time overhead unless you compile with PEGASUS_DEBUG. |
|
The Magic constructor and destructor are empty without PEGASUS_DEBUG and |
|
are discarded by the compiler. |
|
|
|
CAUTION: You may be tempted to compile out the magic member when |
|
PEGASUS_DEBUG undefined. However, this will causes unpredictable |
|
behavior when debug libraries are mixed with non-debug libraries. The |
|
structure alignment and size will be different and will lead to crashes. |
|
This is only safe if a class is internal to a library. |
*/ | */ |
template<Uint32 MAGIC_NUMBER> | template<Uint32 MAGIC_NUMBER> |
class Magic | class Magic |
{ | { |
public: | public: |
| |
|
/** Default constructor. Sets the magic number. |
|
*/ |
Magic() | Magic() |
{ | { |
#ifdef PEGASUS_DEBUG | #ifdef PEGASUS_DEBUG |
|
|
#endif | #endif |
} | } |
| |
|
/** Destructor. Clears the magic number (with the pattern 0xDDDDDDDD). |
|
*/ |
~Magic() | ~Magic() |
{ | { |
#ifdef PEGASUS_DEBUG | #ifdef PEGASUS_DEBUG |
|
|
#endif | #endif |
} | } |
| |
|
/** Allows magic number to be used as a boolean expression. This function |
|
tests the magic number and return true if valid. |
|
*/ |
operator bool() const | operator bool() const |
{ | { |
#ifdef PEGASUS_DEBUG | #ifdef PEGASUS_DEBUG |