/src/FreeImage/Source/DeprecationManager/DeprecationMgr.cpp

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 103 lines · 50 code · 24 blank · 29 comment · 6 complexity · 757a2417760b310f4953311b74ca07e3 MD5 · raw file

  1. // ==========================================================
  2. // Deprecation Manager
  3. //
  4. // Design and implementation by
  5. // - Noel Llopis (Game Programming Gems II)
  6. //
  7. // This file is part of FreeImage 3
  8. //
  9. // COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
  10. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
  11. // THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
  12. // OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
  13. // CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
  14. // THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
  15. // SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
  16. // PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  17. // THIS DISCLAIMER.
  18. //
  19. // Use at your own risk!
  20. // ==========================================================
  21. #ifdef _MSC_VER
  22. #pragma warning (disable : 4786) // identifier was truncated to 'number' characters
  23. #endif
  24. #ifdef _WIN32
  25. #include <windows.h>
  26. #endif // _WIN32
  27. #include "FreeImage.h"
  28. #include "Utilities.h"
  29. #include "DeprecationMgr.h"
  30. // ==========================================================
  31. DeprecationMgr::DeprecationMgr() {
  32. }
  33. DeprecationMgr::~DeprecationMgr() {
  34. #ifdef _WIN32
  35. if (!m_functions.empty()) {
  36. OutputDebugString( "*************************************************************************************\n" );
  37. OutputDebugString( "This is a warning, because you use one or more deprecated functions.\nContinuing to use these functions might eventually render your program uncompilable.\nThe following functions are deprecated:\n\n" );
  38. for (std::map<const char *, DeprecatedFunction>::iterator i = m_functions.begin(); i != m_functions.end(); ++i) {
  39. DeprecatedFunction *function = &((*i).second);
  40. char txt[255];
  41. sprintf(txt, " * %s called from %i different places. Instead use %s.\n", function->old_function_name, function->called_from.size(), function->new_function_name);
  42. OutputDebugString(txt);
  43. }
  44. OutputDebugString( "*************************************************************************************\n" );
  45. m_functions.clear();
  46. }
  47. #endif // _WIN32
  48. }
  49. // ==========================================================
  50. DeprecationMgr *
  51. DeprecationMgr::GetInstance() {
  52. static DeprecationMgr Instance;
  53. return &Instance;
  54. }
  55. // ==========================================================
  56. void
  57. DeprecationMgr::AddDeprecatedFunction(const char *old_function_name, const char *new_function_name, const void *frame_ptr) {
  58. #ifdef _WIN32
  59. int *preturn = (int *)frame_ptr + 1; // usual return address @ [ebp+4]
  60. int called_from = IsBadReadPtr(preturn, 4) ? 0 : *preturn;
  61. // check if this function was already listed as deprecated
  62. // if it wasn't, make a new entry for it
  63. // if it was, keep track of where it's called from.
  64. std::map<const char *, DeprecatedFunction>::iterator existing_function = m_functions.find(old_function_name);
  65. if (existing_function == m_functions.end()) {
  66. DeprecatedFunction function;
  67. function.old_function_name = old_function_name;
  68. function.new_function_name = new_function_name;
  69. function.called_from.insert(called_from);
  70. m_functions[old_function_name] = function;
  71. } else {
  72. // since we're keeping track of the addresses this function
  73. // was called from in a set, we don't need to check whether we've
  74. // already added the address.
  75. DeprecatedFunction *function = &((*existing_function).second);
  76. function->called_from.insert(called_from);
  77. }
  78. #endif // _WIN32
  79. }