cmDebuggerVariables.cxx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmDebuggerVariables.h"
  4. #include <algorithm>
  5. #include <vector>
  6. #include <cm3p/cppdap/optional.h>
  7. #include <cm3p/cppdap/protocol.h>
  8. #include <cm3p/cppdap/types.h>
  9. #include "cmDebuggerVariablesManager.h"
  10. namespace cmDebugger {
  11. namespace {
  12. const dap::VariablePresentationHint PrivatePropertyHint = { {},
  13. "property",
  14. {},
  15. "private" };
  16. const dap::VariablePresentationHint PrivateDataHint = { {},
  17. "data",
  18. {},
  19. "private" };
  20. }
  21. std::atomic<int64_t> cmDebuggerVariables::NextId(1);
  22. cmDebuggerVariables::cmDebuggerVariables(
  23. std::shared_ptr<cmDebuggerVariablesManager> variablesManager,
  24. std::string name, bool supportsVariableType)
  25. : Id(NextId.fetch_add(1))
  26. , Name(std::move(name))
  27. , SupportsVariableType(supportsVariableType)
  28. , VariablesManager(std::move(variablesManager))
  29. {
  30. VariablesManager->RegisterHandler(
  31. Id, [this](dap::VariablesRequest const& request) {
  32. (void)request;
  33. return this->HandleVariablesRequest();
  34. });
  35. }
  36. cmDebuggerVariables::cmDebuggerVariables(
  37. std::shared_ptr<cmDebuggerVariablesManager> variablesManager,
  38. std::string name, bool supportsVariableType,
  39. std::function<std::vector<cmDebuggerVariableEntry>()> getKeyValuesFunction)
  40. : Id(NextId.fetch_add(1))
  41. , Name(std::move(name))
  42. , GetKeyValuesFunction(std::move(getKeyValuesFunction))
  43. , SupportsVariableType(supportsVariableType)
  44. , VariablesManager(std::move(variablesManager))
  45. {
  46. VariablesManager->RegisterHandler(
  47. Id, [this](dap::VariablesRequest const& request) {
  48. (void)request;
  49. return this->HandleVariablesRequest();
  50. });
  51. }
  52. void cmDebuggerVariables::AddSubVariables(
  53. std::shared_ptr<cmDebuggerVariables> const& variables)
  54. {
  55. if (variables != nullptr) {
  56. SubVariables.emplace_back(variables);
  57. }
  58. }
  59. dap::array<dap::Variable> cmDebuggerVariables::HandleVariablesRequest()
  60. {
  61. dap::array<dap::Variable> variables;
  62. if (GetKeyValuesFunction != nullptr) {
  63. auto values = GetKeyValuesFunction();
  64. for (auto const& entry : values) {
  65. if (IgnoreEmptyStringEntries && entry.Type == "string" &&
  66. entry.Value.empty()) {
  67. continue;
  68. }
  69. variables.push_back(dap::Variable{ {},
  70. {},
  71. {},
  72. entry.Name,
  73. {},
  74. PrivateDataHint,
  75. entry.Type,
  76. entry.Value,
  77. 0 });
  78. }
  79. }
  80. EnumerateSubVariablesIfAny(variables);
  81. if (EnableSorting) {
  82. std::sort(variables.begin(), variables.end(),
  83. [](dap::Variable const& a, dap::Variable const& b) {
  84. return a.name < b.name;
  85. });
  86. }
  87. return variables;
  88. }
  89. void cmDebuggerVariables::EnumerateSubVariablesIfAny(
  90. dap::array<dap::Variable>& toBeReturned) const
  91. {
  92. dap::array<dap::Variable> ret;
  93. for (auto const& variables : SubVariables) {
  94. toBeReturned.emplace_back(
  95. dap::Variable{ {},
  96. {},
  97. {},
  98. variables->GetName(),
  99. {},
  100. PrivatePropertyHint,
  101. SupportsVariableType ? "collection" : nullptr,
  102. variables->GetValue(),
  103. variables->GetId() });
  104. }
  105. }
  106. void cmDebuggerVariables::ClearSubVariables()
  107. {
  108. SubVariables.clear();
  109. }
  110. cmDebuggerVariables::~cmDebuggerVariables()
  111. {
  112. ClearSubVariables();
  113. VariablesManager->UnregisterHandler(Id);
  114. }
  115. } // namespace cmDebugger