cmDebuggerVariables.cxx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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) {
  56. SubVariables.emplace_back(variables);
  57. }
  58. }
  59. dap::array<dap::Variable> cmDebuggerVariables::HandleVariablesRequest()
  60. {
  61. dap::array<dap::Variable> variables;
  62. if (GetKeyValuesFunction) {
  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. {},
  73. entry.Name,
  74. {},
  75. PrivateDataHint,
  76. SupportsVariableType ? entry.Type : dap::optional<dap::string>(),
  77. entry.Value,
  78. 0 });
  79. }
  80. }
  81. EnumerateSubVariablesIfAny(variables);
  82. if (EnableSorting) {
  83. std::sort(variables.begin(), variables.end(),
  84. [](dap::Variable const& a, dap::Variable const& b) {
  85. return a.name < b.name;
  86. });
  87. }
  88. return variables;
  89. }
  90. void cmDebuggerVariables::EnumerateSubVariablesIfAny(
  91. dap::array<dap::Variable>& toBeReturned) const
  92. {
  93. dap::array<dap::Variable> ret;
  94. for (auto const& variables : SubVariables) {
  95. toBeReturned.emplace_back(dap::Variable{
  96. {},
  97. {},
  98. {},
  99. variables->GetName(),
  100. {},
  101. PrivatePropertyHint,
  102. SupportsVariableType ? "collection" : dap::optional<dap::string>(),
  103. variables->GetValue(),
  104. variables->GetId() });
  105. }
  106. }
  107. void cmDebuggerVariables::ClearSubVariables()
  108. {
  109. SubVariables.clear();
  110. }
  111. cmDebuggerVariables::~cmDebuggerVariables()
  112. {
  113. ClearSubVariables();
  114. VariablesManager->UnregisterHandler(Id);
  115. }
  116. } // namespace cmDebugger