1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2024-12-14 20:20:35 +00:00

replacing raw pointers by smart pointers in the control message factory

This commit is contained in:
Carles Fernandez 2014-04-13 02:51:00 +02:00
parent a5af6ea889
commit 55da7e9716
8 changed files with 35 additions and 44 deletions

View File

@ -55,9 +55,9 @@ boost::shared_ptr<gr::message> ControlMessageFactory::GetQueueMessage(unsigned i
} }
std::vector<ControlMessage*>* ControlMessageFactory::GetControlMessages(boost::shared_ptr<gr::message> queue_message) std::shared_ptr<std::vector<std::shared_ptr<ControlMessage>>> ControlMessageFactory::GetControlMessages(boost::shared_ptr<gr::message> queue_message)
{ {
std::vector<ControlMessage*>* control_messages = new std::vector<ControlMessage*>(); std::shared_ptr<std::vector<std::shared_ptr<ControlMessage>>> control_messages = std::make_shared<std::vector<std::shared_ptr<ControlMessage>>>();
unsigned int control_messages_count = queue_message->length() / sizeof(ControlMessage); unsigned int control_messages_count = queue_message->length() / sizeof(ControlMessage);
if(queue_message->length() % sizeof(ControlMessage) != 0) if(queue_message->length() % sizeof(ControlMessage) != 0)
{ {
@ -66,10 +66,10 @@ std::vector<ControlMessage*>* ControlMessageFactory::GetControlMessages(boost::s
LOG(WARNING) << "Ignoring this queue message to prevent unexpected results."; LOG(WARNING) << "Ignoring this queue message to prevent unexpected results.";
return control_messages; return control_messages;
} }
for(unsigned int i=0; i<control_messages_count; i++) for(unsigned int i = 0; i < control_messages_count; i++)
{ {
control_messages->push_back(new ControlMessage); control_messages->push_back(std::make_shared<ControlMessage>());
memcpy(control_messages->at(i), queue_message->msg() + (i*sizeof(ControlMessage)), sizeof(ControlMessage)); memcpy(control_messages->at(i).get(), queue_message->msg() + (i*sizeof(ControlMessage)), sizeof(ControlMessage));
} }
return control_messages; return control_messages;
} }

View File

@ -31,6 +31,7 @@
#ifndef GNSS_SDR_CONTROL_MESSAGE_FACTORY_H_ #ifndef GNSS_SDR_CONTROL_MESSAGE_FACTORY_H_
#define GNSS_SDR_CONTROL_MESSAGE_FACTORY_H_ #define GNSS_SDR_CONTROL_MESSAGE_FACTORY_H_
#include <memory>
#include <vector> #include <vector>
#include <gnuradio/message.h> #include <gnuradio/message.h>
@ -58,7 +59,7 @@ public:
virtual ~ControlMessageFactory(); virtual ~ControlMessageFactory();
boost::shared_ptr<gr::message> GetQueueMessage(unsigned int who, unsigned int what); boost::shared_ptr<gr::message> GetQueueMessage(unsigned int who, unsigned int what);
std::vector<ControlMessage*>* GetControlMessages(gr::message::sptr queue_message); std::shared_ptr<std::vector<std::shared_ptr<ControlMessage>>> GetControlMessages(gr::message::sptr queue_message);
}; };
#endif /*GNSS_SDR_CONTROL_MESSAGE_FACTORY_H_*/ #endif /*GNSS_SDR_CONTROL_MESSAGE_FACTORY_H_*/

View File

@ -402,11 +402,9 @@ void ControlThread::process_control_messages()
{ {
flowgraph_->apply_action(control_messages_->at(i)->who, control_messages_->at(i)->what); flowgraph_->apply_action(control_messages_->at(i)->who, control_messages_->at(i)->what);
} }
delete control_messages_->at(i);
processed_control_messages_++; processed_control_messages_++;
} }
control_messages_->clear(); control_messages_->clear();
delete control_messages_;
DLOG(INFO) << "Processed all control messages"; DLOG(INFO) << "Processed all control messages";
} }

View File

