PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 22#ifdef _MSC_VER 
 23#pragma warning (disable : 4786) // identifier was truncated to 'number' characters
 24#endif 
 25
 26#ifdef _WIN32
 27#include <windows.h>
 28#endif // _WIN32
 29#include "FreeImage.h"
 30#include "Utilities.h"
 31#include "DeprecationMgr.h"
 32
 33// ==========================================================
 34
 35DeprecationMgr::DeprecationMgr() {
 36}
 37
 38DeprecationMgr::~DeprecationMgr() {
 39#ifdef _WIN32
 40	if (!m_functions.empty()) {
 41		OutputDebugString( "*************************************************************************************\n" );
 42		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" );
 43
 44		for (std::map<const char *, DeprecatedFunction>::iterator i = m_functions.begin(); i != m_functions.end(); ++i) {
 45			DeprecatedFunction *function = &((*i).second);
 46
 47			char txt[255];
 48
 49			sprintf(txt, " * %s called from %i different places. Instead use %s.\n", function->old_function_name,  function->called_from.size(), function->new_function_name);
 50
 51			OutputDebugString(txt);
 52		}
 53
 54		OutputDebugString( "*************************************************************************************\n" );
 55
 56		m_functions.clear();
 57	}
 58#endif // _WIN32
 59}
 60
 61// ==========================================================
 62
 63DeprecationMgr *
 64DeprecationMgr::GetInstance() {
 65	static DeprecationMgr Instance;
 66	return &Instance;
 67}
 68
 69// ==========================================================
 70
 71void
 72DeprecationMgr::AddDeprecatedFunction(const char *old_function_name, const char *new_function_name, const void *frame_ptr) {
 73#ifdef _WIN32
 74	int *preturn = (int *)frame_ptr + 1; // usual return address @ [ebp+4]
 75	int called_from = IsBadReadPtr(preturn, 4) ? 0 : *preturn;
 76
 77	// check if this function was already listed as deprecated
 78	// if it wasn't, make a new entry for it
 79	// if it was, keep track of where it's called from.
 80
 81	std::map<const char *, DeprecatedFunction>::iterator existing_function = m_functions.find(old_function_name);
 82
 83	if (existing_function == m_functions.end()) {
 84		DeprecatedFunction function;
 85
 86		function.old_function_name = old_function_name;
 87		function.new_function_name = new_function_name;
 88		function.called_from.insert(called_from);
 89
 90		m_functions[old_function_name] = function;
 91	} else {
 92		// since we're keeping track of the addresses this function
 93		// was called from in a set, we don't need to check whether we've
 94		// already added the address.
 95
 96		DeprecatedFunction *function = &((*existing_function).second);
 97
 98		function->called_from.insert(called_from);
 99	}
100#endif // _WIN32
101}
102
103