deprecation.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * Copyright (c) 2006-2019 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. namespace love
  26. {
  27. static std::map<std::string, DeprecationInfo> *deprecated = nullptr;
  28. static std::vector<const DeprecationInfo *> *deprecatedList = nullptr;
  29. static std::atomic<int> initCount;
  30. static thread::Mutex *mutex = nullptr;
  31. static bool outputEnabled = false;
  32. void initDeprecation()
  33. {
  34. if (initCount.fetch_add(1) == 0)
  35. {
  36. mutex = thread::newMutex();
  37. // These are heap-allocated because we want to clear them on deinit,
  38. // and deinit may be called when the program is shutting down in the
  39. // middle of static variable cleanup (eg in the Math module destructor).
  40. // Calling std::map::clear() in that case was causing segfaults.
  41. deprecated = new std::map<std::string, DeprecationInfo>();
  42. deprecatedList = new std::vector<const DeprecationInfo *>();
  43. }
  44. }
  45. void deinitDeprecation()
  46. {
  47. if (initCount.fetch_sub(1) == 1)
  48. {
  49. delete deprecated;
  50. delete deprecatedList;
  51. delete mutex;
  52. deprecated = nullptr;
  53. deprecatedList = nullptr;
  54. mutex = nullptr;
  55. }
  56. }
  57. static void printDeprecationNotice(const DeprecationInfo &info)
  58. {
  59. std::string notice = getDeprecationNotice(info, true);
  60. printf("LOVE - Warning: %s\n", notice.c_str());
  61. }
  62. void setDeprecationOutputEnabled(bool enable)
  63. {
  64. if (enable == outputEnabled)
  65. return;
  66. outputEnabled = enable;
  67. if (enable)
  68. {
  69. GetDeprecated deprecated;
  70. for (const DeprecationInfo *info : deprecated.all)
  71. {
  72. if (info->uses == 1)
  73. printDeprecationNotice(*info);
  74. }
  75. }
  76. }
  77. bool isDeprecationOutputEnabled()
  78. {
  79. return outputEnabled;
  80. }
  81. std::string getDeprecationNotice(const DeprecationInfo &info, bool usewhere)
  82. {
  83. std::string notice;
  84. if (usewhere)
  85. notice += info.where;
  86. notice += "Using deprecated ";
  87. if (info.apiType == API_FUNCTION)
  88. notice += "function ";
  89. else if (info.apiType == API_METHOD)
  90. notice += "method ";
  91. else if (info.apiType == API_FIELD)
  92. notice += "field ";
  93. else if (info.apiType == API_CONSTANT)
  94. notice += "constant ";
  95. else
  96. notice += "API ";
  97. notice += info.name;
  98. if (info.type == DEPRECATED_REPLACED && !info.replacement.empty())
  99. notice += " (replaced by " + info.replacement + ")";
  100. else if (info.type == DEPRECATED_RENAMED && !info.replacement.empty())
  101. notice += " (renamed to " + info.replacement + ")";
  102. return notice;
  103. }
  104. GetDeprecated::GetDeprecated()
  105. : all(*deprecatedList)
  106. {
  107. if (mutex != nullptr)
  108. mutex->lock();
  109. }
  110. GetDeprecated::~GetDeprecated()
  111. {
  112. if (mutex != nullptr)
  113. mutex->unlock();
  114. }
  115. MarkDeprecated::MarkDeprecated(const char *name, APIType api)
  116. : MarkDeprecated(name, api, DEPRECATED_NO_REPLACEMENT, nullptr)
  117. {
  118. }
  119. MarkDeprecated::MarkDeprecated(const char *name, APIType api, DeprecationType type, const char *replacement)
  120. : info(nullptr)
  121. {
  122. if (mutex != nullptr)
  123. mutex->lock();
  124. auto it = deprecated->find(name);
  125. if (it != deprecated->end())
  126. {
  127. it->second.uses++;
  128. info = &it->second;
  129. }
  130. else
  131. {
  132. DeprecationInfo newinfo = {};
  133. newinfo.type = type;
  134. newinfo.apiType = api;
  135. newinfo.uses = 1;
  136. newinfo.name = name;
  137. if (replacement != nullptr)
  138. newinfo.replacement = replacement;
  139. auto inserted = deprecated->insert(std::make_pair(newinfo.name, newinfo));
  140. info = &inserted.first->second;
  141. deprecatedList->push_back(info);
  142. }
  143. }
  144. MarkDeprecated::~MarkDeprecated()
  145. {
  146. if (outputEnabled && info != nullptr && info->uses == 1)
  147. printDeprecationNotice(*info);
  148. if (mutex != nullptr)
  149. mutex->unlock();
  150. }
  151. } // love