1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2024-11-05 01:26:24 +00:00

Use std::array instead of pointers and memcpy

This commit is contained in:
Carles Fernandez 2019-07-03 23:47:42 +02:00
parent 081439d6f8
commit 4de86b1f8a
No known key found for this signature in database
GPG Key ID: 4C583C52B0C3877D
3 changed files with 17 additions and 17 deletions

View File

@ -50,6 +50,7 @@ public:
GOOGLE_PROTOBUF_VERIFY_VERSION; GOOGLE_PROTOBUF_VERIFY_VERSION;
monitor_.New(); monitor_.New();
} }
~Serdes_Monitor_Pvt() ~Serdes_Monitor_Pvt()
{ {
// google::protobuf::ShutdownProtobufLibrary(); // google::protobuf::ShutdownProtobufLibrary();
@ -94,7 +95,6 @@ public:
return data; return data;
} }
inline Monitor_Pvt readProtobuffer(const gnss_sdr::MonitorPvt& mon) //!< Deserialization inline Monitor_Pvt readProtobuffer(const gnss_sdr::MonitorPvt& mon) //!< Deserialization
{ {
Monitor_Pvt monitor; Monitor_Pvt monitor;

View File

@ -34,7 +34,7 @@
#include "gnss_synchro.h" #include "gnss_synchro.h"
#include "gnss_synchro.pb.h" // file created by Protocol Buffers at compile time #include "gnss_synchro.pb.h" // file created by Protocol Buffers at compile time
#include <cstring> // for memcpy #include <array>
#include <vector> #include <vector>
@ -52,6 +52,7 @@ public:
GOOGLE_PROTOBUF_VERIFY_VERSION; GOOGLE_PROTOBUF_VERIFY_VERSION;
observables.New(); observables.New();
} }
~Serdes_Gnss_Synchro() ~Serdes_Gnss_Synchro()
{ {
google::protobuf::ShutdownProtobufLibrary(); google::protobuf::ShutdownProtobufLibrary();
@ -60,21 +61,17 @@ public:
inline std::string createProtobuffer(const std::vector<Gnss_Synchro>& vgs) //!< Serialization into a string inline std::string createProtobuffer(const std::vector<Gnss_Synchro>& vgs) //!< Serialization into a string
{ {
observables.Clear(); observables.Clear();
std::string data; std::string data;
for (auto gs : vgs) for (auto gs : vgs)
{ {
gnss_sdr::GnssSynchro* obs = observables.add_observable(); gnss_sdr::GnssSynchro* obs = observables.add_observable();
char c[2]; char c = gs.System;
c[0] = gs.System; const std::string sys(1, c);
c[1] = '\0';
const std::string sys(c);
char cc[3]; std::array<char, 2> cc;
cc[0] = gs.Signal[0]; cc[0] = gs.Signal[0];
cc[1] = gs.Signal[1]; cc[1] = gs.Signal[1];
cc[2] = '\0'; const std::string sig(cc.cbegin(), cc.cend());
const std::string sig(cc);
obs->set_system(sys); obs->set_system(sys);
obs->set_signal(sig); obs->set_signal(sig);
@ -109,20 +106,19 @@ public:
return data; return data;
} }
inline std::vector<Gnss_Synchro> readProtobuffer(const gnss_sdr::Observables& obs) const //!< Deserialization
inline std::vector<Gnss_Synchro> readProtobuffer(const gnss_sdr::Observables& obs) //!< Deserialization
{ {
std::vector<Gnss_Synchro> vgs; std::vector<Gnss_Synchro> vgs;
vgs.reserve(obs.observable_size()); vgs.reserve(obs.observable_size());
for (int i = 0; i < obs.observable_size(); ++i) for (int i = 0; i < obs.observable_size(); ++i)
{ {
const gnss_sdr::GnssSynchro& gs_read = obs.observable(i); const gnss_sdr::GnssSynchro& gs_read = obs.observable(i);
Gnss_Synchro gs = Gnss_Synchro(); Gnss_Synchro gs = Gnss_Synchro();
const std::string& sys = gs_read.system(); const std::string& sys = gs_read.system();
gs.System = *sys.c_str(); gs.System = gs_read.system()[0];
const std::string& sig = gs_read.signal(); gs.Signal[0] = gs_read.signal()[0];
std::memcpy(static_cast<void*>(gs.Signal), sig.c_str(), 3); gs.Signal[1] = gs_read.signal()[1];
gs.Signal[2] = '\0';
gs.PRN = gs_read.prn(); gs.PRN = gs_read.prn();
gs.Channel_ID = gs_read.channel_id(); gs.Channel_ID = gs_read.channel_id();
@ -153,6 +149,7 @@ public:
} }
return vgs; return vgs;
} }
private: private:
gnss_sdr::Observables observables; gnss_sdr::Observables observables;
}; };

View File

@ -95,12 +95,15 @@ TEST(Protobuf, Works)
Gnss_Synchro gs_read = vgs_read[0]; Gnss_Synchro gs_read = vgs_read[0];
uint32_t prn_read = gs_read.PRN; uint32_t prn_read = gs_read.PRN;
uint32_t prn_read2 = vgs_read[1].PRN; uint32_t prn_read2 = vgs_read[1].PRN;
std::string system_read(1, gs_read.System);
std::string signal_read(gs_read.Signal);
// or without the need of gnss_synchro: // or without the need of gnss_synchro:
int obs_size = obs.observable_size(); int obs_size = obs.observable_size();
uint32_t prn_read3 = obs.observable(0).prn(); uint32_t prn_read3 = obs.observable(0).prn();
EXPECT_EQ(prn_true, prn_read); EXPECT_EQ(sig, signal_read);
EXPECT_EQ(sys, system_read);
EXPECT_EQ(prn_true2, prn_read2); EXPECT_EQ(prn_true2, prn_read2);
EXPECT_EQ(prn_true, prn_read3); EXPECT_EQ(prn_true, prn_read3);
EXPECT_EQ(2, obs_size); EXPECT_EQ(2, obs_size);