@@ -531,7 +531,33 @@ inline std::pair<uint32_t, WasmDataPtr> getStatus() {
531
531
}
532
532
533
533
// Generic selector
534
- inline std::optional<WasmDataPtr> getProperty (std::initializer_list<std::string_view> parts) {
534
+ inline std::optional<WasmDataPtr>
535
+ getProperty (const std::initializer_list<std::string_view> &parts) {
536
+ size_t size = 0 ;
537
+ for (auto part : parts) {
538
+ size += part.size () + 1 ; // null terminated string value
539
+ }
540
+
541
+ char *buffer = static_cast <char *>(::malloc (size));
542
+ char *b = buffer;
543
+
544
+ for (auto part : parts) {
545
+ memcpy (b, part.data (), part.size ());
546
+ b += part.size ();
547
+ *b++ = 0 ;
548
+ }
549
+
550
+ const char *value_ptr = nullptr ;
551
+ size_t value_size = 0 ;
552
+ auto result = proxy_get_property (buffer, size, &value_ptr, &value_size);
553
+ ::free (buffer);
554
+ if (result != WasmResult::Ok) {
555
+ return {};
556
+ }
557
+ return std::make_unique<WasmData>(value_ptr, value_size);
558
+ }
559
+
560
+ template <typename S> inline std::optional<WasmDataPtr> getProperty (const std::vector<S> &parts) {
535
561
size_t size = 0 ;
536
562
for (auto part : parts) {
537
563
size += part.size () + 1 ; // null terminated string value
@@ -560,7 +586,8 @@ inline std::optional<WasmDataPtr> getProperty(std::initializer_list<std::string_
560
586
// Durations are represented as int64 nanoseconds.
561
587
// Timestamps are represented as int64 Unix nanoseconds.
562
588
// Strings and bytes are represented as std::string.
563
- template <typename T> inline bool getValue (std::initializer_list<std::string_view> parts, T *out) {
589
+ template <typename T>
590
+ inline bool getValue (const std::initializer_list<std::string_view> &parts, T *out) {
564
591
auto buf = getProperty (parts);
565
592
if (!buf.has_value () || buf.value ()->size () != sizeof (T)) {
566
593
return false ;
@@ -571,7 +598,39 @@ template <typename T> inline bool getValue(std::initializer_list<std::string_vie
571
598
572
599
// Specialization for bytes and string values
573
600
template <>
574
- inline bool getValue<std::string>(std::initializer_list<std::string_view> parts, std::string *out) {
601
+ inline bool getValue<std::string>(const std::initializer_list<std::string_view> &parts,
602
+ std::string *out) {
603
+ auto buf = getProperty (parts);
604
+ if (!buf.has_value ()) {
605
+ return false ;
606
+ }
607
+ out->assign (buf.value ()->data (), buf.value ()->size ());
608
+ return true ;
609
+ }
610
+
611
+ template <typename S, typename T> inline bool getValue (const std::vector<S> &parts, T *out) {
612
+ auto buf = getProperty (parts);
613
+ if (!buf.has_value () || buf.value ()->size () != sizeof (T)) {
614
+ return false ;
615
+ }
616
+ *out = *reinterpret_cast <const T *>(buf.value ()->data ());
617
+ return true ;
618
+ }
619
+
620
+ template <>
621
+ inline bool getValue<std::string, std::string>(const std::vector<std::string> &parts,
622
+ std::string *out) {
623
+ auto buf = getProperty (parts);
624
+ if (!buf.has_value ()) {
625
+ return false ;
626
+ }
627
+ out->assign (buf.value ()->data (), buf.value ()->size ());
628
+ return true ;
629
+ }
630
+
631
+ template <>
632
+ inline bool getValue<std::string_view, std::string>(const std::vector<std::string_view> &parts,
633
+ std::string *out) {
575
634
auto buf = getProperty (parts);
576
635
if (!buf.has_value ()) {
577
636
return false ;
@@ -582,7 +641,20 @@ inline bool getValue<std::string>(std::initializer_list<std::string_view> parts,
582
641
583
642
// Specialization for message types (including struct value for lists and maps)
584
643
template <typename T>
585
- inline bool getMessageValue (std::initializer_list<std::string_view> parts, T *value_ptr) {
644
+ inline bool getMessageValue (const std::initializer_list<std::string_view> &parts, T *value_ptr) {
645
+ auto buf = getProperty (parts);
646
+ if (!buf.has_value ()) {
647
+ return false ;
648
+ }
649
+ if (buf.value ()->size () == 0 ) {
650
+ value_ptr = nullptr ;
651
+ return true ;
652
+ }
653
+ return value_ptr->ParseFromArray (buf.value ()->data (), buf.value ()->size ());
654
+ }
655
+
656
+ template <typename S, typename T>
657
+ inline bool getMessageValue (const std::vector<S> &parts, T *value_ptr) {
586
658
auto buf = getProperty (parts);
587
659
if (!buf.has_value ()) {
588
660
return false ;
0 commit comments