Files
OrcaSlicer/src/slic3r/GUI/DeviceCore/DevCtrl.cpp
T
Hanif Koh d8793f8353 Stop Leaking json Through Headers and Drop Includes Kept Only for the Name
AppConfig.hpp, DeviceManager.hpp and UserManager.hpp carried a global
"using namespace nlohmann;", json_diff.hpp a global "using json =
nlohmann::json;" and PrinterFileSystem.h a global "using nlohmann::json;".
Every file that included one of them, directly or not, could write a
bare json, and 63 did without declaring it.

The last two also made the include checker treat json_diff.hpp and
PrinterFileSystem.h as the headers that provide json, so they were
included from files that use nothing else from them: 57 of the 59
includers of json_diff.hpp never name json_diff.

The five statements are removed. Headers that use the type now spell
nlohmann::json, source files declare their own "using json =
nlohmann::json;", and the includes that only supplied the name are
dropped or replaced by <nlohmann/json.hpp>.

Eight files reached json_diff.hpp only through an include that is now
gone and with it lost that header's "using namespace std;". The std
names they used unqualified are qualified.
2026-10-07 01:39:17 +08:00

74 lines
1.7 KiB
C++

#include <ctime>
#include <nlohmann/json.hpp>
#include <string>
#include "DevCtrl.h"
// TODO: remove this include
#include "slic3r/GUI/DeviceManager.hpp"
using namespace nlohmann;
namespace Slic3r
{
DevCtrlInfo::DevCtrlInfo(MachineObject* obj, int sequence_id, const json& req_json,
int interval_max, int interval_min)
{
m_request_dev_id = obj->get_dev_id();
m_request_seq = sequence_id;
m_request_time = time(nullptr);
m_request_json = req_json;
m_request_interval_max = interval_max;
m_request_interval_min = interval_min;
}
bool DevCtrlInfo::CheckCanUpdateData(const nlohmann::json& jj)
{
if (m_request_json.empty()) {
return true;
}
if (m_time_out) {
return true;
}
if (time(nullptr) - m_request_time > m_request_interval_max) {
OnTimeOut();
return true;
}
if (time(nullptr) - m_request_time < m_request_interval_min) {
return false;
}
if (m_received) {
return true;
}
try {
if (jj.contains("sequence_id") && jj["sequence_id"].is_string()) {
int sequence_id = stoi(jj["sequence_id"].get<std::string>());
if (sequence_id >= m_request_seq) {
OnReceived();
return true;
}
}
} catch (...) {
;
}
return false;
}
int DevCtrl::command_select_extruder(int id)
{
json j;
j["print"]["sequence_id"] = std::to_string(MachineObject::m_sequence_id++);
j["print"]["command"] = "select_extruder";
j["print"]["extruder_index"] = id;
int rtn = m_obj->publish_json(j, 1);
if (rtn == 0)
{
m_obj->targ_nozzle_id_from_pc = id;
}
return rtn;
}
}