deprecation.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * Copyright (c) 2006-2024 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "common/config.h"
  21. #include "deprecation.h"
  22. #include "thread/threads.h"
  23. #include <atomic>
  24. #include <map>
  25. #include <sstream>
  26. namespace love
  27. {
  28. static std::map<std::string, DeprecationInfo> *deprecated = nullptr;
  29. static std::vector<const DeprecationInfo *> *deprecatedList = nullptr;
  30. static std::atomic<int> initCount;
  31. static thread::Mutex *mutex = nullptr;
  32. static bool outputEnabled = false;
  33. void initDeprecation()
  34. {
  35. if (initCount.fetch_add(1) == 0)
  36. {
  37. mutex = thread::newMutex();
  38. // These are heap-allocated because we want to clear them on deinit,
  39. // and deinit may be called when the program is shutting down in the
  40. // middle of static variable cleanup (eg in the Math module destructor).
  41. // Calling std::map::clear() in that case was causing segfaults.
  42. deprecated = new std::map<std::string, DeprecationInfo>();
  43. deprecatedList = new std::vector<const DeprecationInfo *>();
  44. }
  45. }
  46. void deinitDeprecation()
  47. {
  48. if (initCount.fetch_sub(1) == 1)
  49. {
  50. delete deprecated;
  51. delete deprecatedList;
  52. delete mutex;
  53. deprecated = nullptr;
  54. deprecatedList = nullptr;
  55. mutex = nullptr;
  56. }
  57. }
  58. static void printDeprecationNotice(const DeprecationInfo &info)
  59. {
  60. std::string notice = getDeprecationNotice(info, true);
  61. printf("LOVE - Warning: %s\n", notice.c_str());
  62. }
  63. void setDeprecationOutputEnabled(bool enable)
  64. {
  65. if (enable == outputEnabled)
  66. return;
  67. outputEnabled = enable;
  68. if (enable)
  69. {
  70. GetDeprecated deprecated;
  71. for (const DeprecationInfo *info : deprecated.all)
  72. {
  73. if (info->uses == 1)
  74. printDeprecationNotice(*info);
  75. }
  76. }
  77. }
  78. bool isDeprecationOutputEnabled()
  79. {
  80. return outputEnabled;
  81. }
  82. std::string getDeprecationNotice(const DeprecationInfo &info, bool usewhere)
  83. {
  84. std::stringstream notice;
  85. if (usewhere)
  86. notice << info.where;
  87. notice << "Using deprecated ";
  88. if (info.apiType == API_FUNCTION)
  89. notice << "function ";
  90. else if (info.apiType == API_FUNCTION_VARIANT)
  91. notice << "function variant in ";
  92. else if (info.apiType == API_METHOD)
  93. notice << "method ";
  94. else if (info.apiType == API_METHOD_VARIANT)
  95. notice << "method variant in ";
  96. else if (info.apiType == API_CALLBACK)
  97. notice << "callback ";
  98. else if (info.apiType == API_FIELD)
  99. notice << "field ";
  100. else if (info.apiType == API_CONSTANT)
  101. notice << "constant ";
  102. notice << info.name;
  103. if (info.type == DEPRECATED_REPLACED && !info.replacement.empty())
  104. notice << " (replaced by " << info.replacement << ")";
  105. else if (info.type == DEPRECATED_RENAMED && !info.replacement.empty())
  106. notice << " (renamed to " << info.replacement << ")";
  107. return notice.str();
  108. }
  109. GetDeprecated::GetDeprecated()
  110. : all(*deprecatedList)
  111. {
  112. if (mutex != nullptr)
  113. mutex->lock();
  114. }
  115. GetDeprecated::~GetDeprecated()
  116. {
  117. if (mutex != nullptr)
  118. mutex->unlock();
  119. }
  120. MarkDeprecated::MarkDeprecated(const char *name, APIType api)
  121. : MarkDeprecated(name, api, DEPRECATED_NO_REPLACEMENT, nullptr)
  122. {
  123. }
  124. MarkDeprecated::MarkDeprecated(const char *name, APIType api, DeprecationType type, const char *replacement)
  125. : info(nullptr)
  126. {
  127. if (mutex != nullptr)
  128. mutex->lock();
  129. auto it = deprecated->find(name);
  130. if (it != deprecated->end())
  131. {
  132. it->second.uses++;
  133. info = &it->second;
  134. }
  135. else
  136. {
  137. DeprecationInfo newinfo = {};
  138. newinfo.type = type;
  139. newinfo.apiType = api;
  140. newinfo.uses = 1;
  141. newinfo.name = name;
  142. if (replacement != nullptr)
  143. newinfo.replacement = replacement;
  144. auto inserted = deprecated->insert(std::make_pair(newinfo.name, newinfo));
  145. info = &inserted.first->second;
  146. deprecatedList->push_back(info);
  147. }
  148. }
  149. MarkDeprecated::~MarkDeprecated()
  150. {
  151. if (outputEnabled && info != nullptr && info->uses == 1)
  152. printDeprecationNotice(*info);
  153. if (mutex != nullptr)
  154. mutex->unlock();
  155. }
  156. STRINGMAP_BEGIN(APIType, API_MAX_ENUM, apiType)
  157. {
  158. { "function", API_FUNCTION },
  159. { "functionvariant", API_FUNCTION_VARIANT },
  160. { "method", API_METHOD },
  161. { "methodvariant", API_METHOD_VARIANT },
  162. { "callback", API_CALLBACK },
  163. { "field", API_FIELD },
  164. { "constant", API_CONSTANT },
  165. { "custom", API_CUSTOM },
  166. }
  167. STRINGMAP_END(APIType, API_MAX_ENUM, apiType)
  168. STRINGMAP_BEGIN(DeprecationType, DEPRECATED_MAX_ENUM, deprecationType)
  169. {
  170. { "noreplacement", DEPRECATED_NO_REPLACEMENT },
  171. { "replaced", DEPRECATED_REPLACED },
  172. { "renamed", DEPRECATED_RENAMED },
  173. }
  174. STRINGMAP_END(DeprecationType, DEPRECATED_MAX_ENUM, deprecationType)
  175. } // love