From f26265fb3d468956ccb6a6664754064fc6b6c6be Mon Sep 17 00:00:00 2001 From: Thomas Debrunner Date: Mon, 25 Sep 2023 14:11:47 +0200 Subject: [PATCH] clang-format: re-apply autoformat --- ulog_cpp/data_container.cpp | 16 +-- ulog_cpp/messages.cpp | 75 +++++++------ ulog_cpp/messages.hpp | 216 ++++++++++++++++++------------------ ulog_cpp/simple_writer.cpp | 9 +- ulog_cpp/subscription.hpp | 68 ++++++------ 5 files changed, 200 insertions(+), 184 deletions(-) diff --git a/ulog_cpp/data_container.cpp b/ulog_cpp/data_container.cpp index b25f45a..20e1820 100644 --- a/ulog_cpp/data_container.cpp +++ b/ulog_cpp/data_container.cpp @@ -28,22 +28,22 @@ void DataContainer::headerComplete() // try to resolve all fields for all message infos for (auto& it : _message_info) { - auto& message_info = it.second; - message_info.field().resolveDefinition(_message_formats, 0); + auto& message_info = it.second; + message_info.field().resolveDefinition(_message_formats, 0); } // try to resolve all fields for params - for (auto &it : _default_parameters) { - auto ¶meter = it.second; + for (auto& it : _default_parameters) { + auto& parameter = it.second; parameter.field().resolveDefinition(_message_formats, 0); } - for (auto &it : _initial_parameters) { - auto ¶meter = it.second; - parameter.field().resolveDefinition(_message_formats, 0); + for (auto& it : _initial_parameters) { + auto& parameter = it.second; + parameter.field().resolveDefinition(_message_formats, 0); } - for (auto ¶meter : _changed_parameters) { + for (auto& parameter : _changed_parameters) { parameter.field().resolveDefinition(_message_formats, 0); } diff --git a/ulog_cpp/messages.cpp b/ulog_cpp/messages.cpp index 4dcebd3..19ed293 100644 --- a/ulog_cpp/messages.cpp +++ b/ulog_cpp/messages.cpp @@ -7,8 +7,8 @@ #include #include -#include #include +#include #define CHECK_MSG_SIZE(size, min_required) \ if ((size) < (min_required)) throw ParsingException("message too short") @@ -106,27 +106,28 @@ Field::Field(const char* str, int len) } } -void Field::resolveDefinition(const std::map>& existing_formats, int offset) +void Field::resolveDefinition( + const std::map>& existing_formats, int offset) { - // if this is already resolved, do nothing - if (definitionResolved()) { - return; - } + // if this is already resolved, do nothing + if (definitionResolved()) { + return; + } - _offset_in_message_bytes = offset; - // if this is a basic type, we are done - if (_type.type != Field::BasicType::NESTED) { - return; - } + _offset_in_message_bytes = offset; + // if this is a basic type, we are done + if (_type.type != Field::BasicType::NESTED) { + return; + } - auto it = existing_formats.find(_type.name); - if (it == existing_formats.end()) { - throw ParsingException("Message format not found: " + _type.name); - } - _type.nested_message = it->second; - // recursively resolve nested type - _type.nested_message->resolveDefinition(existing_formats); - _type.size = _type.nested_message->sizeBytes(); + auto it = existing_formats.find(_type.name); + if (it == existing_formats.end()) { + throw ParsingException("Message format not found: " + _type.name); + } + _type.nested_message = it->second; + // recursively resolve nested type + _type.nested_message->resolveDefinition(existing_formats); + _type.size = _type.nested_message->sizeBytes(); } void Field::resolveDefinition(int offset) @@ -142,19 +143,25 @@ void Field::resolveDefinition(int offset) } const std::map Field::kBasicTypes{ - {"int8_t", {"int8_t", Field::BasicType::INT8, 1}}, {"uint8_t", {"uint8_t", Field::BasicType::UINT8, 1}}, - {"int16_t", {"int16_t", Field::BasicType::INT16, 2}}, {"uint16_t", {"uint16_t", Field::BasicType::UINT16, 2}}, - {"int32_t", {"int32_t", Field::BasicType::INT32, 4}}, {"uint32_t", {"uint32_t", Field::BasicType::UINT32, 4}}, - {"int64_t", {"int64_t", Field::BasicType::INT64, 8}}, {"uint64_t", {"uint64_t", Field::BasicType::UINT64, 8}}, - {"float", {"float", Field::BasicType::FLOAT, 4}}, {"double", {"double", Field::BasicType::DOUBLE, 8}}, - {"bool", {"bool", Field::BasicType::BOOL, 1}}, {"char", {"char", Field::BasicType::CHAR, 1}}}; - - -int Field::sizeBytes() const { + {"int8_t", {"int8_t", Field::BasicType::INT8, 1}}, + {"uint8_t", {"uint8_t", Field::BasicType::UINT8, 1}}, + {"int16_t", {"int16_t", Field::BasicType::INT16, 2}}, + {"uint16_t", {"uint16_t", Field::BasicType::UINT16, 2}}, + {"int32_t", {"int32_t", Field::BasicType::INT32, 4}}, + {"uint32_t", {"uint32_t", Field::BasicType::UINT32, 4}}, + {"int64_t", {"int64_t", Field::BasicType::INT64, 8}}, + {"uint64_t", {"uint64_t", Field::BasicType::UINT64, 8}}, + {"float", {"float", Field::BasicType::FLOAT, 4}}, + {"double", {"double", Field::BasicType::DOUBLE, 8}}, + {"bool", {"bool", Field::BasicType::BOOL, 1}}, + {"char", {"char", Field::BasicType::CHAR, 1}}}; + +int Field::sizeBytes() const +{ if (!definitionResolved()) { throw ParsingException("Unresolved type " + _type.name); } - return _type.size * ((_array_length == -1) ? 1: _array_length); + return _type.size * ((_array_length == -1) ? 1 : _array_length); } std::string Field::encode() const @@ -234,7 +241,8 @@ Value::NativeTypeVariant Value::asNativeTypeVariant() const if (_backing_ref.size() < _field_ref.arrayLength()) { throw ParsingException("Decoding fault, memory too short"); } - return std::string(reinterpret_cast(_backing_ref.data()), _field_ref.arrayLength()); + return std::string(reinterpret_cast(_backing_ref.data()), + _field_ref.arrayLength()); case Field::BasicType::NESTED: throw ParsingException("Can't get nested field as basic type. Field " + _field_ref.name()); } @@ -319,7 +327,7 @@ MessageFormat::MessageFormat(const uint8_t* msg) format_str = format_str.substr(semicolon + 1); } } -MessageFormat::MessageFormat(std::string name, const std::vector &fields) +MessageFormat::MessageFormat(std::string name, const std::vector& fields) : _name(std::move(name)) { for (const auto& field : fields) { @@ -332,13 +340,14 @@ MessageFormat::MessageFormat(std::string name, const std::vector &fields) int MessageFormat::sizeBytes() const { int size = 0; - for (const auto &it : _fields) { + for (const auto& it : _fields) { size += it.second->sizeBytes(); } return size; } -void MessageFormat::resolveDefinition(const std::map>& existing_formats) const +void MessageFormat::resolveDefinition( + const std::map>& existing_formats) const { int offset = 0; for (const auto& field : _fields_ordered) { diff --git a/ulog_cpp/messages.hpp b/ulog_cpp/messages.hpp index 0fc465b..db28f0c 100644 --- a/ulog_cpp/messages.hpp +++ b/ulog_cpp/messages.hpp @@ -9,11 +9,10 @@ #include #include #include +#include #include #include #include -#include -#include #include "exception.hpp" #include "raw_messages.hpp" @@ -48,11 +47,10 @@ class FileHeader { bool _has_flag_bits{true}; }; -class MessageFormat; // forward declaration +class MessageFormat; // forward declaration class Field { public: - enum class BasicType { INT8, UINT8, @@ -77,8 +75,10 @@ class Field { TypeAttributes() = default; - TypeAttributes(std::string name, BasicType type_, int size_) : name(std::move(name)), - type(type_), size(size_), nested_message(nullptr) {} + TypeAttributes(std::string name, BasicType type_, int size_) + : name(std::move(name)), type(type_), size(size_), nested_message(nullptr) + { + } }; static const std::map kBasicTypes; @@ -87,67 +87,63 @@ class Field { Field(const char* str, int len); - Field(const std::string &type_str, std::string name_str, int array_length_int = -1) + Field(const std::string& type_str, std::string name_str, int array_length_int = -1) : _array_length(array_length_int), _name(std::move(name_str)) { - auto it = kBasicTypes.find(type_str); - if (it != kBasicTypes.end()) { - _type = it->second; - } else { - // if not a basic type, set it to recursive - _type = TypeAttributes(type_str, BasicType::NESTED, 0); - } + auto it = kBasicTypes.find(type_str); + if (it != kBasicTypes.end()) { + _type = it->second; + } else { + // if not a basic type, set it to recursive + _type = TypeAttributes(type_str, BasicType::NESTED, 0); + } } std::string encode() const; - bool operator==(const Field& field) const { - return _type.name == field._type.name && _array_length == field._array_length && _name == field._name; + return _type.name == field._type.name && _array_length == field._array_length && + _name == field._name; } - inline const TypeAttributes& type() const { - return _type; - } + inline const TypeAttributes& type() const { return _type; } - inline int arrayLength() const { - return _array_length; - } + inline int arrayLength() const { return _array_length; } - inline int offsetInMessage() const { - return _offset_in_message_bytes; - } + inline int offsetInMessage() const { return _offset_in_message_bytes; } - inline const std::string& name() const { - return _name; - } + inline const std::string& name() const { return _name; } inline int sizeBytes() const; - inline bool definitionResolved() const { - return _offset_in_message_bytes >= 0 && (_type.type != BasicType::NESTED || _type.nested_message != nullptr); + inline bool definitionResolved() const + { + return _offset_in_message_bytes >= 0 && + (_type.type != BasicType::NESTED || _type.nested_message != nullptr); }; - void resolveDefinition(const std::map>& existing_formats, int offset); + void resolveDefinition( + const std::map>& existing_formats, int offset); void resolveDefinition(int offset); private: TypeAttributes _type; int _array_length{-1}; ///< -1 means not-an-array - int _offset_in_message_bytes{-1}; ///< default to begin of message. Gets filled on MessageFormat resolution + int _offset_in_message_bytes{ + -1}; ///< default to begin of message. Gets filled on MessageFormat resolution std::string _name; }; class Value { public: - using NativeTypeVariant = std::variant< - int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, - float, double, bool, char, std::vector, std::vector, std::vector, - std::vector, std::vector, std::vector, std::vector, - std::vector, std::vector, std::vector, std::vector, - std::string>; + using NativeTypeVariant = + std::variant, std::vector, + std::vector, std::vector, std::vector, + std::vector, std::vector, std::vector, + std::vector, std::vector, std::vector, std::string>; template struct is_vector : std::false_type {}; @@ -157,87 +153,96 @@ class Value { template struct is_string : std::is_same, std::string> {}; - Value(const Field& field_ref, const std::vector& backing_ref) : _field_ref(field_ref), _backing_ref(backing_ref) {} + Value(const Field& field_ref, const std::vector& backing_ref) + : _field_ref(field_ref), _backing_ref(backing_ref) + { + } NativeTypeVariant asNativeTypeVariant() const; template - T as() const { - T res; - std::visit([&res](auto&& arg) { - using NativeType = std::decay_t; - using ReturnType = T; - if constexpr (is_string::value || is_string::value) { - // there is a string involved. Only allowed when both are string - if constexpr (is_string::value && is_string::value) { - // both are string - res = arg; - } else { - // one is string, the other is not - throw ParsingException("Assign strings and non-string types"); - } - } else if constexpr (is_vector::value) { - // this is natively a vector - if constexpr (is_vector::value) { - // return type is also vector - if constexpr (std::is_same::value) { - // return type is same as native type - res = arg; - } else { - // return type is different from native type, but a vector - res.resize(arg.size()); - for (std::size_t i=0; i(arg[i]); + T as() const + { + T res; + std::visit( + [&res](auto&& arg) { + using NativeType = std::decay_t; + using ReturnType = T; + if constexpr (is_string::value || is_string::value) { + // there is a string involved. Only allowed when both are string + if constexpr (is_string::value && is_string::value) { + // both are string + res = arg; + } else { + // one is string, the other is not + throw ParsingException("Assign strings and non-string types"); } - } - } else { - // return type is not a vector, just return first element - if (arg.size() > 0) { + } else if constexpr (is_vector::value) { + // this is natively a vector + if constexpr (is_vector::value) { + // return type is also vector + if constexpr (std::is_same::value) { + // return type is same as native type + res = arg; + } else { + // return type is different from native type, but a vector + res.resize(arg.size()); + for (std::size_t i = 0; i < arg.size(); i++) { + res[i] = static_cast(arg[i]); + } + } + } else { + // return type is not a vector, just return first element + if (arg.size() > 0) { res = static_cast(arg[0]); - } else { + } else { throw ParsingException("Cannot convert empty vector to non-vector type"); + } + } + } else { + // this is natively not a vector + if constexpr (is_vector::value) { + // return type is a vector + res.resize(1); + res[0] = static_cast(arg); + } else { + // return type is not a vector + res = static_cast(arg); + } } - } - } else { - // this is natively not a vector - if constexpr (is_vector::value) { - // return type is a vector - res.resize(1); - res[0] = static_cast(arg); - } else { - // return type is not a vector - res = static_cast(arg); - } - } - }, asNativeTypeVariant()); + }, + asNativeTypeVariant()); return res; } template - operator T() { + operator T() + { return as(); } - private: - const Field &_field_ref; - const std::vector &_backing_ref; + private: + const Field& _field_ref; + const std::vector& _backing_ref; template static T deserialize(const std::vector& backing, int offset) { T v; if (backing.size() - offset < sizeof(v)) { - throw ParsingException("Unexpected data type size"); + throw ParsingException("Unexpected data type size"); } memcpy(&v, backing.data() + offset, sizeof(v)); return v; } template - static std::vector deserializeVector(const std::vector& backing, int offset, int size) { + static std::vector deserializeVector(const std::vector& backing, int offset, int size) + { std::vector res; res.resize(size); - for (int i=0; i(backing, offset + i * sizeof(T)); } return res; @@ -283,7 +288,7 @@ class MessageFormat { public: explicit MessageFormat(const uint8_t* msg); - explicit MessageFormat(std::string name, const std::vector &fields); + explicit MessageFormat(std::string name, const std::vector& fields); const std::string& name() const { return _name; } const std::map>& fieldMap() const { return _fields; } @@ -294,24 +299,26 @@ class MessageFormat { if (_name != format._name) { return false; } - if (_fields.size() != format._fields.size()) { - return false; - } - for (size_t i=0; i<_fields_ordered.size(); i++) { - if (!(*_fields_ordered[i] == *format._fields_ordered[i])) { - return false; - } - } - return true; + if (_fields.size() != format._fields.size()) { + return false; + } + for (size_t i = 0; i < _fields_ordered.size(); i++) { + if (!(*_fields_ordered[i] == *format._fields_ordered[i])) { + return false; + } + } + return true; } - void resolveDefinition(const std::map>& existing_formats) const; + void resolveDefinition( + const std::map>& existing_formats) const; int sizeBytes() const; std::vector> fields() const { return _fields_ordered; } - std::vector fieldNames() const { + std::vector fieldNames() const + { std::vector names; for (auto& field : _fields_ordered) { names.push_back(field->name()); @@ -319,10 +326,7 @@ class MessageFormat { return names; } - std::shared_ptr field(const std::string& name) const { - return _fields.at(name); - } - + std::shared_ptr field(const std::string& name) const { return _fields.at(name); } private: std::string _name; diff --git a/ulog_cpp/simple_writer.cpp b/ulog_cpp/simple_writer.cpp index 50ef8c2..45b8ef0 100644 --- a/ulog_cpp/simple_writer.cpp +++ b/ulog_cpp/simple_writer.cpp @@ -40,14 +40,14 @@ SimpleWriter::~SimpleWriter() } } -void SimpleWriter::writeMessageFormat(const std::string& name, const std::vector &fields) +void SimpleWriter::writeMessageFormat(const std::string& name, const std::vector& fields) { if (_header_complete) { throw UsageException("Header already complete"); } // Ensure the first field is the 64 bit timestamp. This is a bit stricter than what ULog requires - if (fields.empty() || fields[0].name() != "timestamp" || fields[0].type().type != Field::BasicType::UINT64 || - fields[0].arrayLength() != -1) { + if (fields.empty() || fields[0].name() != "timestamp" || + fields[0].type().type != Field::BasicType::UINT64 || fields[0].arrayLength() != -1) { throw UsageException("First message field must be 'uint64_t timestamp'"); } if (_formats.find(name) != _formats.end()) { @@ -70,7 +70,8 @@ void SimpleWriter::writeMessageFormat(const std::string& name, const std::vector for (const auto& field : fields) { const auto& basic_type_iter = Field::kBasicTypes.find(field.type().name); if (basic_type_iter == Field::kBasicTypes.end()) { - throw UsageException("Invalid field type (nested formats are not supported): " + field.type().name); + throw UsageException("Invalid field type (nested formats are not supported): " + + field.type().name); } const int array_size = field.arrayLength() <= 0 ? 1 : field.arrayLength(); if (message_size % basic_type_iter->second.size != 0) { diff --git a/ulog_cpp/subscription.hpp b/ulog_cpp/subscription.hpp index 8b1ff34..284e65a 100644 --- a/ulog_cpp/subscription.hpp +++ b/ulog_cpp/subscription.hpp @@ -19,37 +19,30 @@ class TypedDataView { std::string name() const { return _message_format_ref.name(); } - Value at(const Field &field) const { - if (!field.definitionResolved()) { - throw ParsingException("Field definition not resolved"); - } - return Value(field, _data_ref.data()); + Value at(const Field& field) const + { + if (!field.definitionResolved()) { + throw ParsingException("Field definition not resolved"); + } + return Value(field, _data_ref.data()); } - Value at(const std::shared_ptr &field) const { - return at(*field); - } + Value at(const std::shared_ptr& field) const { return at(*field); } Value at(const std::string& field_name) const { - auto field = _message_format_ref.field(field_name); - return at(field); + auto field = _message_format_ref.field(field_name); + return at(field); } - Value operator[](const Field &field) const { - return at(field); - } + Value operator[](const Field& field) const { return at(field); } - Value operator[](const std::shared_ptr &field) const { - return at(field); - } + Value operator[](const std::shared_ptr& field) const { return at(field); } - Value operator[](const std::string& field_name) const { - return at(field_name); - } + Value operator[](const std::string& field_name) const { return at(field_name); } - const MessageFormat& format() const { return _message_format_ref; } - const std::vector &rawData() const { return _data_ref.data(); } + const MessageFormat& format() const { return _message_format_ref; } + const std::vector& rawData() const { return _data_ref.data(); } private: const Data& _data_ref; @@ -59,8 +52,8 @@ class TypedDataView { template class SubscriptionIterator { public: - SubscriptionIterator(const base_iterator_type &it, - const std::shared_ptr &message_format) + SubscriptionIterator(const base_iterator_type& it, + const std::shared_ptr& message_format) : _it(it), _message_format(message_format) { } @@ -150,8 +143,6 @@ class SubscriptionIterator { class Subscription { public: - - Subscription(AddLoggedMessage add_logged_message, std::vector samples, std::shared_ptr message_format) : _add_logged_message(std::move(add_logged_message)), @@ -173,21 +164,32 @@ class Subscription { return _message_format->fieldMap(); } - std::vector fieldNames() const - { - return _message_format->fieldNames(); - } + std::vector fieldNames() const { return _message_format->fieldNames(); } std::shared_ptr field(const std::string& name) const { return _message_format->field(name); } - auto begin() { return SubscriptionIterator::iterator>(_samples.begin(), _message_format); } - auto end() { return SubscriptionIterator::iterator>(_samples.end(), _message_format); } + auto begin() + { + return SubscriptionIterator::iterator>(_samples.begin(), _message_format); + } + auto end() + { + return SubscriptionIterator::iterator>(_samples.end(), _message_format); + } - auto begin() const { return SubscriptionIterator::const_iterator>(_samples.begin(), _message_format); } - auto end() const { return SubscriptionIterator::const_iterator>(_samples.cend(), _message_format); } + auto begin() const + { + return SubscriptionIterator::const_iterator>(_samples.begin(), + _message_format); + } + auto end() const + { + return SubscriptionIterator::const_iterator>(_samples.cend(), + _message_format); + } TypedDataView at(std::size_t n) const { return begin()[n]; }