]> sigrok.org Git - pulseview.git/commitdiff
Prefer std::make_shared().
authorUwe Hermann <redacted>
Fri, 10 Mar 2017 19:26:31 +0000 (20:26 +0100)
committerUwe Hermann <redacted>
Sat, 11 Mar 2017 12:06:03 +0000 (13:06 +0100)
This patch was generated using clang-tidy:

  clang-tidy -checks="-*,modernize-make-shared" -fix

(with some additional manual fixups)

Using make_shared() over manual construction has multiple advantages:

 - It's shorter to write and easier to read.

   V1: auto sb = shared_ptr<Foo>(new Foo());
   V2: auto sb = make_shared<Foo>();

 - The type "Foo" is repeated less often (less code duplication, lower
   risk of forgetting to update one of the "Foo"s upon copy-paste etc.)

 - Manual construction leads to two individual allocations (actual data and
   the control block of the shared_ptr). Using make_shared() will only lead
   to one allocation, which has performance, cache-locality and memory
   consumption benefits.

 - It's exception-safe, whereas manual construction is not necessarily:

   V1: func(shared_ptr<Foo>(new Foo()), shared_ptr<Foo>(new Foo()));
   V2: func(make_shared<Foo>(), make_shared<Foo>());

   In "V1", one of the "new" invocations could throw, potentially
   causing memory leaks. No leaks will happen with make_shared().

pv/data/decoderstack.cpp
pv/popups/channels.cpp
pv/session.cpp
pv/view/decodetrace.cpp
pv/view/view.cpp

index c216d8d2627344264d940e4ab7a8d1a3db26c6d5..11b2b70bebc65e7503abc2dca66bf78bac54342a 100644 (file)
@@ -44,6 +44,7 @@ using std::list;
 using std::map;
 using std::pair;
 using std::shared_ptr;
+using std::make_shared;
 using std::vector;
 
 using namespace pv::data::decode;
@@ -74,8 +75,7 @@ DecoderStack::DecoderStack(pv::Session &session,
        connect(&session_, SIGNAL(frame_ended()),
                this, SLOT(on_frame_ended()));
 
-       stack_.push_back(shared_ptr<decode::Decoder>(
-               new decode::Decoder(dec)));
+       stack_.push_back(make_shared<decode::Decoder>(dec));
 }
 
 DecoderStack::~DecoderStack()
index 89080c4fc45426fd5ccaf6b8d2abe8467be9ddad..9c200b9db31eb5d1c6a3e35cafcc85b1ec2cdbbf 100644 (file)
@@ -51,6 +51,7 @@ using std::map;
 using std::mutex;
 using std::set;
 using std::shared_ptr;
+using std::make_shared;
 using std::unordered_set;
 using std::vector;
 
