commit 96cfb46ab25ddadb8d673ec58bc09fa46817f8f4
parent 44926acc9bd070437d83a2095528e5c90019c670
Author: cfillion <cfillion@users.noreply.github.com>
Date: Mon, 7 Mar 2016 01:50:02 -0500
use automatic memory management for repository indexes
this reduce headaches for upcoming changes
Diffstat:
11 files changed, 48 insertions(+), 78 deletions(-)
diff --git a/src/about.cpp b/src/about.cpp
@@ -36,18 +36,13 @@ using namespace std;
enum { ACTION_HISTORY = 300 };
-About::About(const Index *index)
+About::About(IndexPtr index)
: Dialog(IDD_ABOUT_DIALOG), m_index(index),
m_currentCat(-255)
{
RichEdit::Init();
}
-About::~About()
-{
- delete m_index;
-}
-
void About::onInit()
{
m_about = createControl<RichEdit>(IDC_ABOUT);
diff --git a/src/about.hpp b/src/about.hpp
@@ -20,6 +20,7 @@
#include "dialog.hpp"
+#include <memory>
#include <vector>
#include "report.hpp"
@@ -32,12 +33,13 @@ class RichEdit;
class TabBar;
struct Link;
+typedef std::shared_ptr<const Index> IndexPtr;
+
class About : public Dialog {
public:
enum { InstallResult = 100 };
- About(const Index *);
- ~About();
+ About(IndexPtr);
protected:
void onInit() override;
@@ -52,7 +54,7 @@ private:
void openLink(const Link *);
void packageHistory();
- const Index *m_index;
+ IndexPtr m_index;
int m_currentCat;
TabBar *m_tabs;
diff --git a/src/index.cpp b/src/index.cpp
@@ -43,7 +43,7 @@ auto Index::linkTypeFor(const char *rel) -> LinkType
return WebsiteLink;
}
-const Index *Index::load(const string &name)
+IndexPtr Index::load(const string &name)
{
TiXmlDocument doc;
diff --git a/src/index.hpp b/src/index.hpp
@@ -19,6 +19,7 @@
#define REAPACK_INDEX_HPP
#include <map>
+#include <memory>
#include <string>
#include <vector>
@@ -28,12 +29,15 @@ class Category;
typedef std::vector<const Category *> CategoryList;
class Download;
+class Index;
class Path;
class Remote;
class TiXmlElement;
struct Link { std::string name; std::string url; };
+typedef std::shared_ptr<const Index> IndexPtr;
+
class Index {
public:
enum LinkType { WebsiteLink, DonationLink };
@@ -42,7 +46,7 @@ public:
static Path pathFor(const std::string &name);
static LinkType linkTypeFor(const char *rel);
- static const Index *load(const std::string &name);
+ static IndexPtr load(const std::string &name);
static Download *fetch(const Remote &, bool stale = false);
Index(const std::string &name);
@@ -62,7 +66,7 @@ public:
const PackageList &packages() const { return m_packages; }
private:
- static Index *loadV1(TiXmlElement *, const std::string &);
+ static IndexPtr loadV1(TiXmlElement *, const std::string &);
std::string m_name;
std::string m_about;
diff --git a/src/index_v1.cpp b/src/index_v1.cpp
@@ -21,8 +21,6 @@
#include <WDL/tinyxml/tinyxml.h>
-#include <memory>
-
using namespace std;
static void LoadMetadataV1(TiXmlElement *, Index *ri);
@@ -30,7 +28,7 @@ static void LoadCategoryV1(TiXmlElement *, Index *ri);
static void LoadPackageV1(TiXmlElement *, Category *cat);
static void LoadVersionV1(TiXmlElement *, Package *pkg);
-Index *Index::loadV1(TiXmlElement *root, const string &name)
+IndexPtr Index::loadV1(TiXmlElement *root, const string &name)
{
Index *ri = new Index(name);
@@ -52,7 +50,7 @@ Index *Index::loadV1(TiXmlElement *root, const string &name)
LoadMetadataV1(node, ri);
ptr.release();
- return ri;
+ return IndexPtr(ri);
}
void LoadMetadataV1(TiXmlElement *meta, Index *ri)
diff --git a/src/reapack.cpp b/src/reapack.cpp
@@ -303,7 +303,7 @@ void ReaPack::about(const Remote &remote, HWND parent)
if(remote.isNull())
return;
- loadIndex(remote, [=] (const Index *index) {
+ loadIndex(remote, [=] (IndexPtr index) {
const auto ret = Dialog::Show<About>(m_instance, parent, index);
if(ret == About::InstallResult)
diff --git a/src/reapack.hpp b/src/reapack.hpp
@@ -20,6 +20,7 @@
#include <functional>
#include <map>
+#include <memory>
#include "path.hpp"
@@ -36,7 +37,7 @@ class Transaction;
class ReaPack {
public:
typedef std::function<void ()> ActionCallback;
- typedef std::function<void (const Index *)> IndexCallback;
+ typedef std::function<void (std::shared_ptr<const Index>)> IndexCallback;
static const std::string VERSION;
static const std::string BUILDTIME;
diff --git a/src/transaction.cpp b/src/transaction.cpp
@@ -58,9 +58,6 @@ Transaction::~Transaction()
for(Task *task : m_tasks)
delete task;
- for(const Index *ri : m_indexes)
- delete ri;
-
delete m_registry;
}
@@ -71,7 +68,7 @@ void Transaction::synchronize(const Remote &remote, const bool isUserAction)
m_receipt.setEnabled(true);
fetchIndex(remote, [=] {
- const Index *ri;
+ IndexPtr ri;
try {
ri = Index::load(remote.name());
diff --git a/src/transaction.hpp b/src/transaction.hpp
@@ -19,18 +19,21 @@
#define REAPACK_TRANSACTION_HPP
#include "download.hpp"
-#include "path.hpp"
#include "receipt.hpp"
#include "registry.hpp"
#include <boost/signals2.hpp>
#include <functional>
+#include <memory>
#include <set>
-class Remote;
class Index;
+class Path;
+class Remote;
class Task;
+typedef std::shared_ptr<const Index> IndexPtr;
+
struct InstallTicket {
enum Type { Install, Upgrade };
@@ -92,7 +95,7 @@ private:
Receipt m_receipt;
std::multimap<std::string, IndexCallback> m_remotes;
- std::vector<const Index *> m_indexes;
+ std::vector<IndexPtr> m_indexes;
std::vector<Task *> m_tasks;
DownloadQueue m_downloadQueue;
diff --git a/test/index.cpp b/test/index.cpp
@@ -6,7 +6,6 @@
#include <string>
#define RIPATH "test/indexes/"
-#define RIPTR(ptr) unique_ptr<const Index> riptr(ptr)
using namespace std;
@@ -16,8 +15,7 @@ TEST_CASE("file not found", M) {
UseRootPath root(RIPATH);
try {
- const Index *ri = Index::load("404");
- RIPTR(ri);
+ IndexPtr ri = Index::load("404");
FAIL();
}
catch(const reapack_error &e) {
@@ -29,8 +27,7 @@ TEST_CASE("broken", M) {
UseRootPath root(RIPATH);
try {
- const Index *ri = Index::load("broken");
- RIPTR(ri);
+ IndexPtr ri = Index::load("broken");
FAIL();
}
catch(const reapack_error &e) {
@@ -42,8 +39,7 @@ TEST_CASE("wrong root tag name", M) {
UseRootPath root(RIPATH);
try {
- const Index *ri = Index::load("wrong_root");
- RIPTR(ri);
+ IndexPtr ri = Index::load("wrong_root");
FAIL();
}
catch(const reapack_error &e) {
@@ -55,8 +51,7 @@ TEST_CASE("invalid version", M) {
UseRootPath root(RIPATH);
try {
- const Index *ri = Index::load("invalid_version");
- RIPTR(ri);
+ IndexPtr ri = Index::load("invalid_version");
FAIL();
}
catch(const reapack_error &e) {
@@ -68,8 +63,7 @@ TEST_CASE("future version", M) {
UseRootPath root(RIPATH);
try {
- const Index *ri = Index::load("future_version");
- RIPTR(ri);
+ IndexPtr ri = Index::load("future_version");
FAIL();
}
catch(const reapack_error &e) {
@@ -80,15 +74,13 @@ TEST_CASE("future version", M) {
TEST_CASE("unicode index path", M) {
UseRootPath root(RIPATH);
- const Index *ri = Index::load("Новая папка");
- RIPTR(ri);
-
+ IndexPtr ri = Index::load("Новая папка");
REQUIRE(ri->name() == "Новая папка");
}
TEST_CASE("empty index name", M) {
try {
- Index cat{string()};
+ Index idx({});
FAIL();
}
catch(const reapack_error &e) {
diff --git a/test/index_v1.cpp b/test/index_v1.cpp
@@ -3,11 +3,9 @@
#include <index.hpp>
#include <errors.hpp>
-#include <memory>
#include <string>
#define RIPATH "test/indexes/v1/"
-#define RIPTR(ptr) unique_ptr<const Index> riptr(ptr)
using namespace std;
@@ -17,8 +15,7 @@ TEST_CASE("unnamed category", M) {
UseRootPath root(RIPATH);
try {
- const Index *ri = Index::load("unnamed_category");
- RIPTR(ri);
+ IndexPtr ri = Index::load("unnamed_category");
FAIL();
}
catch(const reapack_error &e) {
@@ -29,8 +26,7 @@ TEST_CASE("unnamed category", M) {
TEST_CASE("invalid category tag", M) {
UseRootPath root(RIPATH);
- const Index *ri = Index::load("wrong_category_tag");
- RIPTR(ri);
+ IndexPtr ri = Index::load("wrong_category_tag");
REQUIRE(ri->categories().empty());
}
@@ -38,8 +34,7 @@ TEST_CASE("invalid category tag", M) {
TEST_CASE("invalid package tag", M) {
UseRootPath root(RIPATH);
- const Index *ri = Index::load("wrong_package_tag");
- RIPTR(ri);
+ IndexPtr ri = Index::load("wrong_package_tag");
REQUIRE(ri->categories().empty());
}
@@ -48,8 +43,7 @@ TEST_CASE("null package name", M) {
UseRootPath root(RIPATH);
try {
- const Index *ri = Index::load("unnamed_package");
- RIPTR(ri);
+ IndexPtr ri = Index::load("unnamed_package");
FAIL();
}
catch(const reapack_error &e) {
@@ -61,8 +55,7 @@ TEST_CASE("null package type", M) {
UseRootPath root(RIPATH);
try {
- const Index *ri = Index::load("missing_type");
- RIPTR(ri);
+ IndexPtr ri = Index::load("missing_type");
}
catch(const reapack_error &) {
// no segfault -> test passes!
@@ -72,14 +65,13 @@ TEST_CASE("null package type", M) {
TEST_CASE("unsupported package type", M) {
UseRootPath root(RIPATH);
- const Index *ri = Index::load("unsupported_type");
+ IndexPtr ri = Index::load("unsupported_type");
}
TEST_CASE("read version author", M) {
UseRootPath root(RIPATH);
- const Index *ri = Index::load("author");
- RIPTR(ri);
+ IndexPtr ri = Index::load("author");
CHECK(ri->packages().size() == 1);
REQUIRE(ri->category(0)->package(0)->lastVersion()->author()
@@ -89,9 +81,7 @@ TEST_CASE("read version author", M) {
TEST_CASE("read version time", M) {
UseRootPath root(RIPATH);
- const Index *ri = Index::load("time");
- RIPTR(ri);
-
+ IndexPtr ri = Index::load("time");
CHECK(ri->packages().size() == 1);
const tm &time = ri->category(0)->package(0)->lastVersion()->time();
@@ -103,9 +93,7 @@ TEST_CASE("read version time", M) {
TEST_CASE("invalid version tag", M) {
UseRootPath root(RIPATH);
- const Index *ri = Index::load("wrong_version_tag");
- RIPTR(ri);
-
+ IndexPtr ri = Index::load("wrong_version_tag");
REQUIRE(ri->categories().empty());
}
@@ -113,8 +101,7 @@ TEST_CASE("null package version", M) {
UseRootPath root(RIPATH);
try {
- const Index *ri = Index::load("missing_version");
- RIPTR(ri);
+ IndexPtr ri = Index::load("missing_version");
FAIL();
}
catch(const reapack_error &e) {
@@ -126,8 +113,7 @@ TEST_CASE("null source url", M) {
UseRootPath root(RIPATH);
try {
- const Index *ri = Index::load("missing_source_url");
- RIPTR(ri);
+ IndexPtr ri = Index::load("missing_source_url");
FAIL();
}
catch(const reapack_error &e) {
@@ -138,9 +124,7 @@ TEST_CASE("null source url", M) {
TEST_CASE("null source file", M) {
UseRootPath root(RIPATH);
- const Index *ri = Index::load("missing_source_file");
- RIPTR(ri);
-
+ IndexPtr ri = Index::load("missing_source_file");
CHECK(ri->packages().size() == 1);
const Package *pkg = ri->category(0)->package(0);
@@ -150,8 +134,7 @@ TEST_CASE("null source file", M) {
TEST_CASE("default platform", M) {
UseRootPath root(RIPATH);
- const Index *ri = Index::load("missing_platform");
- RIPTR(ri);
+ IndexPtr ri = Index::load("missing_platform");
CHECK(ri->packages().size() == 1);
REQUIRE(ri->category(0)->package(0)->version(0)->source(0)->platform()
@@ -161,9 +144,7 @@ TEST_CASE("default platform", M) {
TEST_CASE("version changelog", M) {
UseRootPath root(RIPATH);
- const Index *ri = Index::load("changelog");
- RIPTR(ri);
-
+ IndexPtr ri = Index::load("changelog");
CHECK(ri->packages().size() == 1);
REQUIRE(ri->category(0)->package(0)->version(0)->changelog()
@@ -173,9 +154,7 @@ TEST_CASE("version changelog", M) {
TEST_CASE("full index", M) {
UseRootPath root(RIPATH);
- const Index *ri = Index::load("valid_index");
- RIPTR(ri);
-
+ IndexPtr ri = Index::load("valid_index");
REQUIRE(ri->categories().size() == 1);
const Category *cat = ri->category(0);
@@ -201,8 +180,7 @@ TEST_CASE("full index", M) {
TEST_CASE("read index metadata", M) {
UseRootPath root(RIPATH);
- const Index *ri = Index::load("metadata");
- RIPTR(ri);
+ IndexPtr ri = Index::load("metadata");
SECTION("description") {
REQUIRE(ri->aboutText() == "Chunky\nBacon");