gpt4 book ai didi

C++ Boost 函数回调。如何调用函数作为回调并在内部传递参数?

转载 作者:行者123 更新时间:2023-11-30 03:35:27 26 4
gpt4 key购买 nike

我有类 Manager 和类 SchedulerManagerScheduler 的实例。 Manager 启动 SchedulerScheduler 做某事,Scheduler 必须异步返回结果给 Manager。我怎样才能让它像下面的例子一样?

class Manager{
private:
Scheduler *mScheduler;

public:
Manager(){
mScheduler = new Scheduler(boost::bind(&Manager::acceptResult, this));
mScheduler->run();
}

void acceptResult(Result::Shared result){
//do something with result, it's doesn't matter=)
}
};

typedef boost::function<void()> callback;

class Scheduler {
private:
callback mManagerCallback;

public:
Scheduler(callback call){
mManagerCallback = call;
}

void run(){
//some logic
mManagerCallback(result);
}

}

typedef boost::posix_time::ptime Timestamp;
typedef boost::posix_time::time_duration Duration;
typedef uint64_t microseconds_timestamp;

class CommandResult {
public:
typedef shared_ptr<const CommandResult> SharedConst;

private:
CommandUUID mCommandUUID;
uint16_t mResultCode;
Timestamp mTimestampCompleted;
string mResultInformation;

public:
CommandResult(
const CommandUUID &commandUUID,
const uint16_t resultCode);

CommandResult(
const CommandUUID &commandUUID,
const uint16_t resultCode,
string &resultInformation);

const CommandUUID &commandUUID() const;

const uint16_t resultCode() const;

const Timestamp &timestampCompleted() const;

const string serialize() const;

};

CommandResult 与示例中的结果相同。

我需要在Scheduler产生结果时,在他的方法run()中,调用Manager的方法acceptResult() 并将结果作为参数传递。我是 C++ 新手,只是不知道该怎么做。

最佳答案

首先你必须让回调可以接受一个参数:

typedef boost::function<void(CommandResult::SharedConst)> callback;

然后您需要在绑定(bind)中匹配该签名。看起来您想在 Scheduler::run() 逻辑中自由绑定(bind)结果,所以让我们使用一个占位符:

 mScheduler = new Scheduler(boost::bind(&Manager::acceptResult, this, ::_1));

现在在您的逻辑中,您以某种方式得出结果,然后将其传递给回调:

void run(){
boost::uuids::random_generator rgen;
CommandResult::SharedConst result(new CommandResult(rgen(), 404u, "Not found"));
//some logic
mManagerCallback(result);
}

完整演示: Live On Coliru (C++03)

#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/random_generator.hpp>

typedef boost::posix_time::ptime Timestamp;
typedef boost::posix_time::time_duration Duration;
typedef uint64_t microseconds_timestamp;

typedef boost::uuids::uuid CommandUUID;

class CommandResult {
public:
typedef boost::shared_ptr<const CommandResult> SharedConst;

private:
CommandUUID mCommandUUID;
uint16_t mResultCode;
Timestamp mTimestampCompleted;
std::string mResultInformation;

public:
CommandResult(
const CommandUUID &commandUUID,
const uint16_t resultCode,
std::string const &resultInformation = "")
: mCommandUUID(commandUUID),
mResultCode(resultCode),
mTimestampCompleted(boost::posix_time::second_clock::local_time()),
mResultInformation(resultInformation)
{ }

const CommandUUID &commandUUID() const { return mCommandUUID; }
uint16_t resultCode() const { return mResultCode; }
const Timestamp &timestampCompleted() const { return mTimestampCompleted; }
};

typedef boost::function<void(CommandResult::SharedConst)> callback;

class Scheduler {
private:
callback mManagerCallback;

public:
Scheduler(callback call){
mManagerCallback = call;
}

void run(){
boost::uuids::random_generator rgen;
CommandResult::SharedConst result(new CommandResult(rgen(), 404u, "Not found"));
//some logic
mManagerCallback(result);
}
};

#include <iostream>

class Manager{
private:
Scheduler *mScheduler;

public:
Manager(){
//mScheduler = new Scheduler([this](CommandResult::SharedConst result) { acceptResult(result); });
mScheduler = new Scheduler(boost::bind(&Manager::acceptResult, this, ::_1));
mScheduler->run();
}

void acceptResult(CommandResult::SharedConst result){
//do something with result, it's doesn't matter=)
std::cout << "Accepted: " << result->commandUUID() << ", " << result->resultCode() << ": " << result->timestampCompleted() << " \n";
}
};

int main() {
Manager m;
}

打印:

Accepted: cf946fb0-49f3-425a-8235-558d19bf6f8d, 404: 2016-Dec-23 12:18:01 

奖励:C++11

Bind 有点不合时宜(虽然我有时很喜欢它,但对于很少见的工作),所以请考虑改用 lambda:

mScheduler = new Scheduler([this](CommandResult::SharedConst result) { acceptResult(result); });

关于C++ Boost 函数回调。如何调用函数作为回调并在内部传递参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41300372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com