Skip to content

Commit

Permalink
libmbpatcher: Replace C callbacks with std::function
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Gunnerson <[email protected]>
  • Loading branch information
chenxiaolong committed Jun 8, 2018
1 parent 1db5e5f commit cfd3230
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 170 deletions.
5 changes: 2 additions & 3 deletions examples/libmbpatcher_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ static bool get_device(const char *path, mb::device::Device &device)
return true;
}

static void mbp_progress_cb(uint64_t bytes, uint64_t maxBytes, void *userdata)
static void mbp_progress_cb(uint64_t bytes, uint64_t maxBytes)
{
(void) userdata;
printf("Current bytes percentage: %.1f\n",
100.0 * static_cast<double>(bytes) / static_cast<double>(maxBytes));
}
Expand Down Expand Up @@ -140,7 +139,7 @@ int main(int argc, char *argv[]) {
}

patcher->set_file_info(&fi);
bool ret = patcher->patch_file(&mbp_progress_cb, nullptr, nullptr, nullptr);
bool ret = patcher->patch_file(&mbp_progress_cb, nullptr, nullptr);

if (!ret) {
fprintf(stderr, "Error: %d\n", static_cast<int>(patcher->error()));
Expand Down
30 changes: 6 additions & 24 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -688,34 +688,16 @@ PatcherTask::PatcherTask(QWidget *parent)
{
}

static void progressUpdatedCbWrapper(uint64_t bytes, uint64_t maxBytes,
void *userData)
{
PatcherTask *task = static_cast<PatcherTask *>(userData);
task->progressUpdatedCb(bytes, maxBytes);
}

static void filesUpdatedCbWrapper(uint64_t files, uint64_t maxFiles,
void *userData)
{
PatcherTask *task = static_cast<PatcherTask *>(userData);
task->filesUpdatedCb(files, maxFiles);
}

static void detailsUpdatedCbWrapper(const std::string &text, void *userData)
{
PatcherTask *task = static_cast<PatcherTask *>(userData);
task->detailsUpdatedCb(text);
}

void PatcherTask::patch(PatcherPtr patcher, FileInfoPtr info)
{
using namespace std::placeholders;

patcher->set_file_info(info);

bool ret = patcher->patch_file(&progressUpdatedCbWrapper,
&filesUpdatedCbWrapper,
&detailsUpdatedCbWrapper,
this);
bool ret = patcher->patch_file(
std::bind(&PatcherTask::progressUpdatedCb, this, _1, _2),
std::bind(&PatcherTask::filesUpdatedCb, this, _1, _2),
std::bind(&PatcherTask::detailsUpdatedCb, this, _1));

QString newFile(QString::fromStdString(info->output_path()));

Expand Down
10 changes: 5 additions & 5 deletions libmbpatcher/include/mbpatcher/cwrapper/cpatcherinterface.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2015 Andrew Gunnerson <[email protected]>
* Copyright (C) 2014-2018 Andrew Gunnerson <[email protected]>
*
* This file is part of DualBootPatcher
*
Expand Down Expand Up @@ -35,10 +35,10 @@ MB_EXPORT /* enum ErrorCode */ int mbpatcher_patcher_error(const CPatcher *patch
MB_EXPORT char * mbpatcher_patcher_id(const CPatcher *patcher);
MB_EXPORT void mbpatcher_patcher_set_fileinfo(CPatcher *patcher, const CFileInfo *info);
MB_EXPORT bool mbpatcher_patcher_patch_file(CPatcher *patcher,
ProgressUpdatedCallback progressCb,
FilesUpdatedCallback filesCb,
DetailsUpdatedCallback detailsCb,
void *userData);
ProgressUpdatedCallback progress_cb,
FilesUpdatedCallback files_cb,
DetailsUpdatedCallback details_cb,
void *userdata);
MB_EXPORT void mbpatcher_patcher_cancel_patching(CPatcher *patcher);


Expand Down
18 changes: 9 additions & 9 deletions libmbpatcher/include/mbpatcher/patcherinterface.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2015 Andrew Gunnerson <[email protected]>
* Copyright (C) 2014-2018 Andrew Gunnerson <[email protected]>
*
* This file is part of DualBootPatcher
*
Expand All @@ -19,6 +19,8 @@

#pragma once

#include <functional>

#include "mbcommon/common.h"

#include "mbpatcher/errors.h"
Expand All @@ -39,9 +41,9 @@ namespace mb::patcher
class MB_EXPORT Patcher
{
public:
typedef void (*ProgressUpdatedCallback) (uint64_t, uint64_t, void *);
typedef void (*FilesUpdatedCallback) (uint64_t, uint64_t, void *);
typedef void (*DetailsUpdatedCallback) (const std::string &, void *);
using ProgressUpdatedCallback = std::function<void(uint64_t, uint64_t)>;
using FilesUpdatedCallback = std::function<void(uint64_t, uint64_t)>;
using DetailsUpdatedCallback = std::function<void(const std::string &)>;

virtual ~Patcher() {}

Expand Down Expand Up @@ -73,12 +75,10 @@ class MB_EXPORT Patcher
* \param progress_cb Callback for receiving current progress values
* \param files_cb Callback for receiving current files count
* \param details_cb Callback for receiving detailed progress text
* \param userdata Pointer to pass to callback functions
*/
virtual bool patch_file(ProgressUpdatedCallback progress_cb,
FilesUpdatedCallback files_cb,
DetailsUpdatedCallback details_cb,
void *userdata) = 0;
virtual bool patch_file(const ProgressUpdatedCallback &progress_cb,
const FilesUpdatedCallback &files_cb,
const DetailsUpdatedCallback &details_cb) = 0;

/*!
* \brief Cancel the patching of a file
Expand Down
14 changes: 6 additions & 8 deletions libmbpatcher/include/mbpatcher/patchers/odinpatcher.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2015-2017 Andrew Gunnerson <[email protected]>
* Copyright (C) 2015-2018 Andrew Gunnerson <[email protected]>
*
* This file is part of DualBootPatcher
*
Expand Down Expand Up @@ -55,10 +55,9 @@ class OdinPatcher : public Patcher
// Patching
void set_file_info(const FileInfo * const info) override;

bool patch_file(ProgressUpdatedCallback progress_cb,
FilesUpdatedCallback files_cb,
DetailsUpdatedCallback details_cb,
void *userdata) override;
bool patch_file(const ProgressUpdatedCallback &progress_cb,
const FilesUpdatedCallback &files_cb,
const DetailsUpdatedCallback &details_cb) override;

void cancel_patching() override;

Expand All @@ -85,9 +84,8 @@ class OdinPatcher : public Patcher
std::unordered_set<std::string> m_added_files;

// Callbacks
OdinPatcher::ProgressUpdatedCallback m_progress_cb;
OdinPatcher::DetailsUpdatedCallback m_details_cb;
void *m_userdata;
const ProgressUpdatedCallback *m_progress_cb;
const DetailsUpdatedCallback *m_details_cb;

// Patching
archive *m_a_input;
Expand Down
9 changes: 4 additions & 5 deletions libmbpatcher/include/mbpatcher/patchers/ramdiskupdater.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2017 Andrew Gunnerson <[email protected]>
* Copyright (C) 2014-2018 Andrew Gunnerson <[email protected]>
*
* This file is part of DualBootPatcher
*
Expand Down Expand Up @@ -47,10 +47,9 @@ class RamdiskUpdater : public Patcher
// Patching
void set_file_info(const FileInfo * const info) override;

bool patch_file(ProgressUpdatedCallback progress_cb,
FilesUpdatedCallback files_cb,
DetailsUpdatedCallback details_cb,
void *userdata) override;
bool patch_file(const ProgressUpdatedCallback &progress_cb,
const FilesUpdatedCallback &files_cb,
const DetailsUpdatedCallback &details_cb) override;

void cancel_patching() override;

Expand Down
18 changes: 8 additions & 10 deletions libmbpatcher/include/mbpatcher/patchers/zippatcher.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2017 Andrew Gunnerson <[email protected]>
* Copyright (C) 2014-2018 Andrew Gunnerson <[email protected]>
*
* This file is part of DualBootPatcher
*
Expand Down Expand Up @@ -50,10 +50,9 @@ class ZipPatcher : public Patcher
// Patching
void set_file_info(const FileInfo * const info) override;

bool patch_file(ProgressUpdatedCallback progress_cb,
FilesUpdatedCallback files_cb,
DetailsUpdatedCallback details_cb,
void *userdata) override;
bool patch_file(const ProgressUpdatedCallback &progress_cb,
const FilesUpdatedCallback &files_cb,
const DetailsUpdatedCallback &details_cb) override;

void cancel_patching() override;

Expand All @@ -73,10 +72,9 @@ class ZipPatcher : public Patcher
ErrorCode m_error;

// Callbacks
ProgressUpdatedCallback m_progress_cb;
FilesUpdatedCallback m_files_cb;
DetailsUpdatedCallback m_details_cb;
void *m_userdata;
const ProgressUpdatedCallback *m_progress_cb;
const FilesUpdatedCallback *m_files_cb;
const DetailsUpdatedCallback *m_details_cb;

// Patching
ZipCtx *m_z_input = nullptr;
Expand All @@ -98,7 +96,7 @@ class ZipPatcher : public Patcher
void update_files(uint64_t files, uint64_t max_files);
void update_details(const std::string &msg);

static void la_progress_cb(uint64_t bytes, void *userdata);
void la_progress_cb(uint64_t bytes);
};

}
9 changes: 4 additions & 5 deletions libmbpatcher/include/mbpatcher/private/miniziputils.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2017 Andrew Gunnerson <[email protected]>
* Copyright (C) 2014-2018 Andrew Gunnerson <[email protected]>
*
* This file is part of DualBootPatcher
*
Expand All @@ -19,6 +19,7 @@

#pragma once

#include <functional>
#include <string>
#include <vector>

Expand Down Expand Up @@ -61,13 +62,11 @@ class MinizipUtils
static bool copy_file_raw(void *source_handle,
void *target_handle,
const std::string &name,
void (*cb)(uint64_t bytes, void *),
void *userdata);
const std::function<void(uint64_t bytes)> &cb);

static bool read_to_memory(void *handle,
std::vector<unsigned char> &output,
void (*cb)(uint64_t bytes, void *),
void *userdata);
const std::function<void(uint64_t bytes)> &cb);

static bool extract_file(void *handle,
const std::string &directory);
Expand Down
76 changes: 26 additions & 50 deletions libmbpatcher/src/cwrapper/cpatcherinterface.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2015 Andrew Gunnerson <[email protected]>
* Copyright (C) 2014-2018 Andrew Gunnerson <[email protected]>
*
* This file is part of DualBootPatcher
*
Expand Down Expand Up @@ -54,39 +54,6 @@
extern "C"
{

struct CallbackWrapper
{
ProgressUpdatedCallback progress_cb;
FilesUpdatedCallback files_cb;
DetailsUpdatedCallback details_cb;
void *userdata;
};

static void progress_cb_wrapper(uint64_t bytes, uint64_t max_bytes,
void *userdata)
{
CallbackWrapper *wrapper = reinterpret_cast<CallbackWrapper *>(userdata);
if (wrapper->progress_cb != nullptr) {
wrapper->progress_cb(bytes, max_bytes, wrapper->userdata);
}
}

static void files_cb_wrapper(uint64_t files, uint64_t max_files, void *userdata)
{
CallbackWrapper *wrapper = reinterpret_cast<CallbackWrapper *>(userdata);
if (wrapper->files_cb) {
wrapper->files_cb(files, max_files, userdata);
}
}

static void details_cb_wrapper(const std::string &text, void *userdata)
{
CallbackWrapper *wrapper = reinterpret_cast<CallbackWrapper *>(userdata);
if (wrapper->details_cb) {
wrapper->details_cb(text.c_str(), wrapper->userdata);
}
}

/*!
* \brief Get the error information
*
Expand Down Expand Up @@ -143,30 +110,39 @@ void mbpatcher_patcher_set_fileinfo(CPatcher *patcher, const CFileInfo *info)
* \brief Start patching the file
*
* \param patcher CPatcher object
* \param progressCb Callback for receiving current progress value
* \param detailsCb Callback for receiving detailed progress text
* \param userData Pointer to pass to callback functions
* \param progress_cb Callback for receiving current progress value
* \param files_cb Callback for receiving current files count
* \param details_cb Callback for receiving detailed progress text
* \param userdata Pointer to pass to callback functions
* \return true on success, otherwise false (and error set appropriately)
*
* \sa Patcher::patchFile()
*/
bool mbpatcher_patcher_patch_file(CPatcher *patcher,
ProgressUpdatedCallback progressCb,
FilesUpdatedCallback filesCb,
DetailsUpdatedCallback detailsCb,
void *userData)
ProgressUpdatedCallback progress_cb,
FilesUpdatedCallback files_cb,
DetailsUpdatedCallback details_cb,
void *userdata)
{
CASTP(patcher);

CallbackWrapper wrapper;
wrapper.progress_cb = progressCb;
wrapper.files_cb = filesCb;
wrapper.details_cb = detailsCb;
wrapper.userdata = userData;

return p->patch_file(&progress_cb_wrapper, &files_cb_wrapper,
&details_cb_wrapper,
reinterpret_cast<void *>(&wrapper));
return p->patch_file(
[&](uint64_t bytes, uint64_t max_bytes) {
if (progress_cb) {
progress_cb(bytes, max_bytes, userdata);
}
},
[&](uint64_t files, uint64_t max_files) {
if (files_cb) {
files_cb(files, max_files, userdata);
}
},
[&](const std::string &text) {
if (details_cb) {
details_cb(text.c_str(), userdata);
}
}
);
}

/*!
Expand Down
Loading

0 comments on commit cfd3230

Please sign in to comment.