Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
5.0 KiB

  1. /***
  2. *stdexcpt.cpp - defines C++ standard exception classes
  3. *
  4. * Copyright (c) 1994-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Implementation of C++ standard exception classes which must live in
  8. * the main CRT, not the C++ CRT, because they are referenced by RTTI
  9. * support in the main CRT.
  10. *
  11. * exception
  12. * bad_cast
  13. * bad_typeid
  14. * __non_rtti_object
  15. *
  16. *Revision History:
  17. * 04-27-94 BES Module created.
  18. * 10-17-94 BWT Disable code for PPC.
  19. * 02-15-95 JWM Minor cleanups related to Olympus bug 3716
  20. * 07-02-95 JWM Now generally ANSI-compliant; excess baggage removed.
  21. * 06-01-99 PML __exString disappeared as of 5/3/99 Plauger STL drop.
  22. * 11-09-99 PML Use malloc, not new, to avoid recursion (vs7#16826).
  23. * 09-07-00 PML Get rid of /lib:libcp directive in obj (vs7#159463)
  24. * 03-21-01 PML Move bad_cast, bad_typeid, __non_rtti_object function
  25. * defs out of typeinfo.h so _STATIC_CPPLIB will work.
  26. * 01-30-03 BWT Don't mark doFree unless malloc was successful
  27. *
  28. *******************************************************************************/
  29. #define _USE_ANSI_CPP /* Don't emit /lib:libcp directive */
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <eh.h>
  33. #include <stdexcpt.h>
  34. #include <typeinfo.h>
  35. ////////////////////////////////////////////////////////////////////////////////
  36. //
  37. // Implementation of class "exception"
  38. //
  39. //
  40. // Default constructor - initialize to blank
  41. //
  42. exception::exception ()
  43. {
  44. _m_what = NULL;
  45. _m_doFree = 0;
  46. }
  47. //
  48. // Standard constructor: initialize with copy of string
  49. //
  50. exception::exception ( const char * const & what )
  51. {
  52. _m_what = static_cast< char * >( malloc( strlen( what ) + 1 ) );
  53. if ( _m_what != NULL ) {
  54. strcpy( (char*)_m_what, what );
  55. _m_doFree = 1;
  56. } else {
  57. _m_doFree = 0;
  58. }
  59. }
  60. //
  61. // Copy constructor
  62. //
  63. exception::exception ( const exception & that )
  64. {
  65. if (that._m_doFree)
  66. {
  67. _m_what = static_cast< char * >( malloc( strlen( that._m_what ) + 1 ) );
  68. if (_m_what != NULL) {
  69. strcpy( (char*)_m_what, that._m_what );
  70. _m_doFree = 1;
  71. } else {
  72. _m_doFree = 0;
  73. }
  74. } else {
  75. _m_what = that._m_what;
  76. _m_doFree = 0;
  77. }
  78. }
  79. //
  80. // Assignment operator: destruct, then copy-construct
  81. //
  82. exception& exception::operator=( const exception& that )
  83. {
  84. if (this != &that)
  85. {
  86. this->exception::~exception();
  87. this->exception::exception(that);
  88. }
  89. return *this;
  90. }
  91. //
  92. // Destructor: free the storage used by the message string if it was
  93. // dynamicly allocated
  94. //
  95. exception::~exception()
  96. {
  97. if (_m_doFree)
  98. free( const_cast< char * >( _m_what ) );
  99. }
  100. //
  101. // exception::what
  102. // Returns the message string of the exception.
  103. // Default implementation of this method returns the stored string if there
  104. // is one, otherwise returns a standard string.
  105. //
  106. const char * exception::what() const
  107. {
  108. if ( _m_what != NULL )
  109. return _m_what;
  110. else
  111. return "Unknown exception";
  112. }
  113. ////////////////////////////////////////////////////////////////////////////////
  114. //
  115. // Implementation of class "bad_cast"
  116. //
  117. bad_cast::bad_cast(const char * _Message)
  118. : exception(_Message)
  119. {
  120. }
  121. bad_cast::bad_cast(const bad_cast & that)
  122. : exception(that)
  123. {
  124. }
  125. bad_cast::~bad_cast()
  126. {
  127. }
  128. #ifdef CRTDLL
  129. //
  130. // This is a dummy constructor. Previously, the only bad_cast ctor was
  131. // bad_cast(const char * const &). To provide backwards compatibility
  132. // for std::bad_cast, we want the main ctor to be bad_cast(const char *)
  133. // instead. Since you can't have both bad_cast(const char * const &) and
  134. // bad_cast(const char *), we define this bad_cast(const char * const *),
  135. // which will have the exact same codegen as bad_cast(const char * const &),
  136. // and alias the old form with a .def entry.
  137. //
  138. bad_cast::bad_cast(const char * const * _PMessage)
  139. : exception(*_PMessage)
  140. {
  141. }
  142. #endif
  143. ////////////////////////////////////////////////////////////////////////////////
  144. //
  145. // Implementation of class "bad_typeid"
  146. //
  147. bad_typeid::bad_typeid(const char * _Message)
  148. : exception(_Message)
  149. {
  150. }
  151. bad_typeid::bad_typeid(const bad_typeid & that)
  152. : exception(that)
  153. {
  154. }
  155. bad_typeid::~bad_typeid()
  156. {
  157. }
  158. ////////////////////////////////////////////////////////////////////////////////
  159. //
  160. // Implementation of class "__non_rtti_object"
  161. //
  162. __non_rtti_object::__non_rtti_object(const char * _Message)
  163. : bad_typeid(_Message)
  164. {
  165. }
  166. __non_rtti_object::__non_rtti_object(const __non_rtti_object & that)
  167. : bad_typeid(that)
  168. {
  169. }
  170. __non_rtti_object::~__non_rtti_object()
  171. {
  172. }