Skip to content

Commit

Permalink
Use CExpandingVector in more places.
Browse files Browse the repository at this point in the history
Small fixes.
  • Loading branch information
KerstinKeller committed Sep 25, 2024
1 parent 17282cb commit d200eab
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 56 deletions.
16 changes: 6 additions & 10 deletions ecal/core/src/serialization/ecal_serialize_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,7 @@ namespace eCAL

eCAL_pb_TLayer pb_layer = eCAL_pb_TLayer_init_default;
auto* tgt_vector = static_cast<Util::CExpandingVector<eCAL::Registration::TLayer>*>(*arg);
tgt_vector->push_back();
auto& layer = tgt_vector->back();
auto& layer = tgt_vector->push_back();

// decode shm layer parameter
pb_layer.par_layer.layer_par_shm.memory_file_list.funcs.decode = &decode_string_vector_field; // NOLINT(*-pro-type-union-access)
Expand Down Expand Up @@ -341,7 +340,7 @@ namespace eCAL
if (arg == nullptr) return false;
if (*arg == nullptr) return false;

auto* method_vec = static_cast<std::vector<eCAL::Service::Method>*>(*arg);
auto* method_vec = static_cast<eCAL::Util::CExpandingVector<eCAL::Service::Method>*>(*arg);

for (const auto& method : *method_vec)
{
Expand All @@ -367,7 +366,7 @@ namespace eCAL
return true;
}

void encode_service_methods(pb_callback_t& pb_callback, const std::vector<eCAL::Service::Method>& method_vec)
void encode_service_methods(pb_callback_t& pb_callback, const eCAL::Util::CExpandingVector<eCAL::Service::Method>& method_vec)
{
pb_callback.funcs.encode = &encode_service_methods_field; // NOLINT(*-pro-type-union-access)
pb_callback.arg = (void*)(&method_vec);
Expand All @@ -379,7 +378,8 @@ namespace eCAL
if (*arg == nullptr) return false;

eCAL_pb_Method pb_method = eCAL_pb_Method_init_default;
eCAL::Service::Method method{};
auto* method_vec = static_cast<eCAL::Util::CExpandingVector<eCAL::Service::Method>*>(*arg);
auto& method = method_vec->push_back();

// decode method parameter
decode_string(pb_method.mname, method.mname);
Expand All @@ -397,14 +397,10 @@ namespace eCAL
// apply method values
method.call_count = pb_method.call_count;

// add method to vector
auto* method_vec = static_cast<std::vector<eCAL::Service::Method>*>(*arg);
method_vec->emplace_back(method);

return true;
}