@ -35,6 +35,7 @@
#ifndef GNSS_SDR_CONTROL_THREAD_H_ #ifndef GNSS_SDR_CONTROL_THREAD_H_
#define GNSS_SDR_CONTROL_THREAD_H_ #define GNSS_SDR_CONTROL_THREAD_H_
#include <memory>
#include <vector> #include <vector>
#include <boost/thread/thread.hpp> #include <boost/thread/thread.hpp>
#include <gnuradio/msg_queue.h> #include <gnuradio/msg_queue.h>
@ -101,11 +102,11 @@ public:
/*! /*!
* \brief Instantiates a flowgraph * \brief Instantiates a flowgraph
* *
* \return Returns a flowgraph object * \return Returns a pointer to a flowgraph object
*/ */
GNSSFlowgraph* flowgraph() std::shared_ptr<GNSSFlowgraph> flowgraph()
{ {
return flowgraph_.get(); return flowgraph_;
} }
@ -177,7 +178,7 @@ private:
std::shared_ptr<ConfigurationInterface> configuration_; std::shared_ptr<ConfigurationInterface> configuration_;
boost::shared_ptr<gr::msg_queue> control_queue_; boost::shared_ptr<gr::msg_queue> control_queue_;
std::shared_ptr<ControlMessageFactory> control_message_factory_; std::shared_ptr<ControlMessageFactory> control_message_factory_;
std::vector<ControlMessage*> *control_messages_; std::shared_ptr<std::vector<std::shared_ptr<ControlMessage>>> control_messages_;
bool stop_; bool stop_;
bool delete_configuration_; bool delete_configuration_;
unsigned int processed_control_messages_; unsigned int processed_control_messages_;

View File

