Skip to content

Commit 6215c4b

Browse files
committed
use get<T> api
1 parent 5cd781e commit 6215c4b

File tree

4 files changed

+49
-59
lines changed

4 files changed

+49
-59
lines changed

src/viam/examples/modules/complex/gizmo/impl.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@ std::string find_arg1(ResourceConfig cfg) {
2121
buffer << gizmo_name << ": Required parameter `arg1` not found in configuration";
2222
throw std::invalid_argument(buffer.str());
2323
}
24-
25-
const ProtoValue& arg1_val = arg1->second;
26-
if (arg1_val.is_a<std::string>() && !arg1_val.get_unchecked<std::string>().empty()) {
27-
return arg1_val.get_unchecked<std::string>();
24+
const auto* const arg1_string = arg1->second.get<std::string>();
25+
if (!arg1_string || arg1_string->empty()) {
26+
std::ostringstream buffer;
27+
buffer << gizmo_name << ": Required non-empty string parameter `arg1`"
28+
<< "` is either not a string or is an empty string";
29+
throw std::invalid_argument(buffer.str());
2830
}
29-
std::ostringstream buffer;
30-
buffer << gizmo_name << ": Required non-empty string parameter `arg1`"
31-
<< "` is either not a string or is an empty string";
32-
throw std::invalid_argument(buffer.str());
31+
return *arg1_string;
3332
}
3433

