Skip to content

Commit

Permalink
use std::shared_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
kk-mats committed Apr 9, 2019
1 parent 3f8cf30 commit 0e6eb95
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
11 changes: 5 additions & 6 deletions clone_format/jcln.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,7 @@ std::optional<detection_results> jcln::read_detection_results(const QJsonObject
shared_map<int, file> id_file_ptr_map;

// file_table
const auto file_table=json[FILE_TABLE].toArray();
for(const auto &fj:file_table)
for(const auto fj:json[FILE_TABLE].toArray())
{
if(!fj.isObject())
{
Expand All @@ -275,8 +274,7 @@ std::optional<detection_results> jcln::read_detection_results(const QJsonObject
}

// results
const auto results=json[RESULTS].toArray();
for(const auto &rj:results)
for(const auto rj:json[RESULTS].toArray())
{
if(!rj.isObject())
{
Expand All @@ -290,10 +288,11 @@ std::optional<detection_results> jcln::read_detection_results(const QJsonObject
qCritical()<<code_clone_loading_error::invalid_file_format;
return std::nullopt;
}

rs.add(r.value());
results.insert(std::make_shared<detection_result>(std::move(r)));
}

rs.emplace_detection_result();

return std::make_optional(rs);
}

Expand Down
46 changes: 26 additions & 20 deletions core/detection_results.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,36 @@ detection_results::detection_results(const QString &target_path) noexcept
: target_path_(target_path)
{}

std::shared_ptr<file> detection_results::add(QString &&canonical_file_path) noexcept
std::shared_ptr<file> detection_results::emplace(QString &&canonical_file_path) noexcept
{
if(auto itr=std::find(this->files_.begin(), this->files_.end(), [&](const auto &f){ return f.get()==canonical_file_path; }); itr==this->files_.end())
auto itr=std::find_if(this->files_.begin(), this->files_.end(), [&](const auto &f){ return f==canonical_file_path; });
if(itr==this->files_.end())
{
auto ptr=std::make_shared<file>(std::move(canonical_file_path));
this->files_.insert(ptr);
return ptr;
}
else
{
return *itr;
itr=this->files_.insert(std::make_shared<file>(std::move(canonical_file_path)));
}
return *itr;
}

template<class ...Args>
bool detection_results::emplace_detection_result(Args&& ...args) noexcept
std::shared_ptr<detection_result> detection_results::empalce(result_environment &&context, shared_set<clone_pair> &&clone_pairs) noexcept
{
this->results_.insert(std::make_shared<detection_results>(std::move(args...)));
return true;
}

bool detection_results::insert_result(const QString &path) noexcept
{
this->results_.insert()
auto itr=std::find_if(this->results_.begin(), this->results_.end(), [&](const detection_result &r){ return r.context().source()==context.source(); });
if(itr==this->results_.end())
{
itr=this->results_.insert(std::make_shared<detection_result>(std::move(context), std::move(clone_pairs)));
}
return *itr;
}

bool detection_results::remove_result(const std::shared_ptr<detection_result> &ptr) noexcept
bool detection_results::remove(std::shared_ptr<detection_result> &&ptr) noexcept
{
bool has_removed=this->results_.remove(ptr);
if(auto itr=this->results_.find(ptr); itr!=this->results_.end())
{
ptr.reset();
this->results_.erase(itr);
return true;
}
qWarning()<<"ptr not found in detection_results::results_";
return false;
}

QString detection_results::target_path() const noexcept
Expand All @@ -51,4 +52,9 @@ void detection_results::set_target_path(const QString &target_path) noexcept
this->target_path_=target_path;
}

void detection_results::remove_files() noexcept
{

}

}
12 changes: 6 additions & 6 deletions core/detection_results.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define DETECTION_RESULTS_HPP

#include <memory>
#include <type_traits>

#include "matching_table.hpp"
#include "layer/heatmap_layer.hpp"
Expand All @@ -15,13 +16,10 @@ class detection_results final
detection_results() noexcept;
detection_results(const QString &target_path) noexcept;

std::shared_ptr<file> add(QString &&canonical_file_path) noexcept;
std::shared_ptr<file> emplace(QString &&canonical_file_path) noexcept;
std::shared_ptr<detection_result> empalce(result_environment &&context, shared_set<clone_pair> &&clone_pairs) noexcept;

template<class ...Args>
bool emplace_detection_result(Args&& ...args) noexcept;

bool insert_result(const QString &path) noexcept;
bool remove_result(const std::shared_ptr<detection_result> &ptr) noexcept;
bool remove(std::shared_ptr<detection_result> &&ptr) noexcept;

shared_list<detection_result> results() const noexcept;

Expand All @@ -32,6 +30,8 @@ class detection_results final
QString target_path_;
shared_set<file> files_;
shared_set<detection_result> results_;

void remove_files() noexcept;
};

}
Expand Down

0 comments on commit 0e6eb95

Please sign in to comment.