From 019cef5c35ce354636fb8a3ee957d1470e897d5f Mon Sep 17 00:00:00 2001 From: Mitch Bradley Date: Tue, 13 Aug 2024 08:41:18 -1000 Subject: [PATCH] Complain about undefined parameters --- FluidNC/src/Job.cpp | 4 ++-- FluidNC/src/Job.h | 15 +++++++++++---- FluidNC/src/Parameters.cpp | 33 +++++++++++++++++++++------------ 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/FluidNC/src/Job.cpp b/FluidNC/src/Job.cpp index 0bb342377..5a0eda6f0 100644 --- a/FluidNC/src/Job.cpp +++ b/FluidNC/src/Job.cpp @@ -59,8 +59,8 @@ void Job::abort() { } } -float Job::get_param(const std::string& name) { - return job.top()->get_param(name); +bool Job::get_param(const std::string& name, float& value) { + return job.top()->get_param(name, value); } void Job::set_param(const std::string& name, float value) { job.top()->set_param(name, value); diff --git a/FluidNC/src/Job.h b/FluidNC/src/Job.h index 77215a012..3a87b3c23 100644 --- a/FluidNC/src/Job.h +++ b/FluidNC/src/Job.h @@ -11,9 +11,16 @@ class JobSource { public: JobSource(Channel* channel) : _channel(channel) {} - float get_param(const std::string& name) { return _local_params[name]; } - void set_param(const std::string& name, float value) { _local_params[name] = value; } - bool param_exists(const std::string& name) { return _local_params.count(name) != 0; } + bool get_param(const std::string& name, float& value) { + auto it = _local_params.find(name); + if (it == _local_params.end()) { + return false; + } + value = it->second; + return true; + } + void set_param(const std::string& name, float value) { _local_params[name] = value; } + bool param_exists(const std::string& name) { return _local_params.count(name) != 0; } void save() { _channel->save(); } void restore() { _channel->restore(); } @@ -41,7 +48,7 @@ class Job { static void abort(); static JobSource* source(); - static float get_param(const std::string& name); + static bool get_param(const std::string& name, float& value); static void set_param(const std::string& name, float value); static bool param_exists(const std::string& name); static Channel* channel(); diff --git a/FluidNC/src/Parameters.cpp b/FluidNC/src/Parameters.cpp index 07c567f80..aab7d6baa 100644 --- a/FluidNC/src/Parameters.cpp +++ b/FluidNC/src/Parameters.cpp @@ -386,25 +386,30 @@ bool named_param_exists(std::string& name) { return global_named_params.count(search) != 0; } -bool get_param(const param_ref_t& param_ref, float& result) { +bool get_global_named_param(const std::string& name, float& value) { + auto it = global_named_params.find(name); + if (it == global_named_params.end()) { + return false; + } + value = it->second; + return true; +} + +bool get_param(const param_ref_t& param_ref, float& value) { auto name = param_ref.name; if (name.length()) { if (name[0] == '/') { - return get_config_item(name, result); + return get_config_item(name, value); } - bool got; if (name[0] == '_') { - got = get_system_param(name, result); - if (got) { + if (get_system_param(name, value)) { return true; } - result = global_named_params[name]; - return true; + return get_global_named_param(name, value); } - result = Job::active() ? Job::get_param(name) : global_named_params[name]; - return true; + return Job::active() ? Job::get_param(name, value) : get_global_named_param(name, value); } - return get_numbered_param(param_ref.id, result); + return get_numbered_param(param_ref.id, value); } bool get_param_ref(const char* line, size_t& pos, param_ref_t& param_ref) { @@ -462,7 +467,7 @@ bool get_param_ref(const char* line, size_t& pos, param_ref_t& param_ref) { } } -void set_named_param(const char* name, float value) { +void set_named_param(const std::string& name, float value) { global_named_params[name] = value; } @@ -476,7 +481,7 @@ void set_param(const param_ref_t& param_ref, float value) { if (name[0] != '_' && Job::active()) { Job::set_param(name, value); } else { - global_named_params[name] = value; + set_named_param(name, value); } return; } @@ -495,6 +500,10 @@ bool read_number(const char* line, size_t& pos, float& result, bool in_expressio if (!get_param_ref(line, pos, param_ref)) { return false; } + if (get_param(param_ref, result)) { + return true; + } + log_debug("Undefined parameter " << param_ref.name); return get_param(param_ref, result); } if (c == '[') {