void decode_service_methods(pb_callback_t& pb_callback, std::vector<eCAL::Service::Method>& method_vec)
void decode_service_methods(pb_callback_t& pb_callback, eCAL::Util::CExpandingVector<eCAL::Service::Method>& method_vec)
{
pb_callback.funcs.decode = &decode_service_methods_field; // NOLINT(*-pro-type-union-access)
pb_callback.arg = &method_vec;
Expand Down
4 changes: 2 additions & 2 deletions ecal/core/src/serialization/ecal_serialize_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace eCAL
void encode_registration_layer(pb_callback_t& pb_callback, const Util::CExpandingVector<eCAL::Registration::TLayer>& layer_vec);
void decode_registration_layer(pb_callback_t& pb_callback, Util::CExpandingVector<eCAL::Registration::TLayer>& layer_vec);

void encode_service_methods(pb_callback_t& pb_callback, const std::vector<eCAL::Service::Method>& method_vec);
void decode_service_methods(pb_callback_t& pb_callback, std::vector<eCAL::Service::Method>& method_vec);
void encode_service_methods(pb_callback_t& pb_callback, const Util::CExpandingVector<eCAL::Service::Method>& method_vec);
void decode_service_methods(pb_callback_t& pb_callback, Util::CExpandingVector<eCAL::Service::Method>& method_vec);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,7 @@ namespace
// add sample to list
auto* sample_list = static_cast<eCAL::Registration::SampleList*>(*arg);
// Create a new element directly at the end of the vector
sample_list->push_back();
auto& sample = sample_list->back();
auto& sample = sample_list->push_back();

// prepare sample for decoding
PrepareDecoding(pb_sample, sample);
Expand Down
30 changes: 16 additions & 14 deletions ecal/core/src/serialization/ecal_struct_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <cstdint>
#include <tuple>

#include <util/expanding_vector.h>

namespace eCAL
{
namespace Service
Expand Down Expand Up @@ -146,14 +148,14 @@ namespace eCAL
// Service
struct Service
{
int32_t rclock = 0; // Registration clock
std::string pname; // Process name
std::string uname; // Unit name
std::string sname; // Service name
std::vector<Method> methods; // List of methods
uint32_t version = 0; // Service protocol version
uint32_t tcp_port_v0 = 0; // The TCP port used for that service (v0)
uint32_t tcp_port_v1 = 0; // The TCP port used for that service (v1)
int32_t rclock = 0; // Registration clock
std::string pname; // Process name
std::string uname; // Unit name
std::string sname; // Service name
Util::CExpandingVector<Method> methods; // List of methods
uint32_t version = 0; // Service protocol version
uint32_t tcp_port_v0 = 0; // The TCP port used for that service (v0)
uint32_t tcp_port_v1 = 0; // The TCP port used for that service (v1)

bool operator==(const Service& other) const {
return rclock == other.rclock &&
Expand Down Expand Up @@ -182,12 +184,12 @@ namespace eCAL
// Client
struct Client
{
int32_t rclock = 0; // Registration clock
std::string pname; // Process name
std::string uname; // Unit name
std::string sname; // Service name
std::vector<Method> methods; // List of methods
uint32_t version = 0; // Client protocol version
int32_t rclock = 0; // Registration clock
std::string pname; // Process name
std::string uname; // Unit name
std::string sname; // Service name
Util::CExpandingVector<Method> methods; // List of methods
uint32_t version = 0; // Client protocol version

bool operator==(const Client& other) const {
return rclock == other.rclock &&
Expand Down
9 changes: 4 additions & 5 deletions ecal/core/src/util/expanding_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#pragma once

#include <iterator>
#include <stdexcept>
#include <vector>

namespace eCAL
Expand All @@ -40,11 +42,8 @@ namespace eCAL
* However, when calling clear(), a regular vector will destroy the elements which are stored in this vector.
* This class, will instead call the `clear()` functions on all members.
*/
#include <vector>
#include <iterator>
#include <stdexcept>

// Templated class CExpandingVector

// Templated class CExpandingVector
template <class T>
class CExpandingVector {
public:
Expand Down
25 changes: 2 additions & 23 deletions ecal/tests/cpp/serialization_test/src/registration_compare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,35 +49,14 @@ namespace eCAL
(process1.ecal_runtime_version == process2.ecal_runtime_version);
}

// compare two Method vector objects
bool CompareMethods(const std::vector<Service::Method>& method1_vec, const std::vector<Service::Method>& method2_vec)
{
// ensure that both vectors have the same size
if (method1_vec.size() != method2_vec.size()) {
return false;
}

// compare vectors element-wise
return std::equal(method1_vec.begin(), method1_vec.end(), method2_vec.begin(),
[](const Service::Method& method1, const Service::Method& method2) {
// compare Method objects for equality
return (method1.mname == method2.mname) &&
(method1.req_type == method2.req_type) &&
(method1.req_desc == method2.req_desc) &&
(method1.resp_type == method2.resp_type) &&
(method1.resp_desc == method2.resp_desc) &&
(method1.call_count == method2.call_count);
});
}

// compare two Service objects
bool CompareService(const Service::Service& service1, const Service::Service& service2)
{
return (service1.rclock == service2.rclock) &&
(service1.pname == service2.pname) &&
(service1.uname == service2.uname) &&
(service1.sname == service2.sname) &&
CompareMethods(service1.methods, service2.methods) &&
(service1.methods == service2.methods) &&
(service1.version == service2.version) &&
(service1.tcp_port_v0 == service2.tcp_port_v0) &&
(service1.tcp_port_v1 == service2.tcp_port_v1);
Expand All @@ -90,7 +69,7 @@ namespace eCAL
(client1.pname == client2.pname) &&
(client1.uname == client2.uname) &&
(client1.sname == client2.sname) &&
CompareMethods(client1.methods, client2.methods) &&
(client1.methods == client2.methods) &&
(client1.version == client2.version);
}

Expand Down

0 comments on commit d200eab

Please sign in to comment.