improved efficiency of printing out struct queue messages
diff --git a/aos/common/print_field_helpers.h b/aos/common/print_field_helpers.h
new file mode 100644
index 0000000..48acdf6
--- /dev/null
+++ b/aos/common/print_field_helpers.h
@@ -0,0 +1,41 @@
+#ifndef AOS_COMMON_PRINT_FIELD_HELPERS_H_
+#define AOS_COMMON_PRINT_FIELD_HELPERS_H_
+
+#include <stdint.h>
+
+#include <type_traits>
+
+namespace aos {
+
+template<typename T>
+inline bool PrintInteger(char *buf, T val, size_t *output) {
+ static const bool is_signed = ::std::is_signed<T>::value;
+
+ size_t len = 0;
+ if (is_signed && val <= 0) {
+ while (*output > len && (val != 0 || len == 0)) {
+ buf[len++] = '0' - (val % 10);
+ val /= 10;
+ }
+ buf[len++] = '-';
+ } else {
+ while (*output > len && (val != 0 || len == 0)) {
+ buf[len++] = '0' + (val % 10);
+ val /= 10;
+ }
+ }
+ // If we have enough space.
+ if (*output >= len) {
+ for (size_t i = 0; i < (len >> 1); i++) {
+ std::swap(buf[len - 1 - i], buf[i]);
+ }
+ *output -= len;
+ return true;
+ } else {
+ return false;
+ }
+}
+
+} // namespace aos
+
+#endif // AOS_COMMON_PRINT_FIELD_HELPERS_H_
diff --git a/aos/common/queue_types.cc b/aos/common/queue_types.cc
index 93cf135..8c22d90 100644
--- a/aos/common/queue_types.cc
+++ b/aos/common/queue_types.cc
@@ -148,12 +148,12 @@
type_cache::Get(type.fields[i]->type))) {
return false;
}
+ // Ignore the trailing '\0' that the subcall put on.
+ output -= 1;
}
- // Update the input and output pointers, ignoring the trailing '\0' that the
- // subcall put on.
- output += output_bytes_before - *output_bytes - 1;
- *output_bytes += 1;
+ // Update the input and output pointers.
+ output += output_bytes_before - *output_bytes;
input =
static_cast<const char *>(input) + input_bytes_before - *input_bytes;
}
@@ -208,10 +208,8 @@
return false;
}
CHECK_EQ(0u, input_bytes);
- // Update the output pointer, ignoring the trailing '\0' that
- // the subcall put on.
- output += output_bytes_before - *output_bytes - 1;
- *output_bytes += 1;
+ // Update the output pointer.
+ output += output_bytes_before - *output_bytes;
}
if (*output_bytes < 1) return false;
diff --git a/aos/common/queue_types.h b/aos/common/queue_types.h
index b95c946..eb0527e 100644
--- a/aos/common/queue_types.h
+++ b/aos/common/queue_types.h
@@ -102,6 +102,7 @@
//
// Prints the value from 1 primitive message field into output.
// The implementation of this is generated by the ruby code.
+// Does not include a trailing '\0' in what it subtracts from *output_bytes.
bool PrintField(char *output, size_t *output_bytes, const void *input,
size_t *input_bytes, uint32_t type)
__attribute__((warn_unused_result));