@ -1,16 +1,17 @@
/*! /*!
* \file gnss_block_factory.h * \file gnss_block_factory.h
* \brief Interface of a factory that returns instances of GNSS blocks. * \brief Interface of a factory that returns smart pointers to GNSS blocks.
* \author Carlos Aviles, 2010. carlos.avilesr(at)googlemail.com * \author Carlos Aviles, 2010. carlos.avilesr(at)googlemail.com
* Luis Esteve, 2011. luis(at)epsilon-formacion.com * Luis Esteve, 2011. luis(at)epsilon-formacion.com
* Javier Arribas, 2011. jarribas(at)cttc.es * Javier Arribas, 2011. jarribas(at)cttc.es
* Carles Fernandez-Prades, 2014. cfernandez(at)cttc.es
* *
* This class encapsulates the complexity behind the instantiation * This class encapsulates the complexity behind the instantiation
* of GNSS blocks. * of GNSS blocks.
* *
* ------------------------------------------------------------------------- * -------------------------------------------------------------------------
* *
* Copyright (C) 2010-2012 (see AUTHORS file for a list of contributors) * Copyright (C) 2010-2014 (see AUTHORS file for a list of contributors)
* *
* GNSS-SDR is a software defined Global Navigation * GNSS-SDR is a software defined Global Navigation
* Satellite Systems receiver * Satellite Systems receiver
@ -36,9 +37,9 @@
#ifndef GNSS_SDR_BLOCK_FACTORY_H_ #ifndef GNSS_SDR_BLOCK_FACTORY_H_
#define GNSS_SDR_BLOCK_FACTORY_H_ #define GNSS_SDR_BLOCK_FACTORY_H_
#include <vector>
#include <string>
#include <memory> #include <memory>
#include <string>
#include <vector>
#include <gnuradio/msg_queue.h> #include <gnuradio/msg_queue.h>
class ConfigurationInterface; class ConfigurationInterface;

View File

@ -3,6 +3,7 @@
* \brief Implementation of a GNSS receiver flowgraph * \brief Implementation of a GNSS receiver flowgraph
* \author Carlos Aviles, 2010. carlos.avilesr(at)googlemail.com * \author Carlos Aviles, 2010. carlos.avilesr(at)googlemail.com
* Luis Esteve, 2012. luis(at)epsilon-formacion.com * Luis Esteve, 2012. luis(at)epsilon-formacion.com
* Carles Fernandez-Prades, 2014. cfernandez(at)cttc.es
* *
* Detailed description of the file here if needed. * Detailed description of the file here if needed.
* *
@ -35,7 +36,6 @@
#include "unistd.h" #include "unistd.h"
#include <exception> #include <exception>
#include <iostream> #include <iostream>
#include <memory>
#include <set> #include <set>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include <glog/logging.h> #include <glog/logging.h>

View File

@ -3,6 +3,7 @@
* \brief Interface of a GNSS receiver flowgraph. * \brief Interface of a GNSS receiver flowgraph.
* \author Carlos Aviles, 2010. carlos.avilesr(at)googlemail.com * \author Carlos Aviles, 2010. carlos.avilesr(at)googlemail.com
* Luis Esteve, 2011. luis(at)epsilon-formacion.com * Luis Esteve, 2011. luis(at)epsilon-formacion.com
* Carles Fernandez-Prades, 2014. cfernandez(at)cttc.es
* *
* It contains a signal source, * It contains a signal source,
* a signal conditioner, a set of channels, a pvt and an output filter. * a signal conditioner, a set of channels, a pvt and an output filter.
@ -35,10 +36,11 @@
#ifndef GNSS_SDR_GNSS_FLOWGRAPH_H_ #ifndef GNSS_SDR_GNSS_FLOWGRAPH_H_
#define GNSS_SDR_GNSS_FLOWGRAPH_H_ #define GNSS_SDR_GNSS_FLOWGRAPH_H_
#include <list>
#include <memory>
#include <queue>
#include <string> #include <string>
#include <vector> #include <vector>
#include <queue>
#include <list>
#include <gnuradio/top_block.h> #include <gnuradio/top_block.h>
#include <gnuradio/msg_queue.h> #include <gnuradio/msg_queue.h>
#include "GPS_L1_CA.h" #include "GPS_L1_CA.h"

View File

@ -39,17 +39,13 @@
TEST(Control_Message_Factory_Test, GetQueueMessage) TEST(Control_Message_Factory_Test, GetQueueMessage)
{ {
ControlMessageFactory *factory = new ControlMessageFactory(); std::shared_ptr<ControlMessageFactory> factory = std::make_shared<ControlMessageFactory>();
gr::message::sptr queue_message = factory->GetQueueMessage(0, 0); gr::message::sptr queue_message = factory->GetQueueMessage(0, 0);
ControlMessage *control_message = (ControlMessage*)queue_message->msg(); std::shared_ptr<ControlMessage> control_message = std::make_shared<ControlMessage>();
unsigned int expected0 = 0; unsigned int expected0 = 0;
EXPECT_EQ(expected0, control_message->who); EXPECT_EQ(expected0, control_message->who);
EXPECT_EQ(expected0, control_message->what); EXPECT_EQ(expected0, control_message->what);
EXPECT_EQ(sizeof(ControlMessage), queue_message->length()); EXPECT_EQ(sizeof(ControlMessage), queue_message->length());
delete factory;
} }
@ -57,48 +53,40 @@ TEST(Control_Message_Factory_Test, GetQueueMessage)
TEST(Control_Message_Factory_Test, GetControlMessages) TEST(Control_Message_Factory_Test, GetControlMessages)
{ {
ControlMessageFactory *factory = new ControlMessageFactory(); std::shared_ptr<ControlMessageFactory> factory = std::make_shared<ControlMessageFactory>();
ControlMessage *control_message = new ControlMessage; gr::message::sptr queue_message = gr::message::make(0, 0, 0, sizeof(ControlMessage));
std::shared_ptr<ControlMessage> control_message = std::make_shared<ControlMessage>();
control_message->who = 1; control_message->who = 1;
control_message->what = 4; control_message->what = 4;
gr::message::sptr queue_message = gr::message::make(0, 0, 0, sizeof(ControlMessage)); memcpy(queue_message->msg(), control_message.get(), sizeof(ControlMessage));
memcpy(queue_message->msg(), control_message, sizeof(ControlMessage)); std::shared_ptr<std::vector<std::shared_ptr<ControlMessage>>> control_messages = factory->GetControlMessages(queue_message);
std::vector<ControlMessage*> *control_messages = factory->GetControlMessages(queue_message);
unsigned int expected1 = 1; unsigned int expected1 = 1;
unsigned int expected4 = 4; unsigned int expected4 = 4;
EXPECT_EQ(expected1, control_messages->size()); EXPECT_EQ(expected1, control_messages->size());
EXPECT_EQ(expected1, control_messages->at(0)->who); EXPECT_EQ(expected1, control_messages->at(0)->who);
EXPECT_EQ(expected4, control_messages->at(0)->what); EXPECT_EQ(expected4, control_messages->at(0)->what);
delete control_message;
delete control_messages;
delete factory;
} }
/*
TEST(Control_Message_Factory_Test, GetControlMessagesWrongSize) TEST(Control_Message_Factory_Test, GetControlMessagesWrongSize)
{ {
ControlMessageFactory *factory = new ControlMessageFactory(); std::shared_ptr<ControlMessageFactory> factory = std::make_shared<ControlMessageFactory>();
ControlMessage *control_message = new ControlMessage; std::shared_ptr<ControlMessage> control_message = std::make_shared<ControlMessage>();
control_message->who = 1; control_message->who = 1;
control_message->what = 4; control_message->what = 4;
int another_int = 10; int another_int = 10;
gr::message::sptr queue_message = gr::message::make(0, 0, 0, sizeof(ControlMessage) + sizeof(int)); gr::message::sptr queue_message = gr::message::make(0, 0, 0, sizeof(ControlMessage) + sizeof(int));
memcpy(queue_message->msg(), control_message, sizeof(ControlMessage)); memcpy(queue_message->msg(), control_message.get(), sizeof(ControlMessage));
memcpy(queue_message->msg() + sizeof(ControlMessage), &another_int, sizeof(int)); memcpy(queue_message->msg() + sizeof(ControlMessage), &another_int, sizeof(int));
std::vector<ControlMessage*> *control_messages = factory->GetControlMessages(queue_message); std::shared_ptr<std::vector<std::shared_ptr<ControlMessage>>> control_messages = factory->GetControlMessages(queue_message);
unsigned int expected0 = 0; unsigned int expected0 = 0;
EXPECT_EQ(expected0, control_messages->size()); EXPECT_EQ(expected0, control_messages->size());
} */
delete control_message;
delete control_messages;
delete factory;
}