@@ -165,7 +166,7 @@ void Channels::populate_group(shared_ptr<ChannelGroup> group,
        // popup.
        shared_ptr<Device> binding;
        if (group)
-               binding = shared_ptr<Device>(new Device(group));
+               binding = make_shared<Device>(group);
 
        // Create a title if the group is going to have any content
        if ((!sigs.empty() || (binding && !binding->properties().empty())) &&
index 31cbb73c19e1fb37fde8955dff2de9e35062f683..ef2f39bb8ed3ddb88ded541959042ad531a5096a 100644 (file)
@@ -76,6 +76,7 @@ using std::pair;
 using std::recursive_mutex;
 using std::set;
 using std::shared_ptr;
+using std::make_shared;
 using std::string;
 using std::unordered_set;
 using std::vector;
@@ -307,7 +308,7 @@ void Session::restore_settings(QSettings &settings)
                settings.endGroup();
 
                if (QFileInfo(filename).isReadable()) {
-                       device = std::make_shared<devices::SessionFile>(device_manager_.context(),
+                       device = make_shared<devices::SessionFile>(device_manager_.context(),
                                filename.toStdString());
                        set_device(device);
 
@@ -617,8 +618,7 @@ bool Session::add_decoder(srd_decoder *const dec)
 
        try {
                // Create the decoder
-               decoder_stack = shared_ptr<data::DecoderStack>(
-                       new data::DecoderStack(*this, dec));
+               decoder_stack = make_shared<data::DecoderStack>(*this, dec);
 
                // Make a list of all the channels
                std::vector<const srd_channel*> all_channels;
@@ -644,7 +644,7 @@ bool Session::add_decoder(srd_decoder *const dec)
 
                // Create the decode signal
                shared_ptr<data::SignalBase> signalbase =
-                       shared_ptr<data::SignalBase>(new data::SignalBase(nullptr));
+                       make_shared<data::SignalBase>(nullptr);
 
                signalbase->set_decoder_stack(decoder_stack);
                signalbases_.insert(signalbase);
@@ -772,8 +772,7 @@ void Session::update_signals()
                                        switch(channel->type()->id()) {
                                        case SR_CHANNEL_LOGIC:
                                                if (!signalbase) {
-                                                       signalbase = shared_ptr<data::SignalBase>(
-                                                               new data::SignalBase(channel));
+                                                       signalbase = make_shared<data::SignalBase>(channel);
                                                        signalbases_.insert(signalbase);
 
                                                        all_signal_data_.insert(logic_data_);
@@ -789,8 +788,7 @@ void Session::update_signals()
                                        case SR_CHANNEL_ANALOG:
                                        {
                                                if (!signalbase) {
-                                                       signalbase = shared_ptr<data::SignalBase>(
-                                                               new data::SignalBase(channel));
+                                                       signalbase = make_shared<data::SignalBase>(channel);
                                                        signalbases_.insert(signalbase);
 
                                                        shared_ptr<data::Analog> data(new data::Analog());
@@ -954,8 +952,8 @@ void Session::feed_in_logic(shared_ptr<Logic> logic)
                set_capture_state(Running);
 
                // Create a new data segment
-               cur_logic_segment_ = shared_ptr<data::LogicSegment>(
-                       new data::LogicSegment(*logic_data_, logic, cur_samplerate_));
+               cur_logic_segment_ = make_shared<data::LogicSegment>(
+                       *logic_data_, logic, cur_samplerate_);
                logic_data_->push_segment(cur_logic_segment_);
 
                // @todo Putting this here means that only listeners querying
@@ -1006,8 +1004,8 @@ void Session::feed_in_analog(shared_ptr<Analog> analog)
                        assert(data);
 
                        // Create a segment, keep it in the maps of channels
-                       segment = shared_ptr<data::AnalogSegment>(
-                               new data::AnalogSegment(*data, cur_samplerate_));
+                       segment = make_shared<data::AnalogSegment>(
+                               *data, cur_samplerate_);
                        cur_analog_segments_[channel] = segment;
 
                        // Push the segment into the analog data.
index c5fa6e91611f0c062fe08754762ccf012048a094..164c7e5a6075fc719a9cc3e9c117e309ecf37ff8 100644 (file)
@@ -66,6 +66,7 @@ using std::map;
 using std::min;
 using std::pair;
 using std::shared_ptr;
+using std::make_shared;
 using std::tie;
 using std::unordered_set;
 using std::vector;
@@ -961,8 +962,7 @@ void DecodeTrace::on_stack_decoder(srd_decoder *decoder)
 
        assert(decoder);
        assert(decoder_stack);
-       decoder_stack->push(shared_ptr<data::decode::Decoder>(
-               new data::decode::Decoder(decoder)));
+       decoder_stack->push(make_shared<data::decode::Decoder>(decoder));
        decoder_stack->begin_decode();
 
        create_popup_form();
index 23de2087c94120e030834c9e1dfb0e8ec6312952..f46045ad1af482d89d0da7f00d5b853f7ae34ea6 100644 (file)
@@ -627,8 +627,8 @@ std::shared_ptr<CursorPair> View::cursors() const
 
 void View::add_flag(const Timestamp& time)
 {
-       flags_.push_back(shared_ptr<Flag>(new Flag(*this, time,
-               QString("%1").arg(next_flag_text_))));
+       flags_.push_back(make_shared<Flag>(*this, time,
+               QString("%1").arg(next_flag_text_)));
 
        next_flag_text_ = (next_flag_text_ >= 'Z') ? 'A' :
                (next_flag_text_ + 1);
@@ -688,8 +688,7 @@ void View::restack_all_trace_tree_items()
 
 void View::trigger_event(util::Timestamp location)
 {
-       trigger_markers_.push_back(shared_ptr<TriggerMarker>(
-               new TriggerMarker(*this, location)));
+       trigger_markers_.push_back(make_shared<TriggerMarker>(*this, location));
 }
 
 void View::get_scroll_layout(double &length, Timestamp &offset) const