3534
void MyGizmo::reconfigure(const Dependencies& deps, const ResourceConfig& cfg) {

src/viam/examples/modules/complex/summation/impl.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ bool find_subtract(ResourceConfig cfg) {
1717
if (subtract == cfg.attributes().end()) {
1818
return false;
1919
}
20-
21-
return subtract->second.is_a<bool>() && subtract->second.get_unchecked<bool>();
20+
const bool* const subtract_bool = subtract->second.get<bool>();
21+
if (!subtract_bool) {
22+
return false;
23+
}
24+
return *subtract_bool;
2225
}
2326

2427
void MySummation::reconfigure(const Dependencies& deps, const ResourceConfig& cfg) {

src/viam/examples/modules/simple/main.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,15 @@ class Printer : public GenericService, public Reconfigurable {
5959
buffer << printer_name << ": Required parameter `to_print` not found in configuration";
6060
throw std::invalid_argument(buffer.str());
6161
}
62-
const ProtoValue& to_print_val = to_print->second;
63-
if (to_print_val.is_a<std::string>() &&
64-
!to_print_val.get_unchecked<std::string>().empty()) {
65-
return to_print_val.get_unchecked<std::string>();
62+
const auto* const to_print_string = to_print->second.get<std::string>();
63+
if (!to_print_string || to_print_string->empty()) {
64+
std::ostringstream buffer;
65+
buffer << printer_name
66+
<< ": Required non-empty string parameter `to_print` is either not a string "
67+
"or is an empty string";
68+
throw std::invalid_argument(buffer.str());
6669
}
67-
68-
std::ostringstream buffer;
69-
buffer << printer_name
70-
<< ": Required non-empty string parameter `to_print` is either not a string "
71-
"or is an empty string";
72-
throw std::invalid_argument(buffer.str());
70+
return *to_print_string;
7371
}
7472

7573
private:

src/viam/examples/modules/tflite/main.cpp

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -298,59 +298,55 @@ class MLModelServiceTFLite : public vsdk::MLModelService,
298298
<< ": Required parameter `model_path` not found in configuration";
299299
throw std::invalid_argument(buffer.str());
300300
}
301-
302-
const vsdk::ProtoValue& model_path_val = model_path->second;
303-
if (!model_path_val.is_a<std::string>() ||
304-
model_path_val.get_unchecked<std::string>().empty()) {
301+
const auto* const model_path_string = model_path->second.get<std::string>();
302+
if (!model_path_string || model_path_string->empty()) {
305303
std::ostringstream buffer;
306304
buffer << service_name
307305
<< ": Required non-empty string parameter `model_path` is either not a string "
308306
"or is an empty string";
309307
throw std::invalid_argument(buffer.str());
310308
}
311-
const std::string& model_path_string = model_path_val.get_unchecked<std::string>();
312309

313310
// Process any tensor name remappings provided in the config.
314311
auto remappings = attributes.find("tensor_name_remappings");
315312
if (remappings != attributes.end()) {
316-
if (!remappings->second.is_a<vsdk::ProtoStruct>()) {
313+
const auto remappings_attributes = remappings->second.get<vsdk::ProtoStruct>();
314+
if (!remappings_attributes) {
317315
std::ostringstream buffer;
318316
buffer << service_name
319317
<< ": Optional parameter `tensor_name_remappings` must be a dictionary";
320318
throw std::invalid_argument(buffer.str());
321319
}
322-
const auto remappings_attributes =
323-
remappings->second.get_unchecked<vsdk::ProtoStruct>();
324-
325320
const auto populate_remappings = [](const vsdk::ProtoValue& source, auto& target) {
326-
if (!source.is_a<vsdk::ProtoStruct>()) {
321+
const auto source_attributes = source.get<vsdk::ProtoStruct>();
322+
if (!source_attributes) {
327323
std::ostringstream buffer;
328324
buffer << service_name
329-
<< ": Fields `inputs` and `outputs` of `tensor_name_remappings` "
330-
"must be "
325+
<< ": Fields `inputs` and `outputs` of `tensor_name_remappings` must be "
331326
"dictionaries";
332327
throw std::invalid_argument(buffer.str());
333328
}
334-
for (const auto& kv : source.get_unchecked<vsdk::ProtoStruct>()) {
329+
for (const auto& kv : *source_attributes) {
335330
const auto& k = kv.first;
336-
if (!kv.second.is_a<std::string>()) {
331+
const auto* const kv_string = kv.second.get<std::string>();
332+
if (!kv_string) {
337333
std::ostringstream buffer;
338-
buffer << service_name
339-
<< ": Fields `inputs` and `outputs` of `tensor_name_remappings` "
340-
"must "
341-
"be dictionaries with string values";
334+
buffer
335+
<< service_name
336+
<< ": Fields `inputs` and `outputs` of `tensor_name_remappings` must "
337+
"be dictionaries with string values";
342338
throw std::invalid_argument(buffer.str());
343339
}
344-
target[kv.first] = kv.second.get_unchecked<std::string>();
340+
target[kv.first] = *kv_string;
345341
}
346342
};
347343

348-
const auto inputs_where = remappings_attributes.find("inputs");
349-
if (inputs_where != remappings_attributes.end()) {
344+
const auto inputs_where = remappings_attributes->find("inputs");
345+
if (inputs_where != remappings_attributes->end()) {
350346
populate_remappings(inputs_where->second, state->input_name_remappings);
351347
}
352-
const auto outputs_where = remappings_attributes.find("outputs");
353-
if (outputs_where != remappings_attributes.end()) {
348+
const auto outputs_where = remappings_attributes->find("outputs");
349+
if (outputs_where != remappings_attributes->end()) {
354350
populate_remappings(outputs_where->second, state->output_name_remappings);
355351
}
356352
}
@@ -366,11 +362,11 @@ class MLModelServiceTFLite : public vsdk::MLModelService,
366362
// buffer which we can use with `TfLiteModelCreate`. That
367363
// still requires that the buffer be kept valid, but that's
368364
// more easily done.
369-
const std::ifstream in(model_path_string, std::ios::in | std::ios::binary);
365+
const std::ifstream in(*model_path_string, std::ios::in | std::ios::binary);
370366
if (!in) {
371367
std::ostringstream buffer;
372368
buffer << service_name << ": Failed to open file for `model_path` "
373-
<< model_path_string;
369+
<< *model_path_string;
374370
throw std::invalid_argument(buffer.str());
375371
}
376372
std::ostringstream model_path_contents_stream;
@@ -405,27 +401,21 @@ class MLModelServiceTFLite : public vsdk::MLModelService,
405401
// object to carry that information.
406402
auto num_threads = attributes.find("num_threads");
407403
if (num_threads != attributes.end()) {
408-
auto throwError = [&] {
404+
const auto* num_threads_double = num_threads->second.get<double>();
405+
if (!num_threads_double || !std::isnormal(*num_threads_double) ||
406+
(*num_threads_double < 0) ||
407+
(*num_threads_double >= std::numeric_limits<std::int32_t>::max()) ||
408+
(std::trunc(*num_threads_double) != *num_threads_double)) {
409409
std::ostringstream buffer;
410410
buffer << service_name
411-
<< ": Value for field `num_threads` is not a positive integer";
411+
<< ": Value for field `num_threads` is not a positive integer: "
412+
<< *num_threads_double;
412413
throw std::invalid_argument(buffer.str());
413-
};
414-
415-
if (!num_threads->second.is_a<double>()) {
416-
throwError();
417-
}
418-
419-
double num_threads_double = num_threads->second.get_unchecked<double>();
420-
if (!std::isnormal(num_threads_double) || (num_threads_double < 0) ||
421-
(num_threads_double >= std::numeric_limits<std::int32_t>::max()) ||
422-
(std::trunc(num_threads_double) != num_threads_double)) {
423-
throwError();
424414
}
425415

426416
state->interpreter_options.reset(TfLiteInterpreterOptionsCreate());
427417
TfLiteInterpreterOptionsSetNumThreads(state->interpreter_options.get(),
428-
static_cast<int32_t>(num_threads_double));
418+
static_cast<int32_t>(*num_threads_double));
429419
}
430420

431421
// Build the single interpreter.

0 commit comments

Comments
 (0)