diff --git a/src/viam/examples/modules/complex/gizmo/impl.cpp b/src/viam/examples/modules/complex/gizmo/impl.cpp index 7a9272d74..adbf8675a 100644 --- a/src/viam/examples/modules/complex/gizmo/impl.cpp +++ b/src/viam/examples/modules/complex/gizmo/impl.cpp @@ -21,15 +21,14 @@ std::string find_arg1(ResourceConfig cfg) { buffer << gizmo_name << ": Required parameter `arg1` not found in configuration"; throw std::invalid_argument(buffer.str()); } - - const ProtoValue& arg1_val = arg1->second; - if (arg1_val.is_a() && !arg1_val.get_unchecked().empty()) { - return arg1_val.get_unchecked(); + const auto* const arg1_string = arg1->second.get(); + if (!arg1_string || arg1_string->empty()) { + std::ostringstream buffer; + buffer << gizmo_name << ": Required non-empty string parameter `arg1`" + << "` is either not a string or is an empty string"; + throw std::invalid_argument(buffer.str()); } - std::ostringstream buffer; - buffer << gizmo_name << ": Required non-empty string parameter `arg1`" - << "` is either not a string or is an empty string"; - throw std::invalid_argument(buffer.str()); + return *arg1_string; } void MyGizmo::reconfigure(const Dependencies& deps, const ResourceConfig& cfg) { diff --git a/src/viam/examples/modules/simple/main.cpp b/src/viam/examples/modules/simple/main.cpp index 4e506d89b..edc400347 100644 --- a/src/viam/examples/modules/simple/main.cpp +++ b/src/viam/examples/modules/simple/main.cpp @@ -59,17 +59,15 @@ class Printer : public GenericService, public Reconfigurable { buffer << printer_name << ": Required parameter `to_print` not found in configuration"; throw std::invalid_argument(buffer.str()); } - const ProtoValue& to_print_val = to_print->second; - if (to_print_val.is_a() && - !to_print_val.get_unchecked().empty()) { - return to_print_val.get_unchecked(); + const auto* const to_print_string = to_print->second.get(); + if (!to_print_string || to_print_string->empty()) { + std::ostringstream buffer; + buffer << printer_name + << ": Required non-empty string parameter `to_print` is either not a string " + "or is an empty string"; + throw std::invalid_argument(buffer.str()); } - - std::ostringstream buffer; - buffer << printer_name - << ": Required non-empty string parameter `to_print` is either not a string " - "or is an empty string"; - throw std::invalid_argument(buffer.str()); + return *to_print_string; } private: diff --git a/src/viam/examples/modules/tflite/main.cpp b/src/viam/examples/modules/tflite/main.cpp index 3dd17621f..b700353f6 100644 --- a/src/viam/examples/modules/tflite/main.cpp +++ b/src/viam/examples/modules/tflite/main.cpp @@ -298,59 +298,55 @@ class MLModelServiceTFLite : public vsdk::MLModelService, << ": Required parameter `model_path` not found in configuration"; throw std::invalid_argument(buffer.str()); } - - const vsdk::ProtoValue& model_path_val = model_path->second; - if (!model_path_val.is_a() || - model_path_val.get_unchecked().empty()) { + const auto* const model_path_string = model_path->second.get(); + if (!model_path_string || model_path_string->empty()) { std::ostringstream buffer; buffer << service_name << ": Required non-empty string parameter `model_path` is either not a string " "or is an empty string"; throw std::invalid_argument(buffer.str()); } - const std::string& model_path_string = model_path_val.get_unchecked(); // Process any tensor name remappings provided in the config. auto remappings = attributes.find("tensor_name_remappings"); if (remappings != attributes.end()) { - if (!remappings->second.is_a()) { + const auto remappings_attributes = remappings->second.get(); + if (!remappings_attributes) { std::ostringstream buffer; buffer << service_name << ": Optional parameter `tensor_name_remappings` must be a dictionary"; throw std::invalid_argument(buffer.str()); } - const auto remappings_attributes = - remappings->second.get_unchecked(); - const auto populate_remappings = [](const vsdk::ProtoValue& source, auto& target) { - if (!source.is_a()) { + const auto source_attributes = source.get(); + if (!source_attributes) { std::ostringstream buffer; buffer << service_name - << ": Fields `inputs` and `outputs` of `tensor_name_remappings` " - "must be " + << ": Fields `inputs` and `outputs` of `tensor_name_remappings` must be " "dictionaries"; throw std::invalid_argument(buffer.str()); } - for (const auto& kv : source.get_unchecked()) { + for (const auto& kv : *source_attributes) { const auto& k = kv.first; - if (!kv.second.is_a()) { + const auto* const kv_string = kv.second.get(); + if (!kv_string) { std::ostringstream buffer; - buffer << service_name - << ": Fields `inputs` and `outputs` of `tensor_name_remappings` " - "must " - "be dictionaries with string values"; + buffer + << service_name + << ": Fields `inputs` and `outputs` of `tensor_name_remappings` must " + "be dictionaries with string values"; throw std::invalid_argument(buffer.str()); } - target[kv.first] = kv.second.get_unchecked(); + target[kv.first] = *kv_string; } }; - const auto inputs_where = remappings_attributes.find("inputs"); - if (inputs_where != remappings_attributes.end()) { + const auto inputs_where = remappings_attributes->find("inputs"); + if (inputs_where != remappings_attributes->end()) { populate_remappings(inputs_where->second, state->input_name_remappings); } - const auto outputs_where = remappings_attributes.find("outputs"); - if (outputs_where != remappings_attributes.end()) { + const auto outputs_where = remappings_attributes->find("outputs"); + if (outputs_where != remappings_attributes->end()) { populate_remappings(outputs_where->second, state->output_name_remappings); } } @@ -366,11 +362,11 @@ class MLModelServiceTFLite : public vsdk::MLModelService, // buffer which we can use with `TfLiteModelCreate`. That // still requires that the buffer be kept valid, but that's // more easily done. - const std::ifstream in(model_path_string, std::ios::in | std::ios::binary); + const std::ifstream in(*model_path_string, std::ios::in | std::ios::binary); if (!in) { std::ostringstream buffer; buffer << service_name << ": Failed to open file for `model_path` " - << model_path_string; + << *model_path_string; throw std::invalid_argument(buffer.str()); } std::ostringstream model_path_contents_stream; @@ -405,27 +401,21 @@ class MLModelServiceTFLite : public vsdk::MLModelService, // object to carry that information. auto num_threads = attributes.find("num_threads"); if (num_threads != attributes.end()) { - auto throwError = [&] { + const auto* num_threads_double = num_threads->second.get(); + if (!num_threads_double || !std::isnormal(*num_threads_double) || + (*num_threads_double < 0) || + (*num_threads_double >= std::numeric_limits::max()) || + (std::trunc(*num_threads_double) != *num_threads_double)) { std::ostringstream buffer; buffer << service_name - << ": Value for field `num_threads` is not a positive integer"; + << ": Value for field `num_threads` is not a positive integer: " + << *num_threads_double; throw std::invalid_argument(buffer.str()); - }; - - if (!num_threads->second.is_a()) { - throwError(); - } - - double num_threads_double = num_threads->second.get_unchecked(); - if (!std::isnormal(num_threads_double) || (num_threads_double < 0) || - (num_threads_double >= std::numeric_limits::max()) || - (std::trunc(num_threads_double) != num_threads_double)) { - throwError(); } state->interpreter_options.reset(TfLiteInterpreterOptionsCreate()); TfLiteInterpreterOptionsSetNumThreads(state->interpreter_options.get(), - static_cast(num_threads_double)); + static_cast(*num_threads_double)); } // Build the single interpreter.