/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmDebuggerVariables.h" #include #include #include #include #include #include "cmDebuggerVariablesManager.h" namespace cmDebugger { namespace { const dap::VariablePresentationHint PrivatePropertyHint = { {}, "property", {}, "private" }; const dap::VariablePresentationHint PrivateDataHint = { {}, "data", {}, "private" }; } std::atomic cmDebuggerVariables::NextId(1); cmDebuggerVariables::cmDebuggerVariables( std::shared_ptr variablesManager, std::string name, bool supportsVariableType) : Id(NextId.fetch_add(1)) , Name(std::move(name)) , SupportsVariableType(supportsVariableType) , VariablesManager(std::move(variablesManager)) { VariablesManager->RegisterHandler( Id, [this](dap::VariablesRequest const& request) { (void)request; return this->HandleVariablesRequest(); }); } cmDebuggerVariables::cmDebuggerVariables( std::shared_ptr variablesManager, std::string name, bool supportsVariableType, std::function()> getKeyValuesFunction) : Id(NextId.fetch_add(1)) , Name(std::move(name)) , GetKeyValuesFunction(std::move(getKeyValuesFunction)) , SupportsVariableType(supportsVariableType) , VariablesManager(std::move(variablesManager)) { VariablesManager->RegisterHandler( Id, [this](dap::VariablesRequest const& request) { (void)request; return this->HandleVariablesRequest(); }); } void cmDebuggerVariables::AddSubVariables( std::shared_ptr const& variables) { if (variables) { SubVariables.emplace_back(variables); } } dap::array cmDebuggerVariables::HandleVariablesRequest() { dap::array variables; if (GetKeyValuesFunction) { auto values = GetKeyValuesFunction(); for (auto const& entry : values) { if (IgnoreEmptyStringEntries && entry.Type == "string" && entry.Value.empty()) { continue; } variables.push_back(dap::Variable{ {}, {}, {}, entry.Name, {}, PrivateDataHint, SupportsVariableType ? entry.Type : dap::optional(), entry.Value, 0 }); } } EnumerateSubVariablesIfAny(variables); if (EnableSorting) { std::sort(variables.begin(), variables.end(), [](dap::Variable const& a, dap::Variable const& b) { return a.name < b.name; }); } return variables; } void cmDebuggerVariables::EnumerateSubVariablesIfAny( dap::array& toBeReturned) const { dap::array ret; for (auto const& variables : SubVariables) { toBeReturned.emplace_back(dap::Variable{ {}, {}, {}, variables->GetName(), {}, PrivatePropertyHint, SupportsVariableType ? "collection" : dap::optional(), variables->GetValue(), variables->GetId() }); } } void cmDebuggerVariables::ClearSubVariables() { SubVariables.clear(); } cmDebuggerVariables::~cmDebuggerVariables() { ClearSubVariables(); VariablesManager->UnregisterHandler(Id); } } // namespace cmDebugger